Annotation of loncom/homework/randomlabel.pm, revision 1.60
1.1 tsai 1: # The LearningOnline Network with CAPA
2: # random labelling tool
1.8 albertel 3: #
1.60 ! albertel 4: # $Id: randomlabel.pm,v 1.59 2004/02/23 23:25:39 albertel 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.53 albertel 66: my %args;
67: my $cgi_id;
68:
1.13 sakharuk 69: BEGIN {
1.57 albertel 70: &Apache::lonxml::register('Apache::randomlabel',('randomlabel','labelgroup','location','label','bgimg'));
1.1 tsai 71: }
72:
1.23 matthew 73: sub check_int {
74: # utility function to do error checking on a integer.
75: my ($num,$default) = @_;
76: $default = 100 if (! defined($default));
77: $num =~ s/\s+//g; # We dont need no stinkin white space!
78: # If it is a real, just grab the integer part.
79: ($num,undef) = split (/\./,$num) if ($num =~ /\./);
80: # set to default if what we have left doesn't match anything...
81: $num = $default unless ($num =~/^\d+$/);
82: return $num;
83: }
84:
1.1 tsai 85: sub start_randomlabel {
1.47 albertel 86: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
87: my $result='';
88: push (@Apache::lonxml::namespace,'randomlabel');
89: my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
1.60 ! albertel 90: if ( defined($bgimg) && $bgimg !~ /^http:/ ) {
1.47 albertel 91: $bgimg=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$bgimg);
92: if (&Apache::lonnet::repcopy($bgimg) ne OK) {
93: $bgimg='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
94: }
1.42 www 95: }
1.59 albertel 96: $Apache::randomlabel::obj_cnt=0;
1.47 albertel 97: if ($target eq 'web') {
1.53 albertel 98: $cgi_id=&Apache::loncommon::get_cgi_id();
99: %args=();
100: $args{"cgi.$cgi_id.BGIMG"}=&Apache::lonnet::escape($bgimg);
1.47 albertel 101: } elsif ($target eq 'tex') {
1.57 albertel 102: my $w= &check_int(&Apache::lonxml::get_param('width',$parstack,$safeeval));
103: my $h= &check_int(&Apache::lonxml::get_param('height',$parstack,$safeeval));
104: my $texwidth=&adjust_textwidth(&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1));
105: if (!$texwidth) { $texwidth=90; }
106: $result.=&make_eps_image($bgimg,$texwidth,$h,$w);
1.47 albertel 107: } elsif ($target eq 'edit') {
108: $result.=&Apache::edit::tag_start($target,$token);
109: $Apache::edit::bgimgsrc=
110: &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
111: $Apache::edit::bgimgsrccurdepth=$Apache::lonxml::curdepth;
112: $result.=&Apache::edit::text_arg('Image:','bgimg',$token,75).' ';
113: $result.=&Apache::edit::browse('bgimg').' ';
114: $result.=&Apache::edit::search('bgimg').'<br />'.
115: &Apache::edit::text_arg('Width(pixel):' ,'width' ,$token,6).
116: &Apache::edit::text_arg('Height(pixel):','height' ,$token,6).
117: &Apache::edit::text_arg('TeXWidth(mm):' ,'texwidth',$token,6).
118: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
119: } elsif ($target eq 'modified') {
120: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
121: $safeeval,'bgimg','width',
122: 'height','texwidth');
123: if ($constructtag) {
124: $result = &Apache::edit::rebuild_tag($token);
125: $result.=&Apache::edit::handle_insert();
126: }
1.31 sakharuk 127: }
1.47 albertel 128: return $result;
1.1 tsai 129: }
130:
131: sub end_randomlabel {
1.47 albertel 132: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
133: my $result='';
134: my $count;
135: pop @Apache::lonxml::namespace;
136: if ($target eq 'web') {
1.59 albertel 137: $count = $Apache::randomlabel::obj_cnt;
138: if( $count != 0) { $args{"cgi.$cgi_id.OBJCOUNT"}=$count; }
1.53 albertel 139: $result.='<img src="/adm/randomlabel.png?token='.$cgi_id.'" /><br />'."\n";
140: &Apache::lonnet::appenv(%args);
1.47 albertel 141: } elsif ($target eq 'tex') {
142: $result='\end{picture}\\\\';
143: my $height=&Apache::lonxml::get_param('height',$parstack,$safeeval);
144: my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
1.50 sakharuk 145: my $texwidth=&adjust_textwidth(&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1));
1.49 albertel 146: if (!$texwidth) { $texwidth=90; }
1.47 albertel 147: my $howtoskipback = $texwidth*$height/$width;
148: $result.= ' \vskip -'.$howtoskipback.' mm } \\\\ ';
149: } elsif ($target eq 'edit') {
150: $result.=&Apache::edit::end_table;
151: }
152: return $result;
1.1 tsai 153: }
154:
1.57 albertel 155: sub start_bgimg {
156: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
157: my $result='';
158: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
159: &Apache::lonxml::startredirection;
160: }
161: return $result;
162: }
163:
164: sub end_bgimg {
165: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
166: my $result='';
167: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
168: my $bgimg=&Apache::lonxml::endredirection;
169: if ($target eq 'web') {
170: $bgimg=&Apache::imageresponse::clean_up_image($bgimg);
171: $args{"cgi.$cgi_id.BGIMG"}=&Apache::lonnet::escape($bgimg);
172: } elsif ($target eq 'tex') {
173: my $w=&check_int(&Apache::lonxml::get_param('width',$parstack,$safeeval,-2));
174: my $h=&check_int(&Apache::lonxml::get_param('height',$parstack,$safeeval,-2));
175: my $texwidth=&adjust_textwidth(&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,-2));
176: if (!$texwidth) { $texwidth=90; }
177: $result.=&make_eps_image($bgimg,$texwidth,$h,$w);
178: }
179: }
180: return $result;
181: }
182:
183: sub make_eps_image {
184: my ($bgimg,$texwidth,$h,$w)=@_;
185: my $newbgimg = $bgimg;
186: $bgimg=~s/\.(gif|jpg|png|jpeg)$/\.eps/i;
187: $bgimg= &Apache::lonnet::filelocation($bgimg);
188: $bgimg=~s|http:/[^/]*|/home/httpd/html|;
189: $bgimg=~s|/$||;
190: #if no eps file try to replicate it
191: if (not-e $bgimg) {
192: if (&Apache::lonnet::repcopy($bgimg) ne OK ) {
193: #if replication failed try to find ps file
194: $bgimg=~s/\.eps$/\.ps/;
195: #if no ps file try to replicate it
196: if (not -e $bgimg &&
197: &Apache::lonnet::repcopy($bgimg) ne OK) {
198: #if replication failed try to produce eps file dynamically
199: $bgimg=~s/\.ps$/\.eps/;
200: my $temp_file;
201: my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
202: $temp_file = Apache::File->new('>>'.$filename);
203: $newbgimg =~ s|(.*)/res/|/home/httpd/html/res/|;
204: print $temp_file "$newbgimg\n";
205: $bgimg=~s|/home/httpd/html/res|/home/httpd/prtspool|;
206: $bgimg=~s|/home/([^/]*)/public_html/|/home/httpd/prtspool/$1/|;
207: }
208: }
209: }
210: $bgimg=~s|/$||;
211: my $dirtywidth=$texwidth+5;
212: my $result.='\vspace*{2mm}\noindent \parbox{'.$dirtywidth.
213: ' mm}{ \noindent \epsfxsize='.$texwidth.' mm \epsffile{'.$bgimg.
214: '}\setlength{\unitlength}{1mm} \begin{picture}('.
215: $texwidth.','.$texwidth*$h/$w.')(0,-'.$texwidth*$h/$w.')';
216: return $result;
217: }
218:
1.50 sakharuk 219: sub adjust_textwidth {
220: my $texwidth=shift;
221: my $pagewidth=$ENV{'form.textwidth'};
222: $pagewidth=~s/\s*mm\s*$//;
223: if ($texwidth>$pagewidth) {$texwidth=$pagewidth;}
224: return $texwidth;
225: }
226:
1.1 tsai 227: sub start_labelgroup {
1.47 albertel 228: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
229: my $result='';
230: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
231: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
232: $type =~tr/A-Z/a-z/;
1.48 albertel 233: if ($target ne 'modified' && ($name =~ /\W/ || $name =~ /^[0-9]/)) {
234: &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 />");
235: }
1.47 albertel 236: if ($target eq 'web' || $target eq 'tex' ||
237: $target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
238: $Apache::randomlabel::groupname=$name;
239: $Apache::randomlabel::type=$type;
240: @Apache::randomlabel::xcoord = ();
241: @Apache::randomlabel::ycoord = ();
242: @Apache::randomlabel::value = ();
243: @Apache::randomlabel::label_arr = ();
244: @Apache::randomlabel::decription = ();
245: } elsif ($target eq 'edit') {
246: $result.=&Apache::edit::tag_start($target,$token);
247: $result.=&Apache::edit::text_arg('Name:','name',$token).
248: &Apache::edit::select_arg('Type:','type',['text','image'],$token).
249: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
250: } elsif ($target eq 'modified') {
251: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
252: $safeeval,'name','type');
253: if ($constructtag) {
254: $result = &Apache::edit::rebuild_tag($token);
255: $result.=&Apache::edit::handle_insert();
256: }
1.4 albertel 257: }
1.47 albertel 258: return $result;
1.1 tsai 259: }
260:
1.5 albertel 261: sub add_vars {
1.47 albertel 262: my ($name,$order,$label,$labelorder,$value,$image,$safeeval) = @_;
263: my $code = '${'.$name."}{'".($order+1)."'}='".$label."';";
264: my $out=Apache::run::run($code,$safeeval);
265: if ($value) {
266: $code = '${'.$name."}{'value_".($order+1)."'}='".$value."';";
267: $out=Apache::run::run($code,$safeeval);
268: $code = '${'.$name."}{'labelvalue_".($labelorder+1)."'}='".$value."';";
269: $out=Apache::run::run($code,$safeeval);
270: }
271: if ($image) {
272: my $code = '${'.$name."}{'image_".($order+1)."'}='".$image."';";
273: my $out=Apache::run::run($code,$safeeval);
274: }
275: $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
1.5 albertel 276: $out=Apache::run::run($code,$safeeval);
1.4 albertel 277: }
1.5 albertel 278:
1.1 tsai 279: # begin to assign labels to locations
280: sub end_labelgroup {
1.47 albertel 281: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
282: my $gname = $Apache::randomlabel::groupname;
283: my $type = $Apache::randomlabel::type;
284: my $result='';
285: if ($target eq 'web' || $target eq 'answer' || $target eq 'grade' ||
286: $target eq 'analyze') {
287: my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
288: &Apache::structuretags::shuffle(\@idx_arr);
289: for(0 .. $#Apache::randomlabel::label_arr) {
290: my $str;
291: my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$_] ]";
292: my $x = $Apache::randomlabel::xcoord[$_];
293: my $y = $Apache::randomlabel::ycoord[$_];
294: my $value = $Apache::randomlabel::value[$_];
1.59 albertel 295: my $i=$Apache::randomlabel::obj_cnt++;
1.47 albertel 296: if( $type eq 'text') {
297: &add_vars($gname,$_,$label,$idx_arr[$_],$value,'',$safeeval);
1.59 albertel 298: $str = join(':',$x,$y,&Apache::lonnet::escape($label));
299: $args{"cgi.$cgi_id.OBJTYPE"}.='LABEL:';
1.47 albertel 300: } elsif ( $type eq 'image') {
301: &add_vars($gname,$_,
302: $Apache::randomlabel::description[$idx_arr[$_]],
303: $idx_arr[$_],$value,$label,$safeeval);
1.59 albertel 304: $str = join(':',$x,$y,&Apache::lonnet::escape($label));
305: $args{"cgi.$cgi_id.OBJTYPE"}.='IMAGE:';
1.47 albertel 306: } else {
307: &Apache::lonxml::error('Unknown type of label :'.$type.':');
308: }
1.59 albertel 309: if ($target eq 'web') { $args{"cgi.$cgi_id.OBJ$i"} =$str; }
1.47 albertel 310: }
311: } elsif ($target eq 'tex') {
312: my $WX1=0; # Web x-coord. of upper left corner (ULC)
313: my $WY1=0; # Web y-coord. of (ULC)
314: my $wwidth=&Apache::lonxml::get_param('width',$parstack,$safeeval,-2);
315: my $wheight=&Apache::lonxml::get_param('height',$parstack,$safeeval,-2);
1.50 sakharuk 316: my $texwidth=&adjust_textwidth(&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,-2,1));
1.49 albertel 317: if (!$texwidth) { $texwidth=90; }
1.47 albertel 318: my $TX1=0;
319: my $TY1=$texwidth*($wheight/$wwidth);
320: my $TX2=$texwidth;
321: my $TY2=0;
322:
323:
324: my $slopex=($wwidth-$WX1)/($TX2-$TX1);
325: my $slopey=($wheight-$WY1)/($TY2-($TY1-1.0));
326: my $cstx=$wwidth-$slopex*($TX2);
327: my $csty=$wheight-$slopey*($TY2);
328:
329: my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
330: &Apache::structuretags::shuffle(\@idx_arr);
331:
332: &Apache::lonxml::debug("Array is:".$#Apache::randomlabel::label_arr.":");
333: for(my $i=0;$i <= $#Apache::randomlabel::label_arr; $i++) {
334: my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$i] ]";
335: my $x = $Apache::randomlabel::xcoord[$i];
336: my $y = $Apache::randomlabel::ycoord[$i];
337: my $value = $Apache::randomlabel::value[$i];
338: #x latex coordinate
339: my $tcX=($x)*($texwidth/$wwidth);
340: #y latex coordinate
341: # my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
342: my $tcY=$TY1-$y*($TY1/$wheight);
343: $tcX=sprintf('%.2f',$tcX);
344: $tcY=sprintf('%.2f',$tcY);
345: $result.='\put('.$tcX.','.$tcY.'){\normalsize \bf '.$label.'}'."\n";
346: if( $type eq 'text') {
347: &add_vars($gname,$i,$label,$idx_arr[$i],$value,'',$safeeval);
348: } elsif ( $type eq 'image') {
349: &add_vars($gname,$i,
350: $Apache::randomlabel::description[$idx_arr[$i]],
351: $idx_arr[$i],$value,$label,$safeeval);
352: } else {
353: &Apache::lonxml::error('Unknown type of label :'.$type.':');
354: }
355: }
356: } elsif ($target eq 'edit') {
357: $result.=&Apache::edit::end_table;
1.3 tsai 358: }
1.47 albertel 359: return $result;
1.1 tsai 360: }
361:
1.5 albertel 362: # <location x="123" y="456" value="some value"/>
1.1 tsai 363: sub start_location {
1.47 albertel 364: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
365: my $x= &check_int(&Apache::lonxml::get_param('x',$parstack,$safeeval),50);
366: my $y= &check_int(&Apache::lonxml::get_param('y',$parstack,$safeeval),50);
367: my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
368: my $result='';
369: push(@Apache::randomlabel::xcoord,$x);
370: push(@Apache::randomlabel::ycoord,$y);
371: push(@Apache::randomlabel::value,$value);
372: if ($target eq 'edit') {
373: $result.=&Apache::edit::tag_start($target,$token);
374: $result.=&Apache::edit::text_arg('X:','x',$token,4).
375: &Apache::edit::text_arg('Y:','y',$token,4).
376: &Apache::edit::entercoords('x','y','attribute','width','height').' '.
377: &Apache::edit::text_arg('Value:','value',$token).
378: &Apache::edit::end_row();
379: $result.=&Apache::edit::end_table;
380: } elsif ($target eq 'modified') {
381: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
382: $safeeval,'x','y','value');
383: if ($constructtag) {
384: $result = &Apache::edit::rebuild_tag($token);
385: $result.=&Apache::edit::handle_insert();
386: }
1.4 albertel 387: }
1.47 albertel 388: return $result;
1.1 tsai 389: }
390:
391: sub end_location {
1.47 albertel 392: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
393: my @result;
394: if ($target eq 'edit') { @result=('','no') }
395: return @result;
1.1 tsai 396: }
397:
1.2 tsai 398: # <label>$var_abc</label>
1.1 tsai 399: sub start_label {
1.47 albertel 400: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
401: my $result='';
402: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
403: if ($target eq 'web' || $target eq 'tex' ||
404: $target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
1.57 albertel 405: &Apache::lonxml::startredirection;
1.47 albertel 406: } elsif ($target eq 'edit') {
407: $result.=&Apache::edit::tag_start($target,$token,"$type Label");
408: my $text=&Apache::lonxml::get_all_text("/label",$parser);
409: if ($type eq 'image') {
410: $result.=&Apache::edit::end_row().
411: &Apache::edit::start_spanning_row();
412: $result.=&Apache::edit::text_arg('Description:','description',
413: $token);
414: }
415: if ($type eq 'text') { $result.="Label Text:"; }
416: if ($type eq 'image') { $result.="Path to image: "; }
417: $result.=&Apache::edit::editline('',$text,'',50).
418: &Apache::edit::end_table();
419: } elsif ($target eq 'modified') {
420: $result = '<label>';
421: if ($type eq 'image') {
422: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
423: $safeeval,
424: 'description');
425: if ($constructtag) {
426: $result = &Apache::edit::rebuild_tag($token);
427: } else {
428: $result = $token->[4];
429: }
430: }
1.52 albertel 431: $result.=&Apache::edit::modifiedfield("/label",$parser);
1.26 albertel 432: }
1.47 albertel 433: return $result;
1.1 tsai 434: }
435:
436: sub end_label {
1.47 albertel 437: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
438: my @result;
1.57 albertel 439: if ($target eq 'edit') {
440: @result=('','no') ;
441: } elsif ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
442: $target eq 'answer' || $target eq 'analyze') {
443: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
444: my $ltext=&Apache::lonxml::endredirection;
445: if ($type eq 'image') {
446: &Apache::lonxml::debug("Turning $ltext, $Apache::lonxml::pwd[-1]");
447: $ltext=&Apache::imageresponse::clean_up_image($ltext);
448: # $ltext=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],
449: # $ltext);
450: &Apache::lonxml::debug("into $ltext");
451: my $description = &Apache::lonxml::get_param('description',
452: $parstack,$safeeval);
453: push(@Apache::randomlabel::description,$description);
454: }
455: push(@Apache::randomlabel::label_arr,$ltext);
456: }
1.47 albertel 457: return @result;
1.1 tsai 458: }
459:
460: 1;
461: __END__
462:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>