File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.18: download - view: text, annotated - select for diffs
Fri Mar 8 18:31:27 2002 UTC (22 years, 2 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Added call to &Apache::edit::search().

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

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