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