File:
[LON-CAPA] /
loncom /
homework /
imageresponse.pm
Revision
1.31:
download - view:
text,
annotated -
select for diffs
Mon Jun 9 22:42:49 2003 UTC (21 years, 2 months ago) by
albertel
Branches:
MAIN
CVS tags:
version_1_0_2,
version_1_0_1,
version_1_0_0,
version_0_99_5,
version_0_99_4,
version_0_99_3,
version_0_99_2,
HEAD
- takes care of BUG#1676, image click fully editable now
1: # The LearningOnline Network with CAPA
2: # image click response style
3: #
4: # $Id: imageresponse.pm,v 1.31 2003/06/09 22:42:49 albertel Exp $
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: #
28:
29: #FIXME LATER assumes multiple possible submissions but only one is possible
30: #currently
31:
32: package Apache::imageresponse;
33: use strict;
34: use Image::Magick;
35:
36: BEGIN {
37: &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
38: }
39:
40: sub start_imageresponse {
41: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
42: my $result;
43: #when in a radiobutton response use these
44: &Apache::lonxml::register('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
45: push (@Apache::lonxml::namespace,'imageresponse');
46: my $id = &Apache::response::start_response($parstack,$safeeval);
47: if ($target eq 'meta') {
48: $result=&Apache::response::meta_package_write('imageresponse');
49: }
50: return $result;
51: }
52:
53: sub end_imageresponse {
54: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
55: &Apache::response::end_response;
56: pop @Apache::lonxml::namespace;
57: &Apache::lonxml::deregister('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
58: my $result;
59: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
60: return $result;
61: }
62:
63: %Apache::response::foilgroup=();
64: sub start_foilgroup {
65: %Apache::response::foilgroup=();
66: $Apache::imageresponse::conceptgroup=0;
67: &Apache::response::setrandomnumber();
68: return '';
69: }
70:
71: sub getfoilcounts {
72: my ($parstack,$safeeval)=@_;
73:
74: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
75: # +1 since instructors will count from 1
76: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
77: if (&Apache::response::showallfoils()) { $max=$count; }
78: return ($count,$max);
79: }
80:
81: sub whichfoils {
82: my ($max)=@_;
83: if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
84: my @names = @{ $Apache::response::foilgroup{'names'} };
85: my @whichopt =();
86: while ((($#whichopt+1) < $max) && ($#names > -1)) {
87: &Apache::lonxml::debug("Have $#whichopt max is $max");
88: my $aopt;
89: if (&Apache::response::showallfoils()) {
90: $aopt=0;
91: } else {
92: $aopt=int(&Math::Random::random_uniform() * ($#names+1));
93: }
94: &Apache::lonxml::debug("From $#names elms, picking $aopt");
95: $aopt=splice(@names,$aopt,1);
96: &Apache::lonxml::debug("Picked $aopt");
97: push (@whichopt,$aopt);
98: }
99: return @whichopt;
100: }
101:
102: sub displayfoils {
103: my ($target,@whichopt) = @_;
104: my $result ='';
105: my $name;
106: my $temp=1;
107: foreach $name (@whichopt) {
108: $result.=$Apache::response::foilgroup{"$name.text"};
109: &Apache::lonxml::debug("Text is $result");
110: if ($target eq 'tex') {$result.="\\vskip 0 mm \n";} else {$result.="<br />\n";}
111: my $image=$Apache::response::foilgroup{"$name.image"};
112: &Apache::lonxml::debug("image is $image");
113: if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
114: if ($target eq 'tex') {
115: $result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
116: } else {
117: $result.="<img src=\"$image\"/> <br />\n";
118: }
119: } else {
120: if ($target eq 'tex') {
121: $result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
122: } else {
123: $result.="<input type=\"image\" name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\" src=\"$image\"/> <br />\n";
124: }
125: }
126: $temp++;
127: }
128: return $result;
129: }
130:
131: sub gradefoils {
132: my (@whichopt) = @_;
133: my $x;
134: my $y;
135: my $result;
136: my $id=$Apache::inputtags::response['-1'];
137: my $temp=1;
138: foreach my $name (@whichopt) {
139: $x=$ENV{"form.HWVAL_$id:$temp.x"};
140: $y=$ENV{"form.HWVAL_$id:$temp.y"};
141: &Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
142: if (defined($x) && defined($y) &&
143: defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
144: my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
145: my $grade="INCORRECT";
146: foreach my $area (@areas) {
147: &Apache::lonxml::debug("Area is $area for $name");
148: $area =~ m/([a-z]*):/;
149: &Apache::lonxml::debug("Area of type $1");
150: if ($1 eq 'rectangle') {
151: $grade=&grade_rectangle($area,$x,$y);
152: } else {
153: &Apache::lonxml::error("Unknown area style $area");
154: }
155: &Apache::lonxml::debug("Area said $grade");
156: if ($grade eq 'APPROX_ANS') { last; }
157: }
158: &Apache::lonxml::debug("Foil was $grade");
159: if ($grade eq 'INCORRECT') { $result= 'INCORRECT'; }
160: if (($grade eq 'APPROX_ANS') && ($result ne 'APPROX_ANS')) { $result=$grade; }
161: &Apache::lonxml::debug("Question is $result");
162: $temp++;
163: }
164: }
165: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}="$x:$y";
166: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}=$result;
167: return '';
168: }
169:
170: sub end_foilgroup {
171: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
172: my $result='';
173: my @whichopt;
174: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
175: my ($count,$max) = &getfoilcounts($parstack,$safeeval);
176: if ($count>$max) { $count=$max }
177: &Apache::lonxml::debug("Count is $count from $max");
178: @whichopt = &whichfoils($max);
179: if ($target eq 'web' || $target eq 'tex') {
180: $result=&displayfoils($target,@whichopt);
181: } elsif ($target eq 'grade') {
182: if ( defined $ENV{'form.submitted'}) { &gradefoils(@whichopt); }
183: }
184: } elsif ($target eq 'edit') {
185: $result=&Apache::edit::end_table();
186: }
187: return $result;
188: }
189:
190: sub start_conceptgroup {
191: $Apache::imageresponse::conceptgroup=1;
192: %Apache::response::conceptgroup=();
193: return '';
194: }
195:
196: sub end_conceptgroup {
197: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
198: $Apache::imageresponse::conceptgroup=0;
199: my $result;
200: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
201: if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
202: my @names = @{ $Apache::response::conceptgroup{'names'} };
203: my $pick=int(&Math::Random::random_uniform() * ($#names+1));
204: my $name=$names[$pick];
205: if (defined(@{ $Apache::response::conceptgroup{"$name.area"} })) {
206: push @{ $Apache::response::foilgroup{'names'} }, $name;
207: $Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
208: $Apache::response::foilgroup{"$name.image"} = $Apache::response::conceptgroup{"$name.image"};
209: push(@{ $Apache::response::foilgroup{"$name.area"} }, @{ $Apache::response::conceptgroup{"$name.area"} });
210: my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
211: $Apache::response::foilgroup{"$name.concept"} = $concept;
212: &Apache::lonxml::debug("Selecting $name in $concept");
213: }
214: }
215: } elsif ($target eq 'edit') {
216: $result=&Apache::edit::end_table();
217: }
218: return $result;
219: }
220:
221: sub insert_foil {
222: return '
223: <foil>
224: <image></image>
225: <text></text>
226: <rectangle></rectangle>
227: </foil>
228: ';
229: }
230:
231: $Apache::imageresponse::curname='';
232: sub start_foil {
233: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
234: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
235: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
236: if ($name eq '') { $name=$Apache::lonxml::curdepth; }
237: if ( $Apache::imageresponse::conceptgroup
238: && !&Apache::response::showallfoils()) {
239: push(@{ $Apache::response::conceptgroup{'names'} }, $name);
240: } else {
241: push(@{ $Apache::response::foilgroup{'names'} }, $name);
242: }
243: $Apache::imageresponse::curname=$name;
244: }
245: return '';
246: }
247:
248: sub end_foil {
249: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
250: my $result;
251: if ($target eq 'edit') {
252: $result=&Apache::edit::end_table();
253: }
254: return $result;
255: }
256:
257: sub start_text {
258: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
259: my $result='';
260: if ($target eq 'web' || $target eq 'tex') {
261: &Apache::lonxml::startredirection;
262: } elsif ($target eq 'edit') {
263: my $descr=&Apache::lonxml::get_all_text('/text',$parser);
264: $result=&Apache::edit::tag_start($target,$token,'Task Description').
265: &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
266: &Apache::edit::end_row();
267: } elsif ($target eq "modified") {
268: my $descr=&Apache::lonxml::get_all_text('/text',$parser);
269: $result=$token->[4].&Apache::edit::modifiedfield($token);
270: &Apache::lonxml::debug($result);
271: }
272: return $result;
273: }
274:
275: sub end_text {
276: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
277: my $result;
278: if ($target eq 'web' || $target eq 'tex') {
279: my $name = $Apache::imageresponse::curname;
280: if ( $Apache::imageresponse::conceptgroup
281: && !&Apache::response::showallfoils() ) {
282: $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
283: } else {
284: $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
285: }
286: } elsif ($target eq 'edit') {
287: $result=&Apache::edit::end_table();
288: }
289: return $result;
290: }
291:
292: sub start_image {
293: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
294: my $result='';
295: if ($target eq 'web' || $target eq 'tex') {
296: &Apache::lonxml::startredirection;
297: } elsif ($target eq 'edit') {
298: my $bgimg=&Apache::lonxml::get_all_text('/image',$parser);
299: $Apache::edit::bgimgsrc=$bgimg;
300: $Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
301:
302: $result=&Apache::edit::tag_start($target,$token,'Clickable Image').
303: &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
304: $result.=&Apache::edit::browse(undef,'textnode').' ';
305: $result.=&Apache::edit::search(undef,'textnode').
306: &Apache::edit::end_row();
307: } elsif ($target eq "modified") {
308: my $bgimg=&Apache::lonxml::get_all_text('/image',$parser);
309: $result=$token->[4].&Apache::edit::modifiedfield($token);
310: &Apache::lonxml::debug($result);
311: }
312: return $result;
313: }
314:
315: sub end_image {
316: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
317: my $result;
318: my $name = $Apache::imageresponse::curname;
319: if ($target eq 'web') {
320: my $image = &Apache::lonxml::endredirection;
321: &Apache::lonxml::debug("out is $image");
322: if ( $Apache::imageresponse::conceptgroup
323: && !&Apache::response::showallfoils()) {
324: $Apache::response::conceptgroup{"$name.image"} = $image;
325: } else {
326: $Apache::response::foilgroup{"$name.image"} = $image;
327: }
328: } elsif ($target eq 'edit') {
329: $result=&Apache::edit::end_table();
330: } elsif ($target eq 'tex') {
331: my $src = &Apache::lonxml::endredirection;
332: $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
333: my $width_param = '';
334: my $height_param = '';
335: my $scaling = .3;
336: my $image = Image::Magick->new;
337: my $current_figure = $image->Read($src);
338: $width_param = $image->Get('width') * $scaling;;
339: $height_param = $image->Get('height') * $scaling;;
340: undef $image;
341: my $epssrc = $src;
342: $epssrc =~ s/(\.gif|\.jpg)$/\.eps/i;
343: if (not -e $epssrc) {
344: my $localfile = $epssrc;
345: $localfile =~ s/.*(\/res)/$1/;
346: my $file;
347: my $path;
348: if ($localfile =~ m!(.*)/([^/]*)$!) {
349: $file = $2;
350: $path = $1.'/';
351: }
352: my $signal_eps = 0;
353: my @content_directory = &Apache::lonnet::dirlist($path);
354: for (my $iy=0;$iy<=$#content_directory;$iy++) {
355: my @tempo_array = split(/&/,$content_directory[$iy]);
356: $content_directory[$iy] = $tempo_array[0];
357: if ($file eq $tempo_array[0]) {
358: $signal_eps = 1;
359: last;
360: }
361: }
362: if ($signal_eps) {
363: my $eps_file = &Apache::lonnet::getfile($localfile);
364: } else {
365: $localfile = $src;
366: $localfile =~ s/.*(\/res)/$1/;
367: my $as = &Apache::lonnet::getfile($src);
368: }
369: }
370: my $file;
371: my $path;
372: if ($src =~ m!(.*)/([^/]*)$!) {
373: $file = $2;
374: $path = $1.'/';
375: }
376: my $newsrc = $src;
377: $newsrc =~ s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
378: $file=~s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
379: #do we have any specified size of the picture?
380: my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval);
381: my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval);
382: my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval);
383: if ($TeXwidth ne '') {
384: $width_param = $TeXwidth;
385: } elsif ($TeXheight ne '') {
386: $width_param = $TeXheight/$height_param*$width_param;
387: } elsif ($width ne '') {
388: $width_param = $width*$scaling;
389: }
390: #where can we find the picture?
391: if (-e $newsrc) {
392: if ($path) {
393: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
394: }
395: } else {
396: my $temp_file;
397: my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
398: $temp_file = Apache::File->new('>>'.$filename);
399: print $temp_file "$src\n";
400: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \graphicspath{{/home/httpd/prtspool/}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
401: }
402: }
403: return $result;
404: }
405:
406: sub start_rectangle {
407: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
408: my $result='';
409: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
410: &Apache::lonxml::startredirection;
411: } elsif ($target eq 'edit') {
412: my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser);
413: $result=&Apache::edit::tag_start($target,$token,'Rectangle').
414: &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
415: &Apache::edit::entercoordpair(undef,'textnode').
416: &Apache::edit::end_row();
417: } elsif ($target eq "modified") {
418: my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser);
419: $result=$token->[4].&Apache::edit::modifiedfield($token);
420: &Apache::lonxml::debug($result);
421: }
422: return $result;
423: }
424:
425: sub grade_rectangle {
426: my ($spec,$x,$y) = @_;
427: &Apache::lonxml::debug("Spec is $spec");
428: $spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/;
429: my $x1=$1;
430: my $y1=$2;
431: my $x2=$3;
432: my $y2=$4;
433: &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
434: if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
435: if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
436: if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
437: return 'APPROX_ANS';
438: }
439: return 'INCORRECT';
440: }
441:
442: sub end_rectangle {
443: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
444: my $result;
445: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
446: my $name = $Apache::imageresponse::curname;
447: my $area = &Apache::lonxml::endredirection;
448: &Apache::lonxml::debug("out is $area for $name");
449: if ( $Apache::imageresponse::conceptgroup
450: && !&Apache::response::showallfoils()) {
451: push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
452: } else {
453: push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
454: }
455: } elsif ($target eq 'edit') {
456: $result=&Apache::edit::end_table();
457: }
458: return $result;
459: }
460: 1;
461: __END__
462:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>