Annotation of loncom/homework/imageresponse.pm, revision 1.38
1.12 albertel 1: # The LearningOnline Network with CAPA
1.14 albertel 2: # image click response style
3: #
1.38 ! albertel 4: # $Id: imageresponse.pm,v 1.37 2003/10/27 19:27:09 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;
1.38 ! albertel 178: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
! 179: $target eq 'analyze') {
1.36 albertel 180: my ($count,$max) = &getfoilcounts($parstack,$safeeval);
181: if ($count>$max) { $count=$max }
182: &Apache::lonxml::debug("Count is $count from $max");
183: @whichopt = &whichfoils($max);
184: if ($target eq 'web' || $target eq 'tex') {
185: $result=&displayfoils($target,@whichopt);
186: } elsif ($target eq 'grade') {
187: if ( defined $ENV{'form.submitted'}) { &gradefoils(@whichopt); }
1.37 albertel 188: } elsif ( $target eq 'analyze') {
189: &Apache::response::analyze_store_foilgroup(\@whichopt,
190: ['text','image','area']);
191: }
1.36 albertel 192: } elsif ($target eq 'edit') {
193: $result=&Apache::edit::end_table();
194: }
195: return $result;
1.1 albertel 196: }
197:
198: sub start_conceptgroup {
1.36 albertel 199: $Apache::imageresponse::conceptgroup=1;
200: %Apache::response::conceptgroup=();
201: return '';
1.1 albertel 202: }
203:
204: sub end_conceptgroup {
1.36 albertel 205: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
206: $Apache::imageresponse::conceptgroup=0;
207: my $result;
1.37 albertel 208: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
209: $target eq 'analyze') {
210: &Apache::response::pick_foil_for_concept($target,
211: ['area','text','image'],
212: \%Apache::hint::image,
213: $parstack,$safeeval);
1.36 albertel 214: } elsif ($target eq 'edit') {
215: $result=&Apache::edit::end_table();
216: }
217: return $result;
1.31 albertel 218: }
219:
220: sub insert_foil {
221: return '
222: <foil>
223: <image></image>
224: <text></text>
225: <rectangle></rectangle>
226: </foil>
227: ';
1.1 albertel 228: }
229:
1.12 albertel 230: $Apache::imageresponse::curname='';
1.1 albertel 231: sub start_foil {
1.36 albertel 232: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37 albertel 233: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
234: $target eq 'analyze') {
1.36 albertel 235: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
236: if ($name eq '') { $name=$Apache::lonxml::curdepth; }
237: if ( $Apache::imageresponse::conceptgroup
238: && !&Apache::response::showallfoils()) {
239: push(@{ $Apache::response::conceptgroup{'names'} }, $name);
240: } else {
241: push(@{ $Apache::response::foilgroup{'names'} }, $name);
242: }
243: $Apache::imageresponse::curname=$name;
244: }
245: return '';
1.1 albertel 246: }
247:
248: sub end_foil {
1.26 albertel 249: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
250: my $result;
251: if ($target eq 'edit') {
252: $result=&Apache::edit::end_table();
253: }
254: return $result;
1.1 albertel 255: }
256:
257: sub start_text {
1.36 albertel 258: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
259: my $result='';
1.37 albertel 260: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.36 albertel 261: &Apache::lonxml::startredirection;
262: } elsif ($target eq 'edit') {
263: my $descr=&Apache::lonxml::get_all_text('/text',$parser);
264: $result=&Apache::edit::tag_start($target,$token,'Task Description').
265: &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
266: &Apache::edit::end_row();
267: } elsif ($target eq "modified") {
268: my $descr=&Apache::lonxml::get_all_text('/text',$parser);
269: $result=$token->[4].&Apache::edit::modifiedfield($token);
270: &Apache::lonxml::debug($result);
271: }
272: return $result;
1.1 albertel 273: }
274:
275: sub end_text {
1.36 albertel 276: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
277: my $result;
1.37 albertel 278: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.36 albertel 279: my $name = $Apache::imageresponse::curname;
280: if ( $Apache::imageresponse::conceptgroup
281: && !&Apache::response::showallfoils() ) {
282: $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
283: } else {
284: $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
285: }
286: } elsif ($target eq 'edit') {
287: $result=&Apache::edit::end_table();
288: }
289: return $result;
1.1 albertel 290: }
291:
292: sub start_image {
1.36 albertel 293: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
294: my $result='';
1.37 albertel 295: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.36 albertel 296: &Apache::lonxml::startredirection;
297: } elsif ($target eq 'edit') {
298: my $bgimg=&Apache::lonxml::get_all_text('/image',$parser);
299: $Apache::edit::bgimgsrc=$bgimg;
300: $Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
301:
302: $result=&Apache::edit::tag_start($target,$token,'Clickable Image').
303: &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
304: $result.=&Apache::edit::browse(undef,'textnode').' ';
305: $result.=&Apache::edit::search(undef,'textnode').
306: &Apache::edit::end_row();
307: } elsif ($target eq "modified") {
308: my $bgimg=&Apache::lonxml::get_all_text('/image',$parser);
309: $result=$token->[4].&Apache::edit::modifiedfield($token);
310: &Apache::lonxml::debug($result);
311: }
312: return $result;
1.1 albertel 313: }
314:
315: sub end_image {
1.36 albertel 316: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
317: my $result;
318: my $name = $Apache::imageresponse::curname;
319: if ($target eq 'web') {
320: my $image = &Apache::lonxml::endredirection;
321: &Apache::lonxml::debug("original image is $image");
322: my $id=$Apache::inputtags::response['-1'];
323: my $temp=1;
324: my $x=$ENV{"form.HWVAL_$id:$temp.x"};
325: my $y=$ENV{"form.HWVAL_$id:$temp.y"};
326: if (defined ($x) && defined ($y)) {
327: &Apache::lonxml::debug("x and y defined as $x,$y");
328: my $currentImage = &Apache::randomlylabel::get_image('/home/httpd/html'.$image,1);
329: if (! defined($currentImage)) {
330: &Apache::lonnet::logthis('Unable to create image object for '.$image);
331: return '';
332: }
333: my $red;
334: if (!($red = $currentImage->colorResolve(255,0,0))) {
335: $red = $currentImage->colorClosestHWB(255,0,0);
336: }
337: my $length = 6;
338: $currentImage->line($x-$length,$y-$length,$x+$length,$y+$length,$red);
339: $currentImage->line($x-$length,$y+$length,$x+$length,$y-$length,$red);
340:
341: my ($nameWOext) = ($image =~ /^.*\/(.*)\..*$/);
342: &Apache::lonxml::debug("graph name $nameWOext");
343: my $webImageName = "/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_".
344: $nameWOext.'.png'; #needs to be more random or specific
345: my $newImageName = '/home/httpd'.$webImageName;
346:
347: my $imgfh = Apache::File->new('>'.$newImageName);
348: print $imgfh $currentImage->png;
349: $image = $webImageName;
350: }
351: &Apache::lonxml::debug("out image is $image");
1.37 albertel 352: if ( $Apache::imageresponse::conceptgroup
353: && !&Apache::response::showallfoils()) {
354: $Apache::response::conceptgroup{"$name.image"} = $image;
355: } else {
356: $Apache::response::foilgroup{"$name.image"} = $image;
357: }
358: } elsif ($target eq 'analyze') {
359: my $image = &Apache::lonxml::endredirection;
1.36 albertel 360: if ( $Apache::imageresponse::conceptgroup
361: && !&Apache::response::showallfoils()) {
362: $Apache::response::conceptgroup{"$name.image"} = $image;
363: } else {
364: $Apache::response::foilgroup{"$name.image"} = $image;
365: }
366: } elsif ($target eq 'edit') {
367: $result=&Apache::edit::end_table();
368: } elsif ($target eq 'tex') {
369: my $src = &Apache::lonxml::endredirection;
370: $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
371: my $width_param = '';
372: my $height_param = '';
373: my $scaling = .3;
374: my $image = Image::Magick->new;
375: my $current_figure = $image->Read($src);
376: $width_param = $image->Get('width') * $scaling;;
377: $height_param = $image->Get('height') * $scaling;;
378: undef $image;
379: my $epssrc = $src;
380: $epssrc =~ s/(\.gif|\.jpg)$/\.eps/i;
381: if (not -e $epssrc) {
382: my $localfile = $epssrc;
383: $localfile =~ s/.*(\/res)/$1/;
384: my $file;
385: my $path;
386: if ($localfile =~ m!(.*)/([^/]*)$!) {
387: $file = $2;
388: $path = $1.'/';
389: }
390: my $signal_eps = 0;
391: my @content_directory = &Apache::lonnet::dirlist($path);
392: for (my $iy=0;$iy<=$#content_directory;$iy++) {
393: my @tempo_array = split(/&/,$content_directory[$iy]);
394: $content_directory[$iy] = $tempo_array[0];
395: if ($file eq $tempo_array[0]) {
396: $signal_eps = 1;
397: last;
398: }
399: }
400: if ($signal_eps) {
401: my $eps_file = &Apache::lonnet::getfile($localfile);
402: } else {
403: $localfile = $src;
404: $localfile =~ s/.*(\/res)/$1/;
405: my $as = &Apache::lonnet::getfile($src);
406: }
407: }
1.19 sakharuk 408: my $file;
409: my $path;
1.36 albertel 410: if ($src =~ m!(.*)/([^/]*)$!) {
1.19 sakharuk 411: $file = $2;
412: $path = $1.'/';
1.36 albertel 413: }
414: my $newsrc = $src;
415: $newsrc =~ s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
416: $file=~s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
417: #do we have any specified size of the picture?
418: my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval);
419: my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval);
420: my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval);
421: if ($TeXwidth ne '') {
422: $width_param = $TeXwidth;
423: } elsif ($TeXheight ne '') {
424: $width_param = $TeXheight/$height_param*$width_param;
425: } elsif ($width ne '') {
426: $width_param = $width*$scaling;
427: }
428: #where can we find the picture?
429: if (-e $newsrc) {
430: if ($path) {
431: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
1.19 sakharuk 432: }
433: } else {
1.36 albertel 434: my $temp_file;
435: my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
436: $temp_file = Apache::File->new('>>'.$filename);
437: print $temp_file "$src\n";
438: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \graphicspath{{/home/httpd/prtspool/}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
1.19 sakharuk 439: }
1.36 albertel 440: }
441: return $result;
1.1 albertel 442: }
443:
444: sub start_rectangle {
1.36 albertel 445: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
446: my $result='';
1.38 ! albertel 447: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
! 448: $target eq 'analyze') {
1.36 albertel 449: &Apache::lonxml::startredirection;
450: } elsif ($target eq 'edit') {
451: my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser);
452: $result=&Apache::edit::tag_start($target,$token,'Rectangle').
453: &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
454: &Apache::edit::entercoordpair(undef,'textnode').
455: &Apache::edit::end_row();
456: } elsif ($target eq "modified") {
457: my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser);
458: $result=$token->[4].&Apache::edit::modifiedfield($token);
459: &Apache::lonxml::debug($result);
460: }
461: return $result;
1.1 albertel 462: }
463:
1.3 albertel 464: sub grade_rectangle {
1.36 albertel 465: my ($spec,$x,$y) = @_;
466: &Apache::lonxml::debug("Spec is $spec");
467: $spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/;
468: my $x1=$1;
469: my $y1=$2;
470: my $x2=$3;
471: my $y2=$4;
472: &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
473: if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
474: if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
475: if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
476: return 'APPROX_ANS';
477: }
478: return 'INCORRECT';
1.3 albertel 479: }
480:
1.1 albertel 481: sub end_rectangle {
1.36 albertel 482: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
483: my $result;
1.38 ! albertel 484: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
! 485: $target eq 'analyze') {
1.36 albertel 486: my $name = $Apache::imageresponse::curname;
487: my $area = &Apache::lonxml::endredirection;
488: &Apache::lonxml::debug("out is $area for $name");
489: if ( $Apache::imageresponse::conceptgroup
490: && !&Apache::response::showallfoils()) {
491: push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
492: } else {
493: push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
494: }
495: } elsif ($target eq 'edit') {
496: $result=&Apache::edit::end_table();
497: }
498: return $result;
1.1 albertel 499: }
500: 1;
501: __END__
502:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>