Annotation of loncom/homework/imageresponse.pm, revision 1.28
1.12 albertel 1: # The LearningOnline Network with CAPA
1.14 albertel 2: # image click response style
3: #
1.28 ! albertel 4: # $Id: imageresponse.pm,v 1.27 2003/05/06 11:54:08 www 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");
139: if (defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
140: my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
141: my $grade="INCORRECT";
142: foreach my $area (@areas) {
143: &Apache::lonxml::debug("Area is $area for $name");
144: $area =~ m/([a-z]*):/;
145: &Apache::lonxml::debug("Area of type $1");
146: if ($1 eq 'rectangle') {
147: $grade=&grade_rectangle($area,$x,$y);
148: } else {
149: &Apache::lonxml::error("Unknown area style $area");
1.9 albertel 150: }
1.12 albertel 151: &Apache::lonxml::debug("Area said $grade");
152: if ($grade eq 'APPROX_ANS') { last; }
153: }
154: &Apache::lonxml::debug("Foil was $grade");
155: if ($grade eq 'INCORRECT') { $result= 'INCORRECT'; }
156: if (($grade eq 'APPROX_ANS') && ($result ne 'APPROX_ANS')) { $result=$grade; }
157: &Apache::lonxml::debug("Question is $result");
158: $temp++;
159: }
160: }
161: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}="$x:$y";
162: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}=$result;
163: return '';
1.3 albertel 164: }
165:
1.1 albertel 166: sub end_foilgroup {
1.12 albertel 167: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
168: my $result='';
169: my @whichopt;
1.24 sakharuk 170: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
1.12 albertel 171: my ($count,$max) = &getfoilcounts($parstack,$safeeval);
172: if ($count>$max) { $count=$max }
173: &Apache::lonxml::debug("Count is $count from $max");
174: @whichopt = &whichfoils($max);
1.28 ! albertel 175: if ($target eq 'web' || $target eq 'tex') {
! 176: $result=&displayfoils($target,@whichopt);
! 177: } elsif ($target eq 'grade') {
! 178: if ( defined $ENV{'form.submitted'}) { &gradefoils(@whichopt); }
! 179: }
1.26 albertel 180: } elsif ($target eq 'edit') {
181: $result=&Apache::edit::end_table();
1.12 albertel 182: }
183: return $result;
1.1 albertel 184: }
185:
186: sub start_conceptgroup {
1.12 albertel 187: $Apache::imageresponse::conceptgroup=1;
1.20 albertel 188: %Apache::response::conceptgroup=();
1.12 albertel 189: return '';
1.1 albertel 190: }
191:
192: sub end_conceptgroup {
1.12 albertel 193: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.26 albertel 194: $Apache::imageresponse::conceptgroup=0;
195: my $result;
1.24 sakharuk 196: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
1.12 albertel 197: if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
198: my @names = @{ $Apache::response::conceptgroup{'names'} };
1.22 albertel 199: my $pick=int(&Math::Random::random_uniform() * ($#names+1));
1.12 albertel 200: my $name=$names[$pick];
201: if (defined(@{ $Apache::response::conceptgroup{"$name.area"} })) {
202: push @{ $Apache::response::foilgroup{'names'} }, $name;
203: $Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
204: $Apache::response::foilgroup{"$name.image"} = $Apache::response::conceptgroup{"$name.image"};
205: push(@{ $Apache::response::foilgroup{"$name.area"} }, @{ $Apache::response::conceptgroup{"$name.area"} });
206: my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
207: $Apache::response::foilgroup{"$name.concept"} = $concept;
208: &Apache::lonxml::debug("Selecting $name in $concept");
209: }
1.9 albertel 210: }
1.26 albertel 211: } elsif ($target eq 'edit') {
212: $result=&Apache::edit::end_table();
1.12 albertel 213: }
1.26 albertel 214: return $result;
1.1 albertel 215: }
216:
1.12 albertel 217: $Apache::imageresponse::curname='';
1.1 albertel 218: sub start_foil {
1.12 albertel 219: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.24 sakharuk 220: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
1.12 albertel 221: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
222: if ($name eq '') { $name=$Apache::lonxml::curdepth; }
1.18 albertel 223: if ( $Apache::imageresponse::conceptgroup
224: && !&Apache::response::showallfoils()) {
1.12 albertel 225: push(@{ $Apache::response::conceptgroup{'names'} }, $name);
226: } else {
227: push(@{ $Apache::response::foilgroup{'names'} }, $name);
228: }
229: $Apache::imageresponse::curname=$name;
230: }
231: return '';
1.1 albertel 232: }
233:
234: sub end_foil {
1.26 albertel 235: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
236: my $result;
237: if ($target eq 'edit') {
238: $result=&Apache::edit::end_table();
239: }
240: return $result;
1.1 albertel 241: }
242:
243: sub start_text {
1.12 albertel 244: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.25 www 245: my $result='';
246: if ($target eq 'web' || $target eq 'tex') {
247: &Apache::lonxml::startredirection;
248: } elsif ($target eq 'edit') {
1.26 albertel 249: my $descr=&Apache::lonxml::get_all_text('/text',$parser);
1.25 www 250: $result=&Apache::edit::tag_start($target,$token,'Task Description').
251: &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
1.26 albertel 252: &Apache::edit::end_row();
1.25 www 253: } elsif ($target eq "modified") {
1.26 albertel 254: my $descr=&Apache::lonxml::get_all_text('/text',$parser);
1.25 www 255: $result=$token->[4].&Apache::edit::modifiedfield($token);
256: &Apache::lonxml::debug($result);
257: }
258: return $result;
1.1 albertel 259: }
260:
261: sub end_text {
1.12 albertel 262: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.26 albertel 263: my $result;
1.24 sakharuk 264: if ($target eq 'web' || $target eq 'tex') {
1.12 albertel 265: my $name = $Apache::imageresponse::curname;
1.18 albertel 266: if ( $Apache::imageresponse::conceptgroup
267: && !&Apache::response::showallfoils() ) {
1.12 albertel 268: $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
269: } else {
270: $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
1.7 albertel 271: }
1.26 albertel 272: } elsif ($target eq 'edit') {
273: $result=&Apache::edit::end_table();
1.12 albertel 274: }
1.26 albertel 275: return $result;
1.1 albertel 276: }
277:
278: sub start_image {
1.12 albertel 279: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.25 www 280: my $result='';
281: if ($target eq 'web' || $target eq 'tex') {
282: &Apache::lonxml::startredirection;
283: } elsif ($target eq 'edit') {
1.26 albertel 284: my $bgimg=&Apache::lonxml::get_all_text('/image',$parser);
1.27 www 285: $Apache::edit::bgimgsrc=$bgimg;
286: $Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
287:
1.25 www 288: $result=&Apache::edit::tag_start($target,$token,'Clickable Image').
289: &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
290: $result.=&Apache::edit::browse(undef,'textnode').' ';
291: $result.=&Apache::edit::search(undef,'textnode').
1.26 albertel 292: &Apache::edit::end_row();
1.25 www 293: } elsif ($target eq "modified") {
1.26 albertel 294: my $bgimg=&Apache::lonxml::get_all_text('/image',$parser);
1.25 www 295: $result=$token->[4].&Apache::edit::modifiedfield($token);
296: &Apache::lonxml::debug($result);
297: }
298: return $result;
1.1 albertel 299: }
300:
301: sub end_image {
1.26 albertel 302: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
303: my $result;
1.24 sakharuk 304: my $name = $Apache::imageresponse::curname;
1.12 albertel 305: if ($target eq 'web') {
306: my $image = &Apache::lonxml::endredirection;
307: &Apache::lonxml::debug("out is $image");
1.18 albertel 308: if ( $Apache::imageresponse::conceptgroup
309: && !&Apache::response::showallfoils()) {
1.12 albertel 310: $Apache::response::conceptgroup{"$name.image"} = $image;
311: } else {
312: $Apache::response::foilgroup{"$name.image"} = $image;
1.7 albertel 313: }
1.26 albertel 314: } elsif ($target eq 'edit') {
315: $result=&Apache::edit::end_table();
1.19 sakharuk 316: } elsif ($target eq 'tex') {
317: my $src = &Apache::lonxml::endredirection;
318: $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
319: my $width_param = '';
320: my $height_param = '';
321: my $scaling = .3;
322: my $image = Image::Magick->new;
323: my $current_figure = $image->Read($src);
324: $width_param = $image->Get('width') * $scaling;;
325: $height_param = $image->Get('height') * $scaling;;
326: undef $image;
327: my $epssrc = $src;
328: $epssrc =~ s/(\.gif|\.jpg)$/\.eps/i;
329: if (not -e $epssrc) {
330: my $localfile = $epssrc;
331: $localfile =~ s/.*(\/res)/$1/;
332: my $file;
333: my $path;
334: if ($localfile =~ m!(.*)/([^/]*)$!) {
335: $file = $2;
336: $path = $1.'/';
337: }
338: my $signal_eps = 0;
339: my @content_directory = &Apache::lonnet::dirlist($path);
340: for (my $iy=0;$iy<=$#content_directory;$iy++) {
341: my @tempo_array = split(/&/,$content_directory[$iy]);
342: $content_directory[$iy] = $tempo_array[0];
343: if ($file eq $tempo_array[0]) {
344: $signal_eps = 1;
345: last;
346: }
347: }
348: if ($signal_eps) {
349: my $eps_file = &Apache::lonnet::getfile($localfile);
350: } else {
351: $localfile = $src;
352: $localfile =~ s/.*(\/res)/$1/;
353: my $as = &Apache::lonnet::getfile($src);
354: }
355: }
356: my $file;
357: my $path;
358: if ($src =~ m!(.*)/([^/]*)$!) {
359: $file = $2;
360: $path = $1.'/';
361: }
362: my $newsrc = $src;
1.27 www 363: $newsrc =~ s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
364: $file=~s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
1.19 sakharuk 365: #do we have any specified size of the picture?
366: my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval);
367: my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval);
368: my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval);
369: if ($TeXwidth ne '') {
370: $width_param = $TeXwidth;
371: } elsif ($TeXheight ne '') {
372: $width_param = $TeXheight/$height_param*$width_param;
373: } elsif ($width ne '') {
374: $width_param = $width*$scaling;
375: }
376: #where can we find the picture?
377: if (-e $newsrc) {
378: if ($path) {
1.24 sakharuk 379: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
1.19 sakharuk 380: }
381: } else {
382: my $temp_file;
383: my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
384: $temp_file = Apache::File->new('>>'.$filename);
385: print $temp_file "$src\n";
1.24 sakharuk 386: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \graphicspath{{/home/httpd/prtspool/}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
1.19 sakharuk 387: }
1.24 sakharuk 388: }
1.26 albertel 389: return $result;
1.1 albertel 390: }
391:
392: sub start_rectangle {
1.12 albertel 393: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.25 www 394: my $result='';
395: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
396: &Apache::lonxml::startredirection;
397: } elsif ($target eq 'edit') {
1.26 albertel 398: my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser);
1.25 www 399: $result=&Apache::edit::tag_start($target,$token,'Rectangle').
400: &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
1.27 www 401: &Apache::edit::entercoordpair(undef,'textnode').
1.26 albertel 402: &Apache::edit::end_row();
1.25 www 403: } elsif ($target eq "modified") {
1.26 albertel 404: my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser);
1.25 www 405: $result=$token->[4].&Apache::edit::modifiedfield($token);
406: &Apache::lonxml::debug($result);
407: }
408: return $result;
1.1 albertel 409: }
410:
1.3 albertel 411: sub grade_rectangle {
1.12 albertel 412: my ($spec,$x,$y) = @_;
413: &Apache::lonxml::debug("Spec is $spec");
414: $spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/;
415: my $x1=$1;
416: my $y1=$2;
417: my $x2=$3;
418: my $y2=$4;
419: &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
420: if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
421: if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
1.17 albertel 422: if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
423: return 'APPROX_ANS';
424: }
1.12 albertel 425: return 'INCORRECT';
1.3 albertel 426: }
427:
1.1 albertel 428: sub end_rectangle {
1.12 albertel 429: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.26 albertel 430: my $result;
1.24 sakharuk 431: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
1.12 albertel 432: my $name = $Apache::imageresponse::curname;
433: my $area = &Apache::lonxml::endredirection;
434: &Apache::lonxml::debug("out is $area for $name");
1.18 albertel 435: if ( $Apache::imageresponse::conceptgroup
436: && !&Apache::response::showallfoils()) {
1.12 albertel 437: push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
438: } else {
439: push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
1.7 albertel 440: }
1.26 albertel 441: } elsif ($target eq 'edit') {
442: $result=&Apache::edit::end_table();
1.12 albertel 443: }
1.26 albertel 444: return $result;
1.1 albertel 445: }
446: 1;
447: __END__
448:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>