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

1.1       tsai        1: # The LearningOnline Network with CAPA
                      2: # random labelling tool
1.8       albertel    3: #
1.77    ! foxr        4: # $Id: randomlabel.pm,v 1.76 2005/05/31 22:14:32 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: 
                     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: 
        !           210: 	    &Apache::lonxml::debug("Base sizes: $width_param x $height_param");
        !           211: 	
        !           212: 	    my ($plot_x, $plot_y) = &extract_tag_sizes($bgimg, 
        !           213: 						       $width_param,
        !           214: 						       $height_param );
        !           215: 	    &Apache::lonxml::debug("Extracted sizes: $plot_x x $plot_y");
        !           216: 	    $label_xscale         = $plot_x / $width_param;
        !           217: 	    $label_yscale         = $plot_y / $height_param;
        !           218: 	    &Apache::lonxml::debug("Scale factors:   $label_xscale $label_yscale");
        !           219: 
1.75      foxr      220: 	    &Apache::lonxml::debug("Image: $bgimg");
1.57      albertel  221: 	    $bgimg=&Apache::imageresponse::clean_up_image($bgimg);
1.75      foxr      222: 	    &Apache::lonxml::debug("Cleaned image: $bgimg");
1.57      albertel  223: 	    $args{"cgi.$cgi_id.BGIMG"}=&Apache::lonnet::escape($bgimg);
                    224: 	} elsif ($target eq 'tex') {
1.73      foxr      225: 	    #   Some bg images can create latex for us... e.g. gnuplot.
                    226: 	    #   If it looks like we have some latex use that, 
                    227: 	    #   otherwise, assume this is a resource name that must
                    228: 	    #   be converted into the latex to create an eps insertion.
                    229: 	    #
                    230: 	    my $src = $bgimg;
                    231: 	    $src =~ s/\s+$//s;
                    232: 	    $src =~ s/^\s+//s;
                    233: 
1.77    ! foxr      234: 	    #If this is a dynamically generated image, it will
        !           235: 	    #be in latex already, with a comment header that
        !           236: 	    #describes the dimensions:
        !           237: 
        !           238: 	    if($src =~ /^%DYNAMICIMAGE:/) {
        !           239: 		$Apache::lonxml::debug = 0;
        !           240: 		&Apache::lonxml::debug("Dynamic image");
        !           241: 		my ($commentline, $junk) = split(/\n/, $src);
        !           242: 		&Apache::lonxml::debug("Comment line was: $commentline");
        !           243: 		my $trash;
        !           244: 		my $initial_width;
        !           245: 		($trash, $initial_width, $height_param, $width_param) =
        !           246: 		    split(/:/,$commentline);
        !           247: 		&Apache::lonxml::debug("internal web Width/height: $initial_width $height_param");
        !           248: 		&Apache::lonxml::debug("Texwitdh: $width_param");
        !           249: 		if($initial_width == 0) {
        !           250: 		    $initial_width = $width_param;
        !           251: 		}
        !           252: 		# strip off the comments since output does not always
        !           253: 		# preserve \n's:
        !           254:                 #
        !           255: 		$src =~ s/$commentline//;
1.74      foxr      256: 		$scale_factor = $width_param / $initial_width;
                    257: 		$height_param = $height_param*$scale_factor;
1.77    ! foxr      258: 
        !           259: 		$label_xscale = 1.0; #  $scale_factor;
        !           260: 		$label_yscale = 1.0; #  $scale_factor;
        !           261: 	   
1.73      foxr      262: 		&Apache::lonxml::debug("height $height_param");
                    263: 		&Apache::lonxml::debug("Width $width_param");
1.77    ! foxr      264: 		&Apache::lonxml::debug("Scale factors: $label_xscale $label_yscale");
1.73      foxr      265: 		my $dirty_width = $width_param + 5;
                    266: 		$result .= '\parbox{'.$dirty_width.'mm}{';
1.77    ! foxr      267: 		$result  .= " $src \n";
1.74      foxr      268: 		$result  .= '\setlength{\unitlength}{1mm}'."\n";
1.73      foxr      269: 		$result  .= '\begin{picture}('."$height_param,$width_param)";
                    270: 		$result  .= "(0,-$height_param)";
1.74      foxr      271: 		$result  .= "\n";
1.77    ! foxr      272: 		$Apache::lonxml::debug = 0;
1.74      foxr      273: 
1.73      foxr      274: 	    } else {
                    275: 		
                    276: 		
                    277: 		$result.=&make_eps_image($bgimg,$parstack,$safeeval,-2);
                    278: 	    }
1.57      albertel  279: 	}
                    280:     }
                    281:     return $result;
                    282: }
                    283: sub make_eps_image {
1.64      albertel  284:     my ($bgimg,$parstack,$safeeval,$depth)=@_;
1.73      foxr      285:     &Apache::lonxml::debug("image prior to get_eps_image: $bgimg");
1.64      albertel  286:     my ($path,$file) = &Apache::londefdef::get_eps_image($bgimg);
1.73      foxr      287:     &Apache::lonxml::debug("image after:  $bgimg");
1.64      albertel  288:     ($height_param,$width_param)=
                    289: 	&Apache::londefdef::image_size($bgimg,0.3,$parstack,$safeeval,
                    290: 				       $depth,1);
1.73      foxr      291: 
                    292:     &Apache::lonxml::debug("Image size: $height_param x $width_param");
                    293: 
1.64      albertel  294:     my $dirtywidth=$width_param+5;
1.68      foxr      295:     my $result ="\n".'\vspace*{2mm}\noindent'."\n".
                    296: 	'\parbox{'.$dirtywidth.
1.64      albertel  297: 	' mm}{  \noindent \epsfxsize='.$width_param.
                    298: 	' mm \epsffile{'.$path.$file.
1.68      foxr      299: 	'}\setlength{\unitlength}{1mm}'."\n".'  \begin{picture}('.
                    300: 	$width_param.','.$height_param.')(0,-'.$height_param.')'."\n";
1.72      foxr      301:     my $magick = Image::Magick->new;
                    302:     $magick->Read($bgimg);
                    303:     my $initial_width = $magick->Get('width');
1.73      foxr      304:     &Apache::lonxml::debug("ImageMagick thinks width is; $initial_width");
1.72      foxr      305:     $scale_factor = $width_param / $initial_width;
1.57      albertel  306:     return $result;
                    307: }
                    308: 
