File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.6: download - view: text, annotated - select for diffs
Wed Oct 24 19:40:50 2001 UTC (22 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- handels bgimg references properly
- generates proper extlink references for the image and the java applet

    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:   $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=
   45:     '/res/adm/includes/GLabel.class';
   46: 
   47:   my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
   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:   }
   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);
   59:   my $texwidth= &Apache::lonxml::get_param('texwidth',$parstack,$safeeval);
   60:   if (!$code) { $code='GLabel.class'; }
   61:   if (!$codebase) { $codebase='/res/adm/includes/'; }
   62:   $Apache::randomlabel::tlabel_cnt=0;
   63:   $Apache::randomlabel::ilabel_cnt=0;
   64:   if ($target eq 'web') {
   65:     $result.="<applet code=\"$code\" codebase=\"$codebase\" width=\"$w\" height=\"$h\">";
   66:     $result.="<param name=\"bgimg\" value=\"$bgimg\">";
   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)';
   71:   } elsif ($target eq 'edit') {
   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">';
   78:   } elsif ($target eq 'modified') {
   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:     }
   86:   }
   87:   return $result;
   88: }
   89: 
   90: sub end_randomlabel {
   91:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   92:   my $result='';
   93:   my $count;
   94: 
   95: 
   96:   if ($target eq 'web') {
   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\">"; }
  101:     $result .= "</applet><BR />";
  102:   } elsif ($target eq 'tex') {
  103:     $result='\end{picture}';
  104:   } elsif ($target eq 'edit') {
  105:     $result.=&Apache::edit::end_table;
  106:   }
  107:   return $result;
  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);
  115:   $type =~tr/A-Z/a-z/;
  116:   if ($target eq 'web' || $target eq 'tex' || 
  117:       $target eq 'grade' || $target eq 'answer') {
  118:     $Apache::randomlabel::groupname=$name;
  119:     $Apache::randomlabel::type=$type;
  120:     @Apache::randomlabel::xcoord = ();
  121:     @Apache::randomlabel::ycoord = ();
  122:     @Apache::randomlabel::value = ();
  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:   }
  137:   return $result;
  138: }
  139: 
  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);
  149:   }
  150:   $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
  151:   $out=Apache::run::run($code,$safeeval);
  152: }
  153: 
  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;
  158:   my $type  = $Apache::randomlabel::type;
  159:   my $result='';
  160:   if ($target eq 'web' || $target eq 'answer' || $target eq 'grade') {
  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[$_] ]";
  168:       my $x = $Apache::randomlabel::xcoord[$_];
  169:       my $y = $Apache::randomlabel::ycoord[$_];
  170:       my $value = $Apache::randomlabel::value[$_];
  171:       if( $type eq 'text') {
  172: 	&add_vars($gname,$_,$label,$idx_arr[$_],$value,$safeeval);
  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') {
  178: 	&add_vars($gname,$_,$idx_arr[$_],$idx_arr[$_],$value,$safeeval);
  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:       }
  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:       }
  191:     }
  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];
  217:       my $value = $Apache::randomlabel::value[$i];
  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') {
  224: 	&add_vars($gname,$i,$label,$idx_arr[$i],$value,$safeeval);
  225:       } elsif ( $type eq 'image') {
  226: 	&add_vars($gname,$i,$idx_arr[$i],$idx_arr[$i],$value,$safeeval);
  227:       } else {
  228: 	&Apache::lonxml::error('Unknown type of label :'.$type.':');
  229:       }
  230:     }
  231:   } elsif ($target eq 'edit') {
  232:     $result.=&Apache::edit::end_table;
  233:   }
  234:   return $result;
  235: }
  236: 
  237: # <location x="123" y="456" value="some value"/>
  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);
  242:   my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
  243:   my $result='';
  244:   push(@Apache::randomlabel::xcoord,$x);
  245:   push(@Apache::randomlabel::ycoord,$y);
  246:   push(@Apache::randomlabel::value,$value);
  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).
  251: 	&Apache::edit::text_arg('Value:','value',$token).
  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,
  256: 						 'x','y','value');
  257:     if ($constructtag) {
  258:       $result = &Apache::edit::rebuild_tag($token);
  259:       $result.=&Apache::edit::handle_insert();
  260:     }
  261:   }
  262:   return $result;
  263: }
  264: 
  265: sub end_location {
  266:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  267:   my @result;
  268:   if ($target eq 'edit') { @result=('','no') }
  269:   return @result;
  270: }
  271: 
  272: # <label>$var_abc</label>
  273: sub start_label {
  274:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  275:   my $result='';
  276:   if ($target eq 'web' || $target eq 'tex' || 
  277:       $target eq 'grade' || $target eq 'answer') {
  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:   }
  290:   return $result;
  291: }
  292: 
  293: sub end_label {
  294:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  295:   my @result;
  296:   if ($target eq 'edit') { @result=('','no') }
  297:   return @result;
  298: }
  299: 
  300: 
  301: 
  302: 1;
  303: __END__
  304:  

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