File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.34: download - view: text, annotated - select for diffs
Wed Aug 7 21:49:35 2002 UTC (21 years, 10 months ago) by sakharuk
Branches: MAIN
CVS tags: version_0_5, HEAD
Changed algorithm for placing labels and converting width of the box.
In addition now picture is adjusted to the left side of the page.

    1: # The LearningOnline Network with CAPA
    2: # random labelling tool
    3: #
    4: # $Id: randomlabel.pm,v 1.34 2002/08/07 21:49:35 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:     } else {
  114: 	$bgimg=~s/http:\/[^\/]*\/~([^\/]+)/\/home\/$1\/public_html/;
  115: 
  116:     }
  117:     $bgimg=~s/\/$//;
  118:     my $dirtywidth=$texwidth+5;
  119:     if ($texwidth==90) {
  120: 	$result.='\vspace*{2mm}\noindent \parbox{'.$dirtywidth.' mm}{  \noindent \epsfxsize='.$texwidth.' mm \epsffile{'.
  121: 	    $bgimg.'}\setlength{\unitlength}{1mm}  \begin{picture}('.$texwidth.','.$texwidth*$h/$w.')(0,-'.$texwidth*$h/$w.')';
  122:     } else {
  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:     }
  126:   } elsif ($target eq 'edit') {
  127:     $result.=&Apache::edit::tag_start($target,$token);
  128:     $result.=&Apache::edit::text_arg('Image:','bgimg',$token,75).' ';
  129:     $result.=&Apache::edit::browse('bgimg').' ';
  130:     $result.=&Apache::edit::search('bgimg').'<br />'.
  131:         &Apache::edit::text_arg('Width(pixel):' ,'width'   ,$token,6).
  132:         &Apache::edit::text_arg('Height(pixel):','height'  ,$token,6).
  133:         &Apache::edit::text_arg('TeXWidth(mm):' ,'texwidth',$token,6).
  134:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();     
  135:   } elsif ($target eq 'modified') {
  136:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  137: 						 'bgimg','width','height',
  138: 						 'texwidth');
  139:     if ($constructtag) {
  140:       $result = &Apache::edit::rebuild_tag($token);
  141:       $result.=&Apache::edit::handle_insert();
  142:     }
  143:   }
  144:   return $result;
  145: }
  146: 
  147: sub end_randomlabel {
  148:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  149:   my $result='';
  150:   my $count;
  151:   pop @Apache::lonxml::namespace;
  152:   if ($target eq 'web') {
  153:     $count = $Apache::randomlabel::tlabel_cnt;
  154:     if( $count != 0) { $result.= "<param name=\"COUNT\" value=\"$count\">\n"; }
  155:     $count = $Apache::randomlabel::ilabel_cnt;
  156:     if( $count != 0) { $result.= "<param name=\"ICOUNT\" value=\"$count\">\n"; }
  157:     $result .= "</applet>\n<BR />";
  158:   } elsif ($target eq 'tex') {
  159:       $result='\end{picture}\\\\}';
  160: 	  @$parstack[-1]=~/\$height\s*=\s*?"(.+)?"/;
  161:       my $one=$1;
  162: 	  @$parstack[-1]=~/\$width\s*=\s*?"(.+)?"/;
  163:       my $two=$1;
  164: 	  @$parstack[-1]=~/\$texwidth\s*=\s*?"(.+)?"/;
  165:       my $three=$1;
  166:       my $howtoskipback = $three*$one/$two;
  167:       $result.=' \vskip -'.$howtoskipback.' mm ';
  168:   } elsif ($target eq 'edit') {
  169:     $result.=&Apache::edit::end_table;
  170:   }
  171:   return $result;
  172: }
  173: 
  174: sub start_labelgroup {
  175:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  176:   my $result='';
  177:   my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  178:   my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
  179:   $type =~tr/A-Z/a-z/;
  180:   if ($target eq 'web' || $target eq 'tex' || 
  181:       $target eq 'grade' || $target eq 'answer') {
  182:     $Apache::randomlabel::groupname=$name;
  183:     $Apache::randomlabel::type=$type;
  184:     @Apache::randomlabel::xcoord = ();
  185:     @Apache::randomlabel::ycoord = ();
  186:     @Apache::randomlabel::value = ();
  187:     @Apache::randomlabel::label_arr  = ();
  188:     @Apache::randomlabel::decription  = ();
  189:   } elsif ($target eq 'edit') {
  190:     $result.=&Apache::edit::tag_start($target,$token);
  191:     $result.=&Apache::edit::text_arg('Name:','name',$token).
  192:       &Apache::edit::select_arg('Type:','type',['text','image'],$token).
  193: 	  &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  194:   } elsif ($target eq 'modified') {
  195:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  196: 						 'name','type');
  197:     if ($constructtag) {
  198:       $result = &Apache::edit::rebuild_tag($token);
  199:       $result.=&Apache::edit::handle_insert();
  200:     }
  201:   }
  202:   return $result;
  203: }
  204: 
  205: sub add_vars {
  206:   my ($name,$order,$label,$labelorder,$value,$image,$safeeval) = @_;
  207:   my $code = '${'.$name."}{'".($order+1)."'}='".$label."';";
  208:   my $out=Apache::run::run($code,$safeeval);
  209:   if ($value) {
  210:     $code = '${'.$name."}{'value_".($order+1)."'}='".$value."';";
  211:     $out=Apache::run::run($code,$safeeval);
  212:     $code = '${'.$name."}{'labelvalue_".($labelorder+1)."'}='".$value."';";
  213:     $out=Apache::run::run($code,$safeeval);
  214:   }
  215:   if ($image) {
  216:       my $code = '${'.$name."}{'image_".($order+1)."'}='".$image."';";
  217:       my $out=Apache::run::run($code,$safeeval);
  218:   }
  219:   $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
  220:   $out=Apache::run::run($code,$safeeval);
  221: }
  222: 
  223: # begin to assign labels to locations
  224: sub end_labelgroup {
  225:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  226:   my $gname = $Apache::randomlabel::groupname;
  227:   my $type  = $Apache::randomlabel::type;
  228:   my $result='';
  229:   if ($target eq 'web' || $target eq 'answer' || $target eq 'grade') {
  230:     my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
  231:     &Apache::structuretags::shuffle(\@idx_arr);
  232:     for(0 .. $#Apache::randomlabel::label_arr) {
  233:       my $str;
  234:       my $xstr;
  235:       my $ystr;
  236:       my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$_] ]";
  237:       my $x = $Apache::randomlabel::xcoord[$_];
  238:       my $y = $Apache::randomlabel::ycoord[$_];
  239:       my $value = $Apache::randomlabel::value[$_];
  240:       if( $type eq 'text') {
  241: 	&add_vars($gname,$_,$label,$idx_arr[$_],$value,'',$safeeval);
  242: 	$str = 'LB'.$Apache::randomlabel::tlabel_cnt;
  243: 	$xstr = 'X'.$Apache::randomlabel::tlabel_cnt;
  244: 	$ystr = 'Y'.$Apache::randomlabel::tlabel_cnt;
  245: 	$Apache::randomlabel::tlabel_cnt += 1;
  246:       } elsif ( $type eq 'image') {
  247: 	&add_vars($gname,$_,
  248: 		  $Apache::randomlabel::description[$idx_arr[$_]],
  249: 		  $idx_arr[$_],$value,$label,$safeeval);
  250: 	$str = 'IMG'.$Apache::randomlabel::ilabel_cnt;
  251: 	$xstr = 'IX'.$Apache::randomlabel::ilabel_cnt;
  252: 	$ystr = 'IY'.$Apache::randomlabel::ilabel_cnt;
  253: 	$Apache::randomlabel::ilabel_cnt += 1;
  254:       } else {
  255: 	&Apache::lonxml::error('Unknown type of label :'.$type.':');
  256:       }
  257:       if ($target eq 'web') {
  258: 	$result .= '<param name="' . $str  . '" value="'.$label.'">';
  259: 	$result .= '<param name="' . $xstr . '" value="'.$x.'">';
  260: 	$result .= '<param name="' . $ystr . '" value="'.$y.'">'."\n";
  261:       }
  262:     }
  263:   } elsif ($target eq 'tex') {
  264:     my $WX1=0; #  Web x-coord. of upper left corner (ULC)
  265:     my $WY1=0; #  Web y-coord. of (ULC)
  266:     my $wwidth=&Apache::lonxml::get_param('width',$parstack,$safeeval,-2);
  267:     my $wheight=&Apache::lonxml::get_param('height',$parstack,$safeeval,-2);
  268:     my $texwidth=&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,-2);
  269:     my $TX1=0;
  270:     my $TY1=$texwidth*($wheight/$wwidth);
  271:     my $TX2=$texwidth;
  272:     my $TY2=0;
  273: 
  274: 
  275:     my $slopex=($wwidth-$WX1)/($TX2-$TX1);
  276:     my $slopey=($wheight-$WY1)/($TY2-($TY1-1.0));
  277:     my $cstx=$wwidth-$slopex*($TX2);
  278:     my $csty=$wheight-$slopey*($TY2);
  279: 
  280:     my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
  281:     &Apache::structuretags::shuffle(\@idx_arr);
  282: 
  283:     &Apache::lonxml::debug("Array is:".$#Apache::randomlabel::label_arr.":");
  284:     for(my $i=0;$i <= $#Apache::randomlabel::label_arr; $i++) {
  285:       my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$i] ]";
  286:       my $x = $Apache::randomlabel::xcoord[$i];
  287:       my $y = $Apache::randomlabel::ycoord[$i];
  288:       my $value = $Apache::randomlabel::value[$i];
  289:       #x latex coordinate
  290:       my $tcX=($x)*($texwidth/$wwidth);
  291:       #y latex coordinate
  292: #      my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
  293:       my $tcY=$TY1-$y*($TY1/$wheight);
  294:       $tcX=sprintf('%.2f',$tcX);
  295:       $tcY=sprintf('%.2f',$tcY);
  296:       $result.='\put('.$tcX.','.$tcY.'){\normalsize \bf '.$label.'}'."\n";
  297:       if( $type eq 'text') {
  298: 	&add_vars($gname,$i,$label,$idx_arr[$i],$value,'',$safeeval);
  299:       } elsif ( $type eq 'image') {
  300: 	&add_vars($gname,$i,
  301: 		  $Apache::randomlabel::description[$idx_arr[$i]],
  302: 		  $idx_arr[$i],$value,$label,$safeeval);
  303:       } else {
  304: 	&Apache::lonxml::error('Unknown type of label :'.$type.':');
  305:       }
  306:     }
  307:   } elsif ($target eq 'edit') {
  308:     $result.=&Apache::edit::end_table;
  309:   }
  310:   return $result;
  311: }
  312: 
  313: # <location x="123" y="456" value="some value"/>
  314: sub start_location {
  315:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  316:   my $x= &check_int(&Apache::lonxml::get_param('x',$parstack,$safeeval),50);
  317:   my $y= &check_int(&Apache::lonxml::get_param('y',$parstack,$safeeval),50);
  318:   my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
  319:   my $result='';
  320:   push(@Apache::randomlabel::xcoord,$x);
  321:   push(@Apache::randomlabel::ycoord,$y);
  322:   push(@Apache::randomlabel::value,$value);
  323:   if ($target eq 'edit') {
  324:     $result.=&Apache::edit::tag_start($target,$token);
  325:     $result.=&Apache::edit::text_arg('X:','x',$token,4).
  326:       &Apache::edit::text_arg('Y:','y',$token,4).
  327: 	&Apache::edit::text_arg('Value:','value',$token).
  328: 	  &Apache::edit::end_row();
  329:     $result.=&Apache::edit::end_table;
  330:   } elsif ($target eq 'modified') {
  331:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  332: 						 'x','y','value');
  333:     if ($constructtag) {
  334:       $result = &Apache::edit::rebuild_tag($token);
  335:       $result.=&Apache::edit::handle_insert();
  336:     }
  337:   }
  338:   return $result;
  339: }
  340: 
  341: sub end_location {
  342:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  343:   my @result;
  344:   if ($target eq 'edit') { @result=('','no') }
  345:   return @result;
  346: }
  347: 
  348: # <label>$var_abc</label>
  349: sub start_label {
  350:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  351:   my $result='';
  352:   my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
  353:   if ($target eq 'web' || $target eq 'tex' || 
  354:       $target eq 'grade' || $target eq 'answer') {
  355:     my $ltext=&Apache::lonxml::get_all_text("/label",$$parser[-1]);
  356:     $ltext=&Apache::run::evaluate($ltext,$safeeval,$$parstack[-1]);
  357:     if ($type eq 'image') {
  358:       &Apache::lonxml::debug("Turning $ltext and $Apache::lonxml::pwd[-1]");
  359:       $ltext="http://$ENV{'SERVER_NAME'}".&Apache::lonnet::hreflocation($Apache::lonxml::pwd[-1],
  360: 					   $ltext);
  361:       &Apache::lonxml::debug("into $ltext");
  362:       my $description = &Apache::lonxml::get_param('description',
  363: 						   $parstack,$safeeval);
  364:       push(@Apache::randomlabel::description,$description);
  365:     }
  366:     push(@Apache::randomlabel::label_arr,$ltext);
  367:   } elsif ($target eq 'edit') {
  368:     $result.=&Apache::edit::tag_start($target,$token,"$type Label");
  369:     my $text=&Apache::lonxml::get_all_text("/label",$$parser[-1]);
  370:     if ($type eq 'image') {
  371:       $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  372:       $result.=&Apache::edit::text_arg('Description:','description',$token);
  373:     }
  374:     if ($type eq 'text') { $result.="Label Text:"; }
  375:     if ($type eq 'image') { $result.="Path to image:"; }
  376:     $result.=&Apache::edit::editline('',$text,'',20).
  377:       &Apache::edit::end_table();
  378:   } elsif ($target eq 'modified') {
  379:     my $text=$$parser[-1]->get_text("/label");
  380:     $result = '<label>';
  381:     if ($type eq 'image') {
  382:       my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  383: 						   'description');
  384:       if ($constructtag) {
  385: 	$result = &Apache::edit::rebuild_tag($token);
  386:       } else {
  387: 	$result = $token->[4];
  388:       }
  389:     }
  390:     $result.=&Apache::edit::modifiedfield($token);
  391:   }
  392:   return $result;
  393: }
  394: 
  395: sub end_label {
  396:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  397:   my @result;
  398:   if ($target eq 'edit') { @result=('','no') }
  399:   return @result;
  400: }
  401: 
  402: 
  403: 
  404: 1;
  405: __END__
  406:  

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