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