File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.32: download - view: text, annotated - select for diffs
Tue Jul 30 16:12:38 2002 UTC (21 years, 9 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Changed parameter for better adjustment of the blank space.

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

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