File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.26: download - view: text, annotated - select for diffs
Fri May 17 21:16:55 2002 UTC (21 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
*** empty log message ***

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

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