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