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