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