File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.82: download - view: text, annotated - select for diffs
Tue May 30 12:45:37 2006 UTC (17 years, 11 months ago) by www
Branches: MAIN
CVS tags: version_2_2_0, version_2_1_99_3, version_2_1_99_2, version_2_1_99_1, version_2_1_99_0, HEAD
&Apache::lonnet::unescape -> &unescape
&Apache::lonnet::escape -> &escape

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

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