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

1.1       tsai        1: # The LearningOnline Network with CAPA
                      2: # random labelling tool
1.8       albertel    3: #
1.75    ! foxr        4: # $Id: randomlabel.pm,v 1.74 2005/05/23 11:02:13 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.13      sakharuk   70: BEGIN {
1.57      albertel   71:     &Apache::lonxml::register('Apache::randomlabel',('randomlabel','labelgroup','location','label','bgimg'));
1.1       tsai       72: }
                     73: 
1.23      matthew    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: 
1.64      albertel   86: my ($height_param,$width_param);
1.1       tsai       87: sub start_randomlabel {
1.47      albertel   88:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     89:     my $result='';
                     90:     push (@Apache::lonxml::namespace,'randomlabel');
1.64      albertel   91:     ($height_param,$width_param)=(0,0);
1.75    ! foxr       92:     $label_xscale = 1.0;		# Assume image size not overridden.
        !            93:     $label_yscale = 1.0;
1.47      albertel   94:     my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
1.60      albertel   95:     if ( defined($bgimg) && $bgimg !~ /^http:/ ) {
1.47      albertel   96: 	$bgimg=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$bgimg);
1.66      raeburn    97: 	if (&Apache::lonnet::repcopy($bgimg) ne 'ok') {
1.47      albertel   98: 	    $bgimg='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
                     99: 	}
1.42      www       100:     }
1.59      albertel  101:     $Apache::randomlabel::obj_cnt=0;
1.47      albertel  102:     if ($target eq 'web') {
1.53      albertel  103: 	$cgi_id=&Apache::loncommon::get_cgi_id();
                    104: 	%args=();
                    105: 	$args{"cgi.$cgi_id.BGIMG"}=&Apache::lonnet::escape($bgimg);
1.75    ! foxr      106: 	$height_param = &Apache::lonxml::get_param('height',$parstack, $safeeval);
        !           107: 	$width_param  = &Apache::lonxml::get_param('width', $parstack, $safeeval);
1.64      albertel  108:     } elsif ($target eq 'tex' && defined($bgimg)) {
                    109: 	$result.=&make_eps_image($bgimg,$parstack,$safeeval);
1.47      albertel  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: 	}
1.31      sakharuk  130:     }
1.47      albertel  131:     return $result;
1.1       tsai      132: }
                    133: 
                    134: sub end_randomlabel {
1.47      albertel  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') {
1.59      albertel  140: 	$count = $Apache::randomlabel::obj_cnt;
                    141: 	if( $count != 0) { $args{"cgi.$cgi_id.OBJCOUNT"}=$count; }
1.53      albertel  142: 	$result.='<img src="/adm/randomlabel.png?token='.$cgi_id.'" /><br />'."\n";
                    143: 	&Apache::lonnet::appenv(%args);
1.47      albertel  144:     } elsif ($target eq 'tex') {
                    145: 	$result='\end{picture}\\\\';
1.64      albertel  146: 	$result.= ' \vskip -'.$height_param.' mm }  \\\\ ';
1.47      albertel  147:     } elsif ($target eq 'edit') {
                    148: 	$result.=&Apache::edit::end_table;
                    149:     }
                    150:     return $result;
1.1       tsai      151: }
                    152: 
1.57      albertel  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') { 
1.64      albertel  157: 	&Apache::lonxml::startredirection(); 
1.57      albertel  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') { 
1.64      albertel  166: 	my $bgimg=&Apache::lonxml::endredirection(); 
1.57      albertel  167: 	if ($target eq 'web') {
1.75    ! foxr      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");
1.57      albertel  184: 	    $bgimg=&Apache::imageresponse::clean_up_image($bgimg);
1.75    ! foxr      185: 	    &Apache::lonxml::debug("Cleaned image: $bgimg");
1.57      albertel  186: 	    $args{"cgi.$cgi_id.BGIMG"}=&Apache::lonnet::escape($bgimg);
                    187: 	} elsif ($target eq 'tex') {
1.73      foxr      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/) {
1.74      foxr      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;
1.73      foxr      204: 		&Apache::lonxml::debug("height $height_param");
                    205: 		&Apache::lonxml::debug("Width $width_param");
1.74      foxr      206: 
1.73      foxr      207: 		my $dirty_width = $width_param + 5;
                    208: 		$result .= '\parbox{'.$dirty_width.'mm}{';
1.74      foxr      209: 		$result  .= $src."\n";
                    210: 		$result  .= '\setlength{\unitlength}{1mm}'."\n";
1.73      foxr      211: 		$result  .= '\begin{picture}('."$height_param,$width_param)";
                    212: 		$result  .= "(0,-$height_param)";
1.74      foxr      213: 		$result  .= "\n";
                    214: 
1.73      foxr      215: 	    } else {
                    216: 		
                    217: 		
                    218: 		$result.=&make_eps_image($bgimg,$parstack,$safeeval,-2);
                    219: 	    }
