File:
[LON-CAPA] /
loncom /
homework /
randomlabel.pm
Revision
1.39:
download - view:
text,
annotated -
select for diffs
Thu Nov 7 19:48:51 2002 UTC (21 years, 10 months ago) by
albertel
Branches:
MAIN
CVS tags:
HEAD
- <randomlabel> online now server generated on the file rather than use Java
(Maybe make it a runtime switchable item?)
- doesn't yet support subimage <randomlabel>
- (need to support fontsize selection)
1: # The LearningOnline Network with CAPA
2: # random labelling tool
3: #
4: # $Id: randomlabel.pm,v 1.39 2002/11/07 19:48:51 albertel Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: # 7/20/2001 Isaac Tsai, initial syntax
29: # 8/10/2001 Isaac Tsai,
30: # 8/30/2001 Isaac Tsai,
31: # SYNTAX:
32: # <randomlabel bgimg="URL" width="12" height="45" texwidth="50">
33: # <labelgroup name="GroupOne" type="image">
34: # <location x="123" y="456" />
35: # <location x="321" y="654" />
36: # <location x="213" y="546" />
37: # <label description="TEXT-1">IMG-URL</label>
38: # <label description="TEXT-2">IMG-URL</label>
39: # <label description="TEXT-3">IMG-URL</label>
40: # </labelgroup>
41: # <labelgroup name="GroupTwo" type="text">
42: # <location x="12" y="45" />
43: # <location x="32" y="65" />
44: # <location x="21" y="54" />
45: # <label>TEXT-1</label>
46: # <label>TEXT-2</label>
47: # <label>TEXT-3</label>
48: # </labelgroup>
49: # </randomlabel>
50: # ===========================================
51: # side effect:
52: # location (123,456): $GroupOne[0] = 2 # images give out indexes
53: # (321,654): $GroupOne[1] = 1
54: # (213,546): $GroupOne[2] = 0
55: # location (12,45) : $GroupTwo[0] = "TEXT-3"
56: # (32,65) : $GroupTwo[1] = "TEXT-1"
57: # (21,54) : $GroupTwo[2] = "TEXT-2"
58: # ===========================================
59: package Apache::randomlabel;
60: use Apache::lonnet;
61: use strict;
62: use Apache::edit;
63: use Apache::File();
64: use Apache::Constants qw(:common :http);
65:
66: BEGIN {
67: &Apache::lonxml::register('Apache::randomlabel',('randomlabel','labelgroup','location','label'));
68: }
69:
70: sub check_int {
71: # utility function to do error checking on a integer.
72: my ($num,$default) = @_;
73: $default = 100 if (! defined($default));
74: $num =~ s/\s+//g; # We dont need no stinkin white space!
75: # If it is a real, just grab the integer part.
76: ($num,undef) = split (/\./,$num) if ($num =~ /\./);
77: # set to default if what we have left doesn't match anything...
78: $num = $default unless ($num =~/^\d+$/);
79: return $num;
80: }
81:
82: sub start_randomlabel {
83: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
84: my $result='';
85: &Apache::lonxml::startredirection();
86: push (@Apache::lonxml::extlinks, '/res/adm/includes/GLabel.class');
87: push (@Apache::lonxml::namespace,'randomlabel');
88: my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
89: if ( $bgimg !~ /^http:/ ) {
90: push (@Apache::lonxml::extlinks,$bgimg);
91: $bgimg=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$bgimg);
92: if ($bgimg =~ /$Apache::lonnet::perlvar{'lonDocRoot'}/) {
93: $bgimg=~s/$Apache::lonnet::perlvar{'lonDocRoot'}//;
94: } elsif ($bgimg =~ m:^/home/.*/public_html:) {
95: $bgimg =~ s:^/home/(.*)/public_html:/~$1:;
96: }
97: # $bgimg='http://'.$ENV{'SERVER_NAME'}.$bgimg;
98: }
99: my $code = &Apache::lonxml::get_param('code',$parstack,$safeeval);
100: my $codebase = &Apache::lonxml::get_param('codebase',$parstack,$safeeval);
101: my $w= &check_int(&Apache::lonxml::get_param('width',$parstack,$safeeval));
102: my $h= &check_int(&Apache::lonxml::get_param('height',$parstack,$safeeval));
103: my $texwidth= &Apache::lonxml::get_param('texwidth',$parstack,$safeeval);
104: if (!$code) { $code='GLabel.class'; }
105: if (!$codebase) { $codebase='/res/adm/includes/'; }
106: $Apache::randomlabel::tlabel_cnt=0;
107: $Apache::randomlabel::ilabel_cnt=0;
108: if ($target eq 'web') {
109: $result.='<img src="/cgi-bin/randomlabel.png?';
110: $result.="BGIMG=".&Apache::lonnet::escape('/home/httpd/html/'.$bgimg);
111: } elsif ($target eq 'tex') {
112: my $newbgimg = $bgimg;
113: $bgimg=~s/(.gif|.jpg)$/.eps/;
114: $bgimg= &Apache::lonnet::filelocation($bgimg);
115: if (not $ENV{'request.role'}=~/^au\./) {
116: $bgimg=~s/http:\/[^\/]*/\/home\/httpd\/html/;
117: $bgimg=~s/\/$//;
118: #if no eps file try to replicate it
119: if (not-e $bgimg) {
120: if (&Apache::lonnet::repcopy($bgimg) ne OK ) {
121: #if replication failed try to find ps file
122: $bgimg=~s/\.eps$/\.ps/;
123: #if no ps file try to replicate it
124: if (not-e $bgimg &&
125: &Apache::lonnet::repcopy($bgimg) ne OK) {
126: #if replication failed try to produce eps file dynamically
127: $bgimg=~s/\.ps$/\.eps/;
128: my $temp_file;
129: my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
130: $temp_file = Apache::File->new('>>'.$filename);
131: $newbgimg =~ s/(.*)\/res\//\/home\/httpd\/html\/res\//;
132: print $temp_file "$newbgimg\n";
133: $bgimg =~ m/\/([^\/]+)$/;
134: $bgimg = '/home/httpd/prtspool/'.$1;
135: }
136: }
137: }
138: } else {
139: $bgimg=~s/http:\/[^\/]*\/~([^\/]+)/\/home\/$1\/public_html/;
140: }
141: $bgimg=~s/\/$//;
142: my $dirtywidth=$texwidth+5;
143: if ($texwidth==90) {
144: $result.='\vspace*{2mm}\noindent \parbox{'.$dirtywidth.' mm}{ \noindent \epsfxsize='.$texwidth.' mm \epsffile{'.
145: $bgimg.'}\setlength{\unitlength}{1mm} \begin{picture}('.$texwidth.','.$texwidth*$h/$w.')(0,-'.$texwidth*$h/$w.')';
146: } else {
147: $result.='\vspace*{2mm}\noindent \parbox{'.$dirtywidth.' mm}{ \noindent \epsfxsize='.$texwidth.' mm \epsffile{'.
148: $bgimg.'}\setlength{\unitlength}{1mm} \begin{picture}('.$texwidth.','.$texwidth*$h/$w.')(0,-'.$texwidth*$h/$w.')';
149: }
150: } elsif ($target eq 'edit') {
151: $result.=&Apache::edit::tag_start($target,$token);
152: $result.=&Apache::edit::text_arg('Image:','bgimg',$token,75).' ';
153: $result.=&Apache::edit::browse('bgimg').' ';
154: $result.=&Apache::edit::search('bgimg').'<br />'.
155: &Apache::edit::text_arg('Width(pixel):' ,'width' ,$token,6).
156: &Apache::edit::text_arg('Height(pixel):','height' ,$token,6).
157: &Apache::edit::text_arg('TeXWidth(mm):' ,'texwidth',$token,6).
158: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
159: } elsif ($target eq 'modified') {
160: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
161: 'bgimg','width','height',
162: 'texwidth');
163: if ($constructtag) {
164: $result = &Apache::edit::rebuild_tag($token);
165: $result.=&Apache::edit::handle_insert();
166: }
167: }
168: return $result;
169: }
170:
171: sub end_randomlabel {
172: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
173: my $result='';
174: my $count;
175: $result=&Apache::lonxml::endredirection();
176: $result=~s/\s*&/&/g;
177: $result=~s/\s*$//;
178: pop @Apache::lonxml::namespace;
179: if ($target eq 'web') {
180: $count = $Apache::randomlabel::tlabel_cnt;
181: if( $count != 0) { $result.= "&COUNT=$count"; }
182: $count = $Apache::randomlabel::ilabel_cnt;
183: if( $count != 0) { $result.= "&ICOUNT=$count"; }
184: $result.='" /><br />'."\n";
185: } elsif ($target eq 'tex') {
186: $result='\end{picture}\\\\';
187: @$parstack[-1]=~/\$height\s*=\s*?"(.+)?"/;
188: my $one=$1;
189: @$parstack[-1]=~/\$width\s*=\s*?"(.+)?"/;
190: my $two=$1;
191: @$parstack[-1]=~/\$texwidth\s*=\s*?"(.+)?"/;
192: my $three=$1;
193: my $howtoskipback = $three*$one/$two;
194: $result.=' \vskip -'.$howtoskipback.' mm }';
195: } elsif ($target eq 'edit') {
196: $result.=&Apache::edit::end_table;
197: }
198: return $result;
199: }
200:
201: sub start_labelgroup {
202: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
203: my $result='';
204: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
205: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
206: $type =~tr/A-Z/a-z/;
207: if ($target eq 'web' || $target eq 'tex' ||
208: $target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
209: $Apache::randomlabel::groupname=$name;
210: $Apache::randomlabel::type=$type;
211: @Apache::randomlabel::xcoord = ();
212: @Apache::randomlabel::ycoord = ();
213: @Apache::randomlabel::value = ();
214: @Apache::randomlabel::label_arr = ();
215: @Apache::randomlabel::decription = ();
216: } elsif ($target eq 'edit') {
217: $result.=&Apache::edit::tag_start($target,$token);
218: $result.=&Apache::edit::text_arg('Name:','name',$token).
219: &Apache::edit::select_arg('Type:','type',['text','image'],$token).
220: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
221: } elsif ($target eq 'modified') {
222: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
223: 'name','type');
224: if ($constructtag) {
225: $result = &Apache::edit::rebuild_tag($token);
226: $result.=&Apache::edit::handle_insert();
227: }
228: }
229: return $result;
230: }
231:
232: sub add_vars {
233: my ($name,$order,$label,$labelorder,$value,$image,$safeeval) = @_;
234: my $code = '${'.$name."}{'".($order+1)."'}='".$label."';";
235: my $out=Apache::run::run($code,$safeeval);
236: if ($value) {
237: $code = '${'.$name."}{'value_".($order+1)."'}='".$value."';";
238: $out=Apache::run::run($code,$safeeval);
239: $code = '${'.$name."}{'labelvalue_".($labelorder+1)."'}='".$value."';";
240: $out=Apache::run::run($code,$safeeval);
241: }
242: if ($image) {
243: my $code = '${'.$name."}{'image_".($order+1)."'}='".$image."';";
244: my $out=Apache::run::run($code,$safeeval);
245: }
246: $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
247: $out=Apache::run::run($code,$safeeval);
248: }
249:
250: # begin to assign labels to locations
251: sub end_labelgroup {
252: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
253: my $gname = $Apache::randomlabel::groupname;
254: my $type = $Apache::randomlabel::type;
255: my $result='';
256: if ($target eq 'web' || $target eq 'answer' || $target eq 'grade' ||
257: $target eq 'analyze') {
258: my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
259: &Apache::structuretags::shuffle(\@idx_arr);
260: for(0 .. $#Apache::randomlabel::label_arr) {
261: my $str;
262: my $xstr;
263: my $ystr;
264: my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$_] ]";
265: my $x = $Apache::randomlabel::xcoord[$_];
266: my $y = $Apache::randomlabel::ycoord[$_];
267: my $value = $Apache::randomlabel::value[$_];
268: if( $type eq 'text') {
269: &add_vars($gname,$_,$label,$idx_arr[$_],$value,'',$safeeval);
270: $str = 'LB'.$Apache::randomlabel::tlabel_cnt;
271: $xstr = 'X'.$Apache::randomlabel::tlabel_cnt;
272: $ystr = 'Y'.$Apache::randomlabel::tlabel_cnt;
273: $Apache::randomlabel::tlabel_cnt += 1;
274: } elsif ( $type eq 'image') {
275: &add_vars($gname,$_,
276: $Apache::randomlabel::description[$idx_arr[$_]],
277: $idx_arr[$_],$value,$label,$safeeval);
278: $str = 'IMG'.$Apache::randomlabel::ilabel_cnt;
279: $xstr = 'IX'.$Apache::randomlabel::ilabel_cnt;
280: $ystr = 'IY'.$Apache::randomlabel::ilabel_cnt;
281: $Apache::randomlabel::ilabel_cnt += 1;
282: } else {
283: &Apache::lonxml::error('Unknown type of label :'.$type.':');
284: }
285: if ($target eq 'web') {
286: $result .= '&'. $str .'='.$label;
287: $result .= '&'. $xstr .'='.$x;
288: $result .= '&'. $ystr .'='.$y;
289: }
290: }
291: } elsif ($target eq 'tex') {
292: my $WX1=0; # Web x-coord. of upper left corner (ULC)
293: my $WY1=0; # Web y-coord. of (ULC)
294: my $wwidth=&Apache::lonxml::get_param('width',$parstack,$safeeval,-2);
295: my $wheight=&Apache::lonxml::get_param('height',$parstack,$safeeval,-2);
296: my $texwidth=&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,-2);
297: my $TX1=0;
298: my $TY1=$texwidth*($wheight/$wwidth);
299: my $TX2=$texwidth;
300: my $TY2=0;
301:
302:
303: my $slopex=($wwidth-$WX1)/($TX2-$TX1);
304: my $slopey=($wheight-$WY1)/($TY2-($TY1-1.0));
305: my $cstx=$wwidth-$slopex*($TX2);
306: my $csty=$wheight-$slopey*($TY2);
307:
308: my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
309: &Apache::structuretags::shuffle(\@idx_arr);
310:
311: &Apache::lonxml::debug("Array is:".$#Apache::randomlabel::label_arr.":");
312: for(my $i=0;$i <= $#Apache::randomlabel::label_arr; $i++) {
313: my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$i] ]";
314: my $x = $Apache::randomlabel::xcoord[$i];
315: my $y = $Apache::randomlabel::ycoord[$i];
316: my $value = $Apache::randomlabel::value[$i];
317: #x latex coordinate
318: my $tcX=($x)*($texwidth/$wwidth);
319: #y latex coordinate
320: # my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
321: my $tcY=$TY1-$y*($TY1/$wheight);
322: $tcX=sprintf('%.2f',$tcX);
323: $tcY=sprintf('%.2f',$tcY);
324: $result.='\put('.$tcX.','.$tcY.'){\normalsize \bf '.$label.'}'."\n";
325: if( $type eq 'text') {
326: &add_vars($gname,$i,$label,$idx_arr[$i],$value,'',$safeeval);
327: } elsif ( $type eq 'image') {
328: &add_vars($gname,$i,
329: $Apache::randomlabel::description[$idx_arr[$i]],
330: $idx_arr[$i],$value,$label,$safeeval);
331: } else {
332: &Apache::lonxml::error('Unknown type of label :'.$type.':');
333: }
334: }
335: } elsif ($target eq 'edit') {
336: $result.=&Apache::edit::end_table;
337: }
338: return $result;
339: }
340:
341: # <location x="123" y="456" value="some value"/>
342: sub start_location {
343: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
344: my $x= &check_int(&Apache::lonxml::get_param('x',$parstack,$safeeval),50);
345: my $y= &check_int(&Apache::lonxml::get_param('y',$parstack,$safeeval),50);
346: my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
347: my $result='';
348: push(@Apache::randomlabel::xcoord,$x);
349: push(@Apache::randomlabel::ycoord,$y);
350: push(@Apache::randomlabel::value,$value);
351: if ($target eq 'edit') {
352: $result.=&Apache::edit::tag_start($target,$token);
353: $result.=&Apache::edit::text_arg('X:','x',$token,4).
354: &Apache::edit::text_arg('Y:','y',$token,4).
355: &Apache::edit::text_arg('Value:','value',$token).
356: &Apache::edit::end_row();
357: $result.=&Apache::edit::end_table;
358: } elsif ($target eq 'modified') {
359: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
360: 'x','y','value');
361: if ($constructtag) {
362: $result = &Apache::edit::rebuild_tag($token);
363: $result.=&Apache::edit::handle_insert();
364: }
365: }
366: return $result;
367: }
368:
369: sub end_location {
370: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
371: my @result;
372: if ($target eq 'edit') { @result=('','no') }
373: return @result;
374: }
375:
376: # <label>$var_abc</label>
377: sub start_label {
378: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
379: my $result='';
380: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
381: if ($target eq 'web' || $target eq 'tex' ||
382: $target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
383: my $ltext=&Apache::lonxml::get_all_text("/label",$$parser[-1]);
384: $ltext=&Apache::run::evaluate($ltext,$safeeval,$$parstack[-1]);
385: if ($type eq 'image') {
386: &Apache::lonxml::debug("Turning $ltext and $Apache::lonxml::pwd[-1]");
387: $ltext="http://$ENV{'SERVER_NAME'}".&Apache::lonnet::hreflocation($Apache::lonxml::pwd[-1],
388: $ltext);
389: &Apache::lonxml::debug("into $ltext");
390: my $description = &Apache::lonxml::get_param('description',
391: $parstack,$safeeval);
392: push(@Apache::randomlabel::description,$description);
393: }
394: push(@Apache::randomlabel::label_arr,$ltext);
395: } elsif ($target eq 'edit') {
396: $result.=&Apache::edit::tag_start($target,$token,"$type Label");
397: my $text=&Apache::lonxml::get_all_text("/label",$$parser[-1]);
398: if ($type eq 'image') {
399: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
400: $result.=&Apache::edit::text_arg('Description:','description',$token);
401: }
402: if ($type eq 'text') { $result.="Label Text:"; }
403: if ($type eq 'image') { $result.="Path to image:"; }
404: $result.=&Apache::edit::editline('',$text,'',20).
405: &Apache::edit::end_table();
406: } elsif ($target eq 'modified') {
407: my $text=$$parser[-1]->get_text("/label");
408: $result = '<label>';
409: if ($type eq 'image') {
410: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
411: 'description');
412: if ($constructtag) {
413: $result = &Apache::edit::rebuild_tag($token);
414: } else {
415: $result = $token->[4];
416: }
417: }
418: $result.=&Apache::edit::modifiedfield($token);
419: }
420: return $result;
421: }
422:
423: sub end_label {
424: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
425: my @result;
426: if ($target eq 'edit') { @result=('','no') }
427: return @result;
428: }
429:
430:
431:
432: 1;
433: __END__
434:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>