Annotation of loncom/homework/randomlabel.pm, revision 1.74

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

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