File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.5: download - view: text, annotated - select for diffs
Sun Oct 14 05:08:09 2001 UTC (22 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- added the ability to associate values with locations on a randomly labeled
  image

    1: # The LearningOnline Network with CAPA
    2: # random labelling tool
    3: # 7/20/2001 Isaac Tsai, initial syntax
    4: # 8/10/2001 Isaac Tsai, 
    5: # 8/30/2001 Isaac Tsai, 
    6: # SYNTAX:
    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>
   15: #    </labelgroup>
   16: #    <labelgroup name="GroupTwo" type="text">
   17: #      <location x="12" y="45" />
   18: #      <location x="32" y="65" />
   19: #      <location x="21" y="54" />
   20: #      <label>TEXT-1</label>
   21: #      <label>TEXT-2</label>
   22: #      <label>TEXT-3</label>
   23: #    </labelgroup>
   24: #   </randomlabel>
   25: #  ===========================================
   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"
   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);
   49:   my $texwidth= &Apache::lonxml::get_param('texwidth',$parstack,$safeeval);
   50:   if (!$code) { $code='GLabel.class'; }
   51:   if (!$codebase) { $codebase='/res/adm/includes/'; }
   52:   $Apache::randomlabel::tlabel_cnt=0;
   53:   $Apache::randomlabel::ilabel_cnt=0;
   54:   if ($target eq 'web') {
   55:     $result.="<applet code=\"$code\" codebase=\"$codebase\" width=\"$w\" height=\"$h\">";
   56:     $result.="<param name=\"bgimg\" value=\"$bgimg\">";
   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)';
   61:   } elsif ($target eq 'edit') {
   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">';
   68:   } elsif ($target eq 'modified') {
   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:     }
   76:   }
   77:   return $result;
   78: }
   79: 
   80: sub end_randomlabel {
   81:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   82:   my $result='';
   83:   my $count;
   84: 
   85: 
   86:   if ($target eq 'web') {
   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\">"; }
   91:     $result .= "</applet><BR />";
   92:   } elsif ($target eq 'tex') {
   93:     $result='\end{picture}';
   94:   } elsif ($target eq 'edit') {
   95:     $result.=&Apache::edit::end_table;
   96:   }
   97:   return $result;
   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);
  105:   $type =~tr/A-Z/a-z/;
  106:   if ($target eq 'web' || $target eq 'tex' || 
  107:       $target eq 'grade' || $target eq 'answer') {
  108:     $Apache::randomlabel::groupname=$name;
  109:     $Apache::randomlabel::type=$type;
  110:     @Apache::randomlabel::xcoord = ();
  111:     @Apache::randomlabel::ycoord = ();
  112:     @Apache::randomlabel::value = ();
  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:   }
  127:   return $result;
  128: }
  129: 
  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);
  139:   }
  140:   $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
  141:   $out=Apache::run::run($code,$safeeval);
  142: }
  143: 
  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;
  148:   my $type  = $Apache::randomlabel::type;
  149:   my $result='';
  150:   if ($target eq 'web' || $target eq 'answer' || $target eq 'grade') {
  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[$_] ]";
  158:       my $x = $Apache::randomlabel::xcoord[$_];
  159:       my $y = $Apache::randomlabel::ycoord[$_];
  160:       my $value = $Apache::randomlabel::value[$_];
  161:       if( $type eq 'text') {
  162: 	&add_vars($gname,$_,$label,$idx_arr[$_],$value,$safeeval);
  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') {
  168: 	&add_vars($gname,$_,$idx_arr[$_],$idx_arr[$_],$value,$safeeval);
  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:       }
  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:       }
  181:     }
  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];
  207:       my $value = $Apache::randomlabel::value[$i];
  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') {
  214: 	&add_vars($gname,$i,$label,$idx_arr[$i],$value,$safeeval);
  215:       } elsif ( $type eq 'image') {
  216: 	&add_vars($gname,$i,$idx_arr[$i],$idx_arr[$i],$value,$safeeval);
  217:       } else {
  218: 	&Apache::lonxml::error('Unknown type of label :'.$type.':');
  219:       }
  220:     }
  221:   } elsif ($target eq 'edit') {
  222:     $result.=&Apache::edit::end_table;
  223:   }
  224:   return $result;
  225: }
  226: 
  227: # <location x="123" y="456" value="some value"/>
  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);
  232:   my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
  233:   my $result='';
  234:   push(@Apache::randomlabel::xcoord,$x);
  235:   push(@Apache::randomlabel::ycoord,$y);
  236:   push(@Apache::randomlabel::value,$value);
  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).
  241: 	&Apache::edit::text_arg('Value:','value',$token).
  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,
  246: 						 'x','y','value');
  247:     if ($constructtag) {
  248:       $result = &Apache::edit::rebuild_tag($token);
  249:       $result.=&Apache::edit::handle_insert();
  250:     }
  251:   }
  252:   return $result;
  253: }
  254: 
  255: sub end_location {
  256:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  257:   my @result;
  258:   if ($target eq 'edit') { @result=('','no') }
  259:   return @result;
  260: }
  261: 
  262: # <label>$var_abc</label>
  263: sub start_label {
  264:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  265:   my $result='';
  266:   if ($target eq 'web' || $target eq 'tex' || 
  267:       $target eq 'grade' || $target eq 'answer') {
  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:   }
  280:   return $result;
  281: }
  282: 
  283: sub end_label {
  284:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  285:   my @result;
  286:   if ($target eq 'edit') { @result=('','no') }
  287:   return @result;
  288: }
  289: 
  290: 
  291: 
  292: 1;
  293: __END__
  294:  

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