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

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

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