File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.48: download - view: text, annotated - select for diffs
Mon Sep 8 19:17:34 2003 UTC (20 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- the labelgroup name is a variable name, so we need to make sure they make good variable names

    1: # The LearningOnline Network with CAPA
    2: # random labelling tool
    3: #
    4: # $Id: randomlabel.pm,v 1.48 2003/09/08 19:17:34 albertel 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: # 7/20/2001 Isaac Tsai, initial syntax
   29: # 8/10/2001 Isaac Tsai, 
   30: # 8/30/2001 Isaac Tsai, 
   31: # SYNTAX:
   32: # <randomlabel bgimg="URL" width="12" height="45" texwidth="50">
   33: #    <labelgroup name="GroupOne" type="image">
   34: #      <location x="123" y="456" />
   35: #      <location x="321" y="654" />
   36: #      <location x="213" y="546" />
   37: #      <label description="TEXT-1">IMG-URL</label>
   38: #      <label description="TEXT-2">IMG-URL</label>
   39: #      <label description="TEXT-3">IMG-URL</label>
   40: #    </labelgroup>
   41: #    <labelgroup name="GroupTwo" type="text">
   42: #      <location x="12" y="45" />
   43: #      <location x="32" y="65" />
   44: #      <location x="21" y="54" />
   45: #      <label>TEXT-1</label>
   46: #      <label>TEXT-2</label>
   47: #      <label>TEXT-3</label>
   48: #    </labelgroup>
   49: #   </randomlabel>
   50: #  ===========================================
   51: #  side effect:
   52: #    location (123,456): $GroupOne[0] = 2  # images give out indexes
   53: #             (321,654): $GroupOne[1] = 1
   54: #             (213,546): $GroupOne[2] = 0
   55: #    location (12,45)  : $GroupTwo[0] = "TEXT-3"
   56: #             (32,65)  : $GroupTwo[1] = "TEXT-1"
   57: #             (21,54)  : $GroupTwo[2] = "TEXT-2"
   58: #  ===========================================
   59: package Apache::randomlabel;
   60: use Apache::lonnet;
   61: use strict;
   62: use Apache::edit;
   63: use Apache::File();
   64: use Apache::Constants qw(:common :http);
   65: 
   66: BEGIN {
   67:     &Apache::lonxml::register('Apache::randomlabel',('randomlabel','labelgroup','location','label'));
   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: sub start_randomlabel {
   83:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   84:     my $result='';
   85:     push (@Apache::lonxml::namespace,'randomlabel');
   86:     my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
   87:     if ( $bgimg !~ /^http:/ ) {
   88: 	$bgimg=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$bgimg);
   89: 	if (&Apache::lonnet::repcopy($bgimg) ne OK) {
   90: 	    $bgimg='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
   91: 	}
   92:     }
   93:     my $w= &check_int(&Apache::lonxml::get_param('width',$parstack,$safeeval));
   94:     my $h= &check_int(&Apache::lonxml::get_param('height',$parstack,$safeeval));
   95:     my $texwidth= &Apache::lonxml::get_param('texwidth',$parstack,$safeeval);
   96:     $Apache::randomlabel::tlabel_cnt=0;
   97:     $Apache::randomlabel::ilabel_cnt=0;
   98:     if ($target eq 'web') {
   99: 	&Apache::lonxml::startredirection();
  100: 	$result.="BGIMG=".&Apache::lonnet::escape($bgimg);
  101:     } elsif ($target eq 'tex') {
  102: 	my $newbgimg = $bgimg;
  103: 	$bgimg=~s/\.(gif|jpg|png|jpeg)$/\.eps/i;
  104: 	$bgimg= &Apache::lonnet::filelocation($bgimg);
  105: 	$bgimg=~s/http:\/[^\/]*/\/home\/httpd\/html/;
  106: 	$bgimg=~s/\/$//;
  107: 	#if no eps file try to replicate it
  108: 	if (not-e $bgimg) {
  109: 	    if (&Apache::lonnet::repcopy($bgimg) ne OK ) {
  110: 		#if replication failed try to find ps file
  111: 		$bgimg=~s/\.eps$/\.ps/;
  112: 		#if no ps file try to replicate it
  113: 		if (not -e $bgimg &&
  114: 		    &Apache::lonnet::repcopy($bgimg) ne OK) {
  115:                     #if replication failed try to produce eps file dynamically
  116: 		    $bgimg=~s/\.ps$/\.eps/;
  117: 		    my $temp_file;
  118: 		    my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
  119: 		    $temp_file = Apache::File->new('>>'.$filename);
  120: 		    $newbgimg =~ s/(.*)\/res\//\/home\/httpd\/html\/res\//;
  121: 		    print $temp_file "$newbgimg\n";
  122: 		    $bgimg =~ m/\/([^\/]+)$/;
  123: 		    $bgimg = '/home/httpd/prtspool/'.$1;
  124: 		}
  125: 	    }
  126: 	}
  127: 	$bgimg=~s/\/$//;
  128: 	my $dirtywidth=$texwidth+5;
  129: 	if ($texwidth==90) {
  130: 	    $result.='\vspace*{2mm}\noindent \parbox{'.$dirtywidth.' mm}{  \noindent \epsfxsize='.$texwidth.' mm \epsffile{'.
  131: 		$bgimg.'}\setlength{\unitlength}{1mm}  \begin{picture}('.$texwidth.','.$texwidth*$h/$w.')(0,-'.$texwidth*$h/$w.')';
  132: 	} else {
  133: 	    $result.='\vspace*{2mm}\noindent \parbox{'.$dirtywidth.' mm}{  \noindent \epsfxsize='.$texwidth.' mm \epsffile{'.
  134: 		$bgimg.'}\setlength{\unitlength}{1mm}  \begin{picture}('.$texwidth.','.$texwidth*$h/$w.')(0,-'.$texwidth*$h/$w.')';
  135: 	}
  136:     } elsif ($target eq 'edit') {
  137: 	$result.=&Apache::edit::tag_start($target,$token);
  138: 	$Apache::edit::bgimgsrc=
  139: 	    &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
  140: 	$Apache::edit::bgimgsrccurdepth=$Apache::lonxml::curdepth;
  141: 	$result.=&Apache::edit::text_arg('Image:','bgimg',$token,75).' ';
  142: 	$result.=&Apache::edit::browse('bgimg').' ';
  143: 	$result.=&Apache::edit::search('bgimg').'<br />'.
  144: 	    &Apache::edit::text_arg('Width(pixel):' ,'width'   ,$token,6).
  145: 	    &Apache::edit::text_arg('Height(pixel):','height'  ,$token,6).
  146: 	    &Apache::edit::text_arg('TeXWidth(mm):' ,'texwidth',$token,6).
  147: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();     
  148:     } elsif ($target eq 'modified') {
  149: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  150: 						     $safeeval,'bgimg','width',
  151: 						     'height','texwidth');
  152: 	if ($constructtag) {
  153: 	    $result = &Apache::edit::rebuild_tag($token);
  154: 	    $result.=&Apache::edit::handle_insert();
  155: 	}
  156:     }
  157:     return $result;
  158: }
  159: 
  160: sub end_randomlabel {
  161:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  162:     my $result='';
  163:     my $count;
  164:     pop @Apache::lonxml::namespace;
  165:     if ($target eq 'web') {
  166: 	my $args=&Apache::lonxml::endredirection();
  167: 	$args=~s/\s*&/&/g;
  168: 	$args=~s/\s*$//;
  169: 	$count = $Apache::randomlabel::tlabel_cnt;
  170: 	if( $count != 0) { $args.= "&COUNT=$count"; }
  171: 	$count = $Apache::randomlabel::ilabel_cnt;
  172: 	if( $count != 0) { $args.= "&ICOUNT=$count"; }
  173: 	my $token=$Apache::lonxml::curdepth.'_'.$$;
  174: 	$result.='<img src="/adm/randomlabel.png?token='.$token;
  175: 	$result.='" /><br />'."\n";
  176: 	&Apache::lonnet::appenv(("imagerequest.$token"=>&Apache::lonnet::escape($args)));
  177:     } elsif ($target eq 'tex') {
  178: 	$result='\end{picture}\\\\';
  179: 	my $height=&Apache::lonxml::get_param('height',$parstack,$safeeval);
  180: 	my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
  181: 	my $texwidth=&Apache::lonxml::get_param('texwidth',$parstack,$safeeval);
  182: 	my $howtoskipback = $texwidth*$height/$width;
  183: 	$result.= ' \vskip -'.$howtoskipback.' mm }  \\\\ ';
  184:     } elsif ($target eq 'edit') {
  185: 	$result.=&Apache::edit::end_table;
  186:     }
  187:     return $result;
  188: }
  189: 
  190: sub start_labelgroup {
  191:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  192:     my $result='';
  193:     my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  194:     my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
  195:     $type =~tr/A-Z/a-z/;
  196:     if ($target ne 'modified' && ($name =~ /\W/ || $name =~ /^[0-9]/)) {
  197: 	&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 />");
  198:     }
  199:     if ($target eq 'web' || $target eq 'tex' ||
  200: 	$target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
  201: 	$Apache::randomlabel::groupname=$name;
  202: 	$Apache::randomlabel::type=$type;
  203: 	@Apache::randomlabel::xcoord = ();
  204: 	@Apache::randomlabel::ycoord = ();
  205: 	@Apache::randomlabel::value = ();
  206: 	@Apache::randomlabel::label_arr  = ();
  207: 	@Apache::randomlabel::decription  = ();
  208:     } elsif ($target eq 'edit') {
  209: 	$result.=&Apache::edit::tag_start($target,$token);
  210: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
  211: 	    &Apache::edit::select_arg('Type:','type',['text','image'],$token).
  212: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  213:     } elsif ($target eq 'modified') {
  214: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  215: 						     $safeeval,'name','type');
  216: 	if ($constructtag) {
  217: 	    $result = &Apache::edit::rebuild_tag($token);
  218: 	    $result.=&Apache::edit::handle_insert();
  219: 	}
  220:     }
  221:     return $result;
  222: }
  223: 
  224: sub add_vars {
  225:     my ($name,$order,$label,$labelorder,$value,$image,$safeeval) = @_;
  226:     my $code = '${'.$name."}{'".($order+1)."'}='".$label."';";
  227:     my $out=Apache::run::run($code,$safeeval);
  228:     if ($value) {
  229: 	$code = '${'.$name."}{'value_".($order+1)."'}='".$value."';";
  230: 	$out=Apache::run::run($code,$safeeval);
  231: 	$code = '${'.$name."}{'labelvalue_".($labelorder+1)."'}='".$value."';";
  232: 	$out=Apache::run::run($code,$safeeval);
  233:     }
  234:     if ($image) {
  235: 	my $code = '${'.$name."}{'image_".($order+1)."'}='".$image."';";
  236: 	my $out=Apache::run::run($code,$safeeval);
  237:     }
  238:     $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
  239:     $out=Apache::run::run($code,$safeeval);
  240: }
  241: 
  242: # begin to assign labels to locations
  243: sub end_labelgroup {
  244:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  245:     my $gname = $Apache::randomlabel::groupname;
  246:     my $type  = $Apache::randomlabel::type;
  247:     my $result='';
  248:     if ($target eq 'web' || $target eq 'answer' || $target eq 'grade' ||
  249: 	$target eq 'analyze') {
  250: 	my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
  251: 	&Apache::structuretags::shuffle(\@idx_arr);
  252: 	for(0 .. $#Apache::randomlabel::label_arr) {
  253: 	    my $str;
  254: 	    my $xstr;
  255: 	    my $ystr;
  256: 	    my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$_] ]";
  257: 	    my $x = $Apache::randomlabel::xcoord[$_];
  258: 	    my $y = $Apache::randomlabel::ycoord[$_];
  259: 	    my $value = $Apache::randomlabel::value[$_];
  260: 	    if( $type eq 'text') {
  261: 		&add_vars($gname,$_,$label,$idx_arr[$_],$value,'',$safeeval);
  262: 		$str = 'LB'.$Apache::randomlabel::tlabel_cnt;
  263: 		$xstr = 'X'.$Apache::randomlabel::tlabel_cnt;
  264: 		$ystr = 'Y'.$Apache::randomlabel::tlabel_cnt;
  265: 		$Apache::randomlabel::tlabel_cnt += 1;
  266: 	    } elsif ( $type eq 'image') {
  267: 		&add_vars($gname,$_,
  268: 			  $Apache::randomlabel::description[$idx_arr[$_]],
  269: 			  $idx_arr[$_],$value,$label,$safeeval);
  270: 		$str = 'IMG'.$Apache::randomlabel::ilabel_cnt;
  271: 		$xstr = 'IX'.$Apache::randomlabel::ilabel_cnt;
  272: 		$ystr = 'IY'.$Apache::randomlabel::ilabel_cnt;
  273: 		$Apache::randomlabel::ilabel_cnt += 1;
  274: 	    } else {
  275: 		&Apache::lonxml::error('Unknown type of label :'.$type.':');
  276: 	    }
  277: 	    if ($target eq 'web') {
  278: 		$result .= '&'. $str  .'='.&Apache::lonnet::escape($label);
  279: 		$result .= '&'. $xstr .'='.$x;
  280: 		$result .= '&'. $ystr .'='.$y;
  281: 	    }
  282: 	}
  283:     } elsif ($target eq 'tex') {
  284: 	my $WX1=0; #  Web x-coord. of upper left corner (ULC)
  285: 	my $WY1=0; #  Web y-coord. of (ULC)
  286: 	my $wwidth=&Apache::lonxml::get_param('width',$parstack,$safeeval,-2);
  287: 	my $wheight=&Apache::lonxml::get_param('height',$parstack,$safeeval,-2);
  288: 	my $texwidth=&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,-2);
  289: 	my $TX1=0;
  290: 	my $TY1=$texwidth*($wheight/$wwidth);
  291: 	my $TX2=$texwidth;
  292: 	my $TY2=0;
  293: 
  294: 
  295: 	my $slopex=($wwidth-$WX1)/($TX2-$TX1);
  296: 	my $slopey=($wheight-$WY1)/($TY2-($TY1-1.0));
  297: 	my $cstx=$wwidth-$slopex*($TX2);
  298: 	my $csty=$wheight-$slopey*($TY2);
  299: 
  300: 	my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
  301: 	&Apache::structuretags::shuffle(\@idx_arr);
  302: 
  303: 	&Apache::lonxml::debug("Array is:".$#Apache::randomlabel::label_arr.":");
  304: 	for(my $i=0;$i <= $#Apache::randomlabel::label_arr; $i++) {
  305: 	    my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$i] ]";
  306: 	    my $x = $Apache::randomlabel::xcoord[$i];
  307: 	    my $y = $Apache::randomlabel::ycoord[$i];
  308: 	    my $value = $Apache::randomlabel::value[$i];
  309: 	    #x latex coordinate
  310: 	    my $tcX=($x)*($texwidth/$wwidth);
  311: 	    #y latex coordinate
  312:             #      my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
  313: 	    my $tcY=$TY1-$y*($TY1/$wheight);
  314: 	    $tcX=sprintf('%.2f',$tcX);
  315: 	    $tcY=sprintf('%.2f',$tcY);
  316: 	    $result.='\put('.$tcX.','.$tcY.'){\normalsize \bf '.$label.'}'."\n";
  317: 	    if( $type eq 'text') {
  318: 		&add_vars($gname,$i,$label,$idx_arr[$i],$value,'',$safeeval);
  319: 	    } elsif ( $type eq 'image') {
  320: 		&add_vars($gname,$i,
  321: 			  $Apache::randomlabel::description[$idx_arr[$i]],
  322: 			  $idx_arr[$i],$value,$label,$safeeval);
  323: 	    } else {
  324: 		&Apache::lonxml::error('Unknown type of label :'.$type.':');
  325: 	    }
  326: 	}
  327:     } elsif ($target eq 'edit') {
  328: 	$result.=&Apache::edit::end_table;
  329:     }
  330:     return $result;
  331: }
  332: 
  333: # <location x="123" y="456" value="some value"/>
  334: sub start_location {
  335:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  336:     my $x= &check_int(&Apache::lonxml::get_param('x',$parstack,$safeeval),50);
  337:     my $y= &check_int(&Apache::lonxml::get_param('y',$parstack,$safeeval),50);
  338:     my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
  339:     my $result='';
  340:     push(@Apache::randomlabel::xcoord,$x);
  341:     push(@Apache::randomlabel::ycoord,$y);
  342:     push(@Apache::randomlabel::value,$value);
  343:     if ($target eq 'edit') {
  344: 	$result.=&Apache::edit::tag_start($target,$token);
  345: 	$result.=&Apache::edit::text_arg('X:','x',$token,4).
  346: 	    &Apache::edit::text_arg('Y:','y',$token,4).
  347: 	    &Apache::edit::entercoords('x','y','attribute','width','height').'&nbsp;&nbsp;&nbsp;'.
  348: 	    &Apache::edit::text_arg('Value:','value',$token).
  349: 	    &Apache::edit::end_row();
  350: 	$result.=&Apache::edit::end_table;
  351:     } elsif ($target eq 'modified') {
  352: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  353: 						    $safeeval,'x','y','value');
  354: 	if ($constructtag) {
  355: 	    $result = &Apache::edit::rebuild_tag($token);
  356: 	    $result.=&Apache::edit::handle_insert();
  357: 	}
  358:     }
  359:     return $result;
  360: }
  361: 
  362: sub end_location {
  363:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  364:     my @result;
  365:     if ($target eq 'edit') { @result=('','no') }
  366:     return @result;
  367: }
  368: 
  369: # <label>$var_abc</label>
  370: sub start_label {
  371:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  372:     my $result='';
  373:     my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
  374:     if ($target eq 'web' || $target eq 'tex' ||
  375: 	$target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
  376: 	my $ltext=&Apache::lonxml::get_all_text("/label",$parser);
  377: 	$ltext=&Apache::run::evaluate($ltext,$safeeval,$$parstack[-1]);
  378: 	if ($type eq 'image') {
  379: 	    &Apache::lonxml::debug("Turning $ltext, $Apache::lonxml::pwd[-1]");
  380: 	    $ltext=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],
  381: 						 $ltext);
  382: 	    &Apache::lonxml::debug("into $ltext");
  383: 	    my $description = &Apache::lonxml::get_param('description',
  384: 							 $parstack,$safeeval);
  385: 	    push(@Apache::randomlabel::description,$description);
  386: 	}
  387: 	push(@Apache::randomlabel::label_arr,$ltext);
  388:     } elsif ($target eq 'edit') {
  389: 	$result.=&Apache::edit::tag_start($target,$token,"$type Label");
  390: 	my $text=&Apache::lonxml::get_all_text("/label",$parser);
  391: 	if ($type eq 'image') {
  392: 	    $result.=&Apache::edit::end_row().
  393: 		&Apache::edit::start_spanning_row();
  394: 	    $result.=&Apache::edit::text_arg('Description:','description',
  395: 					     $token);
  396: 	}
  397: 	if ($type eq 'text') { $result.="Label Text:"; }
  398: 	if ($type eq 'image') { $result.="Path to image:&nbsp;"; }
  399: 	$result.=&Apache::edit::editline('',$text,'',50).
  400: 	    &Apache::edit::end_table();
  401:     } elsif ($target eq 'modified') {
  402: 	my $text=$$parser[-1]->get_text("/label");
  403: 	$result = '<label>';
  404: 	if ($type eq 'image') {
  405: 	    my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  406: 							 $safeeval,
  407: 							 'description');
  408: 	    if ($constructtag) {
  409: 		$result = &Apache::edit::rebuild_tag($token);
  410: 	    } else {
  411: 		$result = $token->[4];
  412: 	    }
  413: 	}
  414: 	$result.=&Apache::edit::modifiedfield($token);
  415:     }
  416:     return $result;
  417: }
  418: 
  419: sub end_label {
  420:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  421:     my @result;
  422:     if ($target eq 'edit') { @result=('','no') }
  423:     return @result;
  424: }
  425: 
  426: 1;
  427: __END__
  428:  

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