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

1.1       tsai        1: # The LearningOnline Network with CAPA
                      2: # random labelling tool
1.8       albertel    3: #
1.23    ! matthew     4: # $Id: randomlabel.pm,v 1.22 2002/03/22 22:06:06 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: 
1.23    ! matthew    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: 
1.1       tsai       79: sub start_randomlabel {
                     80:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     81:   my $result='';
1.23    ! matthew    82:   push (@Apache::lonxml::extlinks, '/res/adm/includes/GLabel.class');
1.20      albertel   83:   push (@Apache::lonxml::namespace,'randomlabel');
1.1       tsai       84:   my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
1.6       albertel   85:   if ( $bgimg !~ /^http:/ ) {
1.23    ! matthew    86:     push (@Apache::lonxml::extlinks,$bgimg);
1.6       albertel   87:     $bgimg=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$bgimg);
1.7       albertel   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:     }
1.6       albertel   93:     $bgimg="http://".$ENV{'SERVER_NAME'}.$bgimg;
                     94:   }
1.1       tsai       95:   my $code = &Apache::lonxml::get_param('code',$parstack,$safeeval);
                     96:   my $codebase = &Apache::lonxml::get_param('codebase',$parstack,$safeeval);
1.23    ! matthew    97:   my $w= &check_int(&Apache::lonxml::get_param('width',$parstack,$safeeval));
        !            98:   my $h= &check_int(&Apache::lonxml::get_param('height',$parstack,$safeeval));
1.4       albertel   99:   my $texwidth= &Apache::lonxml::get_param('texwidth',$parstack,$safeeval);
                    100:   if (!$code) { $code='GLabel.class'; }
                    101:   if (!$codebase) { $codebase='/res/adm/includes/'; }
1.3       tsai      102:   $Apache::randomlabel::tlabel_cnt=0;
                    103:   $Apache::randomlabel::ilabel_cnt=0;
                    104:   if ($target eq 'web') {
1.4       albertel  105:     $result.="<applet code=\"$code\" codebase=\"$codebase\" width=\"$w\" height=\"$h\">";
1.3       tsai      106:     $result.="<param name=\"bgimg\" value=\"$bgimg\">";
1.4       albertel  107:   } elsif ($target eq 'tex') {
1.12      sakharuk  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.')'
1.3       tsai      112:   } elsif ($target eq 'edit') {
1.4       albertel  113:     $result.=&Apache::edit::tag_start($target,$token);
1.18      matthew   114:     $result.=&Apache::edit::text_arg('Image:','bgimg',$token,75).' ';
                    115:     $result.=&Apache::edit::browse('bgimg').' ';
                    116:     $result.=&Apache::edit::search('bgimg').'<br />'.
1.15      matthew   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();     
1.3       tsai      121:   } elsif ($target eq 'modified') {
1.4       albertel  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:     }
1.1       tsai      129:   }
                    130:   return $result;
                    131: }
                    132: 
                    133: sub end_randomlabel {
1.3       tsai      134:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    135:   my $result='';
                    136:   my $count;
1.21      albertel  137:   pop @Apache::lonxml::namespace;
1.3       tsai      138:   if ($target eq 'web') {
1.4       albertel  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\">"; }
1.3       tsai      143:     $result .= "</applet><BR />";
1.4       albertel  144:   } elsif ($target eq 'tex') {
                    145:     $result='\end{picture}';
                    146:   } elsif ($target eq 'edit') {
                    147:     $result.=&Apache::edit::end_table;
1.3       tsai      148:   }
                    149:   return $result;
1.1       tsai      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);
1.2       tsai      157:   $type =~tr/A-Z/a-z/;
1.5       albertel  158:   if ($target eq 'web' || $target eq 'tex' || 
                    159:       $target eq 'grade' || $target eq 'answer') {
1.4       albertel  160:     $Apache::randomlabel::groupname=$name;
                    161:     $Apache::randomlabel::type=$type;
                    162:     @Apache::randomlabel::xcoord = ();
                    163:     @Apache::randomlabel::ycoord = ();
1.5       albertel  164:     @Apache::randomlabel::value = ();
1.4       albertel  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).
1.13      sakharuk  170: 	  &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.4       albertel  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:   }
1.3       tsai      179:   return $result;
1.1       tsai      180: }
                    181: 
1.5       albertel  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);
1.4       albertel  191:   }
1.5       albertel  192:   $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
                    193:   $out=Apache::run::run($code,$safeeval);
1.4       albertel  194: }
1.5       albertel  195: 
1.1       tsai      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;
1.2       tsai      200:   my $type  = $Apache::randomlabel::type;
1.4       albertel  201:   my $result='';
1.5       albertel  202:   if ($target eq 'web' || $target eq 'answer' || $target eq 'grade') {
1.4       albertel  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[$_] ]";
1.5       albertel  210:       my $x = $Apache::randomlabel::xcoord[$_];
                    211:       my $y = $Apache::randomlabel::ycoord[$_];
                    212:       my $value = $Apache::randomlabel::value[$_];
