Annotation of loncom/homework/randomlabel.pm, revision 1.66
1.1 tsai 1: # The LearningOnline Network with CAPA
2: # random labelling tool
1.8 albertel 3: #
1.66 ! raeburn 4: # $Id: randomlabel.pm,v 1.65 2005/03/02 22:26:36 raeburn 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.64 albertel 85: my ($height_param,$width_param);
1.1 tsai 86: sub start_randomlabel {
1.47 albertel 87: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
88: my $result='';
89: push (@Apache::lonxml::namespace,'randomlabel');
1.64 albertel 90: ($height_param,$width_param)=(0,0);
1.47 albertel 91: my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
1.60 albertel 92: if ( defined($bgimg) && $bgimg !~ /^http:/ ) {
1.47 albertel 93: $bgimg=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$bgimg);
1.66 ! raeburn 94: if (&Apache::lonnet::repcopy($bgimg) ne 'ok') {
1.47 albertel 95: $bgimg='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
96: }
1.42 www 97: }
1.59 albertel 98: $Apache::randomlabel::obj_cnt=0;
1.47 albertel 99: if ($target eq 'web') {
1.53 albertel 100: $cgi_id=&Apache::loncommon::get_cgi_id();
101: %args=();
102: $args{"cgi.$cgi_id.BGIMG"}=&Apache::lonnet::escape($bgimg);
1.64 albertel 103: } elsif ($target eq 'tex' && defined($bgimg)) {
104: $result.=&make_eps_image($bgimg,$parstack,$safeeval);
1.47 albertel 105: } elsif ($target eq 'edit') {
106: $result.=&Apache::edit::tag_start($target,$token);
107: $Apache::edit::bgimgsrc=
108: &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
109: $Apache::edit::bgimgsrccurdepth=$Apache::lonxml::curdepth;
110: $result.=&Apache::edit::text_arg('Image:','bgimg',$token,75).' ';
111: $result.=&Apache::edit::browse('bgimg').' ';
112: $result.=&Apache::edit::search('bgimg').'<br />'.
113: &Apache::edit::text_arg('Width(pixel):' ,'width' ,$token,6).
114: &Apache::edit::text_arg('Height(pixel):','height' ,$token,6).
115: &Apache::edit::text_arg('TeXWidth(mm):' ,'texwidth',$token,6).
116: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
117: } elsif ($target eq 'modified') {
118: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
119: $safeeval,'bgimg','width',
120: 'height','texwidth');
121: if ($constructtag) {
122: $result = &Apache::edit::rebuild_tag($token);
123: $result.=&Apache::edit::handle_insert();
124: }
1.31 sakharuk 125: }
1.47 albertel 126: return $result;
1.1 tsai 127: }
128:
129: sub end_randomlabel {
1.47 albertel 130: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
131: my $result='';
132: my $count;
133: pop @Apache::lonxml::namespace;
134: if ($target eq 'web') {
1.59 albertel 135: $count = $Apache::randomlabel::obj_cnt;
136: if( $count != 0) { $args{"cgi.$cgi_id.OBJCOUNT"}=$count; }
1.53 albertel 137: $result.='<img src="/adm/randomlabel.png?token='.$cgi_id.'" /><br />'."\n";
138: &Apache::lonnet::appenv(%args);
1.47 albertel 139: } elsif ($target eq 'tex') {
140: $result='\end{picture}\\\\';
1.64 albertel 141: $result.= ' \vskip -'.$height_param.' mm } \\\\ ';
1.47 albertel 142: } elsif ($target eq 'edit') {
143: $result.=&Apache::edit::end_table;
144: }
145: return $result;
1.1 tsai 146: }
147:
1.57 albertel 148: sub start_bgimg {
149: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
150: my $result='';
151: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.64 albertel 152: &Apache::lonxml::startredirection();
1.57 albertel 153: }
154: return $result;
155: }
156:
157: sub end_bgimg {
158: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
159: my $result='';
160: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.64 albertel 161: my $bgimg=&Apache::lonxml::endredirection();
1.57 albertel 162: if ($target eq 'web') {
163: $bgimg=&Apache::imageresponse::clean_up_image($bgimg);
164: $args{"cgi.$cgi_id.BGIMG"}=&Apache::lonnet::escape($bgimg);
165: } elsif ($target eq 'tex') {
1.64 albertel 166: $result.=&make_eps_image($bgimg,$parstack,$safeeval,-2);
1.57 albertel 167: }
168: }
169: return $result;
170: }
171:
172: sub make_eps_image {
1.64 albertel 173: my ($bgimg,$parstack,$safeeval,$depth)=@_;
174: my ($path,$file) = &Apache::londefdef::get_eps_image($bgimg);
175: ($height_param,$width_param)=
176: &Apache::londefdef::image_size($bgimg,0.3,$parstack,$safeeval,
177: $depth,1);
178: my $dirtywidth=$width_param+5;
1.57 albertel 179: my $result.='\vspace*{2mm}\noindent \parbox{'.$dirtywidth.
1.64 albertel 180: ' mm}{ \noindent \epsfxsize='.$width_param.
181: ' mm \epsffile{'.$path.$file.
1.57 albertel 182: '}\setlength{\unitlength}{1mm} \begin{picture}('.
1.64 albertel 183: $width_param.','.$height_param.')(0,-'.$height_param.')';
1.57 albertel 184: return $result;
185: }
186:
1.1 tsai 187: sub start_labelgroup {
1.47 albertel 188: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
189: my $result='';
190: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
191: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
192: $type =~tr/A-Z/a-z/;
1.48 albertel 193: if ($target ne 'modified' && ($name =~ /\W/ || $name =~ /^[0-9]/)) {
194: &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 />");
195: }
1.47 albertel 196: if ($target eq 'web' || $target eq 'tex' ||
197: $target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
198: $Apache::randomlabel::groupname=$name;
199: $Apache::randomlabel::type=$type;
200: @Apache::randomlabel::xcoord = ();
201: @Apache::randomlabel::ycoord = ();
202: @Apache::randomlabel::value = ();
203: @Apache::randomlabel::label_arr = ();
204: @Apache::randomlabel::decription = ();
205: } elsif ($target eq 'edit') {
206: $result.=&Apache::edit::tag_start($target,$token);
207: $result.=&Apache::edit::text_arg('Name:','name',$token).
1.63 albertel 208: &Apache::edit::select_arg('Type:','type',['text','image'],$token);
209: if (!defined($token->[2]{'TeXsize'})) {
210: $token->[2]{'TeXsize'}='\normalsize';
211: }
212: $result.=&Apache::edit::select_arg('TeX font size:','TeXsize',
213: ['\tiny','\scriptsize',
214: '\footnotesize','\small',
215: '\normalsize','\large','\Large',
216: '\LARGE','\huge','\Huge'],
217: $token);
218: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.47 albertel 219: } elsif ($target eq 'modified') {
220: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.63 albertel 221: $safeeval,'name','type',
222: 'TeXsize');
1.47 albertel 223: if ($constructtag) {
224: $result = &Apache::edit::rebuild_tag($token);
225: $result.=&Apache::edit::handle_insert();
226: }
1.4 albertel 227: }
1.47 albertel 228: return $result;
1.1 tsai 229: }
230:
1.5 albertel 231: sub add_vars {
1.47 albertel 232: my ($name,$order,$label,$labelorder,$value,$image,$safeeval) = @_;
1.61 albertel 233: if (!defined($name) || $name eq '') { return; }
1.47 albertel 234: my $code = '${'.$name."}{'".($order+1)."'}='".$label."';";
235: my $out=Apache::run::run($code,$safeeval);
236: if ($value) {
237: $code = '${'.$name."}{'value_".($order+1)."'}='".$value."';";
238: $out=Apache::run::run($code,$safeeval);
239: $code = '${'.$name."}{'labelvalue_".($labelorder+1)."'}='".$value."';";
240: $out=Apache::run::run($code,$safeeval);
241: }
242: if ($image) {
243: my $code = '${'.$name."}{'image_".($order+1)."'}='".$image."';";
244: my $out=Apache::run::run($code,$safeeval);
245: }
246: $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
1.5 albertel 247: $out=Apache::run::run($code,$safeeval);
1.4 albertel 248: }
1.5 albertel 249:
1.1 tsai 250: # begin to assign labels to locations
251: sub end_labelgroup {
1.47 albertel 252: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
253: my $gname = $Apache::randomlabel::groupname;
254: my $type = $Apache::randomlabel::type;
255: my $result='';
256: if ($target eq 'web' || $target eq 'answer' || $target eq 'grade' ||
257: $target eq 'analyze') {
258: my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
259: &Apache::structuretags::shuffle(\@idx_arr);
260: for(0 .. $#Apache::randomlabel::label_arr) {
261: my $str;
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[$_];
1.59 albertel 266: my $i=$Apache::randomlabel::obj_cnt++;
1.47 albertel 267: if( $type eq 'text') {
268: &add_vars($gname,$_,$label,$idx_arr[$_],$value,'',$safeeval);
1.59 albertel 269: $str = join(':',$x,$y,&Apache::lonnet::escape($label));
270: $args{"cgi.$cgi_id.OBJTYPE"}.='LABEL:';
1.47 albertel 271: } elsif ( $type eq 'image') {
272: &add_vars($gname,$_,
273: $Apache::randomlabel::description[$idx_arr[$_]],
274: $idx_arr[$_],$value,$label,$safeeval);
1.59 albertel 275: $str = join(':',$x,$y,&Apache::lonnet::escape($label));
276: $args{"cgi.$cgi_id.OBJTYPE"}.='IMAGE:';
1.47 albertel 277: } else {
278: &Apache::lonxml::error('Unknown type of label :'.$type.':');
279: }
1.59 albertel 280: if ($target eq 'web') { $args{"cgi.$cgi_id.OBJ$i"} =$str; }
1.47 albertel 281: }
282: } elsif ($target eq 'tex') {
283: my $WX1=0; # Web x-coord. of upper left corner (ULC)
284: my $WY1=0; # Web y-coord. of (ULC)
285: my $wwidth=&Apache::lonxml::get_param('width',$parstack,$safeeval,-2);
286: my $wheight=&Apache::lonxml::get_param('height',$parstack,$safeeval,-2);
1.63 albertel 287: my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
288: if (!defined($TeXsize)) { $TeXsize='\\normalsize'; }
1.62 albertel 289:
1.47 albertel 290: my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
291: &Apache::structuretags::shuffle(\@idx_arr);
292:
293: &Apache::lonxml::debug("Array is:".$#Apache::randomlabel::label_arr.":");
294: for(my $i=0;$i <= $#Apache::randomlabel::label_arr; $i++) {
295: my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$i] ]";
296: my $x = $Apache::randomlabel::xcoord[$i];
1.62 albertel 297: # FIXME the 3.5 here is the 'height' of the letter in TeX
298: my $y = $Apache::randomlabel::ycoord[$i]-3.5;
1.47 albertel 299: my $value = $Apache::randomlabel::value[$i];
300: #x latex coordinate
1.64 albertel 301: my $tcX=($x)*($width_param/$wwidth);
1.47 albertel 302: #y latex coordinate
303: # my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
1.64 albertel 304: my $tcY=$height_param-$y*($height_param/$wheight);
1.47 albertel 305: $tcX=sprintf('%.2f',$tcX);
306: $tcY=sprintf('%.2f',$tcY);
1.63 albertel 307: $result.='\put('.$tcX.','.$tcY.'){'.$TeXsize.' \bf '.$label.'}'."\n";
1.47 albertel 308: if( $type eq 'text') {
309: &add_vars($gname,$i,$label,$idx_arr[$i],$value,'',$safeeval);
310: } elsif ( $type eq 'image') {
311: &add_vars($gname,$i,
312: $Apache::randomlabel::description[$idx_arr[$i]],
313: $idx_arr[$i],$value,$label,$safeeval);
314: } else {
315: &Apache::lonxml::error('Unknown type of label :'.$type.':');
316: }
317: }
318: } elsif ($target eq 'edit') {
319: $result.=&Apache::edit::end_table;
1.3 tsai 320: }
1.47 albertel 321: return $result;
1.1 tsai 322: }
323:
1.5 albertel 324: # <location x="123" y="456" value="some value"/>
1.1 tsai 325: sub start_location {
1.47 albertel 326: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
327: my $x= &check_int(&Apache::lonxml::get_param('x',$parstack,$safeeval),50);
328: my $y= &check_int(&Apache::lonxml::get_param('y',$parstack,$safeeval),50);
329: my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
330: my $result='';
331: push(@Apache::randomlabel::xcoord,$x);
332: push(@Apache::randomlabel::ycoord,$y);
333: push(@Apache::randomlabel::value,$value);
334: if ($target eq 'edit') {
335: $result.=&Apache::edit::tag_start($target,$token);
336: $result.=&Apache::edit::text_arg('X:','x',$token,4).
337: &Apache::edit::text_arg('Y:','y',$token,4).
338: &Apache::edit::entercoords('x','y','attribute','width','height').' '.
339: &Apache::edit::text_arg('Value:','value',$token).
340: &Apache::edit::end_row();
341: $result.=&Apache::edit::end_table;
342: } elsif ($target eq 'modified') {
343: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
344: $safeeval,'x','y','value');
345: if ($constructtag) {
346: $result = &Apache::edit::rebuild_tag($token);
347: $result.=&Apache::edit::handle_insert();
348: }
1.4 albertel 349: }
1.47 albertel 350: return $result;
1.1 tsai 351: }
352:
353: sub end_location {
1.47 albertel 354: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
355: my @result;
356: if ($target eq 'edit') { @result=('','no') }
357: return @result;
1.1 tsai 358: }
359:
1.2 tsai 360: # <label>$var_abc</label>
1.1 tsai 361: sub start_label {
1.47 albertel 362: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
363: my $result='';
364: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
365: if ($target eq 'web' || $target eq 'tex' ||
366: $target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
1.57 albertel 367: &Apache::lonxml::startredirection;
1.47 albertel 368: } elsif ($target eq 'edit') {
369: $result.=&Apache::edit::tag_start($target,$token,"$type Label");
370: my $text=&Apache::lonxml::get_all_text("/label",$parser);
371: if ($type eq 'image') {
372: $result.=&Apache::edit::end_row().
373: &Apache::edit::start_spanning_row();
374: $result.=&Apache::edit::text_arg('Description:','description',
375: $token);
376: }
377: if ($type eq 'text') { $result.="Label Text:"; }
378: if ($type eq 'image') { $result.="Path to image: "; }
379: $result.=&Apache::edit::editline('',$text,'',50).
380: &Apache::edit::end_table();
381: } elsif ($target eq 'modified') {
382: $result = '<label>';
383: if ($type eq 'image') {
384: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
385: $safeeval,
386: 'description');
387: if ($constructtag) {
388: $result = &Apache::edit::rebuild_tag($token);
389: } else {
390: $result = $token->[4];
391: }
392: }
1.52 albertel 393: $result.=&Apache::edit::modifiedfield("/label",$parser);
1.26 albertel 394: }
1.47 albertel 395: return $result;
1.1 tsai 396: }
397:
398: sub end_label {
1.47 albertel 399: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
400: my @result;
1.57 albertel 401: if ($target eq 'edit') {
402: @result=('','no') ;
403: } elsif ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
404: $target eq 'answer' || $target eq 'analyze') {
405: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
406: my $ltext=&Apache::lonxml::endredirection;
407: if ($type eq 'image') {
408: &Apache::lonxml::debug("Turning $ltext, $Apache::lonxml::pwd[-1]");
409: $ltext=&Apache::imageresponse::clean_up_image($ltext);
410: # $ltext=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],
411: # $ltext);
412: &Apache::lonxml::debug("into $ltext");
413: my $description = &Apache::lonxml::get_param('description',
414: $parstack,$safeeval);
415: push(@Apache::randomlabel::description,$description);
1.61 albertel 416: } else {
417: $ltext=~s/[\r\n]*//gs
1.57 albertel 418: }
419: push(@Apache::randomlabel::label_arr,$ltext);
420: }
1.47 albertel 421: return @result;
1.1 tsai 422: }
423:
424: 1;
425: __END__
426:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>