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

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

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