Annotation of loncom/homework/randomlabel.pm, revision 1.72
1.1 tsai 1: # The LearningOnline Network with CAPA
2: # random labelling tool
1.8 albertel 3: #
1.72 ! foxr 4: # $Id: randomlabel.pm,v 1.71 2005/04/12 21:41:28 foxr 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.1 tsai 28: # SYNTAX:
1.4 albertel 29: # <randomlabel bgimg="URL" width="12" height="45" texwidth="50">
30: # <labelgroup name="GroupOne" type="image">
31: # <location x="123" y="456" />
32: # <location x="321" y="654" />
33: # <location x="213" y="546" />
34: # <label description="TEXT-1">IMG-URL</label>
35: # <label description="TEXT-2">IMG-URL</label>
36: # <label description="TEXT-3">IMG-URL</label>
1.1 tsai 37: # </labelgroup>
1.4 albertel 38: # <labelgroup name="GroupTwo" type="text">
39: # <location x="12" y="45" />
40: # <location x="32" y="65" />
41: # <location x="21" y="54" />
1.3 tsai 42: # <label>TEXT-1</label>
43: # <label>TEXT-2</label>
44: # <label>TEXT-3</label>
1.1 tsai 45: # </labelgroup>
46: # </randomlabel>
47: # ===========================================
1.3 tsai 48: # side effect:
49: # location (123,456): $GroupOne[0] = 2 # images give out indexes
50: # (321,654): $GroupOne[1] = 1
51: # (213,546): $GroupOne[2] = 0
52: # location (12,45) : $GroupTwo[0] = "TEXT-3"
53: # (32,65) : $GroupTwo[1] = "TEXT-1"
54: # (21,54) : $GroupTwo[2] = "TEXT-2"
1.1 tsai 55: # ===========================================
56: package Apache::randomlabel;
1.24 sakharuk 57: use Apache::lonnet;
1.1 tsai 58: use strict;
1.13 sakharuk 59: use Apache::edit;
1.36 sakharuk 60: use Apache::File();
1.38 sakharuk 61: use Apache::Constants qw(:common :http);
1.72 ! foxr 62: use Image::Magick;
1.1 tsai 63:
1.53 albertel 64: my %args;
65: my $cgi_id;
66:
1.13 sakharuk 67: BEGIN {
1.57 albertel 68: &Apache::lonxml::register('Apache::randomlabel',('randomlabel','labelgroup','location','label','bgimg'));
1.1 tsai 69: }
70:
1.23 matthew 71: sub check_int {
72: # utility function to do error checking on a integer.
73: my ($num,$default) = @_;
74: $default = 100 if (! defined($default));
75: $num =~ s/\s+//g; # We dont need no stinkin white space!
76: # If it is a real, just grab the integer part.
77: ($num,undef) = split (/\./,$num) if ($num =~ /\./);
78: # set to default if what we have left doesn't match anything...
79: $num = $default unless ($num =~/^\d+$/);
80: return $num;
81: }
82:
1.64 albertel 83: my ($height_param,$width_param);
1.1 tsai 84: sub start_randomlabel {
1.47 albertel 85: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
86: my $result='';
87: push (@Apache::lonxml::namespace,'randomlabel');
1.64 albertel 88: ($height_param,$width_param)=(0,0);
1.47 albertel 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);
1.66 raeburn 92: if (&Apache::lonnet::repcopy($bgimg) ne 'ok') {
1.47 albertel 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.64 albertel 101: } elsif ($target eq 'tex' && defined($bgimg)) {
102: $result.=&make_eps_image($bgimg,$parstack,$safeeval);
1.47 albertel 103: } elsif ($target eq 'edit') {
104: $result.=&Apache::edit::tag_start($target,$token);
105: $Apache::edit::bgimgsrc=
106: &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
107: $Apache::edit::bgimgsrccurdepth=$Apache::lonxml::curdepth;
108: $result.=&Apache::edit::text_arg('Image:','bgimg',$token,75).' ';
109: $result.=&Apache::edit::browse('bgimg').' ';
110: $result.=&Apache::edit::search('bgimg').'<br />'.
111: &Apache::edit::text_arg('Width(pixel):' ,'width' ,$token,6).
112: &Apache::edit::text_arg('Height(pixel):','height' ,$token,6).
113: &Apache::edit::text_arg('TeXWidth(mm):' ,'texwidth',$token,6).
114: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
115: } elsif ($target eq 'modified') {
116: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
117: $safeeval,'bgimg','width',
118: 'height','texwidth');
119: if ($constructtag) {
120: $result = &Apache::edit::rebuild_tag($token);
121: $result.=&Apache::edit::handle_insert();
122: }
1.31 sakharuk 123: }
1.47 albertel 124: return $result;
1.1 tsai 125: }
126:
127: sub end_randomlabel {
1.47 albertel 128: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
129: my $result='';
130: my $count;
131: pop @Apache::lonxml::namespace;
132: if ($target eq 'web') {
1.59 albertel 133: $count = $Apache::randomlabel::obj_cnt;
134: if( $count != 0) { $args{"cgi.$cgi_id.OBJCOUNT"}=$count; }
1.53 albertel 135: $result.='<img src="/adm/randomlabel.png?token='.$cgi_id.'" /><br />'."\n";
136: &Apache::lonnet::appenv(%args);
1.47 albertel 137: } elsif ($target eq 'tex') {
138: $result='\end{picture}\\\\';
1.64 albertel 139: $result.= ' \vskip -'.$height_param.' mm } \\\\ ';
1.47 albertel 140: } elsif ($target eq 'edit') {
141: $result.=&Apache::edit::end_table;
142: }
143: return $result;
1.1 tsai 144: }
145:
1.57 albertel 146: sub start_bgimg {
147: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
148: my $result='';
149: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.64 albertel 150: &Apache::lonxml::startredirection();
1.57 albertel 151: }
152: return $result;
153: }
154:
155: sub end_bgimg {
156: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
157: my $result='';
158: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.64 albertel 159: my $bgimg=&Apache::lonxml::endredirection();
1.57 albertel 160: if ($target eq 'web') {
161: $bgimg=&Apache::imageresponse::clean_up_image($bgimg);
162: $args{"cgi.$cgi_id.BGIMG"}=&Apache::lonnet::escape($bgimg);
163: } elsif ($target eq 'tex') {
1.64 albertel 164: $result.=&make_eps_image($bgimg,$parstack,$safeeval,-2);
1.57 albertel 165: }
166: }
167: return $result;
168: }
1.72 ! foxr 169: my $scale_factor; # image scale factor.
1.57 albertel 170: sub make_eps_image {
1.64 albertel 171: my ($bgimg,$parstack,$safeeval,$depth)=@_;
172: my ($path,$file) = &Apache::londefdef::get_eps_image($bgimg);
173: ($height_param,$width_param)=
174: &Apache::londefdef::image_size($bgimg,0.3,$parstack,$safeeval,
175: $depth,1);
176: my $dirtywidth=$width_param+5;
1.68 foxr 177: my $result ="\n".'\vspace*{2mm}\noindent'."\n".
178: '\parbox{'.$dirtywidth.
1.64 albertel 179: ' mm}{ \noindent \epsfxsize='.$width_param.
180: ' mm \epsffile{'.$path.$file.
1.68 foxr 181: '}\setlength{\unitlength}{1mm}'."\n".' \begin{picture}('.
182: $width_param.','.$height_param.')(0,-'.$height_param.')'."\n";
1.72 ! foxr 183: my $magick = Image::Magick->new;
! 184: $magick->Read($bgimg);
! 185: my $initial_width = $magick->Get('width');
! 186: $scale_factor = $width_param / $initial_width;
1.57 albertel 187: return $result;
188: }
189:
1.1 tsai 190: sub start_labelgroup {
1.47 albertel 191: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
192: my $result='';
193: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
194: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
195: $type =~tr/A-Z/a-z/;
1.48 albertel 196: if ($target ne 'modified' && ($name =~ /\W/ || $name =~ /^[0-9]/)) {
197: &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 />");
198: }
1.47 albertel 199: if ($target eq 'web' || $target eq 'tex' ||
200: $target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
201: $Apache::randomlabel::groupname=$name;
202: $Apache::randomlabel::type=$type;
203: @Apache::randomlabel::xcoord = ();
204: @Apache::randomlabel::ycoord = ();
205: @Apache::randomlabel::value = ();
206: @Apache::randomlabel::label_arr = ();
1.68 foxr 207: @Apache::randomlabel::description = ();
1.47 albertel 208: } elsif ($target eq 'edit') {
209: $result.=&Apache::edit::tag_start($target,$token);
210: $result.=&Apache::edit::text_arg('Name:','name',$token).
1.63 albertel 211: &Apache::edit::select_arg('Type:','type',['text','image'],$token);
212: if (!defined($token->[2]{'TeXsize'})) {
213: $token->[2]{'TeXsize'}='\normalsize';
214: }
215: $result.=&Apache::edit::select_arg('TeX font size:','TeXsize',
216: ['\tiny','\scriptsize',
217: '\footnotesize','\small',
218: '\normalsize','\large','\Large',
219: '\LARGE','\huge','\Huge'],
220: $token);
221: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.47 albertel 222: } elsif ($target eq 'modified') {
223: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.63 albertel 224: $safeeval,'name','type',
225: 'TeXsize');
1.47 albertel 226: if ($constructtag) {
227: $result = &Apache::edit::rebuild_tag($token);
228: $result.=&Apache::edit::handle_insert();
229: }
1.4 albertel 230: }
1.47 albertel 231: return $result;
1.1 tsai 232: }
233:
1.72 ! foxr 234: #
! 235: # Utility sub to compute the width of a label.
! 236: #
! 237: sub get_label_width {
! 238: my $label = shift;
! 239: &Apache::lonxml::debug("image label = $label");
! 240: if (-e $label) {
! 241: &Apache::lonxml::debug("$label exists");
! 242: } else {
! 243: &Apache::lonxml::debug("$label does not exist");
! 244: }
! 245: my $magick = Image::Magick->new;
! 246: $magick->Read($label);
! 247: my $pixel_width = $magick->Get('width');
! 248: return $pixel_width * $scale_factor;
! 249:
! 250:
! 251: }
1.5 albertel 252: sub add_vars {
1.47 albertel 253: my ($name,$order,$label,$labelorder,$value,$image,$safeeval) = @_;
1.61 albertel 254: if (!defined($name) || $name eq '') { return; }
1.47 albertel 255: my $code = '${'.$name."}{'".($order+1)."'}='".$label."';";
256: my $out=Apache::run::run($code,$safeeval);
257: if ($value) {
258: $code = '${'.$name."}{'value_".($order+1)."'}='".$value."';";
259: $out=Apache::run::run($code,$safeeval);
260: $code = '${'.$name."}{'labelvalue_".($labelorder+1)."'}='".$value."';";
261: $out=Apache::run::run($code,$safeeval);
262: }
263: if ($image) {
264: my $code = '${'.$name."}{'image_".($order+1)."'}='".$image."';";
265: my $out=Apache::run::run($code,$safeeval);
266: }
267: $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
1.5 albertel 268: $out=Apache::run::run($code,$safeeval);
1.4 albertel 269: }
1.5 albertel 270:
1.1 tsai 271: # begin to assign labels to locations
272: sub end_labelgroup {
1.47 albertel 273: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
274: my $gname = $Apache::randomlabel::groupname;
275: my $type = $Apache::randomlabel::type;
276: my $result='';
277: if ($target eq 'web' || $target eq 'answer' || $target eq 'grade' ||
278: $target eq 'analyze') {
279: my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
280: &Apache::structuretags::shuffle(\@idx_arr);
281: for(0 .. $#Apache::randomlabel::label_arr) {
282: my $str;
283: my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$_] ]";
284: my $x = $Apache::randomlabel::xcoord[$_];
285: my $y = $Apache::randomlabel::ycoord[$_];
286: my $value = $Apache::randomlabel::value[$_];
1.59 albertel 287: my $i=$Apache::randomlabel::obj_cnt++;
1.47 albertel 288: if( $type eq 'text') {
289: &add_vars($gname,$_,$label,$idx_arr[$_],$value,'',$safeeval);
1.59 albertel 290: $str = join(':',$x,$y,&Apache::lonnet::escape($label));
291: $args{"cgi.$cgi_id.OBJTYPE"}.='LABEL:';
1.47 albertel 292: } elsif ( $type eq 'image') {
293: &add_vars($gname,$_,
294: $Apache::randomlabel::description[$idx_arr[$_]],
295: $idx_arr[$_],$value,$label,$safeeval);
1.59 albertel 296: $str = join(':',$x,$y,&Apache::lonnet::escape($label));
297: $args{"cgi.$cgi_id.OBJTYPE"}.='IMAGE:';
1.47 albertel 298: } else {
299: &Apache::lonxml::error('Unknown type of label :'.$type.':');
300: }
1.59 albertel 301: if ($target eq 'web') { $args{"cgi.$cgi_id.OBJ$i"} =$str; }
1.47 albertel 302: }
303: } elsif ($target eq 'tex') {
304: my $WX1=0; # Web x-coord. of upper left corner (ULC)
305: my $WY1=0; # Web y-coord. of (ULC)
306: my $wwidth=&Apache::lonxml::get_param('width',$parstack,$safeeval,-2);
307: my $wheight=&Apache::lonxml::get_param('height',$parstack,$safeeval,-2);
1.63 albertel 308: my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
309: if (!defined($TeXsize)) { $TeXsize='\\normalsize'; }
1.62 albertel 310:
1.47 albertel 311: my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
312: &Apache::structuretags::shuffle(\@idx_arr);
313:
314: &Apache::lonxml::debug("Array is:".$#Apache::randomlabel::label_arr.":");
315: for(my $i=0;$i <= $#Apache::randomlabel::label_arr; $i++) {
316: my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$i] ]";
317: my $x = $Apache::randomlabel::xcoord[$i];
1.62 albertel 318: # FIXME the 3.5 here is the 'height' of the letter in TeX
319: my $y = $Apache::randomlabel::ycoord[$i]-3.5;
1.47 albertel 320: my $value = $Apache::randomlabel::value[$i];
321: #x latex coordinate
1.64 albertel 322: my $tcX=($x)*($width_param/$wwidth);
1.47 albertel 323: #y latex coordinate
324: # my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
1.64 albertel 325: my $tcY=$height_param-$y*($height_param/$wheight);
1.47 albertel 326: $tcX=sprintf('%.2f',$tcX);
327: $tcY=sprintf('%.2f',$tcY);
1.70 foxr 328: $result .= '\put('.$tcX.','.$tcY.'){';
1.47 albertel 329: if( $type eq 'text') {
1.70 foxr 330: $result.= $TeXsize.' \bf '.$label."}\n";
1.47 albertel 331: &add_vars($gname,$i,$label,$idx_arr[$i],$value,'',$safeeval);
332: } elsif ( $type eq 'image') {
1.71 foxr 333: my ($path,$file) = &Apache::londefdef::get_eps_image($label);
334: my $image_name = $path.$file;
1.72 ! foxr 335: my $label_width = get_label_width($label);
! 336:
! 337: $result .= '\includegraphics[width='.$label_width.'mm]{'
1.71 foxr 338: .$image_name."}}\n";
1.47 albertel 339: &add_vars($gname,$i,
340: $Apache::randomlabel::description[$idx_arr[$i]],
341: $idx_arr[$i],$value,$label,$safeeval);
342: } else {
343: &Apache::lonxml::error('Unknown type of label :'.$type.':');
344: }
345: }
346: } elsif ($target eq 'edit') {
347: $result.=&Apache::edit::end_table;
1.3 tsai 348: }
1.47 albertel 349: return $result;
1.1 tsai 350: }
351:
1.5 albertel 352: # <location x="123" y="456" value="some value"/>
1.1 tsai 353: sub start_location {
1.47 albertel 354: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
355: my $x= &check_int(&Apache::lonxml::get_param('x',$parstack,$safeeval),50);
356: my $y= &check_int(&Apache::lonxml::get_param('y',$parstack,$safeeval),50);
357: my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
358: my $result='';
359: push(@Apache::randomlabel::xcoord,$x);
360: push(@Apache::randomlabel::ycoord,$y);
361: push(@Apache::randomlabel::value,$value);
362: if ($target eq 'edit') {
363: $result.=&Apache::edit::tag_start($target,$token);
364: $result.=&Apache::edit::text_arg('X:','x',$token,4).
365: &Apache::edit::text_arg('Y:','y',$token,4).
366: &Apache::edit::entercoords('x','y','attribute','width','height').' '.
367: &Apache::edit::text_arg('Value:','value',$token).
368: &Apache::edit::end_row();
369: $result.=&Apache::edit::end_table;
370: } elsif ($target eq 'modified') {
371: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
372: $safeeval,'x','y','value');
373: if ($constructtag) {
374: $result = &Apache::edit::rebuild_tag($token);
375: $result.=&Apache::edit::handle_insert();
376: }
1.4 albertel 377: }
1.47 albertel 378: return $result;
1.1 tsai 379: }
380:
381: sub end_location {
1.47 albertel 382: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
383: my @result;
384: if ($target eq 'edit') { @result=('','no') }
385: return @result;
1.1 tsai 386: }
387:
1.2 tsai 388: # <label>$var_abc</label>
1.1 tsai 389: sub start_label {
1.47 albertel 390: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
391: my $result='';
392: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
393: if ($target eq 'web' || $target eq 'tex' ||
394: $target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
1.57 albertel 395: &Apache::lonxml::startredirection;
1.47 albertel 396: } elsif ($target eq 'edit') {
397: $result.=&Apache::edit::tag_start($target,$token,"$type Label");
398: my $text=&Apache::lonxml::get_all_text("/label",$parser);
399: if ($type eq 'image') {
400: $result.=&Apache::edit::end_row().
401: &Apache::edit::start_spanning_row();
402: $result.=&Apache::edit::text_arg('Description:','description',
403: $token);
404: }
405: if ($type eq 'text') { $result.="Label Text:"; }
406: if ($type eq 'image') { $result.="Path to image: "; }
407: $result.=&Apache::edit::editline('',$text,'',50).
408: &Apache::edit::end_table();
409: } elsif ($target eq 'modified') {
410: $result = '<label>';
411: if ($type eq 'image') {
412: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
413: $safeeval,
414: 'description');
415: if ($constructtag) {
416: $result = &Apache::edit::rebuild_tag($token);
417: } else {
418: $result = $token->[4];
419: }
420: }
1.52 albertel 421: $result.=&Apache::edit::modifiedfield("/label",$parser);
1.26 albertel 422: }
1.47 albertel 423: return $result;
1.1 tsai 424: }
425:
426: sub end_label {
1.47 albertel 427: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
428: my @result;
1.57 albertel 429: if ($target eq 'edit') {
430: @result=('','no') ;
431: } elsif ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
432: $target eq 'answer' || $target eq 'analyze') {
433: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
434: my $ltext=&Apache::lonxml::endredirection;
435: if ($type eq 'image') {
1.70 foxr 436: if ($target eq 'tex') {
437: # For tex targets, our image url has been potentially corrupted
438: # by prepending \'s in front of special latex symbols.
439: # For now we only worry about the _ case (most common?)
440: # There's a whole host of theim in lonxml::latex_special_symbols
441: # that could potentially have to be re-done.
442:
443: $ltext =~ s/\\_/_/g;
444: }
1.57 albertel 445: &Apache::lonxml::debug("Turning $ltext, $Apache::lonxml::pwd[-1]");
446: $ltext=&Apache::imageresponse::clean_up_image($ltext);
447: # $ltext=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],
448: # $ltext);
449: &Apache::lonxml::debug("into $ltext");
450: my $description = &Apache::lonxml::get_param('description',
451: $parstack,$safeeval);
452: push(@Apache::randomlabel::description,$description);
1.61 albertel 453: } else {
454: $ltext=~s/[\r\n]*//gs
1.57 albertel 455: }
456: push(@Apache::randomlabel::label_arr,$ltext);
457: }
1.47 albertel 458: return @result;
1.1 tsai 459: }
460:
461: 1;
462: __END__
463:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>