File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.55: download - view: text, annotated - select for diffs
Thu Jan 8 20:28:20 2004 UTC (20 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- trying to make the regexps readable

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

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