File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.23: download - view: text, annotated - select for diffs
Mon Apr 8 21:28:53 2002 UTC (22 years, 1 month ago) by matthew
Branches: MAIN
CVS tags: HEAD
Sanity checking on all those integers.

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

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