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