File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.9: download - view: text, annotated - select for diffs
Fri Jan 11 16:32:29 2002 UTC (22 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Update to use edit::end_row() and edit::start_spanning_row() as a cleanup
for edit::select_arg and edit::select_or_text_arg.

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

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