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