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