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

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

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