Annotation of loncom/homework/imageresponse.pm, revision 1.39
1.12 albertel 1: # The LearningOnline Network with CAPA
1.14 albertel 2: # image click response style
3: #
1.39 ! albertel 4: # $Id: imageresponse.pm,v 1.38 2003/10/27 20:04:34 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") {
1.39 ! albertel 268: $result=$token->[4].&Apache::edit::modifiedfield('/text',$parser);
1.36 albertel 269: }
270: return $result;
1.1 albertel 271: }
272:
273: sub end_text {
1.36 albertel 274: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
275: my $result;
1.37 albertel 276: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.36 albertel 277: my $name = $Apache::imageresponse::curname;
278: if ( $Apache::imageresponse::conceptgroup
279: && !&Apache::response::showallfoils() ) {
280: $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
281: } else {
282: $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
283: }
284: } elsif ($target eq 'edit') {
285: $result=&Apache::edit::end_table();
286: }
287: return $result;
1.1 albertel 288: }
289:
290: sub start_image {
1.36 albertel 291: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
292: my $result='';
1.37 albertel 293: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.36 albertel 294: &Apache::lonxml::startredirection;
295: } elsif ($target eq 'edit') {
296: my $bgimg=&Apache::lonxml::get_all_text('/image',$parser);
297: $Apache::edit::bgimgsrc=$bgimg;
298: $Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
299:
300: $result=&Apache::edit::tag_start($target,$token,'Clickable Image').
301: &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
302: $result.=&Apache::edit::browse(undef,'textnode').' ';
303: $result.=&Apache::edit::search(undef,'textnode').
304: &Apache::edit::end_row();
305: } elsif ($target eq "modified") {
1.39 ! albertel 306: $result=$token->[4].&Apache::edit::modifiedfield('/image',$parser);
1.36 albertel 307: }
308: return $result;
1.1 albertel 309: }
310:
311: sub end_image {
1.36 albertel 312: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
313: my $result;
314: my $name = $Apache::imageresponse::curname;
315: if ($target eq 'web') {
316: my $image = &Apache::lonxml::endredirection;
317: &Apache::lonxml::debug("original image is $image");
318: my $id=$Apache::inputtags::response['-1'];
319: my $temp=1;
320: my $x=$ENV{"form.HWVAL_$id:$temp.x"};
321: my $y=$ENV{"form.HWVAL_$id:$temp.y"};
322: if (defined ($x) && defined ($y)) {
323: &Apache::lonxml::debug("x and y defined as $x,$y");
324: my $currentImage = &Apache::randomlylabel::get_image('/home/httpd/html'.$image,1);
325: if (! defined($currentImage)) {
326: &Apache::lonnet::logthis('Unable to create image object for '.$image);
327: return '';
328: }
329: my $red;
330: if (!($red = $currentImage->colorResolve(255,0,0))) {
331: $red = $currentImage->colorClosestHWB(255,0,0);
332: }
333: my $length = 6;
334: $currentImage->line($x-$length,$y-$length,$x+$length,$y+$length,$red);
335: $currentImage->line($x-$length,$y+$length,$x+$length,$y-$length,$red);
336:
337: my ($nameWOext) = ($image =~ /^.*\/(.*)\..*$/);
338: &Apache::lonxml::debug("graph name $nameWOext");
339: my $webImageName = "/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_".
340: $nameWOext.'.png'; #needs to be more random or specific
341: my $newImageName = '/home/httpd'.$webImageName;
342:
343: my $imgfh = Apache::File->new('>'.$newImageName);
344: print $imgfh $currentImage->png;
345: $image = $webImageName;
346: }
347: &Apache::lonxml::debug("out image is $image");
1.37 albertel 348: if ( $Apache::imageresponse::conceptgroup
349: && !&Apache::response::showallfoils()) {
350: $Apache::response::conceptgroup{"$name.image"} = $image;
351: } else {
352: $Apache::response::foilgroup{"$name.image"} = $image;
353: }
354: } elsif ($target eq 'analyze') {
355: my $image = &Apache::lonxml::endredirection;
1.36 albertel 356: if ( $Apache::imageresponse::conceptgroup
357: && !&Apache::response::showallfoils()) {
358: $Apache::response::conceptgroup{"$name.image"} = $image;
359: } else {
360: $Apache::response::foilgroup{"$name.image"} = $image;
361: }
362: } elsif ($target eq 'edit') {
363: $result=&Apache::edit::end_table();
364: } elsif ($target eq 'tex') {
365: my $src = &Apache::lonxml::endredirection;
366: $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
367: my $width_param = '';
368: my $height_param = '';
369: my $scaling = .3;
370: my $image = Image::Magick->new;
371: my $current_figure = $image->Read($src);
372: $width_param = $image->Get('width') * $scaling;;
373: $height_param = $image->Get('height') * $scaling;;
374: undef $image;
375: my $epssrc = $src;
376: $epssrc =~ s/(\.gif|\.jpg)$/\.eps/i;
377: if (not -e $epssrc) {
378: my $localfile = $epssrc;
379: $localfile =~ s/.*(\/res)/$1/;
380: my $file;
381: my $path;
382: if ($localfile =~ m!(.*)/([^/]*)$!) {
383: $file = $2;
384: $path = $1.'/';
385: }
386: my $signal_eps = 0;
387: my @content_directory = &Apache::lonnet::dirlist($path);
388: for (my $iy=0;$iy<=$#content_directory;$iy++) {
389: my @tempo_array = split(/&/,$content_directory[$iy]);
390: $content_directory[$iy] = $tempo_array[0];
391: if ($file eq $tempo_array[0]) {
392: $signal_eps = 1;
393: last;
394: }
395: }
396: if ($signal_eps) {
397: my $eps_file = &Apache::lonnet::getfile($localfile);
398: } else {
399: $localfile = $src;
400: $localfile =~ s/.*(\/res)/$1/;
401: my $as = &Apache::lonnet::getfile($src);
402: }
403: }
1.19 sakharuk 404: my $file;
405: my $path;
1.36 albertel 406: if ($src =~ m!(.*)/([^/]*)$!) {
1.19 sakharuk 407: $file = $2;
408: $path = $1.'/';
1.36 albertel 409: }
410: my $newsrc = $src;
411: $newsrc =~ s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
412: $file=~s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
413: #do we have any specified size of the picture?
414: my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval);
415: my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval);
416: my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval);
417: if ($TeXwidth ne '') {
418: $width_param = $TeXwidth;
419: } elsif ($TeXheight ne '') {
420: $width_param = $TeXheight/$height_param*$width_param;
421: } elsif ($width ne '') {
422: $width_param = $width*$scaling;
423: }
424: #where can we find the picture?
425: if (-e $newsrc) {
426: if ($path) {
427: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
1.19 sakharuk 428: }
429: } else {
1.36 albertel 430: my $temp_file;
431: my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
432: $temp_file = Apache::File->new('>>'.$filename);
433: print $temp_file "$src\n";
434: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \graphicspath{{/home/httpd/prtspool/}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
1.19 sakharuk 435: }
1.36 albertel 436: }
437: return $result;
1.1 albertel 438: }
439:
440: sub start_rectangle {
1.36 albertel 441: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
442: my $result='';
1.38 albertel 443: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
444: $target eq 'analyze') {
1.36 albertel 445: &Apache::lonxml::startredirection;
446: } elsif ($target eq 'edit') {
447: my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser);
448: $result=&Apache::edit::tag_start($target,$token,'Rectangle').
449: &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
450: &Apache::edit::entercoordpair(undef,'textnode').
451: &Apache::edit::end_row();
452: } elsif ($target eq "modified") {
1.39 ! albertel 453: $result=$token->[4].&Apache::edit::modifiedfield('/rectangle',$parser);
1.36 albertel 454: }
455: return $result;
1.1 albertel 456: }
457:
1.3 albertel 458: sub grade_rectangle {
1.36 albertel 459: my ($spec,$x,$y) = @_;
460: &Apache::lonxml::debug("Spec is $spec");
461: $spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/;
462: my $x1=$1;
463: my $y1=$2;
464: my $x2=$3;
465: my $y2=$4;
466: &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
467: if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
468: if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
469: if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
470: return 'APPROX_ANS';
471: }
472: return 'INCORRECT';
1.3 albertel 473: }
474:
1.1 albertel 475: sub end_rectangle {
1.36 albertel 476: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
477: my $result;
1.38 albertel 478: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
479: $target eq 'analyze') {
1.36 albertel 480: my $name = $Apache::imageresponse::curname;
481: my $area = &Apache::lonxml::endredirection;
482: &Apache::lonxml::debug("out is $area for $name");
483: if ( $Apache::imageresponse::conceptgroup
484: && !&Apache::response::showallfoils()) {
485: push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
486: } else {
487: push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
488: }
489: } elsif ($target eq 'edit') {
490: $result=&Apache::edit::end_table();
491: }
492: return $result;
1.1 albertel 493: }
494: 1;
495: __END__
496:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>