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

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

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