Annotation of loncom/homework/randomlabel.pm, revision 1.50
1.1 tsai 1: # The LearningOnline Network with CAPA
2: # random labelling tool
1.8 albertel 3: #
1.50 ! sakharuk 4: # $Id: randomlabel.pm,v 1.49 2003/09/23 21:20:42 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.13 sakharuk 66: BEGIN {
1.47 albertel 67: &Apache::lonxml::register('Apache::randomlabel',('randomlabel','labelgroup','location','label'));
1.1 tsai 68: }
69:
1.23 matthew 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:
1.1 tsai 82: sub start_randomlabel {
1.47 albertel 83: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
84: my $result='';
85: push (@Apache::lonxml::namespace,'randomlabel');
86: my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
87: if ( $bgimg !~ /^http:/ ) {
88: $bgimg=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$bgimg);
89: if (&Apache::lonnet::repcopy($bgimg) ne OK) {
90: $bgimg='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
91: }
1.42 www 92: }
1.47 albertel 93: my $w= &check_int(&Apache::lonxml::get_param('width',$parstack,$safeeval));
94: my $h= &check_int(&Apache::lonxml::get_param('height',$parstack,$safeeval));
1.50 ! sakharuk 95: my $texwidth=&adjust_textwidth(&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1));
1.49 albertel 96: if (!$texwidth) { $texwidth=90; }
1.47 albertel 97: $Apache::randomlabel::tlabel_cnt=0;
98: $Apache::randomlabel::ilabel_cnt=0;
99: if ($target eq 'web') {
100: &Apache::lonxml::startredirection();
101: $result.="BGIMG=".&Apache::lonnet::escape($bgimg);
102: } elsif ($target eq 'tex') {
103: my $newbgimg = $bgimg;
104: $bgimg=~s/\.(gif|jpg|png|jpeg)$/\.eps/i;
105: $bgimg= &Apache::lonnet::filelocation($bgimg);
106: $bgimg=~s/http:\/[^\/]*/\/home\/httpd\/html/;
107: $bgimg=~s/\/$//;
108: #if no eps file try to replicate it
109: if (not-e $bgimg) {
110: if (&Apache::lonnet::repcopy($bgimg) ne OK ) {
111: #if replication failed try to find ps file
112: $bgimg=~s/\.eps$/\.ps/;
113: #if no ps file try to replicate it
1.45 sakharuk 114: if (not -e $bgimg &&
1.38 sakharuk 115: &Apache::lonnet::repcopy($bgimg) ne OK) {
116: #if replication failed try to produce eps file dynamically
1.36 sakharuk 117: $bgimg=~s/\.ps$/\.eps/;
118: my $temp_file;
119: my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
120: $temp_file = Apache::File->new('>>'.$filename);
121: $newbgimg =~ s/(.*)\/res\//\/home\/httpd\/html\/res\//;
122: print $temp_file "$newbgimg\n";
123: $bgimg =~ m/\/([^\/]+)$/;
124: $bgimg = '/home/httpd/prtspool/'.$1;
125: }
1.47 albertel 126: }
127: }
128: $bgimg=~s/\/$//;
129: my $dirtywidth=$texwidth+5;
1.50 ! sakharuk 130: $result.='\vspace*{2mm}\noindent \parbox{'.$dirtywidth.' mm}{ \noindent \epsfxsize='.
! 131: $texwidth.' mm \epsffile{'.$bgimg.'}\setlength{\unitlength}{1mm} \begin{picture}('.
! 132: $texwidth.','.$texwidth*$h/$w.')(0,-'.$texwidth*$h/$w.')';
1.47 albertel 133: } elsif ($target eq 'edit') {
134: $result.=&Apache::edit::tag_start($target,$token);
135: $Apache::edit::bgimgsrc=
136: &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
137: $Apache::edit::bgimgsrccurdepth=$Apache::lonxml::curdepth;
138: $result.=&Apache::edit::text_arg('Image:','bgimg',$token,75).' ';
139: $result.=&Apache::edit::browse('bgimg').' ';
140: $result.=&Apache::edit::search('bgimg').'<br />'.
141: &Apache::edit::text_arg('Width(pixel):' ,'width' ,$token,6).
142: &Apache::edit::text_arg('Height(pixel):','height' ,$token,6).
143: &Apache::edit::text_arg('TeXWidth(mm):' ,'texwidth',$token,6).
144: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
145: } elsif ($target eq 'modified') {
146: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
147: $safeeval,'bgimg','width',
148: 'height','texwidth');
149: if ($constructtag) {
150: $result = &Apache::edit::rebuild_tag($token);
151: $result.=&Apache::edit::handle_insert();
152: }
1.31 sakharuk 153: }
1.47 albertel 154: return $result;
1.1 tsai 155: }
156:
157: sub end_randomlabel {
1.47 albertel 158: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
159: my $result='';
160: my $count;
161: pop @Apache::lonxml::namespace;
162: if ($target eq 'web') {
163: my $args=&Apache::lonxml::endredirection();
164: $args=~s/\s*&/&/g;
165: $args=~s/\s*$//;
166: $count = $Apache::randomlabel::tlabel_cnt;
167: if( $count != 0) { $args.= "&COUNT=$count"; }
168: $count = $Apache::randomlabel::ilabel_cnt;
169: if( $count != 0) { $args.= "&ICOUNT=$count"; }
170: my $token=$Apache::lonxml::curdepth.'_'.$$;
171: $result.='<img src="/adm/randomlabel.png?token='.$token;
172: $result.='" /><br />'."\n";
173: &Apache::lonnet::appenv(("imagerequest.$token"=>&Apache::lonnet::escape($args)));
174: } elsif ($target eq 'tex') {
175: $result='\end{picture}\\\\';
176: my $height=&Apache::lonxml::get_param('height',$parstack,$safeeval);
177: my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
1.50 ! sakharuk 178: my $texwidth=&adjust_textwidth(&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1));
1.49 albertel 179: if (!$texwidth) { $texwidth=90; }
1.47 albertel 180: my $howtoskipback = $texwidth*$height/$width;
181: $result.= ' \vskip -'.$howtoskipback.' mm } \\\\ ';
182: } elsif ($target eq 'edit') {
183: $result.=&Apache::edit::end_table;
184: }
185: return $result;
1.1 tsai 186: }
187:
1.50 ! sakharuk 188: sub adjust_textwidth {
! 189: my $texwidth=shift;
! 190: my $pagewidth=$ENV{'form.textwidth'};
! 191: $pagewidth=~s/\s*mm\s*$//;
! 192: if ($texwidth>$pagewidth) {$texwidth=$pagewidth;}
! 193: return $texwidth;
! 194: }
! 195:
1.1 tsai 196: sub start_labelgroup {
1.47 albertel 197: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
198: my $result='';
199: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
200: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
201: $type =~tr/A-Z/a-z/;
1.48 albertel 202: if ($target ne 'modified' && ($name =~ /\W/ || $name =~ /^[0-9]/)) {
203: &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 />");
204: }
1.47 albertel 205: if ($target eq 'web' || $target eq 'tex' ||
206: $target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
207: $Apache::randomlabel::groupname=$name;
208: $Apache::randomlabel::type=$type;
209: @Apache::randomlabel::xcoord = ();
210: @Apache::randomlabel::ycoord = ();
211: @Apache::randomlabel::value = ();
212: @Apache::randomlabel::label_arr = ();
213: @Apache::randomlabel::decription = ();
214: } elsif ($target eq 'edit') {
215: $result.=&Apache::edit::tag_start($target,$token);
216: $result.=&Apache::edit::text_arg('Name:','name',$token).
217: &Apache::edit::select_arg('Type:','type',['text','image'],$token).
218: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
219: } elsif ($target eq 'modified') {
220: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
221: $safeeval,'name','type');
222: if ($constructtag) {
223: $result = &Apache::edit::rebuild_tag($token);
224: $result.=&Apache::edit::handle_insert();
225: }
1.4 albertel 226: }
1.47 albertel 227: return $result;
1.1 tsai 228: }
229:
1.5 albertel 230: sub add_vars {
1.47 albertel 231: my ($name,$order,$label,$labelorder,$value,$image,$safeeval) = @_;
232: my $code = '${'.$name."}{'".($order+1)."'}='".$label."';";
233: my $out=Apache::run::run($code,$safeeval);
234: if ($value) {
235: $code = '${'.$name."}{'value_".($order+1)."'}='".$value."';";
236: $out=Apache::run::run($code,$safeeval);
237: $code = '${'.$name."}{'labelvalue_".($labelorder+1)."'}='".$value."';";
238: $out=Apache::run::run($code,$safeeval);
239: }
240: if ($image) {
241: my $code = '${'.$name."}{'image_".($order+1)."'}='".$image."';";
242: my $out=Apache::run::run($code,$safeeval);
243: }
244: $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
1.5 albertel 245: $out=Apache::run::run($code,$safeeval);
1.4 albertel 246: }
1.5 albertel 247:
1.1 tsai 248: # begin to assign labels to locations
249: sub end_labelgroup {
1.47 albertel 250: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
251: my $gname = $Apache::randomlabel::groupname;
252: my $type = $Apache::randomlabel::type;
253: my $result='';
254: if ($target eq 'web' || $target eq 'answer' || $target eq 'grade' ||
255: $target eq 'analyze') {
256: my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
257: &Apache::structuretags::shuffle(\@idx_arr);
258: for(0 .. $#Apache::randomlabel::label_arr) {
259: my $str;
260: my $xstr;
261: my $ystr;
262: my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$_] ]";
263: my $x = $Apache::randomlabel::xcoord[$_];
264: my $y = $Apache::randomlabel::ycoord[$_];
265: my $value = $Apache::randomlabel::value[$_];
266: if( $type eq 'text') {
267: &add_vars($gname,$_,$label,$idx_arr[$_],$value,'',$safeeval);
268: $str = 'LB'.$Apache::randomlabel::tlabel_cnt;
269: $xstr = 'X'.$Apache::randomlabel::tlabel_cnt;
270: $ystr = 'Y'.$Apache::randomlabel::tlabel_cnt;
271: $Apache::randomlabel::tlabel_cnt += 1;
272: } elsif ( $type eq 'image') {
273: &add_vars($gname,$_,
274: $Apache::randomlabel::description[$idx_arr[$_]],
275: $idx_arr[$_],$value,$label,$safeeval);
276: $str = 'IMG'.$Apache::randomlabel::ilabel_cnt;
277: $xstr = 'IX'.$Apache::randomlabel::ilabel_cnt;
278: $ystr = 'IY'.$Apache::randomlabel::ilabel_cnt;
279: $Apache::randomlabel::ilabel_cnt += 1;
280: } else {
281: &Apache::lonxml::error('Unknown type of label :'.$type.':');
282: }
283: if ($target eq 'web') {
284: $result .= '&'. $str .'='.&Apache::lonnet::escape($label);
285: $result .= '&'. $xstr .'='.$x;
286: $result .= '&'. $ystr .'='.$y;
287: }
288: }
289: } elsif ($target eq 'tex') {
290: my $WX1=0; # Web x-coord. of upper left corner (ULC)
291: my $WY1=0; # Web y-coord. of (ULC)
292: my $wwidth=&Apache::lonxml::get_param('width',$parstack,$safeeval,-2);
293: my $wheight=&Apache::lonxml::get_param('height',$parstack,$safeeval,-2);
1.50 ! sakharuk 294: my $texwidth=&adjust_textwidth(&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,-2,1));
1.49 albertel 295: if (!$texwidth) { $texwidth=90; }
1.47 albertel 296: my $TX1=0;
297: my $TY1=$texwidth*($wheight/$wwidth);
298: my $TX2=$texwidth;
299: my $TY2=0;
300:
301:
302: my $slopex=($wwidth-$WX1)/($TX2-$TX1);
303: my $slopey=($wheight-$WY1)/($TY2-($TY1-1.0));
304: my $cstx=$wwidth-$slopex*($TX2);
305: my $csty=$wheight-$slopey*($TY2);
306:
307: my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
308: &Apache::structuretags::shuffle(\@idx_arr);
309:
310: &Apache::lonxml::debug("Array is:".$#Apache::randomlabel::label_arr.":");
311: for(my $i=0;$i <= $#Apache::randomlabel::label_arr; $i++) {
312: my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$i] ]";
313: my $x = $Apache::randomlabel::xcoord[$i];
314: my $y = $Apache::randomlabel::ycoord[$i];
315: my $value = $Apache::randomlabel::value[$i];
316: #x latex coordinate
317: my $tcX=($x)*($texwidth/$wwidth);
318: #y latex coordinate
319: # my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
320: my $tcY=$TY1-$y*($TY1/$wheight);
321: $tcX=sprintf('%.2f',$tcX);
322: $tcY=sprintf('%.2f',$tcY);
323: $result.='\put('.$tcX.','.$tcY.'){\normalsize \bf '.$label.'}'."\n";
324: if( $type eq 'text') {
325: &add_vars($gname,$i,$label,$idx_arr[$i],$value,'',$safeeval);
326: } elsif ( $type eq 'image') {
327: &add_vars($gname,$i,
328: $Apache::randomlabel::description[$idx_arr[$i]],
329: $idx_arr[$i],$value,$label,$safeeval);
330: } else {
331: &Apache::lonxml::error('Unknown type of label :'.$type.':');
332: }
333: }
334: } elsif ($target eq 'edit') {
335: $result.=&Apache::edit::end_table;
1.3 tsai 336: }
1.47 albertel 337: return $result;
1.1 tsai 338: }
339:
1.5 albertel 340: # <location x="123" y="456" value="some value"/>
1.1 tsai 341: sub start_location {
1.47 albertel 342: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
343: my $x= &check_int(&Apache::lonxml::get_param('x',$parstack,$safeeval),50);
344: my $y= &check_int(&Apache::lonxml::get_param('y',$parstack,$safeeval),50);
345: my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
346: my $result='';
347: push(@Apache::randomlabel::xcoord,$x);
348: push(@Apache::randomlabel::ycoord,$y);
349: push(@Apache::randomlabel::value,$value);
350: if ($target eq 'edit') {
351: $result.=&Apache::edit::tag_start($target,$token);
352: $result.=&Apache::edit::text_arg('X:','x',$token,4).
353: &Apache::edit::text_arg('Y:','y',$token,4).
354: &Apache::edit::entercoords('x','y','attribute','width','height').' '.
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,
360: $safeeval,'x','y','value');
361: if ($constructtag) {
362: $result = &Apache::edit::rebuild_tag($token);
363: $result.=&Apache::edit::handle_insert();
364: }
1.4 albertel 365: }
1.47 albertel 366: return $result;
1.1 tsai 367: }
368:
369: sub end_location {
1.47 albertel 370: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
371: my @result;
372: if ($target eq 'edit') { @result=('','no') }
373: return @result;
1.1 tsai 374: }
375:
1.2 tsai 376: # <label>$var_abc</label>
1.1 tsai 377: sub start_label {
1.47 albertel 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);
384: $ltext=&Apache::run::evaluate($ltext,$safeeval,$$parstack[-1]);
385: if ($type eq 'image') {
386: &Apache::lonxml::debug("Turning $ltext, $Apache::lonxml::pwd[-1]");
387: $ltext=&Apache::lonnet::filelocation($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);
398: if ($type eq 'image') {
399: $result.=&Apache::edit::end_row().
400: &Apache::edit::start_spanning_row();
401: $result.=&Apache::edit::text_arg('Description:','description',
402: $token);
403: }
404: if ($type eq 'text') { $result.="Label Text:"; }
405: if ($type eq 'image') { $result.="Path to image: "; }
406: $result.=&Apache::edit::editline('',$text,'',50).
407: &Apache::edit::end_table();
408: } elsif ($target eq 'modified') {
409: my $text=$$parser[-1]->get_text("/label");
410: $result = '<label>';
411: if ($type eq 'image') {
412: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
413: $safeeval,
414: 'description');
415: if ($constructtag) {
416: $result = &Apache::edit::rebuild_tag($token);
417: } else {
418: $result = $token->[4];
419: }
420: }
421: $result.=&Apache::edit::modifiedfield($token);
1.26 albertel 422: }
1.47 albertel 423: return $result;
1.1 tsai 424: }
425:
426: sub end_label {
1.47 albertel 427: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
428: my @result;
429: if ($target eq 'edit') { @result=('','no') }
430: return @result;
1.1 tsai 431: }
432:
433: 1;
434: __END__
435:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>