File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.98: download - view: text, annotated - select for diffs
Sun Jan 26 16:29:03 2025 UTC (5 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- WCAG 2 compliance
  Add alt attribute to <img> tag for background image in randomlabel.

    1: # The LearningOnline Network with CAPA
    2: # random labelling tool
    3: #
    4: # $Id: randomlabel.pm,v 1.98 2025/01/26 16:29:03 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: 
   28: =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: 
  104: 
  105: package Apache::randomlabel;
  106: use Apache::lonnet;
  107: use strict;
  108: use Apache::edit;
  109: use Apache::File();
  110: use Apache::Constants qw(:common :http);
  111: use Image::Magick;
  112: use Apache::lonplot;
  113: use Apache::lonlocal;
  114: use Apache::lonmeta;
  115: use LONCAPA;
  116:  
  117: 
  118: my %args;
  119: my $cgi_id;
  120: my $scale_factor;		# image scale factor.
  121: my $label_xscale;                # Label scale factor (needed for gnuplot).
  122: my $label_yscale;
  123: my $dirty_width_adjust = 5;     # Width adjustment for e.g. gnuplot images.
  124: 
  125: BEGIN {
  126:     &Apache::lonxml::register('Apache::randomlabel',('randomlabel','labelgroup','location','label','bgimg'));
  127: }
  128: 
  129: 
  130: 
  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: 
  143: 
  144: 
  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: 
  167: my ($height_param,$width_param,$alt_param);
  168: sub start_randomlabel {
  169: 
  170:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  171:     my $result='';
  172:     push (@Apache::lonxml::namespace,'randomlabel');
  173:     ($height_param,$width_param)=(0,0);
  174:     $alt_param = '';
  175:     $label_xscale = 1.0;		# Assume image size not overridden.
  176:     $label_yscale = 1.0;
  177:     my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
  178:     if ( defined($bgimg) && $bgimg !~ /^https?\:/ ) {
  179: 	$bgimg=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$bgimg);
  180: 	if (&Apache::lonnet::repcopy($bgimg) ne 'ok') {
  181: 	    $bgimg=$Apache::lonnet::perlvar{'lonDocRoot'}.'/adm/lonKaputt/lonlogo_broken.gif';
  182: 	}
  183:     }
  184:     $Apache::randomlabel::obj_cnt=0;
  185:     if ($target eq 'web') {
  186: 	$cgi_id=&Apache::loncommon::get_cgi_id();
  187: 	%args=();
  188: 	$args{"cgi.$cgi_id.BGIMG"}=&escape($bgimg);
  189: 	$height_param = &Apache::lonxml::get_param('height',$parstack, $safeeval);
  190: 	$width_param  = &Apache::lonxml::get_param('width', $parstack, $safeeval);
  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:         }
  198:     } elsif ($target eq 'tex' && defined($bgimg)) {
  199: 	$result.=&make_eps_image($bgimg,$parstack,$safeeval);
  200:     } elsif ($target eq 'edit') {
  201:         my $only = join(',',&Apache::loncommon::filecategorytypes('Pictures'));
  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;
  206: 	$result.=&Apache::edit::text_arg('Image:','bgimg',$token,75).' '.
  207: 	         &Apache::edit::browse_or_search('bgimg',undef,undef,$only,undef,1).
  208: 	         '<br />'.
  209:             &Apache::edit::text_arg('Description:'  ,'alt'     ,$token,40).'<br />'.
  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',
  217: 						     'height','texwidth','alt');
  218: 	if ($constructtag) {
  219: 	    $result = &Apache::edit::rebuild_tag($token);
  220: 	}
  221:     }
  222:     return $result;
  223: }
  224: 
  225: sub end_randomlabel {
  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') {
  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,'<>&"');
  238: 	$count = $Apache::randomlabel::obj_cnt;
  239: 	if( $count != 0) { $args{"cgi.$cgi_id.OBJCOUNT"}=$count; }
  240: 	$result.='<img src="/adm/randomlabel.png?token='.$cgi_id.'" alt="'.$alttext.'" /><br />'."\n";
  241: 	&Apache::lonnet::appenv(\%args);
  242:     } elsif ($target eq 'tex') {
  243: 	$result='\end{picture}\\\\';
  244: 	$result.= ' \vskip -'.$height_param.' mm } \\newline ';
  245:     } elsif ($target eq 'edit') {
  246: 	$result.=&Apache::edit::end_table;
  247:     }
  248:     return $result;
  249: }
  250: 
  251: 
  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') { 
  256: 	&Apache::lonxml::startredirection(); 
  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') { 
  265: 	my $bgimg=&Apache::lonxml::endredirection(); 
  266: 	if ($target eq 'web') {
  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,
  275: 						       $height_param);
  276: 	    &Apache::lonxml::debug("Extracted sizes: $plot_x x $plot_y");
  277: 	    if ($width_param) {
  278: 		$label_xscale     = $plot_x / $width_param;
  279: 	    }
  280: 	    if ($height_param) {
  281: 		$label_yscale     = $plot_y / $height_param;
  282: 	    }
  283: 	    &Apache::lonxml::debug("Scale factors:   $label_xscale $label_yscale");
  284: 
  285: 	    &Apache::lonxml::debug("Image: $bgimg");
  286: 	    $bgimg=&Apache::imageresponse::clean_up_image($bgimg);
  287: 	    &Apache::lonxml::debug("Cleaned image: $bgimg");
  288: 	    $args{"cgi.$cgi_id.BGIMG"}=&escape($bgimg);
  289: 	} elsif ($target eq 'tex') {
  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: 
  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//;
  321: 		$scale_factor = $width_param / $initial_width;
  322: 		$height_param = $height_param*$scale_factor;
  323: 
  324: 		$label_xscale = 1.0; #  $scale_factor;
  325: 		$label_yscale = 1.0; #  $scale_factor;
  326: 	   
  327: 		&Apache::lonxml::debug("height $height_param");
  328: 		&Apache::lonxml::debug("Width $width_param");
  329: 		&Apache::lonxml::debug("Scale factors: $label_xscale $label_yscale");
  330: 		my $dirty_width = $width_param + $dirty_width_adjust;
  331: 		my $x_offset    = -$dirty_width_adjust/2.0;
  332: 		#
  333: 		#  Somewhere here it looks like height_param and
  334: 		#  width_param got backwards...
  335: 		#
  336: 		$result .= '\parbox{'.$dirty_width.'mm}{';
  337: 		$result  .= " $src \n";
  338: 		$result  .= '\setlength{\unitlength}{1mm}'."\n";
  339: 		$result  .= '\begin{picture}('."$width_param,$height_param)";
  340: 		$result  .= "($x_offset,-$height_param)";
  341: 		$result  .= "\n";
  342: 		$Apache::lonxml::debug = 0;
  343: 
  344: 	    } else {
  345: 		$result.=&make_eps_image($bgimg,$parstack,$safeeval,-2);
  346: 	    }
  347: 	}
  348:     }
  349:     return $result;
  350: }
  351: sub make_eps_image {
  352:     my ($bgimg,$parstack,$safeeval,$depth)=@_;
  353:     &Apache::lonxml::debug("image prior to get_eps_image: $bgimg");
  354:     my ($path,$file) = &Apache::londefdef::get_eps_image($bgimg);
  355:     &Apache::lonxml::debug("image after:  $bgimg");
  356:     ($height_param,$width_param)=
  357: 	&Apache::londefdef::image_size($bgimg,0.3,$parstack,$safeeval,
  358: 				       $depth,1);
  359: 
  360:     &Apache::lonxml::debug("Image size: $height_param x $width_param");
  361: 
  362:     my $dirtywidth=$width_param+5;
  363: 
  364:     my $result ="\n".'\vspace*{2mm}\noindent'."\n".
  365: 	'\parbox{'.$dirtywidth.
  366: 	' mm}{  \noindent \epsfxsize='.$width_param.
  367: 	' mm \epsffile{'.$path.$file.
  368: 	'}\setlength{\unitlength}{1mm}'."\n".'  \begin{picture}('.
  369: 	$width_param.','.$height_param.')(0,-'.$height_param.')'."\n";
  370:     my $magick = Image::Magick->new;
  371:     $magick->Read($bgimg);
  372:     my $initial_width = $magick->Get('width');
  373:     &Apache::lonxml::debug("ImageMagick thinks width is; $initial_width");
  374:     $scale_factor = $width_param / $initial_width;
  375:     return $result;
  376: }
  377: 
  378: sub start_labelgroup {
  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/;
  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:     }
  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  = ();
  395: 	@Apache::randomlabel::description  = ();
  396:     } elsif ($target eq 'edit') {
  397: 	$result.=&Apache::edit::tag_start($target,$token);
  398: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
  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();
  410:     } elsif ($target eq 'modified') {
  411: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  412: 						     $safeeval,'name','type',
  413: 						     'TeXsize');
  414: 	if ($constructtag) {
  415: 	    $result = &Apache::edit::rebuild_tag($token);
  416: 	}
  417:     }
  418:     return $result;
  419: }
  420: 
  421: 
  422: 
  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: }
  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: 
  453: sub add_vars {
  454:     my ($name,$order,$label,$labelorder,$value,$image,$safeeval) = @_;
  455:     if (!defined($name) || $name eq '') { return; }
  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)."';";
  469:     $out=Apache::run::run($code,$safeeval);
  470: }
  471: 
  472: 
  473: 
  474: sub end_labelgroup {
  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[$_];
  489: 	    my $i=$Apache::randomlabel::obj_cnt++;
  490: 	    if( $type eq 'text') {
  491: 		&add_vars($gname,$_,$label,$idx_arr[$_],$value,'',$safeeval);
  492: 		$str = join(':',$x,$y,&escape($label));
  493: 		$args{"cgi.$cgi_id.OBJTYPE"}.='LABEL:';
  494: 	    } elsif ( $type eq 'image') {
  495: 		&add_vars($gname,$_,
  496: 			  $Apache::randomlabel::description[$idx_arr[$_]],
  497: 			  $idx_arr[$_],$value,$label,$safeeval);
  498: 		$str = join(':',$x,$y,&escape($label));
  499: 		$args{"cgi.$cgi_id.OBJTYPE"}.='IMAGE:';
  500: 	    } else {
  501: 		&Apache::lonxml::error('Unknown type of label :'.$type.':');
  502: 	    }
  503: 	    if ($target eq 'web') { $args{"cgi.$cgi_id.OBJ$i"} =$str; }
  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);
  510: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
  511: 	if (!defined($TeXsize)) { $TeXsize='\\normalsize'; }
  512: 	
  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.":");
  517: 	$Apache::lonxml::debug = 0;
  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];
  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: 	    }
  526: 	    &Apache::lonxml::debug("initially: x= $x y= $y");
  527: 	    my $value = $Apache::randomlabel::value[$i];
  528: 	    #x latex coordinate
  529: 	    my $tcX=($x)*($width_param/$wwidth);
  530: 	    &Apache::lonxml::debug("wparam = $width_param wwidth = $wwidth, texx = $tcX");
  531: 	    #y latex coordinate
  532:             #      my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
  533: 	    my $tcY=$height_param-$y*($height_param/$wheight);
  534: 	    if ( $type eq 'image') {
  535: 		my $label_height = &get_label_height($label);
  536: 		$tcY=$tcY-$label_height;
  537: 	    }
  538: 
  539: 	    &Apache::lonxml::debug("hparam = $height_param wheight = $wheight texy = $tcY");
  540: 	    $tcX=sprintf('%.2f',$tcX);
  541: 	    $tcY=sprintf('%.2f',$tcY);
  542: 	    $result .= '\put('.$tcX.','.$tcY.'){';
  543: 	    if( $type eq 'text') {
  544: 		$result.= $TeXsize.' \bf '.$label."}\n";
  545: 		&add_vars($gname,$i,$label,$idx_arr[$i],$value,'',$safeeval);
  546: 	    } elsif ( $type eq 'image') {
  547: 		my ($path,$file) = &Apache::londefdef::get_eps_image($label);
  548: 		my $image_name = $path.$file;
  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: 		#
  553: 		my $label_width = &get_label_width($label);
  554: 
  555: 		$result .=  '\includegraphics[width='.$label_width.'mm]{'
  556: 		            .$image_name."}}\n";
  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: 	}
  564: 	$Apache::lonxml::debug =0;
  565:     } elsif ($target eq 'edit') {
  566: 	$result.=&Apache::edit::end_table;
  567:     }
  568:     return $result;
  569: }
  570: 
  571: # <location x="123" y="456" value="some value"/>
  572: sub start_location {
  573:     $Apache::lonxml::debug = 0;
  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);
  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");
  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').'&nbsp;&nbsp;&nbsp;'.
  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: 	}
  602:     }
  603:     $Apache::lonxml::debug = 0;
  604:     return $result;
  605: }
  606: 
  607: sub end_location {
  608:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  609:     my @result;
  610:     if ($target eq 'edit') { @result=('','no') }
  611:     return @result;
  612: }
  613: 
  614: # <label>$var_abc</label>
  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: 
  623: sub start_label {
  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') {
  629: 	&Apache::lonxml::startredirection; 
  630:     } elsif ($target eq 'edit') {
  631: 	$result.=&Apache::edit::tag_start($target,$token,"$type Label");
  632: 	my $text=&Apache::lonxml::get_all_text("/label",$parser,$style);
  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:&nbsp;"; }
  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: 	}
  655: 	$result.=&Apache::edit::modifiedfield("/label",$parser);
  656:     }
  657:     return $result;
  658: }
  659: 
  660: sub end_label {
  661:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  662:     my @result;
  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') {
  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: 	    }
  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);
  687: 	} else {
  688: 	    $ltext=~s/[\r\n]*//gs
  689: 	}
  690: 	push(@Apache::randomlabel::label_arr,$ltext);
  691:     }
  692:     return @result;
  693: }
  694: 
  695: 1;
  696: __END__
  697:  

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>