1.1       tsai      309: sub start_labelgroup {
1.47      albertel  310:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    311:     my $result='';
                    312:     my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    313:     my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
                    314:     $type =~tr/A-Z/a-z/;
1.48      albertel  315:     if ($target ne 'modified' && ($name =~ /\W/ || $name =~ /^[0-9]/)) {
                    316: 	&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 />");
                    317:     }
1.47      albertel  318:     if ($target eq 'web' || $target eq 'tex' ||
                    319: 	$target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
                    320: 	$Apache::randomlabel::groupname=$name;
                    321: 	$Apache::randomlabel::type=$type;
                    322: 	@Apache::randomlabel::xcoord = ();
                    323: 	@Apache::randomlabel::ycoord = ();
                    324: 	@Apache::randomlabel::value = ();
                    325: 	@Apache::randomlabel::label_arr  = ();
1.68      foxr      326: 	@Apache::randomlabel::description  = ();
1.47      albertel  327:     } elsif ($target eq 'edit') {
                    328: 	$result.=&Apache::edit::tag_start($target,$token);
                    329: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
1.63      albertel  330: 	    &Apache::edit::select_arg('Type:','type',['text','image'],$token);
                    331: 	if (!defined($token->[2]{'TeXsize'})) {
                    332: 	    $token->[2]{'TeXsize'}='\normalsize';
                    333: 	}
                    334: 	$result.=&Apache::edit::select_arg('TeX font size:','TeXsize',
                    335: 					   ['\tiny','\scriptsize',
                    336: 					    '\footnotesize','\small',
                    337: 					    '\normalsize','\large','\Large',
                    338: 					    '\LARGE','\huge','\Huge'],
                    339: 					   $token);
                    340: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.47      albertel  341:     } elsif ($target eq 'modified') {
                    342: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.63      albertel  343: 						     $safeeval,'name','type',
                    344: 						     'TeXsize');
1.47      albertel  345: 	if ($constructtag) {
                    346: 	    $result = &Apache::edit::rebuild_tag($token);
                    347: 	    $result.=&Apache::edit::handle_insert();
                    348: 	}
1.4       albertel  349:     }
1.47      albertel  350:     return $result;
1.1       tsai      351: }
                    352: 
1.72      foxr      353: #
                    354: #   Utility sub to compute the width of a label.
                    355: #
                    356: sub get_label_width {
                    357:     my $label         = shift;
                    358:     &Apache::lonxml::debug("image label = $label");
                    359:     if (-e $label) {
                    360: 	&Apache::lonxml::debug("$label exists");
                    361:     } else {
                    362: 	&Apache::lonxml::debug("$label does not exist");
                    363:     }
                    364:     my $magick        = Image::Magick->new;
                    365:     $magick->Read($label);
                    366:     my $pixel_width   = $magick->Get('width');
                    367:     return $pixel_width * $scale_factor;
                    368:     
                    369: 	
                    370: }
