File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.50: download - view: text, annotated - select for diffs
Thu Sep 25 14:05:44 2003 UTC (20 years, 7 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Bug 2221 (click on image problems don't properly scale image) is fixed. Additional cleanup is done too.

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

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