File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.75: download - view: text, annotated - select for diffs
Tue May 24 10:10:16 2005 UTC (18 years, 11 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Support correct placement of labels on gnuplot images for both web and
print targets.  (defect 3928).

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

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