1.57      albertel  220: 	}
                    221:     }
                    222:     return $result;
                    223: }
                    224: sub make_eps_image {
1.64      albertel  225:     my ($bgimg,$parstack,$safeeval,$depth)=@_;
1.73      foxr      226:     &Apache::lonxml::debug("image prior to get_eps_image: $bgimg");
1.64      albertel  227:     my ($path,$file) = &Apache::londefdef::get_eps_image($bgimg);
1.73      foxr      228:     &Apache::lonxml::debug("image after:  $bgimg");
1.64      albertel  229:     ($height_param,$width_param)=
                    230: 	&Apache::londefdef::image_size($bgimg,0.3,$parstack,$safeeval,
                    231: 				       $depth,1);
1.73      foxr      232: 
                    233:     &Apache::lonxml::debug("Image size: $height_param x $width_param");
                    234: 
1.64      albertel  235:     my $dirtywidth=$width_param+5;
1.68      foxr      236:     my $result ="\n".'\vspace*{2mm}\noindent'."\n".
                    237: 	'\parbox{'.$dirtywidth.
1.64      albertel  238: 	' mm}{  \noindent \epsfxsize='.$width_param.
                    239: 	' mm \epsffile{'.$path.$file.
1.68      foxr      240: 	'}\setlength{\unitlength}{1mm}'."\n".'  \begin{picture}('.
                    241: 	$width_param.','.$height_param.')(0,-'.$height_param.')'."\n";
1.72      foxr      242:     my $magick = Image::Magick->new;
                    243:     $magick->Read($bgimg);
                    244:     my $initial_width = $magick->Get('width');
1.73      foxr      245:     &Apache::lonxml::debug("ImageMagick thinks width is; $initial_width");
1.72      foxr      246:     $scale_factor = $width_param / $initial_width;
1.57      albertel  247:     return $result;
                    248: }
                    249: 
1.1       tsai      250: sub start_labelgroup {
1.47      albertel  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/;
1.48      albertel  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:     }
1.47      albertel  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  = ();
1.68      foxr      267: 	@Apache::randomlabel::description  = ();
1.47      albertel  268:     } elsif ($target eq 'edit') {
                    269: 	$result.=&Apache::edit::tag_start($target,$token);
                    270: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
1.63      albertel  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();
1.47      albertel  282:     } elsif ($target eq 'modified') {
                    283: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.63      albertel  284: 						     $safeeval,'name','type',
                    285: 						     'TeXsize');
1.47      albertel  286: 	if ($constructtag) {
                    287: 	    $result = &Apache::edit::rebuild_tag($token);
                    288: 	    $result.=&Apache::edit::handle_insert();
                    289: 	}
1.4       albertel  290:     }
1.47      albertel  291:     return $result;
1.1       tsai      292: }
                    293: 
