Annotation of loncom/homework/randomlabel.pm, revision 1.62
1.1 tsai 1: # The LearningOnline Network with CAPA
2: # random labelling tool
1.8 albertel 3: #
1.62 ! albertel 4: # $Id: randomlabel.pm,v 1.61 2004/07/02 08:09:49 albertel Exp $
1.8 albertel 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: #
1.3 tsai 28: # 7/20/2001 Isaac Tsai, initial syntax
29: # 8/10/2001 Isaac Tsai,
30: # 8/30/2001 Isaac Tsai,
1.1 tsai 31: # SYNTAX:
1.4 albertel 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>
1.1 tsai 40: # </labelgroup>
1.4 albertel 41: # <labelgroup name="GroupTwo" type="text">
42: # <location x="12" y="45" />
43: # <location x="32" y="65" />
44: # <location x="21" y="54" />
1.3 tsai 45: # <label>TEXT-1</label>
46: # <label>TEXT-2</label>
47: # <label>TEXT-3</label>
1.1 tsai 48: # </labelgroup>
49: # </randomlabel>
50: # ===========================================
1.3 tsai 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"
1.1 tsai 58: # ===========================================
59: package Apache::randomlabel;
1.24 sakharuk 60: use Apache::lonnet;
1.1 tsai 61: use strict;
1.13 sakharuk 62: use Apache::edit;
1.36 sakharuk 63: use Apache::File();
1.38 sakharuk 64: use Apache::Constants qw(:common :http);
1.1 tsai 65:
1.53 albertel 66: my %args;
67: my $cgi_id;
68:
1.13 sakharuk 69: BEGIN {
1.57 albertel 70: &Apache::lonxml::register('Apache::randomlabel',('randomlabel','labelgroup','location','label','bgimg'));
1.1 tsai 71: }
72:
1.23 matthew 73: sub check_int {
74: # utility function to do error checking on a integer.
75: my ($num,$default) = @_;
76: $default = 100 if (! defined($default));
77: $num =~ s/\s+//g; # We dont need no stinkin white space!
78: # If it is a real, just grab the integer part.
79: ($num,undef) = split (/\./,$num) if ($num =~ /\./);
80: # set to default if what we have left doesn't match anything...
81: $num = $default unless ($num =~/^\d+$/);
82: return $num;
83: }
84:
1.1 tsai 85: sub start_randomlabel {
1.47 albertel 86: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
87: my $result='';
88: push (@Apache::lonxml::namespace,'randomlabel');
89: my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
1.60 albertel 90: if ( defined($bgimg) && $bgimg !~ /^http:/ ) {
1.47 albertel 91: $bgimg=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$bgimg);
92: if (&Apache::lonnet::repcopy($bgimg) ne OK) {
93: $bgimg='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
94: }
1.42 www 95: }
1.59 albertel 96: $Apache::randomlabel::obj_cnt=0;
1.47 albertel 97: if ($target eq 'web') {
1.53 albertel 98: $cgi_id=&Apache::loncommon::get_cgi_id();
99: %args=();
100: $args{"cgi.$cgi_id.BGIMG"}=&Apache::lonnet::escape($bgimg);
1.47 albertel 101: } elsif ($target eq 'tex') {
1.57 albertel 102: my $w= &check_int(&Apache::lonxml::get_param('width',$parstack,$safeeval));
103: my $h= &check_int(&Apache::lonxml::get_param('height',$parstack,$safeeval));
104: my $texwidth=&adjust_textwidth(&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1));
105: if (!$texwidth) { $texwidth=90; }
106: $result.=&make_eps_image($bgimg,$texwidth,$h,$w);
1.47 albertel 107: } elsif ($target eq 'edit') {
108: $result.=&Apache::edit::tag_start($target,$token);
109: $Apache::edit::bgimgsrc=
110: &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
111: $Apache::edit::bgimgsrccurdepth=$Apache::lonxml::curdepth;
112: $result.=&Apache::edit::text_arg('Image:','bgimg',$token,75).' ';
113: $result.=&Apache::edit::browse('bgimg').' ';
114: $result.=&Apache::edit::search('bgimg').'<br />'.
115: &Apache::edit::text_arg('Width(pixel):' ,'width' ,$token,6).
116: &Apache::edit::text_arg('Height(pixel):','height' ,$token,6).
117: &Apache::edit::text_arg('TeXWidth(mm):' ,'texwidth',$token,6).
118: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
119: } elsif ($target eq 'modified') {
120: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
121: $safeeval,'bgimg','width',
122: 'height','texwidth');
123: if ($constructtag) {
124: $result = &Apache::edit::rebuild_tag($token);
125: $result.=&Apache::edit::handle_insert();
126: }
1.31 sakharuk 127: }
1.47 albertel 128: return $result;
1.1 tsai 129: }
130:
131: sub end_randomlabel {
1.47 albertel 132: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
133: my $result='';
134: my $count;
135: pop @Apache::lonxml::namespace;
136: if ($target eq 'web') {
1.59 albertel 137: $count = $Apache::randomlabel::obj_cnt;
138: if( $count != 0) { $args{"cgi.$cgi_id.OBJCOUNT"}=$count; }
1.53 albertel 139: $result.='<img src="/adm/randomlabel.png?token='.$cgi_id.'" /><br />'."\n";
140: &Apache::lonnet::appenv(%args);
1.47 albertel 141: } elsif ($target eq 'tex') {
142: $result='\end{picture}\\\\';
143: my $height=&Apache::lonxml::get_param('height',$parstack,$safeeval);
144: my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
1.50 sakharuk 145: my $texwidth=&adjust_textwidth(&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1));
1.49 albertel 146: if (!$texwidth) { $texwidth=90; }
1.62 ! albertel 147: # what if width is undefined?
1.47 albertel 148: my $howtoskipback = $texwidth*$height/$width;
149: $result.= ' \vskip -'.$howtoskipback.' mm } \\\\ ';
150: } elsif ($target eq 'edit') {
151: $result.=&Apache::edit::end_table;
152: }
153: return $result;
1.1 tsai 154: }
155:
1.57 albertel 156: sub start_bgimg {
157: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
158: my $result='';
159: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
160: &Apache::lonxml::startredirection;
161: }
162: return $result;
163: }
164:
165: sub end_bgimg {
166: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
167: my $result='';
168: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
169: my $bgimg=&Apache::lonxml::endredirection;
170: if ($target eq 'web') {
171: $bgimg=&Apache::imageresponse::clean_up_image($bgimg);
172: $args{"cgi.$cgi_id.BGIMG"}=&Apache::lonnet::escape($bgimg);
173: } elsif ($target eq 'tex') {
174: my $w=&check_int(&Apache::lonxml::get_param('width',$parstack,$safeeval,-2));
175: my $h=&check_int(&Apache::lonxml::get_param('height',$parstack,$safeeval,-2));
176: my $texwidth=&adjust_textwidth(&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,-2));
177: if (!$texwidth) { $texwidth=90; }
178: $result.=&make_eps_image($bgimg,$texwidth,$h,$w);
179: }
180: }
181: return $result;
182: }
183:
184: sub make_eps_image {
185: my ($bgimg,$texwidth,$h,$w)=@_;
186: my $newbgimg = $bgimg;
187: $bgimg=~s/\.(gif|jpg|png|jpeg)$/\.eps/i;
188: $bgimg= &Apache::lonnet::filelocation($bgimg);
189: $bgimg=~s|http:/[^/]*|/home/httpd/html|;
190: $bgimg=~s|/$||;
191: #if no eps file try to replicate it
192: if (not-e $bgimg) {
193: if (&Apache::lonnet::repcopy($bgimg) ne OK ) {
194: #if replication failed try to find ps file
195: $bgimg=~s/\.eps$/\.ps/;
196: #if no ps file try to replicate it
197: if (not -e $bgimg &&
198: &Apache::lonnet::repcopy($bgimg) ne OK) {
199: #if replication failed try to produce eps file dynamically
200: $bgimg=~s/\.ps$/\.eps/;
201: my $temp_file;
202: my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
203: $temp_file = Apache::File->new('>>'.$filename);
204: $newbgimg =~ s|(.*)/res/|/home/httpd/html/res/|;
205: print $temp_file "$newbgimg\n";
206: $bgimg=~s|/home/httpd/html/res|/home/httpd/prtspool|;
207: $bgimg=~s|/home/([^/]*)/public_html/|/home/httpd/prtspool/$1/|;
208: }
209: }
210: }
211: $bgimg=~s|/$||;
212: my $dirtywidth=$texwidth+5;
213: my $result.='\vspace*{2mm}\noindent \parbox{'.$dirtywidth.
214: ' mm}{ \noindent \epsfxsize='.$texwidth.' mm \epsffile{'.$bgimg.
215: '}\setlength{\unitlength}{1mm} \begin{picture}('.
216: $texwidth.','.$texwidth*$h/$w.')(0,-'.$texwidth*$h/$w.')';
217: return $result;
218: }
219:
1.50 sakharuk 220: sub adjust_textwidth {
221: my $texwidth=shift;
222: my $pagewidth=$ENV{'form.textwidth'};
223: $pagewidth=~s/\s*mm\s*$//;
224: if ($texwidth>$pagewidth) {$texwidth=$pagewidth;}
225: return $texwidth;
226: }
227:
1.1 tsai 228: sub start_labelgroup {
1.47 albertel 229: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
230: my $result='';
231: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
232: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
233: $type =~tr/A-Z/a-z/;
1.48 albertel 234: if ($target ne 'modified' && ($name =~ /\W/ || $name =~ /^[0-9]/)) {
235: &Apache::lonxml::error("Only _ a-z A-Z and 0-9 are allowed in the name to a labelgroup, and the first character can not be a number.<br />");
236: }
1.47 albertel 237: if ($target eq 'web' || $target eq 'tex' ||
238: $target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
239: $Apache::randomlabel::groupname=$name;
240: $Apache::randomlabel::type=$type;
241: @Apache::randomlabel::xcoord = ();
242: @Apache::randomlabel::ycoord = ();
243: @Apache::randomlabel::value = ();
244: @Apache::randomlabel::label_arr = ();
245: @Apache::randomlabel::decription = ();
246: } elsif ($target eq 'edit') {
247: $result.=&Apache::edit::tag_start($target,$token);
248: $result.=&Apache::edit::text_arg('Name:','name',$token).
249: &Apache::edit::select_arg('Type:','type',['text','image'],$token).
250: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
251: } elsif ($target eq 'modified') {
252: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
253: $safeeval,'name','type');
254: if ($constructtag) {
255: $result = &Apache::edit::rebuild_tag($token);
256: $result.=&Apache::edit::handle_insert();
257: }
1.4 albertel 258: }
1.47 albertel 259: return $result;
1.1 tsai 260: }
261:
1.5 albertel 262: sub add_vars {
1.47 albertel 263: my ($name,$order,$label,$labelorder,$value,$image,$safeeval) = @_;
1.61 albertel 264: if (!defined($name) || $name eq '') { return; }
1.47 albertel 265: my $code = '${'.$name."}{'".($order+1)."'}='".$label."';";
266: my $out=Apache::run::run($code,$safeeval);
267: if ($value) {
268: $code = '${'.$name."}{'value_".($order+1)."'}='".$value."';";
269: $out=Apache::run::run($code,$safeeval);
270: $code = '${'.$name."}{'labelvalue_".($labelorder+1)."'}='".$value."';";
271: $out=Apache::run::run($code,$safeeval);
272: }
273: if ($image) {
274: my $code = '${'.$name."}{'image_".($order+1)."'}='".$image."';";
275: my $out=Apache::run::run($code,$safeeval);
276: }
277: $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
1.5 albertel 278: $out=Apache::run::run($code,$safeeval);
1.4 albertel 279: }
1.5 albertel 280:
1.1 tsai 281: # begin to assign labels to locations
282: sub end_labelgroup {
1.47 albertel 283: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
284: my $gname = $Apache::randomlabel::groupname;
285: my $type = $Apache::randomlabel::type;
286: my $result='';
287: if ($target eq 'web' || $target eq 'answer' || $target eq 'grade' ||
288: $target eq 'analyze') {
289: my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
290: &Apache::structuretags::shuffle(\@idx_arr);
291: for(0 .. $#Apache::randomlabel::label_arr) {
292: my $str;
293: my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$_] ]";
294: my $x = $Apache::randomlabel::xcoord[$_];
295: my $y = $Apache::randomlabel::ycoord[$_];
296: my $value = $Apache::randomlabel::value[$_];
1.59 albertel 297: my $i=$Apache::randomlabel::obj_cnt++;
1.47 albertel 298: if( $type eq 'text') {
299: &add_vars($gname,$_,$label,$idx_arr[$_],$value,'',$safeeval);
1.59 albertel 300: $str = join(':',$x,$y,&Apache::lonnet::escape($label));
301: $args{"cgi.$cgi_id.OBJTYPE"}.='LABEL:';
1.47 albertel 302: } elsif ( $type eq 'image') {
303: &add_vars($gname,$_,
304: $Apache::randomlabel::description[$idx_arr[$_]],
305: $idx_arr[$_],$value,$label,$safeeval);
1.59 albertel 306: $str = join(':',$x,$y,&Apache::lonnet::escape($label));
307: $args{"cgi.$cgi_id.OBJTYPE"}.='IMAGE:';
1.47 albertel 308: } else {
309: &Apache::lonxml::error('Unknown type of label :'.$type.':');
310: }
1.59 albertel 311: if ($target eq 'web') { $args{"cgi.$cgi_id.OBJ$i"} =$str; }
1.47 albertel 312: }
313: } elsif ($target eq 'tex') {
314: my $WX1=0; # Web x-coord. of upper left corner (ULC)
315: my $WY1=0; # Web y-coord. of (ULC)
316: my $wwidth=&Apache::lonxml::get_param('width',$parstack,$safeeval,-2);
317: my $wheight=&Apache::lonxml::get_param('height',$parstack,$safeeval,-2);
1.50 sakharuk 318: my $texwidth=&adjust_textwidth(&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,-2,1));
1.49 albertel 319: if (!$texwidth) { $texwidth=90; }
1.62 ! albertel 320: my $texheight=$texwidth*($wheight/$wwidth);
! 321:
1.47 albertel 322: my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
323: &Apache::structuretags::shuffle(\@idx_arr);
324:
325: &Apache::lonxml::debug("Array is:".$#Apache::randomlabel::label_arr.":");
326: for(my $i=0;$i <= $#Apache::randomlabel::label_arr; $i++) {
327: my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$i] ]";
328: my $x = $Apache::randomlabel::xcoord[$i];
1.62 ! albertel 329: # FIXME the 3.5 here is the 'height' of the letter in TeX
! 330: my $y = $Apache::randomlabel::ycoord[$i]-3.5;
1.47 albertel 331: my $value = $Apache::randomlabel::value[$i];
332: #x latex coordinate
333: my $tcX=($x)*($texwidth/$wwidth);
334: #y latex coordinate
335: # my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
1.62 ! albertel 336: my $tcY=$texheight-$y*($texheight/$wheight);
1.47 albertel 337: $tcX=sprintf('%.2f',$tcX);
338: $tcY=sprintf('%.2f',$tcY);
339: $result.='\put('.$tcX.','.$tcY.'){\normalsize \bf '.$label.'}'."\n";
340: if( $type eq 'text') {
341: &add_vars($gname,$i,$label,$idx_arr[$i],$value,'',$safeeval);
342: } elsif ( $type eq 'image') {
343: &add_vars($gname,$i,
344: $Apache::randomlabel::description[$idx_arr[$i]],
345: $idx_arr[$i],$value,$label,$safeeval);
346: } else {
347: &Apache::lonxml::error('Unknown type of label :'.$type.':');
348: }
349: }
350: } elsif ($target eq 'edit') {
351: $result.=&Apache::edit::end_table;
1.3 tsai 352: }
1.47 albertel 353: return $result;
1.1 tsai 354: }
355:
1.5 albertel 356: # <location x="123" y="456" value="some value"/>
1.1 tsai 357: sub start_location {
1.47 albertel 358: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
359: my $x= &check_int(&Apache::lonxml::get_param('x',$parstack,$safeeval),50);
360: my $y= &check_int(&Apache::lonxml::get_param('y',$parstack,$safeeval),50);
361: my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
362: my $result='';
363: push(@Apache::randomlabel::xcoord,$x);
364: push(@Apache::randomlabel::ycoord,$y);
365: push(@Apache::randomlabel::value,$value);
366: if ($target eq 'edit') {
367: $result.=&Apache::edit::tag_start($target,$token);
368: $result.=&Apache::edit::text_arg('X:','x',$token,4).
369: &Apache::edit::text_arg('Y:','y',$token,4).
370: &Apache::edit::entercoords('x','y','attribute','width','height').' '.
371: &Apache::edit::text_arg('Value:','value',$token).
372: &Apache::edit::end_row();
373: $result.=&Apache::edit::end_table;
374: } elsif ($target eq 'modified') {
375: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
376: $safeeval,'x','y','value');
377: if ($constructtag) {
378: $result = &Apache::edit::rebuild_tag($token);
379: $result.=&Apache::edit::handle_insert();
380: }
1.4 albertel 381: }
1.47 albertel 382: return $result;
1.1 tsai 383: }
384:
385: sub end_location {
1.47 albertel 386: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
387: my @result;
388: if ($target eq 'edit') { @result=('','no') }
389: return @result;
1.1 tsai 390: }
391:
1.2 tsai 392: # <label>$var_abc</label>
1.1 tsai 393: sub start_label {
1.47 albertel 394: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
395: my $result='';
396: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
397: if ($target eq 'web' || $target eq 'tex' ||
398: $target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
1.57 albertel 399: &Apache::lonxml::startredirection;
1.47 albertel 400: } elsif ($target eq 'edit') {
401: $result.=&Apache::edit::tag_start($target,$token,"$type Label");
402: my $text=&Apache::lonxml::get_all_text("/label",$parser);
403: if ($type eq 'image') {
404: $result.=&Apache::edit::end_row().
405: &Apache::edit::start_spanning_row();
406: $result.=&Apache::edit::text_arg('Description:','description',
407: $token);
408: }
409: if ($type eq 'text') { $result.="Label Text:"; }
410: if ($type eq 'image') { $result.="Path to image: "; }
411: $result.=&Apache::edit::editline('',$text,'',50).
412: &Apache::edit::end_table();
413: } elsif ($target eq 'modified') {
414: $result = '<label>';
415: if ($type eq 'image') {
416: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
417: $safeeval,
418: 'description');
419: if ($constructtag) {
420: $result = &Apache::edit::rebuild_tag($token);
421: } else {
422: $result = $token->[4];
423: }
424: }
1.52 albertel 425: $result.=&Apache::edit::modifiedfield("/label",$parser);
1.26 albertel 426: }
1.47 albertel 427: return $result;
1.1 tsai 428: }
429:
430: sub end_label {
1.47 albertel 431: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
432: my @result;
1.57 albertel 433: if ($target eq 'edit') {
434: @result=('','no') ;
435: } elsif ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
436: $target eq 'answer' || $target eq 'analyze') {
437: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
438: my $ltext=&Apache::lonxml::endredirection;
439: if ($type eq 'image') {
440: &Apache::lonxml::debug("Turning $ltext, $Apache::lonxml::pwd[-1]");
441: $ltext=&Apache::imageresponse::clean_up_image($ltext);
442: # $ltext=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],
443: # $ltext);
444: &Apache::lonxml::debug("into $ltext");
445: my $description = &Apache::lonxml::get_param('description',
446: $parstack,$safeeval);
447: push(@Apache::randomlabel::description,$description);
1.61 albertel 448: } else {
449: $ltext=~s/[\r\n]*//gs
1.57 albertel 450: }
451: push(@Apache::randomlabel::label_arr,$ltext);
452: }
1.47 albertel 453: return @result;
1.1 tsai 454: }
455:
456: 1;
457: __END__
458:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>