Annotation of loncom/homework/randomlabel.pm, revision 1.7
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='';
1.6 albertel 44: $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=
45: '/res/adm/includes/GLabel.class';
46:
1.1 tsai 47: my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
1.6 albertel 48:
49: if ( $bgimg !~ /^http:/ ) {
50: $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=$bgimg;
51: $bgimg=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$bgimg);
1.7 ! albertel 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: }
1.6 albertel 57: $bgimg="http://".$ENV{'SERVER_NAME'}.$bgimg;
58: }
1.1 tsai 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);
1.4 albertel 63: my $texwidth= &Apache::lonxml::get_param('texwidth',$parstack,$safeeval);
64: if (!$code) { $code='GLabel.class'; }
65: if (!$codebase) { $codebase='/res/adm/includes/'; }
1.3 tsai 66: $Apache::randomlabel::tlabel_cnt=0;
67: $Apache::randomlabel::ilabel_cnt=0;
68: if ($target eq 'web') {
1.4 albertel 69: $result.="<applet code=\"$code\" codebase=\"$codebase\" width=\"$w\" height=\"$h\">";
1.3 tsai 70: $result.="<param name=\"bgimg\" value=\"$bgimg\">";
1.4 albertel 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)';
1.3 tsai 75: } elsif ($target eq 'edit') {
1.4 albertel 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">';
1.3 tsai 82: } elsif ($target eq 'modified') {
1.4 albertel 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: }
1.1 tsai 90: }
91: return $result;
92: }
93:
94: sub end_randomlabel {
1.3 tsai 95: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
96: my $result='';
97: my $count;
98:
1.4 albertel 99:
1.3 tsai 100: if ($target eq 'web') {
1.4 albertel 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\">"; }
1.3 tsai 105: $result .= "</applet><BR />";
1.4 albertel 106: } elsif ($target eq 'tex') {
107: $result='\end{picture}';
108: } elsif ($target eq 'edit') {
109: $result.=&Apache::edit::end_table;
1.3 tsai 110: }
111: return $result;
1.1 tsai 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);
1.2 tsai 119: $type =~tr/A-Z/a-z/;
1.5 albertel 120: if ($target eq 'web' || $target eq 'tex' ||
121: $target eq 'grade' || $target eq 'answer') {
1.4 albertel 122: $Apache::randomlabel::groupname=$name;
123: $Apache::randomlabel::type=$type;
124: @Apache::randomlabel::xcoord = ();
125: @Apache::randomlabel::ycoord = ();
1.5 albertel 126: @Apache::randomlabel::value = ();
1.4 albertel 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: }
1.3 tsai 141: return $result;
1.1 tsai 142: }
143:
1.5 albertel 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);
1.4 albertel 153: }
1.5 albertel 154: $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
155: $out=Apache::run::run($code,$safeeval);
1.4 albertel 156: }
1.5 albertel 157:
1.1 tsai 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;
1.2 tsai 162: my $type = $Apache::randomlabel::type;
1.4 albertel 163: my $result='';
1.5 albertel 164: if ($target eq 'web' || $target eq 'answer' || $target eq 'grade') {
1.4 albertel 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[$_] ]";
1.5 albertel 172: my $x = $Apache::randomlabel::xcoord[$_];
173: my $y = $Apache::randomlabel::ycoord[$_];
174: my $value = $Apache::randomlabel::value[$_];
1.4 albertel 175: if( $type eq 'text') {
1.5 albertel 176: &add_vars($gname,$_,$label,$idx_arr[$_],$value,$safeeval);
1.4 albertel 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') {
1.5 albertel 182: &add_vars($gname,$_,$idx_arr[$_],$idx_arr[$_],$value,$safeeval);
1.4 albertel 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: }
1.5 albertel 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: }
1.3 tsai 195: }
1.4 albertel 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];
1.5 albertel 221: my $value = $Apache::randomlabel::value[$i];
1.4 albertel 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') {
1.5 albertel 228: &add_vars($gname,$i,$label,$idx_arr[$i],$value,$safeeval);
1.4 albertel 229: } elsif ( $type eq 'image') {
1.5 albertel 230: &add_vars($gname,$i,$idx_arr[$i],$idx_arr[$i],$value,$safeeval);
1.4 albertel 231: } else {
232: &Apache::lonxml::error('Unknown type of label :'.$type.':');
233: }
234: }
235: } elsif ($target eq 'edit') {
236: $result.=&Apache::edit::end_table;
1.1 tsai 237: }
1.4 albertel 238: return $result;
1.1 tsai 239: }
240:
1.5 albertel 241: # <location x="123" y="456" value="some value"/>
1.1 tsai 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);
1.5 albertel 246: my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.3 tsai 247: my $result='';
1.1 tsai 248: push(@Apache::randomlabel::xcoord,$x);
1.4 albertel 249: push(@Apache::randomlabel::ycoord,$y);
1.5 albertel 250: push(@Apache::randomlabel::value,$value);
1.4 albertel 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).
1.5 albertel 255: &Apache::edit::text_arg('Value:','value',$token).
1.4 albertel 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,
1.5 albertel 260: 'x','y','value');
1.4 albertel 261: if ($constructtag) {
262: $result = &Apache::edit::rebuild_tag($token);
263: $result.=&Apache::edit::handle_insert();
264: }
265: }
1.3 tsai 266: return $result;
1.1 tsai 267: }
268:
269: sub end_location {
1.3 tsai 270: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.4 albertel 271: my @result;
272: if ($target eq 'edit') { @result=('','no') }
273: return @result;
1.1 tsai 274: }
275:
1.2 tsai 276: # <label>$var_abc</label>
1.1 tsai 277: sub start_label {
278: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.3 tsai 279: my $result='';
1.5 albertel 280: if ($target eq 'web' || $target eq 'tex' ||
281: $target eq 'grade' || $target eq 'answer') {
1.4 albertel 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: }
1.3 tsai 294: return $result;
1.1 tsai 295: }
296:
297: sub end_label {
1.3 tsai 298: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.4 albertel 299: my @result;
300: if ($target eq 'edit') { @result=('','no') }
301: return @result;
1.1 tsai 302: }
303:
304:
305:
306: 1;
307: __END__
308:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>