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

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

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