1:
2: # The LearningOnline Network with CAPA
3: # image click response style
4: #
5: # $Id: imageresponse.pm,v 1.60 2005/01/31 22:46:22 albertel Exp $
6: #
7: # Copyright Michigan State University Board of Trustees
8: #
9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
10: #
11: # LON-CAPA is free software; you can redistribute it and/or modify
12: # it under the terms of the GNU General Public License as published by
13: # the Free Software Foundation; either version 2 of the License, or
14: # (at your option) any later version.
15: #
16: # LON-CAPA is distributed in the hope that it will be useful,
17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: # GNU General Public License for more details.
20: #
21: # You should have received a copy of the GNU General Public License
22: # along with LON-CAPA; if not, write to the Free Software
23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24: #
25: # /home/httpd/html/adm/gpl.txt
26: #
27: # http://www.lon-capa.org/
28: #
29: #FIXME LATER assumes multiple possible submissions but only one is possible
30: #currently
31:
32: package Apache::imageresponse;
33: use strict;
34: use Image::Magick();
35: use Apache::randomlylabel();
36: use Apache::londefdef();
37: use Apache::Constants qw(:common :http);
38: use Apache::lonlocal;
39:
40: BEGIN {
41: &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
42: }
43:
44: sub start_imageresponse {
45: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
46: my $result;
47: #when in a radiobutton response use these
48: &Apache::lonxml::register('Apache::imageresponse',
49: ('foilgroup','foil','text','image','rectangle',
50: 'polygon','conceptgroup'));
51: push (@Apache::lonxml::namespace,'imageresponse');
52: my $id = &Apache::response::start_response($parstack,$safeeval);
53: undef(%Apache::response::foilnames);
54: if ($target eq 'meta') {
55: $result=&Apache::response::meta_package_write('imageresponse');
56: } elsif ($target eq 'analyze') {
57: my $part_id="$Apache::inputtags::part.$id";
58: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
59: }
60: return $result;
61: }
62:
63: sub end_imageresponse {
64: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
65: &Apache::response::end_response;
66: pop @Apache::lonxml::namespace;
67: &Apache::lonxml::deregister('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
68: my $result;
69: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
70: undef(%Apache::response::foilnames);
71: return $result;
72: }
73:
74: %Apache::response::foilgroup=();
75: sub start_foilgroup {
76: %Apache::response::foilgroup=();
77: $Apache::imageresponse::conceptgroup=0;
78: &Apache::response::pushrandomnumber();
79: return '';
80: }
81:
82: sub getfoilcounts {
83: my ($parstack,$safeeval)=@_;
84:
85: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
86: # +1 since instructors will count from 1
87: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
88: #if (&Apache::response::showallfoils()) { $max=$count; }
89: return ($count,$max);
90: }
91:
92: sub whichfoils {
93: my ($max)=@_;
94: if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
95: my @names = @{ $Apache::response::foilgroup{'names'} };
96: my @whichopt =();
97: while ((($#whichopt+1) < $max) && ($#names > -1)) {
98: &Apache::lonxml::debug("Have $#whichopt max is $max");
99: my $aopt;
100: # if (&Apache::response::showallfoils()) {
101: # $aopt=0;
102: # } else {
103: $aopt=int(&Math::Random::random_uniform() * ($#names+1));
104: # }
105: &Apache::lonxml::debug("From $#names elms, picking $aopt");
106: $aopt=splice(@names,$aopt,1);
107: &Apache::lonxml::debug("Picked $aopt");
108: push (@whichopt,$aopt);
109: }
110: return @whichopt;
111: }
112:
113: sub prep_image {
114: my ($image,$mode,$name)=@_;
115: my $part=$Apache::inputtags::part;
116: my $respid=$Apache::inputtags::response['-1'];
117: my $id=&Apache::loncommon::get_cgi_id();
118: my (%x,$i);
119: $x{"cgi.$id.BGIMG"}=&Apache::lonnet::escape($image);
120: my ($x,$y)=split(/:/,$Apache::lonhomework::history{"resource.$part.$respid.submission"});
121: #draws 2 xs on the image at the clicked location
122: #one in white and then one in red on top of the one in white
123: if (defined($x) && $x=~/\S/ && defined($y) && $y =~/\S/ && !&Apache::response::show_answer()) {
124: my $length = 6;
125: my $width = 1;
126: my $extrawidth = 2;
127: my $xmin=($x-$length);
128: my $xmax=($x+$length);
129: my $ymin=($y-$length);
130: my $ymax=($y+$length);
131:
132: $x{"cgi.$id.OBJTYPE"}.='LINE:';
133: $i=$x{"cgi.$id.OBJCOUNT"}++;
134: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
135: "FFFFFF",($width+$extrawidth)));
136: $x{"cgi.$id.OBJTYPE"}.='LINE:';
137: $i=$x{"cgi.$id.OBJCOUNT"}++;
138: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
139: "FFFFFF",($width+$extrawidth)));
140: $x{"cgi.$id.OBJTYPE"}.='LINE:';
141: $i=$x{"cgi.$id.OBJCOUNT"}++;
142: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
143: "FF0000",($width)));
144: $x{"cgi.$id.OBJTYPE"}.='LINE:';
145: $i=$x{"cgi.$id.OBJCOUNT"}++;
146: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
147: "FF0000",($width)));
148: }
149: if ($mode eq 'answer') {
150: my $width = 1;
151: my $extrawidth = 2;
152: my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
153: foreach my $area (@areas) {
154: if ($area=~/^rectangle:/) {
155: $x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
156: $i=$x{"cgi.$id.OBJCOUNT"}++;
157: my ($x1,$y1,$x2,$y2)=
158: ($area=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
159: $x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"FFFFFF",
160: ($width+$extrawidth)));
161: $x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
162: $i=$x{"cgi.$id.OBJCOUNT"}++;
163: $x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"00FF00",$width));
164: } elsif ($area=~/^polygon:(.*)/) {
165: $x{"cgi.$id.OBJTYPE"}.='POLYGON:';
166: $i=$x{"cgi.$id.OBJCOUNT"}++;
167: $x{"cgi.$id.OBJ$i"}=join(':',("FFFFFF",($width+$extrawidth)));
168: $x{"cgi.$id.OBJEXTRA$i"}=$1;
169: $x{"cgi.$id.OBJTYPE"}.='POLYGON:';
170: $i=$x{"cgi.$id.OBJCOUNT"}++;
171: $x{"cgi.$id.OBJ$i"}=join(':',("00FF00",$width));
172: $x{"cgi.$id.OBJEXTRA$i"}=$1;
173: }
174: }
175: }
176: &Apache::lonnet::appenv(%x);
177: return $id;
178: }
179:
180: sub displayfoils {
181: my ($target,@whichopt) = @_;
182: my $result ='';
183: my $name;
184: my $temp=1;
185: foreach $name (@whichopt) {
186: $result.=$Apache::response::foilgroup{"$name.text"};
187: &Apache::lonxml::debug("Text is $result");
188: if ($target eq 'tex') {$result.="\\vskip 0 mm \n";} else {$result.="<br />\n";}
189: my $image=$Apache::response::foilgroup{"$name.image"};
190: &Apache::lonxml::debug("image is $image");
191: if ( $target eq 'web' && $image !~ /^http:/ ) {
192: $image=&clean_up_image($image);
193: }
194: &Apache::lonxml::debug("image is $image");
195: if ( &Apache::response::show_answer() ) {
196: if ($target eq 'tex') {
197: $result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
198: } else {
199: my $token=&prep_image($image,'answer',$name);
200: $result.="<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
201: }
202: } else {
203: if ($target eq 'tex') {
204: $result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
205: } else {
206: my $id=$Apache::inputtags::response['-1'];
207: my $token=&prep_image($image);
208: my $temp=1;
209: $result.="<input type=\"image\" name=\"HWVAL_$id:$temp\" ".
210: "src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
211: }
212: }
213: $temp++;
214: }
215: return $result;
216: }
217:
218: sub clean_up_image {
219: my ($image)=@_;
220: if ($image =~ /\s*<img\s*/) {
221: ($image) = ($image =~ /src\s*=\s*[\"\']([^\"\']+)[\"\']/i);
222: if ($image !~ /^http:/) {
223: $image=&Apache::lonnet::hreflocation('',$image);
224: }
225: if (!$image) {
226: $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
227: }
228: } else {
229: $image=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$image);
230: if (&Apache::lonnet::repcopy($image) ne OK) {
231: $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
232: }
233: }
234: return $image;
235: }
236:
237: sub gradefoils {
238: my (@whichopt) = @_;
239: my $x;
240: my $y;
241: my $result;
242: my $id=$Apache::inputtags::response['-1'];
243: my $temp=1;
244: foreach my $name (@whichopt) {
245: $x=$ENV{"form.HWVAL_$id:$temp.x"};
246: $y=$ENV{"form.HWVAL_$id:$temp.y"};
247: &Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
248: if (defined($x) && defined($y) &&
249: defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
250: my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
251: my $grade="INCORRECT";
252: foreach my $area (@areas) {
253: &Apache::lonxml::debug("Area is $area for $name");
254: $area =~ m/([a-z]*):/;
255: &Apache::lonxml::debug("Area of type $1");
256: if ($1 eq 'rectangle') {
257: $grade=&grade_rectangle($area,$x,$y);
258: } elsif ($1 eq 'polygon') {
259: $grade=&grade_polygon($area,$x,$y);
260: } else {
261: &Apache::lonxml::error("Unknown area style $area");
262: }
263: &Apache::lonxml::debug("Area said $grade");
264: if ($grade eq 'APPROX_ANS') { last; }
265: }
266: &Apache::lonxml::debug("Foil was $grade");
267: if ($grade eq 'INCORRECT') { $result= 'INCORRECT'; }
268: if (($grade eq 'APPROX_ANS') && ($result ne 'APPROX_ANS')) { $result=$grade; }
269: &Apache::lonxml::debug("Question is $result");
270: $temp++;
271: }
272: }
273: my $responsestr="$x:$y";
274: my $part=$Apache::inputtags::part;
275: my $id = $Apache::inputtags::response['-1'];
276: my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
277: if ($result
278: && $Apache::lonhomework::type eq 'survey') { $result='SUBMITTED'; }
279: &Apache::response::handle_previous(\%previous,$result);
280: $Apache::lonhomework::results{"resource.$part.$id.submission"}=$responsestr;
281: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$result;
282: return '';
283: }
284:
285: sub end_foilgroup {
286: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
287: my $result='';
288: my @whichopt;
289: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
290: $target eq 'analyze') {
291: my ($count,$max) = &getfoilcounts($parstack,$safeeval);
292: if ($count>$max) { $count=$max }
293: &Apache::lonxml::debug("Count is $count from $max");
294: @whichopt = &whichfoils($max);
295: if ($target eq 'web' || $target eq 'tex') {
296: $result=&displayfoils($target,@whichopt);
297: } elsif ($target eq 'grade') {
298: if ( defined $ENV{'form.submitted'}) { &gradefoils(@whichopt); }
299: } elsif ( $target eq 'analyze') {
300: &Apache::response::analyze_store_foilgroup(\@whichopt,
301: ['text','image','area']);
302: }
303: } elsif ($target eq 'edit') {
304: $result=&Apache::edit::end_table();
305: }
306: &Apache::response::poprandomnumber();
307: return $result;
308: }
309:
310: sub start_conceptgroup {
311: $Apache::imageresponse::conceptgroup=1;
312: %Apache::response::conceptgroup=();
313: return '';
314: }
315:
316: sub end_conceptgroup {
317: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
318: $Apache::imageresponse::conceptgroup=0;
319: my $result;
320: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
321: $target eq 'analyze') {
322: &Apache::response::pick_foil_for_concept($target,
323: ['area','text','image'],
324: \%Apache::hint::image,
325: $parstack,$safeeval);
326: } elsif ($target eq 'edit') {
327: $result=&Apache::edit::end_table();
328: }
329: return $result;
330: }
331:
332: sub insert_foil {
333: return '
334: <foil>
335: <image></image>
336: <text></text>
337: <rectangle></rectangle>
338: </foil>
339: ';
340: }
341:
342: $Apache::imageresponse::curname='';
343: sub start_foil {
344: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
345: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
346: $target eq 'analyze') {
347: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
348: if (!$name) {
349: &Apache::lonxml::error("Foils without names exist. This can cause problems to malfunction.");
350: $name=$Apache::lonxml::curdepth;
351: }
352: if (defined($Apache::response::foilnames{$name})) {
353: &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
354: }
355: $Apache::response::foilnames{$name}++;
356: if ( $Apache::imageresponse::conceptgroup
357: #&& !&Apache::response::showallfoils()
358: ) {
359: push(@{ $Apache::response::conceptgroup{'names'} }, $name);
360: } else {
361: push(@{ $Apache::response::foilgroup{'names'} }, $name);
362: }
363: $Apache::imageresponse::curname=$name;
364: }
365: return '';
366: }
367:
368: sub end_foil {
369: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
370: my $result;
371: if ($target eq 'edit') {
372: $result=&Apache::edit::end_table();
373: }
374: return $result;
375: }
376:
377: sub start_text {
378: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
379: my $result='';
380: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
381: &Apache::lonxml::startredirection;
382: } elsif ($target eq 'edit') {
383: my $descr=&Apache::lonxml::get_all_text('/text',$parser);
384: $result=&Apache::edit::tag_start($target,$token,'Task Description').
385: &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
386: &Apache::edit::end_row();
387: } elsif ($target eq "modified") {
388: $result=$token->[4].&Apache::edit::modifiedfield('/text',$parser);
389: }
390: return $result;
391: }
392:
393: sub end_text {
394: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
395: my $result;
396: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
397: my $name = $Apache::imageresponse::curname;
398: if ( $Apache::imageresponse::conceptgroup
399: #&& !&Apache::response::showallfoils()
400: ) {
401: $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
402: } else {
403: $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
404: }
405: } elsif ($target eq 'edit') {
406: $result=&Apache::edit::end_table();
407: }
408: return $result;
409: }
410:
411: sub start_image {
412: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
413: my $result='';
414: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
415: &Apache::lonxml::startredirection;
416: } elsif ($target eq 'edit') {
417: my $bgimg=&Apache::lonxml::get_all_text('/image',$parser);
418: $Apache::edit::bgimgsrc=$bgimg;
419: $Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
420:
421: $result=&Apache::edit::tag_start($target,$token,'Clickable Image').
422: &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
423: $result.=&Apache::edit::browse(undef,'textnode').' ';
424: $result.=&Apache::edit::search(undef,'textnode').
425: &Apache::edit::end_row();
426: } elsif ($target eq "modified") {
427: $result=$token->[4].&Apache::edit::modifiedfield('/image',$parser);
428: }
429: return $result;
430: }
431:
432: sub end_image {
433: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
434: my $result;
435: my $name = $Apache::imageresponse::curname;
436: if ($target eq 'web') {
437: my $image = &Apache::lonxml::endredirection;
438: &Apache::lonxml::debug("original image is $image");
439: if ( $Apache::imageresponse::conceptgroup
440: #&& !&Apache::response::showallfoils()
441: ) {
442: $Apache::response::conceptgroup{"$name.image"} = $image;
443: } else {
444: $Apache::response::foilgroup{"$name.image"} = $image;
445: }
446: } elsif ($target eq 'analyze') {
447: my $image = &Apache::lonxml::endredirection;
448: if ( $Apache::imageresponse::conceptgroup
449: #&& !&Apache::response::showallfoils()
450: ) {
451: $Apache::response::conceptgroup{"$name.image"} = $image;
452: } else {
453: $Apache::response::foilgroup{"$name.image"} = $image;
454: }
455: } elsif ($target eq 'edit') {
456: $result=&Apache::edit::end_table();
457: } elsif ($target eq 'tex') {
458: my $src = &Apache::lonxml::endredirection;
459: $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
460: my $width_param = '';
461: my $height_param = '';
462: my $scaling = .3;
463: my $image = Image::Magick->new;
464: my $current_figure = $image->Read($src);
465: $width_param = $image->Get('width') * $scaling;;
466: $height_param = $image->Get('height') * $scaling;;
467: undef $image;
468: my $epssrc = $src;
469: $epssrc =~ s/(\.gif|\.jpg)$/\.eps/i;
470: if (not -e $epssrc) {
471: my $localfile = $epssrc;
472: $localfile =~ s/.*(\/res)/$1/;
473: my $file;
474: my $path;
475: if ($localfile =~ m!(.*)/([^/]*)$!) {
476: $file = $2;
477: $path = $1.'/';
478: }
479: my $signal_eps = 0;
480: my @content_directory = &Apache::lonnet::dirlist($path);
481: for (my $iy=0;$iy<=$#content_directory;$iy++) {
482: my @tempo_array = split(/&/,$content_directory[$iy]);
483: $content_directory[$iy] = $tempo_array[0];
484: if ($file eq $tempo_array[0]) {
485: $signal_eps = 1;
486: last;
487: }
488: }
489: if ($signal_eps) {
490: my $eps_file = &Apache::lonnet::getfile($localfile);
491: } else {
492: $localfile = $src;
493: $localfile =~ s/.*(\/res)/$1/;
494: my $as = &Apache::lonnet::getfile($src);
495: }
496: }
497: my $file;
498: my $path;
499: if ($src =~ m!(.*)/([^/]*)$!) {
500: $file = $2;
501: $path = $1.'/';
502: }
503: my $newsrc = $src;
504: $newsrc =~ s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
505: $file=~s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
506: #do we have any specified size of the picture?
507: my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval);
508: my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval);
509: my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval);
510: if ($TeXwidth ne '') {
511: $width_param = $TeXwidth;
512: } elsif ($TeXheight ne '') {
513: $width_param = $TeXheight/$height_param*$width_param;
514: } elsif ($width ne '') {
515: $width_param = $width*$scaling;
516: }
517: $width_param=&Apache::randomlabel::adjust_textwidth($width_param);
518: #where can we find the picture?
519: if (-e $newsrc) {
520: if ($path) {
521: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
522: }
523: } else {
524: #care about eps dynamical generation
525: $Apache::response::foilgroup{"$name.image"}='\vskip 0 mm '.&Apache::londefdef::eps_generation($src,$file,$width_param);
526: }
527: }
528: return $result;
529: }
530:
531: sub start_rectangle {
532: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
533: my $result='';
534: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
535: $target eq 'analyze') {
536: &Apache::lonxml::startredirection;
537: } elsif ($target eq 'edit') {
538: my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser);
539: $result=&Apache::edit::tag_start($target,$token,'Rectangle').
540: &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
541: &Apache::edit::entercoord(undef,'textnode',undef,undef,'box').
542: &Apache::edit::end_row();
543: } elsif ($target eq "modified") {
544: &Apache::edit::deletecoorddata();
545: $result=$token->[4].&Apache::edit::modifiedfield('/rectangle',$parser);
546: }
547: return $result;
548: }
549:
550: sub grade_rectangle {
551: my ($spec,$x,$y) = @_;
552: &Apache::lonxml::debug("Spec is $spec");
553: my ($x1,$y1,$x2,$y2)=($spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
554: &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
555: if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
556: if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
557: if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
558: return 'APPROX_ANS';
559: }
560: return 'INCORRECT';
561: }
562:
563: sub end_rectangle {
564: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
565: my $result;
566: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
567: $target eq 'analyze') {
568: my $name = $Apache::imageresponse::curname;
569: my $area = &Apache::lonxml::endredirection;
570: $area=~s/\s//g;
571: &Apache::lonxml::debug("out is $area for $name");
572: if ( $Apache::imageresponse::conceptgroup
573: #&& !&Apache::response::showallfoils()
574: ) {
575: push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
576: } else {
577: push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
578: }
579: } elsif ($target eq 'edit') {
580: $result=&Apache::edit::end_table();
581: }
582: return $result;
583: }
584:
585: sub start_polygon {
586: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
587: my $result='';
588: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
589: $target eq 'analyze') {
590: &Apache::lonxml::startredirection;
591: } elsif ($target eq 'edit') {
592: my $coords=&Apache::lonxml::get_all_text('/polygon',$parser);
593: $result=&Apache::edit::tag_start($target,$token,'Polygon').
594: &Apache::edit::editline($token->[1],$coords,'Coordinate list',40).
595: &Apache::edit::entercoord(undef,'textnode',undef,undef,'polygon').
596: &Apache::edit::end_row();
597: } elsif ($target eq "modified") {
598: $result=$token->[4].&Apache::edit::modifiedfield('/polygon',$parser);
599: }
600: return $result;
601: }
602:
603: sub grade_polygon {
604: my ($spec,$x,$y) = @_;
605: &Apache::lonxml::debug("Spec is $spec");
606: $spec=~s/^polygon://;
607: my @polygon;
608: foreach my $coord (split('-',$spec)) {
609: my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
610: &Apache::lonxml::debug("x $x y $y");
611: push @polygon, {'x'=>$x,'y'=>$y};
612: }
613: #make end point start point
614: push @polygon, $polygon[0];
615: # cribbed from
616: # http://geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm
617: my $crossing = 0; # the crossing number counter
618:
619: # loop through all edges of the polygon
620: for (my $i=0; $i<$#polygon; $i++) { # edge from V[i] to V[i+1]
621: if ((($polygon[$i]->{'y'} <= $y)
622: && ($polygon[$i+1]->{'y'} > $y)) # an upward crossing
623: ||
624: (($polygon[$i]->{'y'} > $y)
625: && ($polygon[$i+1]->{'y'} <= $y))) { # a downward crossing
626: # compute the actual edge-ray intersect x-coordinate
627: my $vt = ($y - $polygon[$i]->{'y'})
628: / ($polygon[$i+1]->{'y'} - $polygon[$i]->{'y'});
629: if ($x < $polygon[$i]->{'x'} + $vt *
630: ($polygon[$i+1]->{'x'} - $polygon[$i]->{'x'})) { # x<intersect
631: $crossing++; # a valid crossing of y=P.y right of P.x
632: }
633: }
634: }
635:
636: # 0 if even (out), and 1 if odd (in)
637: if ($crossing%2) {
638: return 'APPROX_ANS';
639: } else {
640: return 'INCORRECT';
641: }
642: }
643:
644: sub end_polygon {
645: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
646: my $result;
647: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
648: $target eq 'analyze') {
649: my $name = $Apache::imageresponse::curname;
650: my $area = &Apache::lonxml::endredirection;
651: $area=~s/\s*//g;
652: &Apache::lonxml::debug("out is $area for $name");
653: if ( $Apache::imageresponse::conceptgroup
654: #&& !&Apache::response::showallfoils()
655: ) {
656: push @{ $Apache::response::conceptgroup{"$name.area"} },"polygon:$area";
657: } else {
658: push @{ $Apache::response::foilgroup{"$name.area"} },"polygon:$area";
659: }
660: } elsif ($target eq 'edit') {
661: $result=&Apache::edit::end_table();
662: }
663: return $result;
664: }
665: 1;
666: __END__
667:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>