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