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: $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: }
125: return $result;
126: }
127:
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: }
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;
141: my $type = $Apache::randomlabel::type;
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.'">';
171: }
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;
212: }
213: return $result;
214: }
215:
216: # <location x=123 y=456 />
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);
221: my $result='';
222: push(@Apache::randomlabel::xcoord,$x);
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: }
238: return $result;
239: }
240:
241: sub end_location {
242: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
243: my @result;
244: if ($target eq 'edit') { @result=('','no') }
245: return @result;
246: }
247:
248: # <label>$var_abc</label>
249: sub start_label {
250: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
251: my $result='';
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: }
265: return $result;
266: }
267:
268: sub end_label {
269: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
270: my @result;
271: if ($target eq 'edit') { @result=('','no') }
272: return @result;
273: }
274:
275:
276:
277: 1;
278: __END__
279:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>