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