1.5       albertel  371: sub add_vars {
1.47      albertel  372:     my ($name,$order,$label,$labelorder,$value,$image,$safeeval) = @_;
1.61      albertel  373:     if (!defined($name) || $name eq '') { return; }
1.47      albertel  374:     my $code = '${'.$name."}{'".($order+1)."'}='".$label."';";
                    375:     my $out=Apache::run::run($code,$safeeval);
                    376:     if ($value) {
                    377: 	$code = '${'.$name."}{'value_".($order+1)."'}='".$value."';";
                    378: 	$out=Apache::run::run($code,$safeeval);
                    379: 	$code = '${'.$name."}{'labelvalue_".($labelorder+1)."'}='".$value."';";
                    380: 	$out=Apache::run::run($code,$safeeval);
                    381:     }
                    382:     if ($image) {
                    383: 	my $code = '${'.$name."}{'image_".($order+1)."'}='".$image."';";
                    384: 	my $out=Apache::run::run($code,$safeeval);
                    385:     }
                    386:     $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
1.5       albertel  387:     $out=Apache::run::run($code,$safeeval);
1.4       albertel  388: }
1.5       albertel  389: 
1.1       tsai      390: # begin to assign labels to locations
                    391: sub end_labelgroup {
1.47      albertel  392:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    393:     my $gname = $Apache::randomlabel::groupname;
                    394:     my $type  = $Apache::randomlabel::type;
                    395:     my $result='';
                    396:     if ($target eq 'web' || $target eq 'answer' || $target eq 'grade' ||
                    397: 	$target eq 'analyze') {
                    398: 	my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
                    399: 	&Apache::structuretags::shuffle(\@idx_arr);
                    400: 	for(0 .. $#Apache::randomlabel::label_arr) {
                    401: 	    my $str;
                    402: 	    my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$_] ]";
                    403: 	    my $x = $Apache::randomlabel::xcoord[$_];
                    404: 	    my $y = $Apache::randomlabel::ycoord[$_];
                    405: 	    my $value = $Apache::randomlabel::value[$_];
1.59      albertel  406: 	    my $i=$Apache::randomlabel::obj_cnt++;
1.47      albertel  407: 	    if( $type eq 'text') {
                    408: 		&add_vars($gname,$_,$label,$idx_arr[$_],$value,'',$safeeval);
1.59      albertel  409: 		$str = join(':',$x,$y,&Apache::lonnet::escape($label));
                    410: 		$args{"cgi.$cgi_id.OBJTYPE"}.='LABEL:';
1.47      albertel  411: 	    } elsif ( $type eq 'image') {
                    412: 		&add_vars($gname,$_,
                    413: 			  $Apache::randomlabel::description[$idx_arr[$_]],
                    414: 			  $idx_arr[$_],$value,$label,$safeeval);
1.59      albertel  415: 		$str = join(':',$x,$y,&Apache::lonnet::escape($label));
                    416: 		$args{"cgi.$cgi_id.OBJTYPE"}.='IMAGE:';
1.47      albertel  417: 	    } else {
                    418: 		&Apache::lonxml::error('Unknown type of label :'.$type.':');
                    419: 	    }
1.59      albertel  420: 	    if ($target eq 'web') { $args{"cgi.$cgi_id.OBJ$i"} =$str; }
1.47      albertel  421: 	}
                    422:     } elsif ($target eq 'tex') {
                    423: 	my $WX1=0; #  Web x-coord. of upper left corner (ULC)
                    424: 	my $WY1=0; #  Web y-coord. of (ULC)
                    425: 	my $wwidth=&Apache::lonxml::get_param('width',$parstack,$safeeval,-2);
                    426: 	my $wheight=&Apache::lonxml::get_param('height',$parstack,$safeeval,-2);
1.63      albertel  427: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
                    428: 	if (!defined($TeXsize)) { $TeXsize='\\normalsize'; }
1.62      albertel  429: 	
1.47      albertel  430: 	my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
                    431: 	&Apache::structuretags::shuffle(\@idx_arr);
                    432: 
                    433: 	&Apache::lonxml::debug("Array is:".$#Apache::randomlabel::label_arr.":");
1.77    ! foxr      434: 	$Apache::lonxml::debug = 0;
1.47      albertel  435: 	for(my $i=0;$i <= $#Apache::randomlabel::label_arr; $i++) {
                    436: 	    my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$i] ]";
                    437: 	    my $x = $Apache::randomlabel::xcoord[$i];
1.62      albertel  438: 	    # FIXME the 3.5 here is the 'height' of the letter in TeX
                    439: 	    my $y = $Apache::randomlabel::ycoord[$i]-3.5;
1.77    ! foxr      440: 	    &Apache::lonxml::debug("initially: x= $x y= $y");
1.47      albertel  441: 	    my $value = $Apache::randomlabel::value[$i];
                    442: 	    #x latex coordinate
1.64      albertel  443: 	    my $tcX=($x)*($width_param/$wwidth);
1.77    ! foxr      444: 	    &Apache::lonxml::debug("wparam = $width_param wwidth = $wwidth, texx = $tcX");
1.47      albertel  445: 	    #y latex coordinate
                    446:             #      my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
1.64      albertel  447: 	    my $tcY=$height_param-$y*($height_param/$wheight);
1.77    ! foxr      448: 	    &Apache::lonxml::debug("hparam = $height_param wheight = $wheight texy = $tcY");
1.47      albertel  449: 	    $tcX=sprintf('%.2f',$tcX);
                    450: 	    $tcY=sprintf('%.2f',$tcY);
1.70      foxr      451: 	    $result .= '\put('.$tcX.','.$tcY.'){';
1.47      albertel  452: 	    if( $type eq 'text') {
1.70      foxr      453: 		$result.= $TeXsize.' \bf '.$label."}\n";
1.47      albertel  454: 		&add_vars($gname,$i,$label,$idx_arr[$i],$value,'',$safeeval);
                    455: 	    } elsif ( $type eq 'image') {
1.71      foxr      456: 		my ($path,$file) = &Apache::londefdef::get_eps_image($label);
                    457: 		my $image_name = $path.$file;
1.72      foxr      458: 		my $label_width = get_label_width($label);
                    459: 
                    460: 		$result .=  '\includegraphics[width='.$label_width.'mm]{'
1.71      foxr      461: 		            .$image_name."}}\n";
1.47      albertel  462: 		&add_vars($gname,$i,
                    463: 			  $Apache::randomlabel::description[$idx_arr[$i]],
                    464: 			  $idx_arr[$i],$value,$label,$safeeval);
                    465: 	    } else {
                    466: 		&Apache::lonxml::error('Unknown type of label :'.$type.':');
                    467: 	    }
                    468: 	}
