Annotation of loncom/homework/randomlabel.pm, revision 1.21

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

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