1.4       albertel  213:       if( $type eq 'text') {
1.5       albertel  214: 	&add_vars($gname,$_,$label,$idx_arr[$_],$value,$safeeval);
1.4       albertel  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') {
1.5       albertel  220: 	&add_vars($gname,$_,$idx_arr[$_],$idx_arr[$_],$value,$safeeval);
1.4       albertel  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:       }
1.5       albertel  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:       }
1.3       tsai      233:     }
1.4       albertel  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];
1.5       albertel  259:       my $value = $Apache::randomlabel::value[$i];
1.16      sakharuk  260: #      my $tcX=$x*($texwidth/$wwidth) - $texwidth;
                    261:       my $tcX=$x*($texwidth/$wwidth);
                    262: #      my $tcY=$TY1-$y*($TY1/$wheight)-2;
1.19      albertel  263:       my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
                    264:       my $tcY=$TY1-$y*($TY1/$wheight)+$texwidth*$ratio;
1.4       albertel  265:       $tcX=sprintf('%.2f',$tcX);
                    266:       $tcY=sprintf('%.2f',$tcY);
                    267:       $result.='\put('.$tcX.','.$tcY.'){\normalsize \bf '.$label.'}'."\n";
                    268:       if( $type eq 'text') {
1.5       albertel  269: 	&add_vars($gname,$i,$label,$idx_arr[$i],$value,$safeeval);
1.4       albertel  270:       } elsif ( $type eq 'image') {
1.5       albertel  271: 	&add_vars($gname,$i,$idx_arr[$i],$idx_arr[$i],$value,$safeeval);
1.4       albertel  272:       } else {
                    273: 	&Apache::lonxml::error('Unknown type of label :'.$type.':');
                    274:       }
                    275:     }
                    276:   } elsif ($target eq 'edit') {
                    277:     $result.=&Apache::edit::end_table;
1.1       tsai      278:   }
1.4       albertel  279:   return $result;
1.1       tsai      280: }
                    281: 
1.5       albertel  282: # <location x="123" y="456" value="some value"/>
1.1       tsai      283: sub start_location {
                    284:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.23    ! matthew   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);
1.5       albertel  287:   my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.3       tsai      288:   my $result='';
1.1       tsai      289:   push(@Apache::randomlabel::xcoord,$x);
1.4       albertel  290:   push(@Apache::randomlabel::ycoord,$y);
1.5       albertel  291:   push(@Apache::randomlabel::value,$value);
1.4       albertel  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).
1.5       albertel  296: 	&Apache::edit::text_arg('Value:','value',$token).
1.13      sakharuk  297: 	  &Apache::edit::end_row();
1.4       albertel  298:     $result.=&Apache::edit::end_table;
                    299:   } elsif ($target eq 'modified') {
                    300:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
1.5       albertel  301: 						 'x','y','value');
1.4       albertel  302:     if ($constructtag) {
                    303:       $result = &Apache::edit::rebuild_tag($token);
                    304:       $result.=&Apache::edit::handle_insert();
                    305:     }
                    306:   }
1.3       tsai      307:   return $result;
1.1       tsai      308: }
                    309: 
                    310: sub end_location {
1.3       tsai      311:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.4       albertel  312:   my @result;
                    313:   if ($target eq 'edit') { @result=('','no') }
                    314:   return @result;
1.1       tsai      315: }
                    316: 
1.2       tsai      317: # <label>$var_abc</label>
1.1       tsai      318: sub start_label {
                    319:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.3       tsai      320:   my $result='';
1.5       albertel  321:   if ($target eq 'web' || $target eq 'tex' || 
                    322:       $target eq 'grade' || $target eq 'answer') {
1.4       albertel  323:     my $ltext=&Apache::lonxml::get_all_text("/label",$$parser[-1]);
1.14      albertel  324:     $ltext=&Apache::run::evaluate($ltext,$safeeval,$$parstack[-1]);
1.4       albertel  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]);
1.13      sakharuk  329:     $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row().
1.22      albertel  330:       &Apache::edit::editline('',$text,'',20).
1.4       albertel  331: 	&Apache::edit::end_table();
                    332:   } elsif ($target eq 'modified') {
                    333:     my $text=$$parser[-1]->get_text("/label");
1.13      sakharuk  334:     $result.='<label>'.&Apache::edit::modifiedfield($token);
1.4       albertel  335:   }
1.3       tsai      336:   return $result;
1.1       tsai      337: }
                    338: 
                    339: sub end_label {
1.3       tsai      340:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.4       albertel  341:   my @result;
                    342:   if ($target eq 'edit') { @result=('','no') }
                    343:   return @result;
1.1       tsai      344: }
                    345: 
                    346: 
                    347: 
                    348: 1;
                    349: __END__
                    350:  

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