File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.70: download - view: text, annotated - select for diffs
Tue Apr 12 11:03:08 2005 UTC (19 years, 1 month ago) by foxr
Branches: MAIN
CVS tags: HEAD
At least it attempts to insert label images. Need to take the image and
convert it to, or queue it to convert to an .eps however..as now i've
got it trying to directly include e.g. .jpg's which LaTeX has no
love for.

    1: # The LearningOnline Network with CAPA
    2: # random labelling tool
    3: #
    4: # $Id: randomlabel.pm,v 1.70 2005/04/12 11:03:08 foxr Exp $
    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: #
   28: # SYNTAX:
   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>
   37: #    </labelgroup>
   38: #    <labelgroup name="GroupTwo" type="text">
   39: #      <location x="12" y="45" />
   40: #      <location x="32" y="65" />
   41: #      <location x="21" y="54" />
   42: #      <label>TEXT-1</label>
   43: #      <label>TEXT-2</label>
   44: #      <label>TEXT-3</label>
   45: #    </labelgroup>
   46: #   </randomlabel>
   47: #  ===========================================
   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"
   55: #  ===========================================
   56: package Apache::randomlabel;
   57: use Apache::lonnet;
   58: use strict;
   59: use Apache::edit;
   60: use Apache::File();
   61: use Apache::Constants qw(:common :http);
   62: 
   63: my %args;
   64: my $cgi_id;
   65: 
   66: BEGIN {
   67:     &Apache::lonxml::register('Apache::randomlabel',('randomlabel','labelgroup','location','label','bgimg'));
   68: }
   69: 
   70: sub check_int {
   71:     # utility function to do error checking on a integer.
   72:     my ($num,$default) = @_;
   73:     $default = 100 if (! defined($default));
   74:     $num =~ s/\s+//g;  # We dont need no stinkin white space!
   75:     # If it is a real, just grab the integer part.
   76:     ($num,undef) = split (/\./,$num) if ($num =~ /\./); 
   77:     # set to default if what we have left doesn't match anything...
   78:     $num = $default unless ($num =~/^\d+$/);
   79:     return $num;
   80: }
   81: 
   82: my ($height_param,$width_param);
   83: sub start_randomlabel {
   84:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   85:     my $result='';
   86:     push (@Apache::lonxml::namespace,'randomlabel');
   87:     ($height_param,$width_param)=(0,0);
   88:     my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
   89:     if ( defined($bgimg) && $bgimg !~ /^http:/ ) {
   90: 	$bgimg=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$bgimg);
   91: 	if (&Apache::lonnet::repcopy($bgimg) ne 'ok') {
   92: 	    $bgimg='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
   93: 	}
   94:     }
   95:     $Apache::randomlabel::obj_cnt=0;
   96:     if ($target eq 'web') {
   97: 	$cgi_id=&Apache::loncommon::get_cgi_id();
   98: 	%args=();
   99: 	$args{"cgi.$cgi_id.BGIMG"}=&Apache::lonnet::escape($bgimg);
  100:     } elsif ($target eq 'tex' && defined($bgimg)) {
  101: 	$result.=&make_eps_image($bgimg,$parstack,$safeeval);
  102:     } elsif ($target eq 'edit') {
  103: 	$result.=&Apache::edit::tag_start($target,$token);
  104: 	$Apache::edit::bgimgsrc=
  105: 	    &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
  106: 	$Apache::edit::bgimgsrccurdepth=$Apache::lonxml::curdepth;
  107: 	$result.=&Apache::edit::text_arg('Image:','bgimg',$token,75).' ';
  108: 	$result.=&Apache::edit::browse('bgimg').' ';
  109: 	$result.=&Apache::edit::search('bgimg').'<br />'.
  110: 	    &Apache::edit::text_arg('Width(pixel):' ,'width'   ,$token,6).
  111: 	    &Apache::edit::text_arg('Height(pixel):','height'  ,$token,6).
  112: 	    &Apache::edit::text_arg('TeXWidth(mm):' ,'texwidth',$token,6).
  113: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();     
  114:     } elsif ($target eq 'modified') {
  115: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  116: 						     $safeeval,'bgimg','width',
  117: 						     'height','texwidth');
  118: 	if ($constructtag) {
  119: 	    $result = &Apache::edit::rebuild_tag($token);
  120: 	    $result.=&Apache::edit::handle_insert();
  121: 	}
  122:     }
  123:     return $result;
  124: }
  125: 
  126: sub end_randomlabel {
  127:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  128:     my $result='';
  129:     my $count;
  130:     pop @Apache::lonxml::namespace;
  131:     if ($target eq 'web') {
  132: 	$count = $Apache::randomlabel::obj_cnt;
  133: 	if( $count != 0) { $args{"cgi.$cgi_id.OBJCOUNT"}=$count; }
  134: 	$result.='<img src="/adm/randomlabel.png?token='.$cgi_id.'" /><br />'."\n";
  135: 	&Apache::lonnet::appenv(%args);
  136:     } elsif ($target eq 'tex') {
  137: 	$result='\end{picture}\\\\';
  138: 	$result.= ' \vskip -'.$height_param.' mm }  \\\\ ';
  139:     } elsif ($target eq 'edit') {
  140: 	$result.=&Apache::edit::end_table;
  141:     }
  142:     return $result;
  143: }
  144: 
  145: sub start_bgimg {
  146:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  147:     my $result='';
  148:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
  149: 	&Apache::lonxml::startredirection(); 
  150:     }
  151:     return $result;
  152: }
  153: 
  154: sub end_bgimg {
  155:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  156:     my $result='';
  157:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
  158: 	my $bgimg=&Apache::lonxml::endredirection(); 
  159: 	if ($target eq 'web') {
  160: 	    $bgimg=&Apache::imageresponse::clean_up_image($bgimg);
  161: 	    $args{"cgi.$cgi_id.BGIMG"}=&Apache::lonnet::escape($bgimg);
  162: 	} elsif ($target eq 'tex') {
  163: 	    $result.=&make_eps_image($bgimg,$parstack,$safeeval,-2);
  164: 	}
  165:     }
  166:     return $result;
  167: }
  168: 
  169: sub make_eps_image {
  170:     my ($bgimg,$parstack,$safeeval,$depth)=@_;
  171:     my ($path,$file) = &Apache::londefdef::get_eps_image($bgimg);
  172:     ($height_param,$width_param)=
  173: 	&Apache::londefdef::image_size($bgimg,0.3,$parstack,$safeeval,
  174: 				       $depth,1);
  175:     my $dirtywidth=$width_param+5;
  176:     my $result ="\n".'\vspace*{2mm}\noindent'."\n".
  177: 	'\parbox{'.$dirtywidth.
  178: 	' mm}{  \noindent \epsfxsize='.$width_param.
  179: 	' mm \epsffile{'.$path.$file.
  180: 	'}\setlength{\unitlength}{1mm}'."\n".'  \begin{picture}('.
  181: 	$width_param.','.$height_param.')(0,-'.$height_param.')'."\n";
  182:     return $result;
  183: }
  184: 
  185: sub start_labelgroup {
  186:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  187:     my $result='';
  188:     my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  189:     my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
  190:     $type =~tr/A-Z/a-z/;
  191:     if ($target ne 'modified' && ($name =~ /\W/ || $name =~ /^[0-9]/)) {
  192: 	&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 />");
  193:     }
  194:     if ($target eq 'web' || $target eq 'tex' ||
  195: 	$target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
  196: 	$Apache::randomlabel::groupname=$name;
  197: 	$Apache::randomlabel::type=$type;
  198: 	@Apache::randomlabel::xcoord = ();
  199: 	@Apache::randomlabel::ycoord = ();
  200: 	@Apache::randomlabel::value = ();
  201: 	@Apache::randomlabel::label_arr  = ();
  202: 	@Apache::randomlabel::description  = ();
  203:     } elsif ($target eq 'edit') {
  204: 	$result.=&Apache::edit::tag_start($target,$token);
  205: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
  206: 	    &Apache::edit::select_arg('Type:','type',['text','image'],$token);
  207: 	if (!defined($token->[2]{'TeXsize'})) {
  208: 	    $token->[2]{'TeXsize'}='\normalsize';
  209: 	}
  210: 	$result.=&Apache::edit::select_arg('TeX font size:','TeXsize',
  211: 					   ['\tiny','\scriptsize',
  212: 					    '\footnotesize','\small',
  213: 					    '\normalsize','\large','\Large',
  214: 					    '\LARGE','\huge','\Huge'],
  215: 					   $token);
  216: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  217:     } elsif ($target eq 'modified') {
  218: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  219: 						     $safeeval,'name','type',
  220: 						     'TeXsize');
  221: 	if ($constructtag) {
  222: 	    $result = &Apache::edit::rebuild_tag($token);
  223: 	    $result.=&Apache::edit::handle_insert();
  224: 	}
  225:     }
  226:     return $result;
  227: }
  228: 
  229: sub add_vars {
  230:     my ($name,$order,$label,$labelorder,$value,$image,$safeeval) = @_;
  231:     if (!defined($name) || $name eq '') { return; }
  232:     my $code = '${'.$name."}{'".($order+1)."'}='".$label."';";
  233:     my $out=Apache::run::run($code,$safeeval);
  234:     if ($value) {
  235: 	$code = '${'.$name."}{'value_".($order+1)."'}='".$value."';";
  236: 	$out=Apache::run::run($code,$safeeval);
  237: 	$code = '${'.$name."}{'labelvalue_".($labelorder+1)."'}='".$value."';";
  238: 	$out=Apache::run::run($code,$safeeval);
  239:     }
  240:     if ($image) {
  241: 	my $code = '${'.$name."}{'image_".($order+1)."'}='".$image."';";
  242: 	my $out=Apache::run::run($code,$safeeval);
  243:     }
  244:     $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
  245:     $out=Apache::run::run($code,$safeeval);
  246: }
  247: 
  248: # begin to assign labels to locations
  249: sub end_labelgroup {
  250:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  251:     my $gname = $Apache::randomlabel::groupname;
  252:     my $type  = $Apache::randomlabel::type;
  253:     my $result='';
  254:     if ($target eq 'web' || $target eq 'answer' || $target eq 'grade' ||
  255: 	$target eq 'analyze') {
  256: 	my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
  257: 	&Apache::structuretags::shuffle(\@idx_arr);
  258: 	for(0 .. $#Apache::randomlabel::label_arr) {
  259: 	    my $str;
  260: 	    my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$_] ]";
  261: 	    my $x = $Apache::randomlabel::xcoord[$_];
  262: 	    my $y = $Apache::randomlabel::ycoord[$_];
  263: 	    my $value = $Apache::randomlabel::value[$_];
  264: 	    my $i=$Apache::randomlabel::obj_cnt++;
  265: 	    if( $type eq 'text') {
  266: 		&add_vars($gname,$_,$label,$idx_arr[$_],$value,'',$safeeval);
  267: 		$str = join(':',$x,$y,&Apache::lonnet::escape($label));
  268: 		$args{"cgi.$cgi_id.OBJTYPE"}.='LABEL:';
  269: 	    } elsif ( $type eq 'image') {
  270: 		&add_vars($gname,$_,
  271: 			  $Apache::randomlabel::description[$idx_arr[$_]],
  272: 			  $idx_arr[$_],$value,$label,$safeeval);
  273: 		$str = join(':',$x,$y,&Apache::lonnet::escape($label));
  274: 		$args{"cgi.$cgi_id.OBJTYPE"}.='IMAGE:';
  275: 	    } else {
  276: 		&Apache::lonxml::error('Unknown type of label :'.$type.':');
  277: 	    }
  278: 	    if ($target eq 'web') { $args{"cgi.$cgi_id.OBJ$i"} =$str; }
  279: 	}
  280:     } elsif ($target eq 'tex') {
  281: 	my $WX1=0; #  Web x-coord. of upper left corner (ULC)
  282: 	my $WY1=0; #  Web y-coord. of (ULC)
  283: 	my $wwidth=&Apache::lonxml::get_param('width',$parstack,$safeeval,-2);
  284: 	my $wheight=&Apache::lonxml::get_param('height',$parstack,$safeeval,-2);
  285: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
  286: 	if (!defined($TeXsize)) { $TeXsize='\\normalsize'; }
  287: 	
  288: 	my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
  289: 	&Apache::structuretags::shuffle(\@idx_arr);
  290: 
  291: 	&Apache::lonxml::debug("Array is:".$#Apache::randomlabel::label_arr.":");
  292: 	for(my $i=0;$i <= $#Apache::randomlabel::label_arr; $i++) {
  293: 	    my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$i] ]";
  294: 	    my $x = $Apache::randomlabel::xcoord[$i];
  295: 	    # FIXME the 3.5 here is the 'height' of the letter in TeX
  296: 	    my $y = $Apache::randomlabel::ycoord[$i]-3.5;
  297: 	    my $value = $Apache::randomlabel::value[$i];
  298: 	    #x latex coordinate
  299: 	    my $tcX=($x)*($width_param/$wwidth);
  300: 	    #y latex coordinate
  301:             #      my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
  302: 	    my $tcY=$height_param-$y*($height_param/$wheight);
  303: 	    $tcX=sprintf('%.2f',$tcX);
  304: 	    $tcY=sprintf('%.2f',$tcY);
  305: 	    $result .= '\put('.$tcX.','.$tcY.'){';
  306: 	    if( $type eq 'text') {
  307: 		$result.= $TeXsize.' \bf '.$label."}\n";
  308: 		&add_vars($gname,$i,$label,$idx_arr[$i],$value,'',$safeeval);
  309: 	    } elsif ( $type eq 'image') {
  310: 		$result .=  '\includegraphics{'.$label."}}\n";
  311: 		&add_vars($gname,$i,
  312: 			  $Apache::randomlabel::description[$idx_arr[$i]],
  313: 			  $idx_arr[$i],$value,$label,$safeeval);
  314: 	    } else {
  315: 		&Apache::lonxml::error('Unknown type of label :'.$type.':');
  316: 	    }
  317: 	}
  318:     } elsif ($target eq 'edit') {
  319: 	$result.=&Apache::edit::end_table;
  320:     }
  321:     return $result;
  322: }
  323: 
  324: # <location x="123" y="456" value="some value"/>
  325: sub start_location {
  326:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  327:     my $x= &check_int(&Apache::lonxml::get_param('x',$parstack,$safeeval),50);
  328:     my $y= &check_int(&Apache::lonxml::get_param('y',$parstack,$safeeval),50);
  329:     my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
  330:     my $result='';
  331:     push(@Apache::randomlabel::xcoord,$x);
  332:     push(@Apache::randomlabel::ycoord,$y);
  333:     push(@Apache::randomlabel::value,$value);
  334:     if ($target eq 'edit') {
  335: 	$result.=&Apache::edit::tag_start($target,$token);
  336: 	$result.=&Apache::edit::text_arg('X:','x',$token,4).
  337: 	    &Apache::edit::text_arg('Y:','y',$token,4).
  338: 	    &Apache::edit::entercoords('x','y','attribute','width','height').'&nbsp;&nbsp;&nbsp;'.
  339: 	    &Apache::edit::text_arg('Value:','value',$token).
  340: 	    &Apache::edit::end_row();
  341: 	$result.=&Apache::edit::end_table;
  342:     } elsif ($target eq 'modified') {
  343: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  344: 						    $safeeval,'x','y','value');
  345: 	if ($constructtag) {
  346: 	    $result = &Apache::edit::rebuild_tag($token);
  347: 	    $result.=&Apache::edit::handle_insert();
  348: 	}
  349:     }
  350:     return $result;
  351: }
  352: 
  353: sub end_location {
  354:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  355:     my @result;
  356:     if ($target eq 'edit') { @result=('','no') }
  357:     return @result;
  358: }
  359: 
  360: # <label>$var_abc</label>
  361: sub start_label {
  362:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  363:     my $result='';
  364:     my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
  365:     if ($target eq 'web' || $target eq 'tex' ||
  366: 	$target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
  367: 	&Apache::lonxml::startredirection; 
  368:     } elsif ($target eq 'edit') {
  369: 	$result.=&Apache::edit::tag_start($target,$token,"$type Label");
  370: 	my $text=&Apache::lonxml::get_all_text("/label",$parser);
  371: 	if ($type eq 'image') {
  372: 	    $result.=&Apache::edit::end_row().
  373: 		&Apache::edit::start_spanning_row();
  374: 	    $result.=&Apache::edit::text_arg('Description:','description',
  375: 					     $token);
  376: 	}
  377: 	if ($type eq 'text') { $result.="Label Text:"; }
  378: 	if ($type eq 'image') { $result.="Path to image:&nbsp;"; }
  379: 	$result.=&Apache::edit::editline('',$text,'',50).
  380: 	    &Apache::edit::end_table();
  381:     } elsif ($target eq 'modified') {
  382: 	$result = '<label>';
  383: 	if ($type eq 'image') {
  384: 	    my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  385: 							 $safeeval,
  386: 							 'description');
  387: 	    if ($constructtag) {
  388: 		$result = &Apache::edit::rebuild_tag($token);
  389: 	    } else {
  390: 		$result = $token->[4];
  391: 	    }
  392: 	}
  393: 	$result.=&Apache::edit::modifiedfield("/label",$parser);
  394:     }
  395:     return $result;
  396: }
  397: 
  398: sub end_label {
  399:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  400:     my @result;
  401:     if ($target eq 'edit') {
  402: 	@result=('','no') ;
  403:     } elsif ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
  404: 	     $target eq 'answer' || $target eq 'analyze') {
  405: 	my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
  406: 	my $ltext=&Apache::lonxml::endredirection; 
  407: 	if ($type eq 'image') {
  408: 	    if ($target eq 'tex') {
  409: 		# For tex targets, our image url has been potentially corrupted
  410: 		# by prepending \'s in front of special latex symbols.
  411: 		# For now we only worry about the _ case (most common?)
  412: 		# There's a whole host of theim in lonxml::latex_special_symbols
  413: 		# that could potentially have to be re-done.
  414: 
  415: 		$ltext =~ s/\\_/_/g;
  416: 	    }
  417: 	    &Apache::lonxml::debug("Turning $ltext, $Apache::lonxml::pwd[-1]");
  418: 	    $ltext=&Apache::imageresponse::clean_up_image($ltext);
  419: #	    $ltext=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],
  420: #						 $ltext);
  421: 	    &Apache::lonxml::debug("into $ltext");
  422: 	    my $description = &Apache::lonxml::get_param('description',
  423: 							 $parstack,$safeeval);
  424: 	    push(@Apache::randomlabel::description,$description);
  425: 	} else {
  426: 	    $ltext=~s/[\r\n]*//gs
  427: 	}
  428: 	push(@Apache::randomlabel::label_arr,$ltext);
  429:     }
  430:     return @result;
  431: }
  432: 
  433: 1;
  434: __END__
  435:  

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