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