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