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