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