File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.7: download - view: text, annotated - select for diffs
Fri Oct 26 18:47:43 2001 UTC (22 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: stable_2001_fall, HEAD
- makes proper references in contruction space to images

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

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