File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.35: download - view: text, annotated - select for diffs
Tue Aug 27 15:40:07 2002 UTC (21 years, 9 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Added replication of eps. picture in the case if original eps does not exist on current library server.

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

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