1.77    ! foxr      469: 	$Apache::lonxml::debug =0;
1.47      albertel  470:     } elsif ($target eq 'edit') {
                    471: 	$result.=&Apache::edit::end_table;
1.3       tsai      472:     }
1.47      albertel  473:     return $result;
1.1       tsai      474: }
                    475: 
1.5       albertel  476: # <location x="123" y="456" value="some value"/>
1.1       tsai      477: sub start_location {
1.77    ! foxr      478:     $Apache::lonxml::debug = 0;
1.47      albertel  479:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    480:     my $x= &check_int(&Apache::lonxml::get_param('x',$parstack,$safeeval),50);
                    481:     my $y= &check_int(&Apache::lonxml::get_param('y',$parstack,$safeeval),50);
1.75      foxr      482:     &Apache::lonxml::debug("x = $x y = $y");
                    483:     $x = $x*$label_xscale;
                    484:     $y = $y*$label_yscale;
                    485:     &Apache::lonxml::debug(" H = $height_param W = $width_param");
                    486:     &Apache::lonxml::debug(" XS = $label_xscale YS = $label_yscale");
                    487:     &Apache::lonxml::debug(" X  = $x Y = $y");
1.47      albertel  488:     my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
                    489:     my $result='';
                    490:     push(@Apache::randomlabel::xcoord,$x);
                    491:     push(@Apache::randomlabel::ycoord,$y);
                    492:     push(@Apache::randomlabel::value,$value);
                    493:     if ($target eq 'edit') {
                    494: 	$result.=&Apache::edit::tag_start($target,$token);
                    495: 	$result.=&Apache::edit::text_arg('X:','x',$token,4).
                    496: 	    &Apache::edit::text_arg('Y:','y',$token,4).
                    497: 	    &Apache::edit::entercoords('x','y','attribute','width','height').'&nbsp;&nbsp;&nbsp;'.
                    498: 	    &Apache::edit::text_arg('Value:','value',$token).
                    499: 	    &Apache::edit::end_row();
                    500: 	$result.=&Apache::edit::end_table;
                    501:     } elsif ($target eq 'modified') {
                    502: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    503: 						    $safeeval,'x','y','value');
                    504: 	if ($constructtag) {
                    505: 	    $result = &Apache::edit::rebuild_tag($token);
                    506: 	    $result.=&Apache::edit::handle_insert();
                    507: 	}
1.4       albertel  508:     }
1.77    ! foxr      509:     $Apache::lonxml::debug = 0;
1.47      albertel  510:     return $result;
1.1       tsai      511: }
                    512: 
                    513: sub end_location {
1.47      albertel  514:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    515:     my @result;
                    516:     if ($target eq 'edit') { @result=('','no') }
                    517:     return @result;
1.1       tsai      518: }
                    519: 
