Annotation of loncom/homework/imageresponse.pm, revision 1.41
1.12 albertel 1: # The LearningOnline Network with CAPA
1.14 albertel 2: # image click response style
3: #
1.41 ! albertel 4: # $Id: imageresponse.pm,v 1.40 2003/11/10 23:29:27 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;
32: use strict;
1.21 harris41 33: use Image::Magick;
1.40 albertel 34: use Apache::randomlylabel;
35: use Apache::Constants qw(:common :http);
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:
1.40 albertel 106: sub prep_image {
107: my ($image)=@_;
108: my $part=$Apache::inputtags::part;
1.41 ! albertel 109: my $respid=$Apache::inputtags::response['-1'];
! 110: my $id=&Apache::loncommon::get_cgi_id();
! 111: my %x;
! 112: $x{"cgi.$id.BGIMG"}=$image;
! 113: my ($x,$y)=split(/:/,$Apache::lonhomework::history{"resource.$part.$respid.submission"});
1.40 albertel 114: #draws 2 xs on the image at the clicked location
115: #one in white and then one in red on top of the one in white
116: if (defined($x) && defined($y)) {
1.41 ! albertel 117: $x{"cgi.$id.LINECOUNT"}=4;
1.40 albertel 118: my $length = 6;
119: my $width = 1;
120: my $extrawidth = 2;
1.41 ! albertel 121: $x{"cgi.$id.LINE0"}=
! 122: join(':',(($x-$length),($y-$length),($x+$length),($y+$length),
! 123: "FFFFFF",($width+$extrawidth)));
! 124: $x{"cgi.$id.LINE1"}=
! 125: join(':',(($x-$length),($y+$length),($x+$length),($y-$length),
! 126: "FFFFFF",($width+$extrawidth)));
! 127: $x{"cgi.$id.LINE2"}=
! 128: join(':',(($x-$length),($y-$length),($x+$length),($y+$length),
! 129: "FF0000",($width)));
! 130: $x{"cgi.$id.LINE3"}=
! 131: join(':',(($x-$length),($y+$length),($x+$length),($y-$length),
! 132: "FF0000",($width)));
1.40 albertel 133: }
1.41 ! albertel 134: &Apache::lonnet::appenv(%x);
! 135: return $id;
1.40 albertel 136: }
137:
1.2 albertel 138: sub displayfoils {
1.36 albertel 139: my ($target,@whichopt) = @_;
140: my $result ='';
141: my $name;
142: my $temp=1;
143: foreach $name (@whichopt) {
144: $result.=$Apache::response::foilgroup{"$name.text"};
145: &Apache::lonxml::debug("Text is $result");
146: if ($target eq 'tex') {$result.="\\vskip 0 mm \n";} else {$result.="<br />\n";}
147: my $image=$Apache::response::foilgroup{"$name.image"};
148: &Apache::lonxml::debug("image is $image");
1.40 albertel 149: if ( $target eq 'web' && $image !~ /^http:/ ) {
150: $image=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$image);
151: if (&Apache::lonnet::repcopy($image) ne OK) {
152: $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
153: }
154: }
155: &Apache::lonxml::debug("image is $image");
1.36 albertel 156: if ( &Apache::response::show_answer() ) {
157: if ($target eq 'tex') {
158: $result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
159: } else {
1.40 albertel 160: my $token=&prep_image($image);
161: $result.="<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
1.36 albertel 162: }
163: } else {
164: if ($target eq 'tex') {
165: $result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
166: } else {
1.40 albertel 167: my $id=$Apache::inputtags::response['-1'];
168: my $token=&prep_image($image);
169: my $temp=1;
170: $result.="<input type=\"image\" name=\"HWVAL_$id:$temp\" ".
171: "src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
1.36 albertel 172: }
173: }
174: $temp++;
175: }
176: return $result;
1.2 albertel 177: }
178:
1.3 albertel 179: sub gradefoils {
1.36 albertel 180: my (@whichopt) = @_;
181: my $x;
182: my $y;
183: my $result;
184: my $id=$Apache::inputtags::response['-1'];
185: my $temp=1;
186: foreach my $name (@whichopt) {
187: $x=$ENV{"form.HWVAL_$id:$temp.x"};
188: $y=$ENV{"form.HWVAL_$id:$temp.y"};
189: &Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
190: if (defined($x) && defined($y) &&
191: defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
192: my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
193: my $grade="INCORRECT";
194: foreach my $area (@areas) {
195: &Apache::lonxml::debug("Area is $area for $name");
196: $area =~ m/([a-z]*):/;
197: &Apache::lonxml::debug("Area of type $1");
198: if ($1 eq 'rectangle') {
199: $grade=&grade_rectangle($area,$x,$y);
200: } else {
201: &Apache::lonxml::error("Unknown area style $area");
202: }
203: &Apache::lonxml::debug("Area said $grade");
204: if ($grade eq 'APPROX_ANS') { last; }
205: }
206: &Apache::lonxml::debug("Foil was $grade");
207: if ($grade eq 'INCORRECT') { $result= 'INCORRECT'; }
208: if (($grade eq 'APPROX_ANS') && ($result ne 'APPROX_ANS')) { $result=$grade; }
209: &Apache::lonxml::debug("Question is $result");
210: $temp++;
1.9 albertel 211: }
1.36 albertel 212: }
213: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}="$x:$y";
214: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}=$result;
215: return '';
1.3 albertel 216: }
217:
1.1 albertel 218: sub end_foilgroup {
1.36 albertel 219: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
220: my $result='';
221: my @whichopt;
1.38 albertel 222: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
223: $target eq 'analyze') {
1.36 albertel 224: my ($count,$max) = &getfoilcounts($parstack,$safeeval);
225: if ($count>$max) { $count=$max }
226: &Apache::lonxml::debug("Count is $count from $max");
227: @whichopt = &whichfoils($max);
228: if ($target eq 'web' || $target eq 'tex') {
229: $result=&displayfoils($target,@whichopt);
230: } elsif ($target eq 'grade') {
231: if ( defined $ENV{'form.submitted'}) { &gradefoils(@whichopt); }
1.37 albertel 232: } elsif ( $target eq 'analyze') {
233: &Apache::response::analyze_store_foilgroup(\@whichopt,
234: ['text','image','area']);
235: }
1.36 albertel 236: } elsif ($target eq 'edit') {
237: $result=&Apache::edit::end_table();
238: }
239: return $result;
1.1 albertel 240: }
241:
242: sub start_conceptgroup {
1.36 albertel 243: $Apache::imageresponse::conceptgroup=1;
244: %Apache::response::conceptgroup=();
245: return '';
1.1 albertel 246: }
247:
248: sub end_conceptgroup {
1.36 albertel 249: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
250: $Apache::imageresponse::conceptgroup=0;
251: my $result;
1.37 albertel 252: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
253: $target eq 'analyze') {
254: &Apache::response::pick_foil_for_concept($target,
255: ['area','text','image'],
256: \%Apache::hint::image,
257: $parstack,$safeeval);
1.36 albertel 258: } elsif ($target eq 'edit') {
259: $result=&Apache::edit::end_table();
260: }
261: return $result;
1.31 albertel 262: }
263:
264: sub insert_foil {
265: return '
266: <foil>
267: <image></image>
268: <text></text>
269: <rectangle></rectangle>
270: </foil>
271: ';
1.1 albertel 272: }
273:
1.12 albertel 274: $Apache::imageresponse::curname='';
1.1 albertel 275: sub start_foil {
1.36 albertel 276: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37 albertel 277: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
278: $target eq 'analyze') {
1.36 albertel 279: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
280: if ($name eq '') { $name=$Apache::lonxml::curdepth; }
281: if ( $Apache::imageresponse::conceptgroup
282: && !&Apache::response::showallfoils()) {
283: push(@{ $Apache::response::conceptgroup{'names'} }, $name);
284: } else {
285: push(@{ $Apache::response::foilgroup{'names'} }, $name);
286: }
287: $Apache::imageresponse::curname=$name;
288: }
289: return '';
1.1 albertel 290: }
291:
292: sub end_foil {
1.26 albertel 293: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
294: my $result;
295: if ($target eq 'edit') {
296: $result=&Apache::edit::end_table();
297: }
298: return $result;
1.1 albertel 299: }
300:
301: sub start_text {
1.36 albertel 302: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
303: my $result='';
1.37 albertel 304: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.36 albertel 305: &Apache::lonxml::startredirection;
306: } elsif ($target eq 'edit') {
307: my $descr=&Apache::lonxml::get_all_text('/text',$parser);
308: $result=&Apache::edit::tag_start($target,$token,'Task Description').
309: &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
310: &Apache::edit::end_row();
311: } elsif ($target eq "modified") {
1.39 albertel 312: $result=$token->[4].&Apache::edit::modifiedfield('/text',$parser);
1.36 albertel 313: }
314: return $result;
1.1 albertel 315: }
316:
317: sub end_text {
1.36 albertel 318: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
319: my $result;
1.37 albertel 320: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.36 albertel 321: my $name = $Apache::imageresponse::curname;
322: if ( $Apache::imageresponse::conceptgroup
323: && !&Apache::response::showallfoils() ) {
324: $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
325: } else {
326: $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
327: }
328: } elsif ($target eq 'edit') {
329: $result=&Apache::edit::end_table();
330: }
331: return $result;
1.1 albertel 332: }
333:
334: sub start_image {
1.36 albertel 335: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
336: my $result='';
1.37 albertel 337: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.36 albertel 338: &Apache::lonxml::startredirection;
339: } elsif ($target eq 'edit') {
340: my $bgimg=&Apache::lonxml::get_all_text('/image',$parser);
341: $Apache::edit::bgimgsrc=$bgimg;
342: $Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
343:
344: $result=&Apache::edit::tag_start($target,$token,'Clickable Image').
345: &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
346: $result.=&Apache::edit::browse(undef,'textnode').' ';
347: $result.=&Apache::edit::search(undef,'textnode').
348: &Apache::edit::end_row();
349: } elsif ($target eq "modified") {
1.39 albertel 350: $result=$token->[4].&Apache::edit::modifiedfield('/image',$parser);
1.36 albertel 351: }
352: return $result;
1.1 albertel 353: }
354:
355: sub end_image {
1.36 albertel 356: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
357: my $result;
358: my $name = $Apache::imageresponse::curname;
359: if ($target eq 'web') {
360: my $image = &Apache::lonxml::endredirection;
361: &Apache::lonxml::debug("original image is $image");
1.37 albertel 362: if ( $Apache::imageresponse::conceptgroup
363: && !&Apache::response::showallfoils()) {
364: $Apache::response::conceptgroup{"$name.image"} = $image;
365: } else {
366: $Apache::response::foilgroup{"$name.image"} = $image;
367: }
368: } elsif ($target eq 'analyze') {
369: my $image = &Apache::lonxml::endredirection;
1.36 albertel 370: if ( $Apache::imageresponse::conceptgroup
371: && !&Apache::response::showallfoils()) {
372: $Apache::response::conceptgroup{"$name.image"} = $image;
373: } else {
374: $Apache::response::foilgroup{"$name.image"} = $image;
375: }
376: } elsif ($target eq 'edit') {
377: $result=&Apache::edit::end_table();
378: } elsif ($target eq 'tex') {
379: my $src = &Apache::lonxml::endredirection;
380: $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
381: my $width_param = '';
382: my $height_param = '';
383: my $scaling = .3;
384: my $image = Image::Magick->new;
385: my $current_figure = $image->Read($src);
386: $width_param = $image->Get('width') * $scaling;;
387: $height_param = $image->Get('height') * $scaling;;
388: undef $image;
389: my $epssrc = $src;
390: $epssrc =~ s/(\.gif|\.jpg)$/\.eps/i;
391: if (not -e $epssrc) {
392: my $localfile = $epssrc;
393: $localfile =~ s/.*(\/res)/$1/;
394: my $file;
395: my $path;
396: if ($localfile =~ m!(.*)/([^/]*)$!) {
397: $file = $2;
398: $path = $1.'/';
399: }
400: my $signal_eps = 0;
401: my @content_directory = &Apache::lonnet::dirlist($path);
402: for (my $iy=0;$iy<=$#content_directory;$iy++) {
403: my @tempo_array = split(/&/,$content_directory[$iy]);
404: $content_directory[$iy] = $tempo_array[0];
405: if ($file eq $tempo_array[0]) {
406: $signal_eps = 1;
407: last;
408: }
409: }
410: if ($signal_eps) {
411: my $eps_file = &Apache::lonnet::getfile($localfile);
412: } else {
413: $localfile = $src;
414: $localfile =~ s/.*(\/res)/$1/;
415: my $as = &Apache::lonnet::getfile($src);
416: }
417: }
1.19 sakharuk 418: my $file;
419: my $path;
1.36 albertel 420: if ($src =~ m!(.*)/([^/]*)$!) {
1.19 sakharuk 421: $file = $2;
422: $path = $1.'/';
1.36 albertel 423: }
424: my $newsrc = $src;
425: $newsrc =~ s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
426: $file=~s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
427: #do we have any specified size of the picture?
428: my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval);
429: my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval);
430: my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval);
431: if ($TeXwidth ne '') {
432: $width_param = $TeXwidth;
433: } elsif ($TeXheight ne '') {
434: $width_param = $TeXheight/$height_param*$width_param;
435: } elsif ($width ne '') {
436: $width_param = $width*$scaling;
437: }
438: #where can we find the picture?
439: if (-e $newsrc) {
440: if ($path) {
441: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
1.19 sakharuk 442: }
443: } else {
1.36 albertel 444: my $temp_file;
445: my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
446: $temp_file = Apache::File->new('>>'.$filename);
447: print $temp_file "$src\n";
448: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \graphicspath{{/home/httpd/prtspool/}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
1.19 sakharuk 449: }
1.36 albertel 450: }
451: return $result;
1.1 albertel 452: }
453:
454: sub start_rectangle {
1.36 albertel 455: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
456: my $result='';
1.38 albertel 457: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
458: $target eq 'analyze') {
1.36 albertel 459: &Apache::lonxml::startredirection;
460: } elsif ($target eq 'edit') {
461: my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser);
462: $result=&Apache::edit::tag_start($target,$token,'Rectangle').
463: &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
464: &Apache::edit::entercoordpair(undef,'textnode').
465: &Apache::edit::end_row();
466: } elsif ($target eq "modified") {
1.39 albertel 467: $result=$token->[4].&Apache::edit::modifiedfield('/rectangle',$parser);
1.36 albertel 468: }
469: return $result;
1.1 albertel 470: }
471:
1.3 albertel 472: sub grade_rectangle {
1.36 albertel 473: my ($spec,$x,$y) = @_;
474: &Apache::lonxml::debug("Spec is $spec");
475: $spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/;
476: my $x1=$1;
477: my $y1=$2;
478: my $x2=$3;
479: my $y2=$4;
480: &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
481: if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
482: if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
483: if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
484: return 'APPROX_ANS';
485: }
486: return 'INCORRECT';
1.3 albertel 487: }
488:
1.1 albertel 489: sub end_rectangle {
1.36 albertel 490: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
491: my $result;
1.38 albertel 492: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
493: $target eq 'analyze') {
1.36 albertel 494: my $name = $Apache::imageresponse::curname;
495: my $area = &Apache::lonxml::endredirection;
496: &Apache::lonxml::debug("out is $area for $name");
497: if ( $Apache::imageresponse::conceptgroup
498: && !&Apache::response::showallfoils()) {
499: push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
500: } else {
501: push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
502: }
503: } elsif ($target eq 'edit') {
504: $result=&Apache::edit::end_table();
505: }
506: return $result;
1.1 albertel 507: }
508: 1;
509: __END__
510:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>