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

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.4     ! albertel  106:   if ($target eq 'web' || $target eq 'tex') {
        !           107:     $Apache::randomlabel::groupname=$name;
        !           108:     $Apache::randomlabel::type=$type;
        !           109:     @Apache::randomlabel::xcoord = ();
        !           110:     @Apache::randomlabel::ycoord = ();
        !           111:     @Apache::randomlabel::label_arr  = ();
        !           112:   } elsif ($target eq 'edit') {
        !           113:     $result.=&Apache::edit::tag_start($target,$token);
        !           114:     $result.=&Apache::edit::text_arg('Name:','name',$token).
        !           115:       &Apache::edit::select_arg('Type:','type',['text','image'],$token).
        !           116: 	'</td></tr><tr><td colspan="3">';
        !           117:   } elsif ($target eq 'modified') {
        !           118:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
        !           119: 						 'name','type');
        !           120:     if ($constructtag) {
        !           121:       $result = &Apache::edit::rebuild_tag($token);
        !           122:       $result.=&Apache::edit::handle_insert();
        !           123:     }
        !           124:   }
1.3       tsai      125:   return $result;
1.1       tsai      126: }
                    127: 
1.4     ! albertel  128: sub add_var {
        !           129:   my ($name,$value,$type,$safeeval) = @_;
        !           130:   if ($type eq 'array') {
        !           131:     my $code = 'push(@'.$name.',\''.$value.'\');';
        !           132:     my $out=Apache::run::run($code,$safeeval);
        !           133:   } else {
        !           134:     &Apache::lonxml::error('Unsupported add_var type :'.$type.':');
        !           135:   }
        !           136: }
1.1       tsai      137: # begin to assign labels to locations
                    138: sub end_labelgroup {
                    139:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    140:   my $gname = $Apache::randomlabel::groupname;
1.2       tsai      141:   my $type  = $Apache::randomlabel::type;
1.4     ! albertel  142:   my $result='';
        !           143:   if ($target eq 'web') {
        !           144:     my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
        !           145:     &Apache::structuretags::shuffle(\@idx_arr);
        !           146:     for(0 .. $#Apache::randomlabel::label_arr) {
        !           147:       my $str;
        !           148:       my $xstr;
        !           149:       my $ystr;
        !           150:       my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$_] ]";
        !           151:       if( $type eq 'text') {
        !           152: 	&add_var($gname,$label,'array',$safeeval);
        !           153: 	$str = 'LB'.$Apache::randomlabel::tlabel_cnt;
        !           154: 	$xstr = 'X'.$Apache::randomlabel::tlabel_cnt;
        !           155: 	$ystr = 'Y'.$Apache::randomlabel::tlabel_cnt;
        !           156: 	$Apache::randomlabel::tlabel_cnt += 1;
        !           157:       } elsif ( $type eq 'image') {
        !           158: 	&add_var($gname,$idx_arr[$_],'array',$safeeval);
        !           159: 	$str = 'LB'.$Apache::randomlabel::ilabel_cnt;
        !           160: 	$xstr = 'X'.$Apache::randomlabel::ilabel_cnt;
        !           161: 	$ystr = 'Y'.$Apache::randomlabel::ilabel_cnt;
        !           162: 	$Apache::randomlabel::ilabel_cnt += 1;
        !           163:       } else {
        !           164: 	&Apache::lonxml::error('Unknown type of label :'.$type.':');
        !           165:       }
        !           166:       my $x = $Apache::randomlabel::xcoord[$_];
        !           167:       my $y = $Apache::randomlabel::ycoord[$_];
        !           168:       $result .= '<param name="' . $str  . '" value="'.$label.'">';
        !           169:       $result .= '<param name="' . $xstr . '" value="'.$x.'">';
        !           170:       $result .= '<param name="' . $ystr . '" value="'.$y.'">';
1.3       tsai      171:     }
1.4     ! albertel  172:   } elsif ($target eq 'tex') {
        !           173:     my $WX1=0; #  Web x-coord. of upper left corner (ULC)
        !           174:     my $WY1=0; #  Web y-coord. of (ULC)
        !           175:     my $wwidth=&Apache::lonxml::get_param('width',$parstack,$safeeval,-2);
        !           176:     my $wheight=&Apache::lonxml::get_param('height',$parstack,$safeeval,-2);
        !           177:     my $texwidth=&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,-2);
        !           178:     my $TX1=0;
        !           179:     my $TY1=$texwidth*($wheight/$wwidth);
        !           180:     my $TX2=$texwidth;
        !           181:     my $TY2=0;
        !           182: 
        !           183: 
        !           184:     my $slopex=($wwidth-$WX1)/($TX2-$TX1);
        !           185:     my $slopey=($wheight-$WY1)/($TY2-($TY1-1.0));
        !           186:     my $cstx=$wwidth-$slopex*($TX2);
        !           187:     my $csty=$wheight-$slopey*($TY2);
        !           188: 
        !           189:     my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
        !           190:     &Apache::structuretags::shuffle(\@idx_arr);
        !           191: 
        !           192:     &Apache::lonxml::debug("Array is:".$#Apache::randomlabel::label_arr.":");
        !           193:     for(my $i=0;$i <= $#Apache::randomlabel::label_arr; $i++) {
        !           194:       my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$i] ]";
        !           195:       my $x = $Apache::randomlabel::xcoord[$i];
        !           196:       my $y = $Apache::randomlabel::ycoord[$i];
        !           197:       my $tcX=$x*($texwidth/$wwidth);
        !           198:       my $tcY=$TY1-$y*($TY1/$wheight)-2;
        !           199:       $tcX=sprintf('%.2f',$tcX);
        !           200:       $tcY=sprintf('%.2f',$tcY);
        !           201:       $result.='\put('.$tcX.','.$tcY.'){\normalsize \bf '.$label.'}'."\n";
        !           202:       if( $type eq 'text') {
        !           203: 	&add_var($gname,$label,'array',$safeeval);
        !           204:       } elsif ( $type eq 'image') {
        !           205: 	&add_var($gname,$idx_arr[$i],'array',$safeeval);
        !           206:       } else {
        !           207: 	&Apache::lonxml::error('Unknown type of label :'.$type.':');
        !           208:       }
        !           209:     }
        !           210:   } elsif ($target eq 'edit') {
        !           211:     $result.=&Apache::edit::end_table;
1.1       tsai      212:   }
1.4     ! albertel  213:   return $result;
1.1       tsai      214: }
                    215: 
