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