1.72      foxr      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: }
1.5       albertel  312: sub add_vars {
1.47      albertel  313:     my ($name,$order,$label,$labelorder,$value,$image,$safeeval) = @_;
1.61      albertel  314:     if (!defined($name) || $name eq '') { return; }
1.47      albertel  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)."';";
1.5       albertel  328:     $out=Apache::run::run($code,$safeeval);
1.4       albertel  329: }
1.5       albertel  330: 
1.1       tsai      331: # begin to assign labels to locations
                    332: sub end_labelgroup {
1.47      albertel  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[$_];
1.59      albertel  347: 	    my $i=$Apache::randomlabel::obj_cnt++;
1.47      albertel  348: 	    if( $type eq 'text') {
                    349: 		&add_vars($gname,$_,$label,$idx_arr[$_],$value,'',$safeeval);
1.59      albertel  350: 		$str = join(':',$x,$y,&Apache::lonnet::escape($label));
                    351: 		$args{"cgi.$cgi_id.OBJTYPE"}.='LABEL:';
1.47      albertel  352: 	    } elsif ( $type eq 'image') {
                    353: 		&add_vars($gname,$_,
                    354: 			  $Apache::randomlabel::description[$idx_arr[$_]],
                    355: 			  $idx_arr[$_],$value,$label,$safeeval);
1.59      albertel  356: 		$str = join(':',$x,$y,&Apache::lonnet::escape($label));
                    357: 		$args{"cgi.$cgi_id.OBJTYPE"}.='IMAGE:';
1.47      albertel  358: 	    } else {
                    359: 		&Apache::lonxml::error('Unknown type of label :'.$type.':');
                    360: 	    }
1.59      albertel  361: 	    if ($target eq 'web') { $args{"cgi.$cgi_id.OBJ$i"} =$str; }
1.47      albertel  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);
1.63      albertel  368: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
                    369: 	if (!defined($TeXsize)) { $TeXsize='\\normalsize'; }
1.62      albertel  370: 	
1.47      albertel  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];
1.62      albertel  378: 	    # FIXME the 3.5 here is the 'height' of the letter in TeX
                    379: 	    my $y = $Apache::randomlabel::ycoord[$i]-3.5;
1.47      albertel  380: 	    my $value = $Apache::randomlabel::value[$i];
                    381: 	    #x latex coordinate
1.64      albertel  382: 	    my $tcX=($x)*($width_param/$wwidth);
1.47      albertel  383: 	    #y latex coordinate
                    384:             #      my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
1.64      albertel  385: 	    my $tcY=$height_param-$y*($height_param/$wheight);
1.47      albertel  386: 	    $tcX=sprintf('%.2f',$tcX);
                    387: 	    $tcY=sprintf('%.2f',$tcY);
1.70      foxr      388: 	    $result .= '\put('.$tcX.','.$tcY.'){';
1.47      albertel  389: 	    if( $type eq 'text') {
1.70      foxr      390: 		$result.= $TeXsize.' \bf '.$label."}\n";
1.47      albertel  391: 		&add_vars($gname,$i,$label,$idx_arr[$i],$value,'',$safeeval);
                    392: 	    } elsif ( $type eq 'image') {
1.71      foxr      393: 		my ($path,$file) = &Apache::londefdef::get_eps_image($label);
                    394: 		my $image_name = $path.$file;
1.72      foxr      395: 		my $label_width = get_label_width($label);
                    396: 
                    397: 		$result .=  '\includegraphics[width='.$label_width.'mm]{'
1.71      foxr      398: 		            .$image_name."}}\n";
1.47      albertel  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;
1.3       tsai      408:     }
1.47      albertel  409:     return $result;
1.1       tsai      410: }
                    411: 
1.5       albertel  412: # <location x="123" y="456" value="some value"/>
1.1       tsai      413: sub start_location {
1.47      albertel  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);
1.75    ! foxr      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");
1.47      albertel  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: 	}
1.4       albertel  443:     }
1.47      albertel  444:     return $result;
1.1       tsai      445: }
                    446: 
                    447: sub end_location {
1.47      albertel  448:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    449:     my @result;
                    450:     if ($target eq 'edit') { @result=('','no') }
                    451:     return @result;
1.1       tsai      452: }
                    453: 
1.2       tsai      454: # <label>$var_abc</label>
1.1       tsai      455: sub start_label {
1.47      albertel  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') {
1.57      albertel  461: 	&Apache::lonxml::startredirection; 
1.47      albertel  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: 	}
1.52      albertel  487: 	$result.=&Apache::edit::modifiedfield("/label",$parser);
1.26      albertel  488:     }
1.47      albertel  489:     return $result;
1.1       tsai      490: }
                    491: 
                    492: sub end_label {
1.47      albertel  493:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    494:     my @result;
1.57      albertel  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') {
1.70      foxr      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: 	    }
1.57      albertel  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);
1.61      albertel  519: 	} else {
                    520: 	    $ltext=~s/[\r\n]*//gs
1.57      albertel  521: 	}
                    522: 	push(@Apache::randomlabel::label_arr,$ltext);
                    523:     }
1.47      albertel  524:     return @result;
1.1       tsai      525: }
                    526: 
                    527: 1;
                    528: __END__
                    529:  

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