1.2       tsai      216: # <location x=123 y=456 />
1.1       tsai      217: sub start_location {
                    218:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    219:   my $x= &Apache::lonxml::get_param('x',$parstack,$safeeval);
                    220:   my $y= &Apache::lonxml::get_param('y',$parstack,$safeeval);
1.3       tsai      221:   my $result='';
1.1       tsai      222:   push(@Apache::randomlabel::xcoord,$x);
1.4     ! albertel  223:   push(@Apache::randomlabel::ycoord,$y);
        !           224:   if ($target eq 'edit') {
        !           225:     $result.=&Apache::edit::tag_start($target,$token);
        !           226:     $result.=&Apache::edit::text_arg('X:','x',$token,4).
        !           227:       &Apache::edit::text_arg('Y:','y',$token,4).
        !           228: 	'</td></tr><tr><td colspan="3">';
        !           229:     $result.=&Apache::edit::end_table;
        !           230:   } elsif ($target eq 'modified') {
        !           231:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
        !           232: 						 'x','y');
        !           233:     if ($constructtag) {
        !           234:       $result = &Apache::edit::rebuild_tag($token);
        !           235:       $result.=&Apache::edit::handle_insert();
        !           236:     }
        !           237:   }
1.3       tsai      238:   return $result;
1.1       tsai      239: }
                    240: 
                    241: sub end_location {
1.3       tsai      242:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.4     ! albertel  243:   my @result;
        !           244:   if ($target eq 'edit') { @result=('','no') }
        !           245:   return @result;
1.1       tsai      246: }
                    247: 
1.2       tsai      248: # <label>$var_abc</label>
1.1       tsai      249: sub start_label {
                    250:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.3       tsai      251:   my $result='';
1.4     ! albertel  252:   if ($target eq 'web' || $target eq 'tex') {
        !           253:     my $ltext=&Apache::lonxml::get_all_text("/label",$$parser[-1]);
        !           254:     push(@Apache::randomlabel::label_arr,$ltext);
        !           255:   } elsif ($target eq 'edit') {
        !           256:     $result.=&Apache::edit::tag_start($target,$token);
        !           257:     my $text=&Apache::lonxml::get_all_text("/label",$$parser[-1]);
        !           258:     $result.='</td></tr><tr><td colspan="3">'.
        !           259:       &Apache::edit::editfield('',$text,'',20,1).
        !           260: 	&Apache::edit::end_table();
        !           261:   } elsif ($target eq 'modified') {
        !           262:     my $text=$$parser[-1]->get_text("/label");
        !           263:     $result.=&Apache::edit::modifiedfield($token);
        !           264:   }
1.3       tsai      265:   return $result;
1.1       tsai      266: }
                    267: 
                    268: sub end_label {
1.3       tsai      269:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.4     ! albertel  270:   my @result;
        !           271:   if ($target eq 'edit') { @result=('','no') }
        !           272:   return @result;
1.1       tsai      273: }
                    274: 
                    275: 
                    276: 
                    277: 1;
                    278: __END__
                    279:  

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