File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.81: download - view: text, annotated - select for diffs
Thu Dec 1 18:46:31 2005 UTC (18 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: version_2_1_X, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_0_99_1, HEAD
- finish off the conversion to allowing style tags get_all_text across tags
- BUG#4428

    1: # The LearningOnline Network with CAPA
    2: # random labelling tool
    3: #
    4: # $Id: randomlabel.pm,v 1.81 2005/12/01 18:46:31 albertel 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: # SYNTAX:
   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>
   37: #    </labelgroup>
   38: #    <labelgroup name="GroupTwo" type="text">
   39: #      <location x="12" y="45" />
   40: #      <location x="32" y="65" />
   41: #      <location x="21" y="54" />
   42: #      <label>TEXT-1</label>
   43: #      <label>TEXT-2</label>
   44: #      <label>TEXT-3</label>
   45: #    </labelgroup>
   46: #   </randomlabel>
   47: #  ===========================================
   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"
   55: #  ===========================================
   56: package Apache::randomlabel;
   57: use Apache::lonnet;
   58: use strict;
   59: use Apache::edit;
   60: use Apache::File();
   61: use Apache::Constants qw(:common :http);
   62: use Image::Magick;
   63: use Apache::lonplot;
   64: 
   65: my %args;
   66: my $cgi_id;
   67: my $scale_factor;		# image scale factor.
   68: my $label_xscale;                # Label scale factor (needed for gnuplot).
   69: my $label_yscale;
   70: 
   71: 
   72: BEGIN {
   73:     &Apache::lonxml::register('Apache::randomlabel',('randomlabel','labelgroup','location','label','bgimg'));
   74: }
   75: 
   76: 
   77: 
   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: 
   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: 
  121: my ($height_param,$width_param);
  122: sub start_randomlabel {
  123: 
  124:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  125:     my $result='';
  126:     push (@Apache::lonxml::namespace,'randomlabel');
  127:     ($height_param,$width_param)=(0,0);
  128:     $label_xscale = 1.0;		# Assume image size not overridden.
  129:     $label_yscale = 1.0;
  130:     my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
  131:     if ( defined($bgimg) && $bgimg !~ /^http:/ ) {
  132: 	$bgimg=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$bgimg);
  133: 	if (&Apache::lonnet::repcopy($bgimg) ne 'ok') {
  134: 	    $bgimg='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
  135: 	}
  136:     }
  137:     $Apache::randomlabel::obj_cnt=0;
  138:     if ($target eq 'web') {
  139: 	$cgi_id=&Apache::loncommon::get_cgi_id();
  140: 	%args=();
  141: 	$args{"cgi.$cgi_id.BGIMG"}=&Apache::lonnet::escape($bgimg);
  142: 	$height_param = &Apache::lonxml::get_param('height',$parstack, $safeeval);
  143: 	$width_param  = &Apache::lonxml::get_param('width', $parstack, $safeeval);
  144:     } elsif ($target eq 'tex' && defined($bgimg)) {
  145: 	$result.=&make_eps_image($bgimg,$parstack,$safeeval);
  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: 	}
  166:     }
  167:     return $result;
  168: }
  169: 
  170: sub end_randomlabel {
  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') {
  176: 	$count = $Apache::randomlabel::obj_cnt;
  177: 	if( $count != 0) { $args{"cgi.$cgi_id.OBJCOUNT"}=$count; }
  178: 	$result.='<img src="/adm/randomlabel.png?token='.$cgi_id.'" /><br />'."\n";
  179: 	&Apache::lonnet::appenv(%args);
  180:     } elsif ($target eq 'tex') {
  181: 	$result='\end{picture}\\\\';
  182: 	$result.= ' \vskip -'.$height_param.' mm }  \\\\ ';
  183:     } elsif ($target eq 'edit') {
  184: 	$result.=&Apache::edit::end_table;
  185:     }
  186:     return $result;
  187: }
  188: 
  189: 
  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') { 
  194: 	&Apache::lonxml::startredirection(); 
  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') { 
  203: 	my $bgimg=&Apache::lonxml::endredirection(); 
  204: 	if ($target eq 'web') {
  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,
  213: 						       $height_param);
  214: 	    &Apache::lonxml::debug("Extracted sizes: $plot_x x $plot_y");
  215: 	    if ($width_param) {
  216: 		$label_xscale     = $plot_x / $width_param;
  217: 	    }
  218: 	    if ($height_param) {
  219: 		$label_yscale     = $plot_y / $height_param;
  220: 	    }
  221: 	    &Apache::lonxml::debug("Scale factors:   $label_xscale $label_yscale");
  222: 
  223: 	    &Apache::lonxml::debug("Image: $bgimg");
  224: 	    $bgimg=&Apache::imageresponse::clean_up_image($bgimg);
  225: 	    &Apache::lonxml::debug("Cleaned image: $bgimg");
  226: 	    $args{"cgi.$cgi_id.BGIMG"}=&Apache::lonnet::escape($bgimg);
  227: 	} elsif ($target eq 'tex') {
  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: 
  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//;
  259: 		$scale_factor = $width_param / $initial_width;
  260: 		$height_param = $height_param*$scale_factor;
  261: 
  262: 		$label_xscale = 1.0; #  $scale_factor;
  263: 		$label_yscale = 1.0; #  $scale_factor;
  264: 	   
  265: 		&Apache::lonxml::debug("height $height_param");
  266: 		&Apache::lonxml::debug("Width $width_param");
  267: 		&Apache::lonxml::debug("Scale factors: $label_xscale $label_yscale");
  268: 		my $dirty_width = $width_param + 5;
  269: 		$result .= '\parbox{'.$dirty_width.'mm}{';
  270: 		$result  .= " $src \n";
  271: 		$result  .= '\setlength{\unitlength}{1mm}'."\n";
  272: 		$result  .= '\begin{picture}('."$height_param,$width_param)";
  273: 		$result  .= "(0,-$height_param)";
  274: 		$result  .= "\n";
  275: 		$Apache::lonxml::debug = 0;
  276: 
  277: 	    } else {
  278: 		$result.=&make_eps_image($bgimg,$parstack,$safeeval,-2);
  279: 	    }
  280: 	}
  281:     }
  282:     return $result;
  283: }
  284: sub make_eps_image {
  285:     my ($bgimg,$parstack,$safeeval,$depth)=@_;
  286:     &Apache::lonxml::debug("image prior to get_eps_image: $bgimg");
  287:     my ($path,$file) = &Apache::londefdef::get_eps_image($bgimg);
  288:     &Apache::lonxml::debug("image after:  $bgimg");
  289:     ($height_param,$width_param)=
  290: 	&Apache::londefdef::image_size($bgimg,0.3,$parstack,$safeeval,
  291: 				       $depth,1);
  292: 
  293:     &Apache::lonxml::debug("Image size: $height_param x $width_param");
  294: 
  295:     my $dirtywidth=$width_param+5;
  296:     my $result ="\n".'\vspace*{2mm}\noindent'."\n".
  297: 	'\parbox{'.$dirtywidth.
  298: 	' mm}{  \noindent \epsfxsize='.$width_param.
  299: 	' mm \epsffile{'.$path.$file.
  300: 	'}\setlength{\unitlength}{1mm}'."\n".'  \begin{picture}('.
  301: 	$width_param.','.$height_param.')(0,-'.$height_param.')'."\n";
  302:     my $magick = Image::Magick->new;
  303:     $magick->Read($bgimg);
  304:     my $initial_width = $magick->Get('width');
  305:     &Apache::lonxml::debug("ImageMagick thinks width is; $initial_width");
  306:     $scale_factor = $width_param / $initial_width;
  307:     return $result;
  308: }
  309: 
  310: sub start_labelgroup {
  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/;
  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:     }
  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  = ();
  327: 	@Apache::randomlabel::description  = ();
  328:     } elsif ($target eq 'edit') {
  329: 	$result.=&Apache::edit::tag_start($target,$token);
  330: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
  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();
  342:     } elsif ($target eq 'modified') {
  343: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  344: 						     $safeeval,'name','type',
  345: 						     'TeXsize');
  346: 	if ($constructtag) {
  347: 	    $result = &Apache::edit::rebuild_tag($token);
  348: 	    $result.=&Apache::edit::handle_insert();
  349: 	}
  350:     }
  351:     return $result;
  352: }
  353: 
  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: }
  372: 
  373: sub get_label_height {
  374:     my $label         = shift;
  375:     &Apache::lonxml::debug("image label = $label");
  376:     if (-e $label) {
  377: 	&Apache::lonxml::debug("$label exists");
  378:     } else {
  379: 	&Apache::lonxml::debug("$label does not exist");
  380:     }
  381:     my $magick        = Image::Magick->new;
  382:     $magick->Read($label);
  383:     my $pixel_height   = $magick->Get('height');
  384:     return $pixel_height * $scale_factor;
  385: }
  386: 
  387: sub add_vars {
  388:     my ($name,$order,$label,$labelorder,$value,$image,$safeeval) = @_;
  389:     if (!defined($name) || $name eq '') { return; }
  390:     my $code = '${'.$name."}{'".($order+1)."'}='".$label."';";
  391:     my $out=Apache::run::run($code,$safeeval);
  392:     if ($value) {
  393: 	$code = '${'.$name."}{'value_".($order+1)."'}='".$value."';";
  394: 	$out=Apache::run::run($code,$safeeval);
  395: 	$code = '${'.$name."}{'labelvalue_".($labelorder+1)."'}='".$value."';";
  396: 	$out=Apache::run::run($code,$safeeval);
  397:     }
  398:     if ($image) {
  399: 	my $code = '${'.$name."}{'image_".($order+1)."'}='".$image."';";
  400: 	my $out=Apache::run::run($code,$safeeval);
  401:     }
  402:     $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
  403:     $out=Apache::run::run($code,$safeeval);
  404: }
  405: 
  406: # begin to assign labels to locations
  407: sub end_labelgroup {
  408:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  409:     my $gname = $Apache::randomlabel::groupname;
  410:     my $type  = $Apache::randomlabel::type;
  411:     my $result='';
  412:     if ($target eq 'web' || $target eq 'answer' || $target eq 'grade' ||
  413: 	$target eq 'analyze') {
  414: 	my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
  415: 	&Apache::structuretags::shuffle(\@idx_arr);
  416: 	for(0 .. $#Apache::randomlabel::label_arr) {
  417: 	    my $str;
  418: 	    my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$_] ]";
  419: 	    my $x = $Apache::randomlabel::xcoord[$_];
  420: 	    my $y = $Apache::randomlabel::ycoord[$_];
  421: 	    my $value = $Apache::randomlabel::value[$_];
  422: 	    my $i=$Apache::randomlabel::obj_cnt++;
  423: 	    if( $type eq 'text') {
  424: 		&add_vars($gname,$_,$label,$idx_arr[$_],$value,'',$safeeval);
  425: 		$str = join(':',$x,$y,&Apache::lonnet::escape($label));
  426: 		$args{"cgi.$cgi_id.OBJTYPE"}.='LABEL:';
  427: 	    } elsif ( $type eq 'image') {
  428: 		&add_vars($gname,$_,
  429: 			  $Apache::randomlabel::description[$idx_arr[$_]],
  430: 			  $idx_arr[$_],$value,$label,$safeeval);
  431: 		$str = join(':',$x,$y,&Apache::lonnet::escape($label));
  432: 		$args{"cgi.$cgi_id.OBJTYPE"}.='IMAGE:';
  433: 	    } else {
  434: 		&Apache::lonxml::error('Unknown type of label :'.$type.':');
  435: 	    }
  436: 	    if ($target eq 'web') { $args{"cgi.$cgi_id.OBJ$i"} =$str; }
  437: 	}
  438:     } elsif ($target eq 'tex') {
  439: 	my $WX1=0; #  Web x-coord. of upper left corner (ULC)
  440: 	my $WY1=0; #  Web y-coord. of (ULC)
  441: 	my $wwidth=&Apache::lonxml::get_param('width',$parstack,$safeeval,-2);
  442: 	my $wheight=&Apache::lonxml::get_param('height',$parstack,$safeeval,-2);
  443: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
  444: 	if (!defined($TeXsize)) { $TeXsize='\\normalsize'; }
  445: 	
  446: 	my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
  447: 	&Apache::structuretags::shuffle(\@idx_arr);
  448: 
  449: 	&Apache::lonxml::debug("Array is:".$#Apache::randomlabel::label_arr.":");
  450: 	$Apache::lonxml::debug = 0;
  451: 	for(my $i=0;$i <= $#Apache::randomlabel::label_arr; $i++) {
  452: 	    my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$i] ]";
  453: 	    my $x = $Apache::randomlabel::xcoord[$i];
  454: 	    my $y = $Apache::randomlabel::ycoord[$i];
  455: 	    if ( $type eq 'text' ) {
  456: 		# FIXME the 3.5 here is the 'height' of the letter in TeX
  457: 		$y=$y-3.5;
  458: 	    }
  459: 	    &Apache::lonxml::debug("initially: x= $x y= $y");
  460: 	    my $value = $Apache::randomlabel::value[$i];
  461: 	    #x latex coordinate
  462: 	    my $tcX=($x)*($width_param/$wwidth);
  463: 	    &Apache::lonxml::debug("wparam = $width_param wwidth = $wwidth, texx = $tcX");
  464: 	    #y latex coordinate
  465:             #      my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
  466: 	    my $tcY=$height_param-$y*($height_param/$wheight);
  467: 	    if ( $type eq 'image') {
  468: 		my $label_height = &get_label_height($label);
  469: 		$tcY=$tcY-$label_height;
  470: 	    }
  471: 
  472: 	    &Apache::lonxml::debug("hparam = $height_param wheight = $wheight texy = $tcY");
  473: 	    $tcX=sprintf('%.2f',$tcX);
  474: 	    $tcY=sprintf('%.2f',$tcY);
  475: 	    $result .= '\put('.$tcX.','.$tcY.'){';
  476: 	    if( $type eq 'text') {
  477: 		$result.= $TeXsize.' \bf '.$label."}\n";
  478: 		&add_vars($gname,$i,$label,$idx_arr[$i],$value,'',$safeeval);
  479: 	    } elsif ( $type eq 'image') {
  480: 		my ($path,$file) = &Apache::londefdef::get_eps_image($label);
  481: 		my $image_name = $path.$file;
  482: 		my $label_width = &get_label_width($label);
  483: 
  484: 		$result .=  '\includegraphics[width='.$label_width.'mm]{'
  485: 		            .$image_name."}}\n";
  486: 		&add_vars($gname,$i,
  487: 			  $Apache::randomlabel::description[$idx_arr[$i]],
  488: 			  $idx_arr[$i],$value,$label,$safeeval);
  489: 	    } else {
  490: 		&Apache::lonxml::error('Unknown type of label :'.$type.':');
  491: 	    }
  492: 	}
  493: 	$Apache::lonxml::debug =0;
  494:     } elsif ($target eq 'edit') {
  495: 	$result.=&Apache::edit::end_table;
  496:     }
  497:     return $result;
  498: }
  499: 
  500: # <location x="123" y="456" value="some value"/>
  501: sub start_location {
  502:     $Apache::lonxml::debug = 0;
  503:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  504:     my $x= &check_int(&Apache::lonxml::get_param('x',$parstack,$safeeval),50);
  505:     my $y= &check_int(&Apache::lonxml::get_param('y',$parstack,$safeeval),50);
  506:     &Apache::lonxml::debug("x = $x y = $y");
  507:     $x = $x*$label_xscale;
  508:     $y = $y*$label_yscale;
  509:     &Apache::lonxml::debug(" H = $height_param W = $width_param");
  510:     &Apache::lonxml::debug(" XS = $label_xscale YS = $label_yscale");
  511:     &Apache::lonxml::debug(" X  = $x Y = $y");
  512:     my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
  513:     my $result='';
  514:     push(@Apache::randomlabel::xcoord,$x);
  515:     push(@Apache::randomlabel::ycoord,$y);
  516:     push(@Apache::randomlabel::value,$value);
  517:     if ($target eq 'edit') {
  518: 	$result.=&Apache::edit::tag_start($target,$token);
  519: 	$result.=&Apache::edit::text_arg('X:','x',$token,4).
  520: 	    &Apache::edit::text_arg('Y:','y',$token,4).
  521: 	    &Apache::edit::entercoords('x','y','attribute','width','height').'&nbsp;&nbsp;&nbsp;'.
  522: 	    &Apache::edit::text_arg('Value:','value',$token).
  523: 	    &Apache::edit::end_row();
  524: 	$result.=&Apache::edit::end_table;
  525:     } elsif ($target eq 'modified') {
  526: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  527: 						    $safeeval,'x','y','value');
  528: 	if ($constructtag) {
  529: 	    $result = &Apache::edit::rebuild_tag($token);
  530: 	    $result.=&Apache::edit::handle_insert();
  531: 	}
  532:     }
  533:     $Apache::lonxml::debug = 0;
  534:     return $result;
  535: }
  536: 
  537: sub end_location {
  538:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  539:     my @result;
  540:     if ($target eq 'edit') { @result=('','no') }
  541:     return @result;
  542: }
  543: 
  544: # <label>$var_abc</label>
  545: sub start_label {
  546:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  547:     my $result='';
  548:     my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
  549:     if ($target eq 'web' || $target eq 'tex' ||
  550: 	$target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
  551: 	&Apache::lonxml::startredirection; 
  552:     } elsif ($target eq 'edit') {
  553: 	$result.=&Apache::edit::tag_start($target,$token,"$type Label");
  554: 	my $text=&Apache::lonxml::get_all_text("/label",$parser,$style);
  555: 	if ($type eq 'image') {
  556: 	    $result.=&Apache::edit::end_row().
  557: 		&Apache::edit::start_spanning_row();
  558: 	    $result.=&Apache::edit::text_arg('Description:','description',
  559: 					     $token);
  560: 	}
  561: 	if ($type eq 'text') { $result.="Label Text:"; }
  562: 	if ($type eq 'image') { $result.="Path to image:&nbsp;"; }
  563: 	$result.=&Apache::edit::editline('',$text,'',50).
  564: 	    &Apache::edit::end_table();
  565:     } elsif ($target eq 'modified') {
  566: 	$result = '<label>';
  567: 	if ($type eq 'image') {
  568: 	    my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  569: 							 $safeeval,
  570: 							 'description');
  571: 	    if ($constructtag) {
  572: 		$result = &Apache::edit::rebuild_tag($token);
  573: 	    } else {
  574: 		$result = $token->[4];
  575: 	    }
  576: 	}
  577: 	$result.=&Apache::edit::modifiedfield("/label",$parser);
  578:     }
  579:     return $result;
  580: }
  581: 
  582: sub end_label {
  583:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  584:     my @result;
  585:     if ($target eq 'edit') {
  586: 	@result=('','no') ;
  587:     } elsif ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
  588: 	     $target eq 'answer' || $target eq 'analyze') {
  589: 	my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
  590: 	my $ltext=&Apache::lonxml::endredirection; 
  591: 	if ($type eq 'image') {
  592: 	    if ($target eq 'tex') {
  593: 		# For tex targets, our image url has been potentially corrupted
  594: 		# by prepending \'s in front of special latex symbols.
  595: 		# For now we only worry about the _ case (most common?)
  596: 		# There's a whole host of theim in lonxml::latex_special_symbols
  597: 		# that could potentially have to be re-done.
  598: 
  599: 		$ltext =~ s/\\_/_/g;
  600: 	    }
  601: 	    &Apache::lonxml::debug("Turning $ltext, $Apache::lonxml::pwd[-1]");
  602: 	    $ltext=&Apache::imageresponse::clean_up_image($ltext);
  603: #	    $ltext=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],
  604: #						 $ltext);
  605: 	    &Apache::lonxml::debug("into $ltext");
  606: 	    my $description = &Apache::lonxml::get_param('description',
  607: 							 $parstack,$safeeval);
  608: 	    push(@Apache::randomlabel::description,$description);
  609: 	} else {
  610: 	    $ltext=~s/[\r\n]*//gs
  611: 	}
  612: 	push(@Apache::randomlabel::label_arr,$ltext);
  613:     }
  614:     return @result;
  615: }
  616: 
  617: 1;
  618: __END__
  619:  

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