File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.76: download - view: text, annotated - select for diffs
Tue May 31 22:14:32 2005 UTC (18 years, 11 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Use gnuplot callbacks to determine if the bkg image was gnuplot.
This is more reliable than attempting to decode the produced stuff (I hope).

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

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