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

1.1       tsai        1: # The LearningOnline Network with CAPA
                      2: # random labelling tool
1.8       albertel    3: #
1.76    ! foxr        4: # $Id: randomlabel.pm,v 1.75 2005/05/24 10:10:16 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.75      foxr       68: my $label_xscale;                # Label scale factor (needed for gnuplot).
                     69: my $label_yscale;
1.76    ! foxr       70: my $gnuplot_background;		# This will be true if a gnuplot backgound
        !            71: 
        !            72: 
1.13      sakharuk   73: BEGIN {
1.57      albertel   74:     &Apache::lonxml::register('Apache::randomlabel',('randomlabel','labelgroup','location','label','bgimg'));
1.1       tsai       75: }
                     76: 
1.76    ! foxr       77: # Gnuplot calback called if a </gnuplot> tag is encountered:
        !            78: #
        !            79: sub gnuplot_handler {
        !            80:     $gnuplot_background = 1;
        !            81: }
        !            82: 
1.23      matthew    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: 
1.64      albertel   95: my ($height_param,$width_param);
1.1       tsai       96: sub start_randomlabel {
1.76    ! foxr       97: 
1.47      albertel   98:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.76    ! foxr       99:     $gnuplot_background = 0;
        !           100:     &Apache::lonplot::register_callback(\&gnuplot_handler);
1.47      albertel  101:     my $result='';
                    102:     push (@Apache::lonxml::namespace,'randomlabel');
1.64      albertel  103:     ($height_param,$width_param)=(0,0);
1.75      foxr      104:     $label_xscale = 1.0;		# Assume image size not overridden.
                    105:     $label_yscale = 1.0;
1.47      albertel  106:     my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
1.60      albertel  107:     if ( defined($bgimg) && $bgimg !~ /^http:/ ) {
1.47      albertel  108: 	$bgimg=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$bgimg);
1.66      raeburn   109: 	if (&Apache::lonnet::repcopy($bgimg) ne 'ok') {
1.47      albertel  110: 	    $bgimg='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
                    111: 	}
1.42      www       112:     }
1.59      albertel  113:     $Apache::randomlabel::obj_cnt=0;
1.47      albertel  114:     if ($target eq 'web') {
1.53      albertel  115: 	$cgi_id=&Apache::loncommon::get_cgi_id();
                    116: 	%args=();
                    117: 	$args{"cgi.$cgi_id.BGIMG"}=&Apache::lonnet::escape($bgimg);
1.75      foxr      118: 	$height_param = &Apache::lonxml::get_param('height',$parstack, $safeeval);
                    119: 	$width_param  = &Apache::lonxml::get_param('width', $parstack, $safeeval);
1.64      albertel  120:     } elsif ($target eq 'tex' && defined($bgimg)) {
                    121: 	$result.=&make_eps_image($bgimg,$parstack,$safeeval);
1.47      albertel  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: 	}
1.31      sakharuk  142:     }
1.47      albertel  143:     return $result;
1.1       tsai      144: }
                    145: 
                    146: sub end_randomlabel {
1.47      albertel  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') {
1.59      albertel  152: 	$count = $Apache::randomlabel::obj_cnt;
                    153: 	if( $count != 0) { $args{"cgi.$cgi_id.OBJCOUNT"}=$count; }
1.53      albertel  154: 	$result.='<img src="/adm/randomlabel.png?token='.$cgi_id.'" /><br />'."\n";
                    155: 	&Apache::lonnet::appenv(%args);
1.47      albertel  156:     } elsif ($target eq 'tex') {
                    157: 	$result='\end{picture}\\\\';
1.64      albertel  158: 	$result.= ' \vskip -'.$height_param.' mm }  \\\\ ';
1.47      albertel  159:     } elsif ($target eq 'edit') {
                    160: 	$result.=&Apache::edit::end_table;
                    161:     }
1.76    ! foxr      162:     &Apache::lonplot::clear_callback();
1.47      albertel  163:     return $result;
1.1       tsai      164: }
                    165: 
