File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.40: download - view: text, annotated - select for diffs
Fri Nov 8 14:41:53 2002 UTC (21 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- converted to use new handler based mechanism

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

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