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