1.57      albertel  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') { 
1.64      albertel  170: 	&Apache::lonxml::startredirection(); 
1.57      albertel  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') { 
1.64      albertel  179: 	my $bgimg=&Apache::lonxml::endredirection(); 
1.57      albertel  180: 	if ($target eq 'web') {
1.75      foxr      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?"
1.76    ! foxr      186: 	    if ( $gnuplot_background) {
1.75      foxr      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");
1.57      albertel  196: 	    $bgimg=&Apache::imageresponse::clean_up_image($bgimg);
1.75      foxr      197: 	    &Apache::lonxml::debug("Cleaned image: $bgimg");
1.57      albertel  198: 	    $args{"cgi.$cgi_id.BGIMG"}=&Apache::lonnet::escape($bgimg);
                    199: 	} elsif ($target eq 'tex') {
1.73      foxr      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: 
1.76    ! foxr      210: 	    if ($gnuplot_background) {
1.74      foxr      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;
1.73      foxr      216: 		&Apache::lonxml::debug("height $height_param");
                    217: 		&Apache::lonxml::debug("Width $width_param");
1.74      foxr      218: 
1.73      foxr      219: 		my $dirty_width = $width_param + 5;
                    220: 		$result .= '\parbox{'.$dirty_width.'mm}{';
1.74      foxr      221: 		$result  .= $src."\n";
                    222: 		$result  .= '\setlength{\unitlength}{1mm}'."\n";
1.73      foxr      223: 		$result  .= '\begin{picture}('."$height_param,$width_param)";
                    224: 		$result  .= "(0,-$height_param)";
1.74      foxr      225: 		$result  .= "\n";
                    226: 
1.73      foxr      227: 	    } else {
                    228: 		
                    229: 		
                    230: 		$result.=&make_eps_image($bgimg,$parstack,$safeeval,-2);
                    231: 	    }
1.57      albertel  232: 	}
                    233:     }
                    234:     return $result;
                    235: }
                    236: sub make_eps_image {
1.64      albertel  237:     my ($bgimg,$parstack,$safeeval,$depth)=@_;
1.73      foxr      238:     &Apache::lonxml::debug("image prior to get_eps_image: $bgimg");
1.64      albertel  239:     my ($path,$file) = &Apache::londefdef::get_eps_image($bgimg);
1.73      foxr      240:     &Apache::lonxml::debug("image after:  $bgimg");
1.64      albertel  241:     ($height_param,$width_param)=
                    242: 	&Apache::londefdef::image_size($bgimg,0.3,$parstack,$safeeval,
                    243: 				       $depth,1);
1.73      foxr      244: 
                    245:     &Apache::lonxml::debug("Image size: $height_param x $width_param");
                    246: 
1.64      albertel  247:     my $dirtywidth=$width_param+5;
1.68      foxr      248:     my $result ="\n".'\vspace*{2mm}\noindent'."\n".
                    249: 	'\parbox{'.$dirtywidth.
1.64      albertel  250: 	' mm}{  \noindent \epsfxsize='.$width_param.
                    251: 	' mm \epsffile{'.$path.$file.
1.68      foxr      252: 	'}\setlength{\unitlength}{1mm}'."\n".'  \begin{picture}('.
                    253: 	$width_param.','.$height_param.')(0,-'.$height_param.')'."\n";
1.72      foxr      254:     my $magick = Image::Magick->new;
                    255:     $magick->Read($bgimg);
                    256:     my $initial_width = $magick->Get('width');
1.73      foxr      257:     &Apache::lonxml::debug("ImageMagick thinks width is; $initial_width");
1.72      foxr      258:     $scale_factor = $width_param / $initial_width;
1.57      albertel  259:     return $result;
                    260: }
                    261: 
1.1       tsai      262: sub start_labelgroup {
1.47      albertel  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/;
1.48      albertel  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:     }
1.47      albertel  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  = ();
1.68      foxr      279: 	@Apache::randomlabel::description  = ();
1.47      albertel  280:     } elsif ($target eq 'edit') {
                    281: 	$result.=&Apache::edit::tag_start($target,$token);
                    282: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
1.63      albertel  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();
1.47      albertel  294:     } elsif ($target eq 'modified') {
                    295: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.63      albertel  296: 						     $safeeval,'name','type',
                    297: 						     'TeXsize');
1.47      albertel  298: 	if ($constructtag) {
                    299: 	    $result = &Apache::edit::rebuild_tag($token);
                    300: 	    $result.=&Apache::edit::handle_insert();
                    301: 	}
1.4       albertel  302:     }
1.47      albertel  303:     return $result;
1.1       tsai      304: }
                    305: 
