Annotation of loncom/homework/randomlabel.pm, revision 1.83
1.1 tsai 1: # The LearningOnline Network with CAPA
2: # random labelling tool
1.8 albertel 3: #
1.83 ! foxr 4: # $Id: randomlabel.pm,v 1.82 2006/05/30 12:45:37 www 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.73 foxr 63: use Apache::lonplot;
1.82 www 64: use lib '/home/httpd/lib/perl/';
65: use LONCAPA;
66:
1.1 tsai 67:
1.53 albertel 68: my %args;
69: my $cgi_id;
1.73 foxr 70: my $scale_factor; # image scale factor.
1.75 foxr 71: my $label_xscale; # Label scale factor (needed for gnuplot).
72: my $label_yscale;
1.76 foxr 73:
74:
1.13 sakharuk 75: BEGIN {
1.57 albertel 76: &Apache::lonxml::register('Apache::randomlabel',('randomlabel','labelgroup','location','label','bgimg'));
1.1 tsai 77: }
78:
1.77 foxr 79:
1.76 foxr 80:
1.23 matthew 81: sub check_int {
82: # utility function to do error checking on a integer.
83: my ($num,$default) = @_;
84: $default = 100 if (! defined($default));
85: $num =~ s/\s+//g; # We dont need no stinkin white space!
86: # If it is a real, just grab the integer part.
87: ($num,undef) = split (/\./,$num) if ($num =~ /\./);
88: # set to default if what we have left doesn't match anything...
89: $num = $default unless ($num =~/^\d+$/);
90: return $num;
91: }
92:
1.77 foxr 93: # Get width/height from an image tag...
94: #
95: # Parameters:
96: # tag - tag potentially containing height/width attributes.
97: # def_width - Default width.
98: # def_height - Default height.
99: # Returns:
100: # list containing width/height.
101: #
102: sub extract_tag_sizes {
103: my ($tag, $dw, $dh) = @_;
104: $tag =~ s/\s+/ /g; # Collapse whitespace.
105: $tag =~ s/\s*=\s*/=/g; # kill space around ='s.
106: $tag =~ s/[<>\"]//g; # Get rid of the <">'s too.
107:
108: &Apache::lonxml::debug("Compressed tag: $tag");
109: my @taglist = split(/ /,$tag);
110: foreach my $attribute (@taglist) {
111: if ($attribute =~ /^width/i) {
112: my ($e, $s)= split(/=/,$attribute);
113: $dw = $s;
114: }
115: if ($attribute =~ /^height/i) {
116: my ($e, $s) = split(/=/,$attribute);
117: $dh = $s;
118: }
119: }
120: return($dw, $dh);
121:
122: }
123:
1.64 albertel 124: my ($height_param,$width_param);
1.1 tsai 125: sub start_randomlabel {
1.76 foxr 126:
1.47 albertel 127: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
128: my $result='';
129: push (@Apache::lonxml::namespace,'randomlabel');
1.64 albertel 130: ($height_param,$width_param)=(0,0);
1.75 foxr 131: $label_xscale = 1.0; # Assume image size not overridden.
132: $label_yscale = 1.0;
1.47 albertel 133: my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
1.60 albertel 134: if ( defined($bgimg) && $bgimg !~ /^http:/ ) {
1.47 albertel 135: $bgimg=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$bgimg);
1.66 raeburn 136: if (&Apache::lonnet::repcopy($bgimg) ne 'ok') {
1.47 albertel 137: $bgimg='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
138: }
1.42 www 139: }
1.59 albertel 140: $Apache::randomlabel::obj_cnt=0;
1.47 albertel 141: if ($target eq 'web') {
1.53 albertel 142: $cgi_id=&Apache::loncommon::get_cgi_id();
143: %args=();
1.82 www 144: $args{"cgi.$cgi_id.BGIMG"}=&escape($bgimg);
1.75 foxr 145: $height_param = &Apache::lonxml::get_param('height',$parstack, $safeeval);
146: $width_param = &Apache::lonxml::get_param('width', $parstack, $safeeval);
1.64 albertel 147: } elsif ($target eq 'tex' && defined($bgimg)) {
148: $result.=&make_eps_image($bgimg,$parstack,$safeeval);
1.47 albertel 149: } elsif ($target eq 'edit') {
150: $result.=&Apache::edit::tag_start($target,$token);
151: $Apache::edit::bgimgsrc=
152: &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
153: $Apache::edit::bgimgsrccurdepth=$Apache::lonxml::curdepth;
154: $result.=&Apache::edit::text_arg('Image:','bgimg',$token,75).' ';
155: $result.=&Apache::edit::browse('bgimg').' ';
156: $result.=&Apache::edit::search('bgimg').'<br />'.
157: &Apache::edit::text_arg('Width(pixel):' ,'width' ,$token,6).
158: &Apache::edit::text_arg('Height(pixel):','height' ,$token,6).
159: &Apache::edit::text_arg('TeXWidth(mm):' ,'texwidth',$token,6).
160: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
161: } elsif ($target eq 'modified') {
162: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
163: $safeeval,'bgimg','width',
164: 'height','texwidth');
165: if ($constructtag) {
166: $result = &Apache::edit::rebuild_tag($token);
167: $result.=&Apache::edit::handle_insert();
168: }
1.31 sakharuk 169: }
1.47 albertel 170: return $result;
1.1 tsai 171: }
172:
173: sub end_randomlabel {
1.47 albertel 174: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
175: my $result='';
176: my $count;
177: pop @Apache::lonxml::namespace;
178: if ($target eq 'web') {
1.59 albertel 179: $count = $Apache::randomlabel::obj_cnt;
180: if( $count != 0) { $args{"cgi.$cgi_id.OBJCOUNT"}=$count; }
1.53 albertel 181: $result.='<img src="/adm/randomlabel.png?token='.$cgi_id.'" /><br />'."\n";
182: &Apache::lonnet::appenv(%args);
1.47 albertel 183: } elsif ($target eq 'tex') {
184: $result='\end{picture}\\\\';
1.64 albertel 185: $result.= ' \vskip -'.$height_param.' mm } \\\\ ';
1.47 albertel 186: } elsif ($target eq 'edit') {
187: $result.=&Apache::edit::end_table;
188: }
189: return $result;
1.1 tsai 190: }
191:
1.77 foxr 192:
1.57 albertel 193: sub start_bgimg {
194: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
195: my $result='';
196: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.64 albertel 197: &Apache::lonxml::startredirection();
1.57 albertel 198: }
199: return $result;
200: }
201:
202: sub end_bgimg {
203: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
204: my $result='';
205: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.64 albertel 206: my $bgimg=&Apache::lonxml::endredirection();
1.57 albertel 207: if ($target eq 'web') {
1.77 foxr 208:
209: # If the tag produced has sizes, they override ours.
210: # (for now anyway).
211: #
212: &Apache::lonxml::debug("Base sizes: $width_param x $height_param");
213:
214: my ($plot_x, $plot_y) = &extract_tag_sizes($bgimg,
215: $width_param,
1.78 albertel 216: $height_param);
1.77 foxr 217: &Apache::lonxml::debug("Extracted sizes: $plot_x x $plot_y");
1.78 albertel 218: if ($width_param) {
219: $label_xscale = $plot_x / $width_param;
220: }
221: if ($height_param) {
222: $label_yscale = $plot_y / $height_param;
223: }
1.77 foxr 224: &Apache::lonxml::debug("Scale factors: $label_xscale $label_yscale");
225:
1.75 foxr 226: &Apache::lonxml::debug("Image: $bgimg");
1.57 albertel 227: $bgimg=&Apache::imageresponse::clean_up_image($bgimg);
1.75 foxr 228: &Apache::lonxml::debug("Cleaned image: $bgimg");
1.82 www 229: $args{"cgi.$cgi_id.BGIMG"}=&escape($bgimg);
1.57 albertel 230: } elsif ($target eq 'tex') {
1.73 foxr 231: # Some bg images can create latex for us... e.g. gnuplot.
232: # If it looks like we have some latex use that,
233: # otherwise, assume this is a resource name that must
234: # be converted into the latex to create an eps insertion.
235: #
236: my $src = $bgimg;
237: $src =~ s/\s+$//s;
238: $src =~ s/^\s+//s;
239:
1.77 foxr 240: #If this is a dynamically generated image, it will
241: #be in latex already, with a comment header that
242: #describes the dimensions:
243:
244: if($src =~ /^%DYNAMICIMAGE:/) {
245: $Apache::lonxml::debug = 0;
246: &Apache::lonxml::debug("Dynamic image");
247: my ($commentline, $junk) = split(/\n/, $src);
248: &Apache::lonxml::debug("Comment line was: $commentline");
249: my $trash;
250: my $initial_width;
251: ($trash, $initial_width, $height_param, $width_param) =
252: split(/:/,$commentline);
253: &Apache::lonxml::debug("internal web Width/height: $initial_width $height_param");
254: &Apache::lonxml::debug("Texwitdh: $width_param");
255: if($initial_width == 0) {
256: $initial_width = $width_param;
257: }
258: # strip off the comments since output does not always
259: # preserve \n's:
260: #
261: $src =~ s/$commentline//;
1.74 foxr 262: $scale_factor = $width_param / $initial_width;
263: $height_param = $height_param*$scale_factor;
1.77 foxr 264:
265: $label_xscale = 1.0; # $scale_factor;
266: $label_yscale = 1.0; # $scale_factor;
267:
1.73 foxr 268: &Apache::lonxml::debug("height $height_param");
269: &Apache::lonxml::debug("Width $width_param");
1.77 foxr 270: &Apache::lonxml::debug("Scale factors: $label_xscale $label_yscale");
1.73 foxr 271: my $dirty_width = $width_param + 5;
272: $result .= '\parbox{'.$dirty_width.'mm}{';
1.77 foxr 273: $result .= " $src \n";
1.74 foxr 274: $result .= '\setlength{\unitlength}{1mm}'."\n";
1.73 foxr 275: $result .= '\begin{picture}('."$height_param,$width_param)";
276: $result .= "(0,-$height_param)";
1.74 foxr 277: $result .= "\n";
1.77 foxr 278: $Apache::lonxml::debug = 0;
1.74 foxr 279:
1.73 foxr 280: } else {
281: $result.=&make_eps_image($bgimg,$parstack,$safeeval,-2);
282: }
1.57 albertel 283: }
284: }
285: return $result;
286: }
287: sub make_eps_image {
1.64 albertel 288: my ($bgimg,$parstack,$safeeval,$depth)=@_;
1.73 foxr 289: &Apache::lonxml::debug("image prior to get_eps_image: $bgimg");
1.64 albertel 290: my ($path,$file) = &Apache::londefdef::get_eps_image($bgimg);
1.73 foxr 291: &Apache::lonxml::debug("image after: $bgimg");
1.64 albertel 292: ($height_param,$width_param)=
293: &Apache::londefdef::image_size($bgimg,0.3,$parstack,$safeeval,
294: $depth,1);
1.73 foxr 295:
296: &Apache::lonxml::debug("Image size: $height_param x $width_param");
297:
1.64 albertel 298: my $dirtywidth=$width_param+5;
1.83 ! foxr 299: #
! 300: # Note that lonprintout/prinout will conspire to replace
! 301: # spaces in paths and names with _'s since spacess cause problems
! 302: # for latex included filenames. Therefore:
! 303: #
! 304: my $safe_path = $path;
! 305: $safe_path =~ s/ /\_/g;
! 306: my $safe_file = $file;
! 307: $safe_file =~ s/ /\_/g;
! 308:
1.68 foxr 309: my $result ="\n".'\vspace*{2mm}\noindent'."\n".
310: '\parbox{'.$dirtywidth.
1.64 albertel 311: ' mm}{ \noindent \epsfxsize='.$width_param.
1.83 ! foxr 312: ' mm \epsffile{'.$safe_path.$safe_file.
1.68 foxr 313: '}\setlength{\unitlength}{1mm}'."\n".' \begin{picture}('.
314: $width_param.','.$height_param.')(0,-'.$height_param.')'."\n";
1.72 foxr 315: my $magick = Image::Magick->new;
316: $magick->Read($bgimg);
317: my $initial_width = $magick->Get('width');
1.73 foxr 318: &Apache::lonxml::debug("ImageMagick thinks width is; $initial_width");
1.72 foxr 319: $scale_factor = $width_param / $initial_width;
1.57 albertel 320: return $result;
321: }
322:
1.1 tsai 323: sub start_labelgroup {
1.47 albertel 324: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
325: my $result='';
326: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
327: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
328: $type =~tr/A-Z/a-z/;
1.48 albertel 329: if ($target ne 'modified' && ($name =~ /\W/ || $name =~ /^[0-9]/)) {
330: &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 />");
331: }
1.47 albertel 332: if ($target eq 'web' || $target eq 'tex' ||
333: $target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
334: $Apache::randomlabel::groupname=$name;
335: $Apache::randomlabel::type=$type;
336: @Apache::randomlabel::xcoord = ();
337: @Apache::randomlabel::ycoord = ();
338: @Apache::randomlabel::value = ();
339: @Apache::randomlabel::label_arr = ();
1.68 foxr 340: @Apache::randomlabel::description = ();
1.47 albertel 341: } elsif ($target eq 'edit') {
342: $result.=&Apache::edit::tag_start($target,$token);
343: $result.=&Apache::edit::text_arg('Name:','name',$token).
1.63 albertel 344: &Apache::edit::select_arg('Type:','type',['text','image'],$token);
345: if (!defined($token->[2]{'TeXsize'})) {
346: $token->[2]{'TeXsize'}='\normalsize';
347: }
348: $result.=&Apache::edit::select_arg('TeX font size:','TeXsize',
349: ['\tiny','\scriptsize',
350: '\footnotesize','\small',
351: '\normalsize','\large','\Large',
352: '\LARGE','\huge','\Huge'],
353: $token);
354: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.47 albertel 355: } elsif ($target eq 'modified') {
356: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.63 albertel 357: $safeeval,'name','type',
358: 'TeXsize');
1.47 albertel 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:
1.72 foxr 367: #
368: # Utility sub to compute the width of a label.
369: #
370: sub get_label_width {
371: my $label = shift;
372: &Apache::lonxml::debug("image label = $label");
373: if (-e $label) {
374: &Apache::lonxml::debug("$label exists");
375: } else {
376: &Apache::lonxml::debug("$label does not exist");
377: }
378: my $magick = Image::Magick->new;
379: $magick->Read($label);
380: my $pixel_width = $magick->Get('width');
381: return $pixel_width * $scale_factor;
382:
383:
384: }
1.79 albertel 385:
386: sub get_label_height {
387: my $label = shift;
388: &Apache::lonxml::debug("image label = $label");
389: if (-e $label) {
390: &Apache::lonxml::debug("$label exists");
391: } else {
392: &Apache::lonxml::debug("$label does not exist");
393: }
394: my $magick = Image::Magick->new;
395: $magick->Read($label);
396: my $pixel_height = $magick->Get('height');
397: return $pixel_height * $scale_factor;
398: }
399:
1.5 albertel 400: sub add_vars {
1.47 albertel 401: my ($name,$order,$label,$labelorder,$value,$image,$safeeval) = @_;
1.61 albertel 402: if (!defined($name) || $name eq '') { return; }
1.47 albertel 403: my $code = '${'.$name."}{'".($order+1)."'}='".$label."';";
404: my $out=Apache::run::run($code,$safeeval);
405: if ($value) {
406: $code = '${'.$name."}{'value_".($order+1)."'}='".$value."';";
407: $out=Apache::run::run($code,$safeeval);
408: $code = '${'.$name."}{'labelvalue_".($labelorder+1)."'}='".$value."';";
409: $out=Apache::run::run($code,$safeeval);
410: }
411: if ($image) {
412: my $code = '${'.$name."}{'image_".($order+1)."'}='".$image."';";
413: my $out=Apache::run::run($code,$safeeval);
414: }
415: $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
1.5 albertel 416: $out=Apache::run::run($code,$safeeval);
1.4 albertel 417: }
1.5 albertel 418:
1.1 tsai 419: # begin to assign labels to locations
420: sub end_labelgroup {
1.47 albertel 421: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
422: my $gname = $Apache::randomlabel::groupname;
423: my $type = $Apache::randomlabel::type;
424: my $result='';
425: if ($target eq 'web' || $target eq 'answer' || $target eq 'grade' ||
426: $target eq 'analyze') {
427: my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
428: &Apache::structuretags::shuffle(\@idx_arr);
429: for(0 .. $#Apache::randomlabel::label_arr) {
430: my $str;
431: my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$_] ]";
432: my $x = $Apache::randomlabel::xcoord[$_];
433: my $y = $Apache::randomlabel::ycoord[$_];
434: my $value = $Apache::randomlabel::value[$_];
1.59 albertel 435: my $i=$Apache::randomlabel::obj_cnt++;
1.47 albertel 436: if( $type eq 'text') {
437: &add_vars($gname,$_,$label,$idx_arr[$_],$value,'',$safeeval);
1.82 www 438: $str = join(':',$x,$y,&escape($label));
1.59 albertel 439: $args{"cgi.$cgi_id.OBJTYPE"}.='LABEL:';
1.47 albertel 440: } elsif ( $type eq 'image') {
441: &add_vars($gname,$_,
442: $Apache::randomlabel::description[$idx_arr[$_]],
443: $idx_arr[$_],$value,$label,$safeeval);
1.82 www 444: $str = join(':',$x,$y,&escape($label));
1.59 albertel 445: $args{"cgi.$cgi_id.OBJTYPE"}.='IMAGE:';
1.47 albertel 446: } else {
447: &Apache::lonxml::error('Unknown type of label :'.$type.':');
448: }
1.59 albertel 449: if ($target eq 'web') { $args{"cgi.$cgi_id.OBJ$i"} =$str; }
1.47 albertel 450: }
451: } elsif ($target eq 'tex') {
452: my $WX1=0; # Web x-coord. of upper left corner (ULC)
453: my $WY1=0; # Web y-coord. of (ULC)
454: my $wwidth=&Apache::lonxml::get_param('width',$parstack,$safeeval,-2);
455: my $wheight=&Apache::lonxml::get_param('height',$parstack,$safeeval,-2);
1.63 albertel 456: my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
457: if (!defined($TeXsize)) { $TeXsize='\\normalsize'; }
1.62 albertel 458:
1.47 albertel 459: my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
460: &Apache::structuretags::shuffle(\@idx_arr);
461:
462: &Apache::lonxml::debug("Array is:".$#Apache::randomlabel::label_arr.":");
1.77 foxr 463: $Apache::lonxml::debug = 0;
1.47 albertel 464: for(my $i=0;$i <= $#Apache::randomlabel::label_arr; $i++) {
465: my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$i] ]";
466: my $x = $Apache::randomlabel::xcoord[$i];
1.80 albertel 467: my $y = $Apache::randomlabel::ycoord[$i];
468: if ( $type eq 'text' ) {
469: # FIXME the 3.5 here is the 'height' of the letter in TeX
470: $y=$y-3.5;
471: }
1.77 foxr 472: &Apache::lonxml::debug("initially: x= $x y= $y");
1.47 albertel 473: my $value = $Apache::randomlabel::value[$i];
474: #x latex coordinate
1.64 albertel 475: my $tcX=($x)*($width_param/$wwidth);
1.77 foxr 476: &Apache::lonxml::debug("wparam = $width_param wwidth = $wwidth, texx = $tcX");
1.47 albertel 477: #y latex coordinate
478: # my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
1.64 albertel 479: my $tcY=$height_param-$y*($height_param/$wheight);
1.79 albertel 480: if ( $type eq 'image') {
481: my $label_height = &get_label_height($label);
482: $tcY=$tcY-$label_height;
483: }
484:
1.77 foxr 485: &Apache::lonxml::debug("hparam = $height_param wheight = $wheight texy = $tcY");
1.47 albertel 486: $tcX=sprintf('%.2f',$tcX);
487: $tcY=sprintf('%.2f',$tcY);
1.70 foxr 488: $result .= '\put('.$tcX.','.$tcY.'){';
1.47 albertel 489: if( $type eq 'text') {
1.70 foxr 490: $result.= $TeXsize.' \bf '.$label."}\n";
1.47 albertel 491: &add_vars($gname,$i,$label,$idx_arr[$i],$value,'',$safeeval);
492: } elsif ( $type eq 'image') {
1.71 foxr 493: my ($path,$file) = &Apache::londefdef::get_eps_image($label);
494: my $image_name = $path.$file;
1.83 ! foxr 495: #
! 496: # Note that spaces in e.. \includegraphics cause problems for Latex
! 497: # so they get replaced by _'s by lonprintout/printout and us:
! 498: #
! 499: $image_name =~ s/ /\_/g;
1.79 albertel 500: my $label_width = &get_label_width($label);
1.72 foxr 501:
502: $result .= '\includegraphics[width='.$label_width.'mm]{'
1.71 foxr 503: .$image_name."}}\n";
1.47 albertel 504: &add_vars($gname,$i,
505: $Apache::randomlabel::description[$idx_arr[$i]],
506: $idx_arr[$i],$value,$label,$safeeval);
507: } else {
508: &Apache::lonxml::error('Unknown type of label :'.$type.':');
509: }
510: }
1.77 foxr 511: $Apache::lonxml::debug =0;
1.47 albertel 512: } elsif ($target eq 'edit') {
513: $result.=&Apache::edit::end_table;
1.3 tsai 514: }
1.47 albertel 515: return $result;
1.1 tsai 516: }
517:
1.5 albertel 518: # <location x="123" y="456" value="some value"/>
1.1 tsai 519: sub start_location {
1.77 foxr 520: $Apache::lonxml::debug = 0;
1.47 albertel 521: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
522: my $x= &check_int(&Apache::lonxml::get_param('x',$parstack,$safeeval),50);
523: my $y= &check_int(&Apache::lonxml::get_param('y',$parstack,$safeeval),50);
1.75 foxr 524: &Apache::lonxml::debug("x = $x y = $y");
525: $x = $x*$label_xscale;
526: $y = $y*$label_yscale;
527: &Apache::lonxml::debug(" H = $height_param W = $width_param");
528: &Apache::lonxml::debug(" XS = $label_xscale YS = $label_yscale");
529: &Apache::lonxml::debug(" X = $x Y = $y");
1.47 albertel 530: my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
531: my $result='';
532: push(@Apache::randomlabel::xcoord,$x);
533: push(@Apache::randomlabel::ycoord,$y);
534: push(@Apache::randomlabel::value,$value);
535: if ($target eq 'edit') {
536: $result.=&Apache::edit::tag_start($target,$token);
537: $result.=&Apache::edit::text_arg('X:','x',$token,4).
538: &Apache::edit::text_arg('Y:','y',$token,4).
539: &Apache::edit::entercoords('x','y','attribute','width','height').' '.
540: &Apache::edit::text_arg('Value:','value',$token).
541: &Apache::edit::end_row();
542: $result.=&Apache::edit::end_table;
543: } elsif ($target eq 'modified') {
544: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
545: $safeeval,'x','y','value');
546: if ($constructtag) {
547: $result = &Apache::edit::rebuild_tag($token);
548: $result.=&Apache::edit::handle_insert();
549: }
1.4 albertel 550: }
1.77 foxr 551: $Apache::lonxml::debug = 0;
1.47 albertel 552: return $result;
1.1 tsai 553: }
554:
555: sub end_location {
1.47 albertel 556: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
557: my @result;
558: if ($target eq 'edit') { @result=('','no') }
559: return @result;
1.1 tsai 560: }
561:
1.2 tsai 562: # <label>$var_abc</label>
1.1 tsai 563: sub start_label {
1.47 albertel 564: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
565: my $result='';
566: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
567: if ($target eq 'web' || $target eq 'tex' ||
568: $target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
1.57 albertel 569: &Apache::lonxml::startredirection;
1.47 albertel 570: } elsif ($target eq 'edit') {
571: $result.=&Apache::edit::tag_start($target,$token,"$type Label");
1.81 albertel 572: my $text=&Apache::lonxml::get_all_text("/label",$parser,$style);
1.47 albertel 573: if ($type eq 'image') {
574: $result.=&Apache::edit::end_row().
575: &Apache::edit::start_spanning_row();
576: $result.=&Apache::edit::text_arg('Description:','description',
577: $token);
578: }
579: if ($type eq 'text') { $result.="Label Text:"; }
580: if ($type eq 'image') { $result.="Path to image: "; }
581: $result.=&Apache::edit::editline('',$text,'',50).
582: &Apache::edit::end_table();
583: } elsif ($target eq 'modified') {
584: $result = '<label>';
585: if ($type eq 'image') {
586: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
587: $safeeval,
588: 'description');
589: if ($constructtag) {
590: $result = &Apache::edit::rebuild_tag($token);
591: } else {
592: $result = $token->[4];
593: }
594: }
1.52 albertel 595: $result.=&Apache::edit::modifiedfield("/label",$parser);
1.26 albertel 596: }
1.47 albertel 597: return $result;
1.1 tsai 598: }
599:
600: sub end_label {
1.47 albertel 601: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
602: my @result;
1.57 albertel 603: if ($target eq 'edit') {
604: @result=('','no') ;
605: } elsif ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
606: $target eq 'answer' || $target eq 'analyze') {
607: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
608: my $ltext=&Apache::lonxml::endredirection;
609: if ($type eq 'image') {
1.70 foxr 610: if ($target eq 'tex') {
611: # For tex targets, our image url has been potentially corrupted
612: # by prepending \'s in front of special latex symbols.
613: # For now we only worry about the _ case (most common?)
614: # There's a whole host of theim in lonxml::latex_special_symbols
615: # that could potentially have to be re-done.
616:
617: $ltext =~ s/\\_/_/g;
618: }
1.57 albertel 619: &Apache::lonxml::debug("Turning $ltext, $Apache::lonxml::pwd[-1]");
620: $ltext=&Apache::imageresponse::clean_up_image($ltext);
621: # $ltext=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],
622: # $ltext);
623: &Apache::lonxml::debug("into $ltext");
624: my $description = &Apache::lonxml::get_param('description',
625: $parstack,$safeeval);
626: push(@Apache::randomlabel::description,$description);
1.61 albertel 627: } else {
628: $ltext=~s/[\r\n]*//gs
1.57 albertel 629: }
630: push(@Apache::randomlabel::label_arr,$ltext);
631: }
1.47 albertel 632: return @result;
1.1 tsai 633: }
634:
635: 1;
636: __END__
637:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>