1.2       tsai      520: # <label>$var_abc</label>
1.1       tsai      521: sub start_label {
1.47      albertel  522:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    523:     my $result='';
                    524:     my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
                    525:     if ($target eq 'web' || $target eq 'tex' ||
                    526: 	$target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
1.57      albertel  527: 	&Apache::lonxml::startredirection; 
1.47      albertel  528:     } elsif ($target eq 'edit') {
                    529: 	$result.=&Apache::edit::tag_start($target,$token,"$type Label");
                    530: 	my $text=&Apache::lonxml::get_all_text("/label",$parser);
                    531: 	if ($type eq 'image') {
                    532: 	    $result.=&Apache::edit::end_row().
                    533: 		&Apache::edit::start_spanning_row();
                    534: 	    $result.=&Apache::edit::text_arg('Description:','description',
                    535: 					     $token);
                    536: 	}
                    537: 	if ($type eq 'text') { $result.="Label Text:"; }
                    538: 	if ($type eq 'image') { $result.="Path to image:&nbsp;"; }
                    539: 	$result.=&Apache::edit::editline('',$text,'',50).
                    540: 	    &Apache::edit::end_table();
                    541:     } elsif ($target eq 'modified') {
                    542: 	$result = '<label>';
                    543: 	if ($type eq 'image') {
                    544: 	    my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    545: 							 $safeeval,
                    546: 							 'description');
                    547: 	    if ($constructtag) {
                    548: 		$result = &Apache::edit::rebuild_tag($token);
                    549: 	    } else {
                    550: 		$result = $token->[4];
                    551: 	    }
                    552: 	}
1.52      albertel  553: 	$result.=&Apache::edit::modifiedfield("/label",$parser);
1.26      albertel  554:     }
1.47      albertel  555:     return $result;
1.1       tsai      556: }
                    557: 
                    558: sub end_label {
1.47      albertel  559:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    560:     my @result;
1.57      albertel  561:     if ($target eq 'edit') {
                    562: 	@result=('','no') ;
                    563:     } elsif ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
                    564: 	     $target eq 'answer' || $target eq 'analyze') {
                    565: 	my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
                    566: 	my $ltext=&Apache::lonxml::endredirection; 
                    567: 	if ($type eq 'image') {
1.70      foxr      568: 	    if ($target eq 'tex') {
                    569: 		# For tex targets, our image url has been potentially corrupted
                    570: 		# by prepending \'s in front of special latex symbols.
                    571: 		# For now we only worry about the _ case (most common?)
                    572: 		# There's a whole host of theim in lonxml::latex_special_symbols
                    573: 		# that could potentially have to be re-done.
                    574: 
                    575: 		$ltext =~ s/\\_/_/g;
                    576: 	    }
1.57      albertel  577: 	    &Apache::lonxml::debug("Turning $ltext, $Apache::lonxml::pwd[-1]");
                    578: 	    $ltext=&Apache::imageresponse::clean_up_image($ltext);
                    579: #	    $ltext=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],
                    580: #						 $ltext);
                    581: 	    &Apache::lonxml::debug("into $ltext");
                    582: 	    my $description = &Apache::lonxml::get_param('description',
                    583: 							 $parstack,$safeeval);
                    584: 	    push(@Apache::randomlabel::description,$description);
1.61      albertel  585: 	} else {
                    586: 	    $ltext=~s/[\r\n]*//gs
1.57      albertel  587: 	}
                    588: 	push(@Apache::randomlabel::label_arr,$ltext);
                    589:     }
1.47      albertel  590:     return @result;
1.1       tsai      591: }
                    592: 
                    593: 1;
                    594: __END__
                    595:  

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