Annotation of loncom/homework/randomlabel.pm, revision 1.51
1.1 tsai 1: # The LearningOnline Network with CAPA
2: # random labelling tool
1.8 albertel 3: #
1.51 ! sakharuk 4: # $Id: randomlabel.pm,v 1.50 2003/09/25 14:05:44 sakharuk Exp $
1.8 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.3 tsai 28: # 7/20/2001 Isaac Tsai, initial syntax
29: # 8/10/2001 Isaac Tsai,
30: # 8/30/2001 Isaac Tsai,
1.1 tsai 31: # SYNTAX:
1.4 albertel 32: # <randomlabel bgimg="URL" width="12" height="45" texwidth="50">
33: # <labelgroup name="GroupOne" type="image">
34: # <location x="123" y="456" />
35: # <location x="321" y="654" />
36: # <location x="213" y="546" />
37: # <label description="TEXT-1">IMG-URL</label>
38: # <label description="TEXT-2">IMG-URL</label>
39: # <label description="TEXT-3">IMG-URL</label>
1.1 tsai 40: # </labelgroup>
1.4 albertel 41: # <labelgroup name="GroupTwo" type="text">
42: # <location x="12" y="45" />
43: # <location x="32" y="65" />
44: # <location x="21" y="54" />
1.3 tsai 45: # <label>TEXT-1</label>
46: # <label>TEXT-2</label>
47: # <label>TEXT-3</label>
1.1 tsai 48: # </labelgroup>
49: # </randomlabel>
50: # ===========================================
1.3 tsai 51: # side effect:
52: # location (123,456): $GroupOne[0] = 2 # images give out indexes
53: # (321,654): $GroupOne[1] = 1
54: # (213,546): $GroupOne[2] = 0
55: # location (12,45) : $GroupTwo[0] = "TEXT-3"
56: # (32,65) : $GroupTwo[1] = "TEXT-1"
57: # (21,54) : $GroupTwo[2] = "TEXT-2"
1.1 tsai 58: # ===========================================
59: package Apache::randomlabel;
1.24 sakharuk 60: use Apache::lonnet;
1.1 tsai 61: use strict;
1.13 sakharuk 62: use Apache::edit;
1.36 sakharuk 63: use Apache::File();
1.38 sakharuk 64: use Apache::Constants qw(:common :http);
1.1 tsai 65:
1.13 sakharuk 66: BEGIN {
1.47 albertel 67: &Apache::lonxml::register('Apache::randomlabel',('randomlabel','labelgroup','location','label'));
1.1 tsai 68: }
69:
1.23 matthew 70: sub check_int {
71: # utility function to do error checking on a integer.
72: my ($num,$default) = @_;
73: $default = 100 if (! defined($default));
74: $num =~ s/\s+//g; # We dont need no stinkin white space!
75: # If it is a real, just grab the integer part.
76: ($num,undef) = split (/\./,$num) if ($num =~ /\./);
77: # set to default if what we have left doesn't match anything...
78: $num = $default unless ($num =~/^\d+$/);
79: return $num;
80: }
81:
1.1 tsai 82: sub start_randomlabel {
1.47 albertel 83: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
84: my $result='';
85: push (@Apache::lonxml::namespace,'randomlabel');
86: my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
87: if ( $bgimg !~ /^http:/ ) {
88: $bgimg=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$bgimg);
89: if (&Apache::lonnet::repcopy($bgimg) ne OK) {
90: $bgimg='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
91: }
1.42 www 92: }
1.47 albertel 93: my $w= &check_int(&Apache::lonxml::get_param('width',$parstack,$safeeval));
94: my $h= &check_int(&Apache::lonxml::get_param('height',$parstack,$safeeval));
1.50 sakharuk 95: my $texwidth=&adjust_textwidth(&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1));
1.49 albertel 96: if (!$texwidth) { $texwidth=90; }
1.47 albertel 97: $Apache::randomlabel::tlabel_cnt=0;
98: $Apache::randomlabel::ilabel_cnt=0;
99: if ($target eq 'web') {
100: &Apache::lonxml::startredirection();
101: $result.="BGIMG=".&Apache::lonnet::escape($bgimg);
102: } elsif ($target eq 'tex') {
103: my $newbgimg = $bgimg;
104: $bgimg=~s/\.(gif|jpg|png|jpeg)$/\.eps/i;
105: $bgimg= &Apache::lonnet::filelocation($bgimg);
106: $bgimg=~s/http:\/[^\/]*/\/home\/httpd\/html/;
107: $bgimg=~s/\/$//;
108: #if no eps file try to replicate it
109: if (not-e $bgimg) {
110: if (&Apache::lonnet::repcopy($bgimg) ne OK ) {
111: #if replication failed try to find ps file
112: $bgimg=~s/\.eps$/\.ps/;
113: #if no ps file try to replicate it
1.45 sakharuk 114: if (not -e $bgimg &&
1.38 sakharuk 115: &Apache::lonnet::repcopy($bgimg) ne OK) {
116: #if replication failed try to produce eps file dynamically
1.36 sakharuk 117: $bgimg=~s/\.ps$/\.eps/;
118: my $temp_file;
119: my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
120: $temp_file = Apache::File->new('>>'.$filename);
121: $newbgimg =~ s/(.*)\/res\//\/home\/httpd\/html\/res\//;
122: print $temp_file "$newbgimg\n";
1.51 ! sakharuk 123: $bgimg=~s/\/home\/httpd\/html\/res/\/home\/httpd\/prtspool/; }
1.47 albertel 124: }
125: }
126: $bgimg=~s/\/$//;
127: my $dirtywidth=$texwidth+5;
1.50 sakharuk 128: $result.='\vspace*{2mm}\noindent \parbox{'.$dirtywidth.' mm}{ \noindent \epsfxsize='.
129: $texwidth.' mm \epsffile{'.$bgimg.'}\setlength{\unitlength}{1mm} \begin{picture}('.
130: $texwidth.','.$texwidth*$h/$w.')(0,-'.$texwidth*$h/$w.')';
1.47 albertel 131: } elsif ($target eq 'edit') {
132: $result.=&Apache::edit::tag_start($target,$token);
133: $Apache::edit::bgimgsrc=
134: &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
135: $Apache::edit::bgimgsrccurdepth=$Apache::lonxml::curdepth;
136: $result.=&Apache::edit::text_arg('Image:','bgimg',$token,75).' ';
137: $result.=&Apache::edit::browse('bgimg').' ';
138: $result.=&Apache::edit::search('bgimg').'<br />'.
139: &Apache::edit::text_arg('Width(pixel):' ,'width' ,$token,6).
140: &Apache::edit::text_arg('Height(pixel):','height' ,$token,6).
141: &Apache::edit::text_arg('TeXWidth(mm):' ,'texwidth',$token,6).
142: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
143: } elsif ($target eq 'modified') {
144: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
145: $safeeval,'bgimg','width',
146: 'height','texwidth');
147: if ($constructtag) {
148: $result = &Apache::edit::rebuild_tag($token);
149: $result.=&Apache::edit::handle_insert();
150: }
1.31 sakharuk 151: }
1.47 albertel 152: return $result;
1.1 tsai 153: }
154:
155: sub end_randomlabel {
1.47 albertel 156: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
157: my $result='';
158: my $count;
159: pop @Apache::lonxml::namespace;
160: if ($target eq 'web') {
161: my $args=&Apache::lonxml::endredirection();
162: $args=~s/\s*&/&/g;
163: $args=~s/\s*$//;
164: $count = $Apache::randomlabel::tlabel_cnt;
165: if( $count != 0) { $args.= "&COUNT=$count"; }
166: $count = $Apache::randomlabel::ilabel_cnt;
167: if( $count != 0) { $args.= "&ICOUNT=$count"; }
168: my $token=$Apache::lonxml::curdepth.'_'.$$;
169: $result.='<img src="/adm/randomlabel.png?token='.$token;
170: $result.='" /><br />'."\n";
171: &Apache::lonnet::appenv(("imagerequest.$token"=>&Apache::lonnet::escape($args)));
172: } elsif ($target eq 'tex') {
173: $result='\end{picture}\\\\';
174: my $height=&Apache::lonxml::get_param('height',$parstack,$safeeval);
175: my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
1.50 sakharuk 176: my $texwidth=&adjust_textwidth(&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1));
1.49 albertel 177: if (!$texwidth) { $texwidth=90; }
1.47 albertel 178: my $howtoskipback = $texwidth*$height/$width;
179: $result.= ' \vskip -'.$howtoskipback.' mm } \\\\ ';
180: } elsif ($target eq 'edit') {
181: $result.=&Apache::edit::end_table;
182: }
183: return $result;
1.1 tsai 184: }
185:
1.50 sakharuk 186: sub adjust_textwidth {
187: my $texwidth=shift;
188: my $pagewidth=$ENV{'form.textwidth'};
189: $pagewidth=~s/\s*mm\s*$//;
190: if ($texwidth>$pagewidth) {$texwidth=$pagewidth;}
191: return $texwidth;
192: }
193:
1.1 tsai 194: sub start_labelgroup {
1.47 albertel 195: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
196: my $result='';
197: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
198: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
199: $type =~tr/A-Z/a-z/;
1.48 albertel 200: if ($target ne 'modified' && ($name =~ /\W/ || $name =~ /^[0-9]/)) {
201: &Apache::lonxml::error("Only _ a-z A-Z and 0-9 are allowed in the name to a labelgroup, and the first character can not be a number.<br />");
202: }
1.47 albertel 203: if ($target eq 'web' || $target eq 'tex' ||
204: $target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
205: $Apache::randomlabel::groupname=$name;
206: $Apache::randomlabel::type=$type;
207: @Apache::randomlabel::xcoord = ();
208: @Apache::randomlabel::ycoord = ();
209: @Apache::randomlabel::value = ();
210: @Apache::randomlabel::label_arr = ();
211: @Apache::randomlabel::decription = ();
212: } elsif ($target eq 'edit') {
213: $result.=&Apache::edit::tag_start($target,$token);
214: $result.=&Apache::edit::text_arg('Name:','name',$token).
215: &Apache::edit::select_arg('Type:','type',['text','image'],$token).
216: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
217: } elsif ($target eq 'modified') {
218: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
219: $safeeval,'name','type');
220: if ($constructtag) {
221: $result = &Apache::edit::rebuild_tag($token);
222: $result.=&Apache::edit::handle_insert();
223: }
1.4 albertel 224: }
1.47 albertel 225: return $result;
1.1 tsai 226: }
227:
1.5 albertel 228: sub add_vars {
1.47 albertel 229: my ($name,$order,$label,$labelorder,$value,$image,$safeeval) = @_;
230: my $code = '${'.$name."}{'".($order+1)."'}='".$label."';";
231: my $out=Apache::run::run($code,$safeeval);
232: if ($value) {
233: $code = '${'.$name."}{'value_".($order+1)."'}='".$value."';";
234: $out=Apache::run::run($code,$safeeval);
235: $code = '${'.$name."}{'labelvalue_".($labelorder+1)."'}='".$value."';";
236: $out=Apache::run::run($code,$safeeval);
237: }
238: if ($image) {
239: my $code = '${'.$name."}{'image_".($order+1)."'}='".$image."';";
240: my $out=Apache::run::run($code,$safeeval);
241: }
242: $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
1.5 albertel 243: $out=Apache::run::run($code,$safeeval);
1.4 albertel 244: }
1.5 albertel 245:
1.1 tsai 246: # begin to assign labels to locations
247: sub end_labelgroup {
1.47 albertel 248: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
249: my $gname = $Apache::randomlabel::groupname;
250: my $type = $Apache::randomlabel::type;
251: my $result='';
252: if ($target eq 'web' || $target eq 'answer' || $target eq 'grade' ||
253: $target eq 'analyze') {
254: my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
255: &Apache::structuretags::shuffle(\@idx_arr);
256: for(0 .. $#Apache::randomlabel::label_arr) {
257: my $str;
258: my $xstr;
259: my $ystr;
260: my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$_] ]";
261: my $x = $Apache::randomlabel::xcoord[$_];
262: my $y = $Apache::randomlabel::ycoord[$_];
263: my $value = $Apache::randomlabel::value[$_];
264: if( $type eq 'text') {
265: &add_vars($gname,$_,$label,$idx_arr[$_],$value,'',$safeeval);
266: $str = 'LB'.$Apache::randomlabel::tlabel_cnt;
267: $xstr = 'X'.$Apache::randomlabel::tlabel_cnt;
268: $ystr = 'Y'.$Apache::randomlabel::tlabel_cnt;
269: $Apache::randomlabel::tlabel_cnt += 1;
270: } elsif ( $type eq 'image') {
271: &add_vars($gname,$_,
272: $Apache::randomlabel::description[$idx_arr[$_]],
273: $idx_arr[$_],$value,$label,$safeeval);
274: $str = 'IMG'.$Apache::randomlabel::ilabel_cnt;
275: $xstr = 'IX'.$Apache::randomlabel::ilabel_cnt;
276: $ystr = 'IY'.$Apache::randomlabel::ilabel_cnt;
277: $Apache::randomlabel::ilabel_cnt += 1;
278: } else {
279: &Apache::lonxml::error('Unknown type of label :'.$type.':');
280: }
281: if ($target eq 'web') {
282: $result .= '&'. $str .'='.&Apache::lonnet::escape($label);
283: $result .= '&'. $xstr .'='.$x;
284: $result .= '&'. $ystr .'='.$y;
285: }
286: }
287: } elsif ($target eq 'tex') {
288: my $WX1=0; # Web x-coord. of upper left corner (ULC)
289: my $WY1=0; # Web y-coord. of (ULC)
290: my $wwidth=&Apache::lonxml::get_param('width',$parstack,$safeeval,-2);
291: my $wheight=&Apache::lonxml::get_param('height',$parstack,$safeeval,-2);
1.50 sakharuk 292: my $texwidth=&adjust_textwidth(&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,-2,1));
1.49 albertel 293: if (!$texwidth) { $texwidth=90; }
1.47 albertel 294: my $TX1=0;
295: my $TY1=$texwidth*($wheight/$wwidth);
296: my $TX2=$texwidth;
297: my $TY2=0;
298:
299:
300: my $slopex=($wwidth-$WX1)/($TX2-$TX1);
301: my $slopey=($wheight-$WY1)/($TY2-($TY1-1.0));
302: my $cstx=$wwidth-$slopex*($TX2);
303: my $csty=$wheight-$slopey*($TY2);
304:
305: my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
306: &Apache::structuretags::shuffle(\@idx_arr);
307:
308: &Apache::lonxml::debug("Array is:".$#Apache::randomlabel::label_arr.":");
309: for(my $i=0;$i <= $#Apache::randomlabel::label_arr; $i++) {
310: my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$i] ]";
311: my $x = $Apache::randomlabel::xcoord[$i];
312: my $y = $Apache::randomlabel::ycoord[$i];
313: my $value = $Apache::randomlabel::value[$i];
314: #x latex coordinate
315: my $tcX=($x)*($texwidth/$wwidth);
316: #y latex coordinate
317: # my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
318: my $tcY=$TY1-$y*($TY1/$wheight);
319: $tcX=sprintf('%.2f',$tcX);
320: $tcY=sprintf('%.2f',$tcY);
321: $result.='\put('.$tcX.','.$tcY.'){\normalsize \bf '.$label.'}'."\n";
322: if( $type eq 'text') {
323: &add_vars($gname,$i,$label,$idx_arr[$i],$value,'',$safeeval);
324: } elsif ( $type eq 'image') {
325: &add_vars($gname,$i,
326: $Apache::randomlabel::description[$idx_arr[$i]],
327: $idx_arr[$i],$value,$label,$safeeval);
328: } else {
329: &Apache::lonxml::error('Unknown type of label :'.$type.':');
330: }
331: }
332: } elsif ($target eq 'edit') {
333: $result.=&Apache::edit::end_table;
1.3 tsai 334: }
1.47 albertel 335: return $result;
1.1 tsai 336: }
337:
1.5 albertel 338: # <location x="123" y="456" value="some value"/>
1.1 tsai 339: sub start_location {
1.47 albertel 340: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
341: my $x= &check_int(&Apache::lonxml::get_param('x',$parstack,$safeeval),50);
342: my $y= &check_int(&Apache::lonxml::get_param('y',$parstack,$safeeval),50);
343: my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
344: my $result='';
345: push(@Apache::randomlabel::xcoord,$x);
346: push(@Apache::randomlabel::ycoord,$y);
347: push(@Apache::randomlabel::value,$value);
348: if ($target eq 'edit') {
349: $result.=&Apache::edit::tag_start($target,$token);
350: $result.=&Apache::edit::text_arg('X:','x',$token,4).
351: &Apache::edit::text_arg('Y:','y',$token,4).
352: &Apache::edit::entercoords('x','y','attribute','width','height').' '.
353: &Apache::edit::text_arg('Value:','value',$token).
354: &Apache::edit::end_row();
355: $result.=&Apache::edit::end_table;
356: } elsif ($target eq 'modified') {
357: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
358: $safeeval,'x','y','value');
359: if ($constructtag) {
360: $result = &Apache::edit::rebuild_tag($token);
361: $result.=&Apache::edit::handle_insert();
362: }
1.4 albertel 363: }
1.47 albertel 364: return $result;
1.1 tsai 365: }
366:
367: sub end_location {
1.47 albertel 368: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
369: my @result;
370: if ($target eq 'edit') { @result=('','no') }
371: return @result;
1.1 tsai 372: }
373:
1.2 tsai 374: # <label>$var_abc</label>
1.1 tsai 375: sub start_label {
1.47 albertel 376: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
377: my $result='';
378: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
379: if ($target eq 'web' || $target eq 'tex' ||
380: $target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
381: my $ltext=&Apache::lonxml::get_all_text("/label",$parser);
382: $ltext=&Apache::run::evaluate($ltext,$safeeval,$$parstack[-1]);
383: if ($type eq 'image') {
384: &Apache::lonxml::debug("Turning $ltext, $Apache::lonxml::pwd[-1]");
385: $ltext=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],
386: $ltext);
387: &Apache::lonxml::debug("into $ltext");
388: my $description = &Apache::lonxml::get_param('description',
389: $parstack,$safeeval);
390: push(@Apache::randomlabel::description,$description);
391: }
392: push(@Apache::randomlabel::label_arr,$ltext);
393: } elsif ($target eq 'edit') {
394: $result.=&Apache::edit::tag_start($target,$token,"$type Label");
395: my $text=&Apache::lonxml::get_all_text("/label",$parser);
396: if ($type eq 'image') {
397: $result.=&Apache::edit::end_row().
398: &Apache::edit::start_spanning_row();
399: $result.=&Apache::edit::text_arg('Description:','description',
400: $token);
401: }
402: if ($type eq 'text') { $result.="Label Text:"; }
403: if ($type eq 'image') { $result.="Path to image: "; }
404: $result.=&Apache::edit::editline('',$text,'',50).
405: &Apache::edit::end_table();
406: } elsif ($target eq 'modified') {
407: my $text=$$parser[-1]->get_text("/label");
408: $result = '<label>';
409: if ($type eq 'image') {
410: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
411: $safeeval,
412: 'description');
413: if ($constructtag) {
414: $result = &Apache::edit::rebuild_tag($token);
415: } else {
416: $result = $token->[4];
417: }
418: }
419: $result.=&Apache::edit::modifiedfield($token);
1.26 albertel 420: }
1.47 albertel 421: return $result;
1.1 tsai 422: }
423:
424: sub end_label {
1.47 albertel 425: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
426: my @result;
427: if ($target eq 'edit') { @result=('','no') }
428: return @result;
1.1 tsai 429: }
430:
431: 1;
432: __END__
433:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>