1.72      foxr      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: }
1.5       albertel  324: sub add_vars {
1.47      albertel  325:     my ($name,$order,$label,$labelorder,$value,$image,$safeeval) = @_;
1.61      albertel  326:     if (!defined($name) || $name eq '') { return; }
1.47      albertel  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)."';";
1.5       albertel  340:     $out=Apache::run::run($code,$safeeval);
1.4       albertel  341: }
1.5       albertel  342: 
1.1       tsai      343: # begin to assign labels to locations
                    344: sub end_labelgroup {
1.47      albertel  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[$_];
1.59      albertel  359: 	    my $i=$Apache::randomlabel::obj_cnt++;
1.47      albertel  360: 	    if( $type eq 'text') {
                    361: 		&add_vars($gname,$_,$label,$idx_arr[$_],$value,'',$safeeval);
1.59      albertel  362: 		$str = join(':',$x,$y,&Apache::lonnet::escape($label));
                    363: 		$args{"cgi.$cgi_id.OBJTYPE"}.='LABEL:';
1.47      albertel  364: 	    } elsif ( $type eq 'image') {
                    365: 		&add_vars($gname,$_,
                    366: 			  $Apache::randomlabel::description[$idx_arr[$_]],
                    367: 			  $idx_arr[$_],$value,$label,$safeeval);
1.59      albertel  368: 		$str = join(':',$x,$y,&Apache::lonnet::escape($label));
                    369: 		$args{"cgi.$cgi_id.OBJTYPE"}.='IMAGE:';
1.47      albertel  370: 	    } else {
                    371: 		&Apache::lonxml::error('Unknown type of label :'.$type.':');
                    372: 	    }
1.59      albertel  373: 	    if ($target eq 'web') { $args{"cgi.$cgi_id.OBJ$i"} =$str; }
1.47      albertel  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);
1.63      albertel  380: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
                    381: 	if (!defined($TeXsize)) { $TeXsize='\\normalsize'; }
1.62      albertel  382: 	
1.47      albertel  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];
1.62      albertel  390: 	    # FIXME the 3.5 here is the 'height' of the letter in TeX
                    391: 	    my $y = $Apache::randomlabel::ycoord[$i]-3.5;
1.47      albertel  392: 	    my $value = $Apache::randomlabel::value[$i];
                    393: 	    #x latex coordinate
1.64      albertel  394: 	    my $tcX=($x)*($width_param/$wwidth);
1.47      albertel  395: 	    #y latex coordinate
                    396:             #      my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
1.64      albertel  397: 	    my $tcY=$height_param-$y*($height_param/$wheight);
1.47      albertel  398: 	    $tcX=sprintf('%.2f',$tcX);
                    399: 	    $tcY=sprintf('%.2f',$tcY);
1.70      foxr      400: 	    $result .= '\put('.$tcX.','.$tcY.'){';
1.47      albertel  401: 	    if( $type eq 'text') {
1.70      foxr      402: 		$result.= $TeXsize.' \bf '.$label."}\n";
1.47      albertel  403: 		&add_vars($gname,$i,$label,$idx_arr[$i],$value,'',$safeeval);
                    404: 	    } elsif ( $type eq 'image') {
1.71      foxr      405: 		my ($path,$file) = &Apache::londefdef::get_eps_image($label);
                    406: 		my $image_name = $path.$file;
1.72      foxr      407: 		my $label_width = get_label_width($label);
                    408: 
                    409: 		$result .=  '\includegraphics[width='.$label_width.'mm]{'
1.71      foxr      410: 		            .$image_name."}}\n";
1.47      albertel  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;
1.3       tsai      420:     }
1.47      albertel  421:     return $result;
1.1       tsai      422: }
                    423: 
1.5       albertel  424: # <location x="123" y="456" value="some value"/>
1.1       tsai      425: sub start_location {
1.47      albertel  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);
1.75      foxr      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");
1.47      albertel  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: 	}
1.4       albertel  455:     }
1.47      albertel  456:     return $result;
1.1       tsai      457: }
                    458: 
                    459: sub end_location {
1.47      albertel  460:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    461:     my @result;
                    462:     if ($target eq 'edit') { @result=('','no') }
                    463:     return @result;
1.1       tsai      464: }
                    465: 
1.2       tsai      466: # <label>$var_abc</label>
1.1       tsai      467: sub start_label {
1.47      albertel  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') {
1.57      albertel  473: 	&Apache::lonxml::startredirection; 
1.47      albertel  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: 	}
1.52      albertel  499: 	$result.=&Apache::edit::modifiedfield("/label",$parser);
1.26      albertel  500:     }
1.47      albertel  501:     return $result;
1.1       tsai      502: }
                    503: 
                    504: sub end_label {
1.47      albertel  505:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    506:     my @result;
1.57      albertel  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') {
1.70      foxr      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: 	    }
1.57      albertel  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);
1.61      albertel  531: 	} else {
                    532: 	    $ltext=~s/[\r\n]*//gs
1.57      albertel  533: 	}
                    534: 	push(@Apache::randomlabel::label_arr,$ltext);
                    535:     }
1.47      albertel  536:     return @result;
1.1       tsai      537: }
                    538: 
                    539: 1;
                    540: __END__
                    541:  

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