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