Annotation of loncom/homework/imageresponse.pm, revision 1.75
1.58 albertel 1:
1.12 albertel 2: # The LearningOnline Network with CAPA
1.14 albertel 3: # image click response style
4: #
1.75 ! albertel 5: # $Id: imageresponse.pm,v 1.74 2006/03/09 01:11:12 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)=@_;
1.74 albertel 66:
67: pop(@Apache::lonxml::namespace);
1.30 albertel 68: &Apache::lonxml::deregister('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
1.74 albertel 69:
1.30 albertel 70: my $result;
1.74 albertel 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:
1.51 albertel 77: undef(%Apache::response::foilnames);
1.74 albertel 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:
1.30 albertel 85: return $result;
1.1 albertel 86: }
87:
1.20 albertel 88: %Apache::response::foilgroup=();
1.1 albertel 89: sub start_foilgroup {
1.36 albertel 90: %Apache::response::foilgroup=();
91: $Apache::imageresponse::conceptgroup=0;
1.53 albertel 92: &Apache::response::pushrandomnumber();
1.36 albertel 93: return '';
1.1 albertel 94: }
95:
1.2 albertel 96: sub getfoilcounts {
1.36 albertel 97: my ($parstack,$safeeval)=@_;
1.12 albertel 98:
1.36 albertel 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;
1.56 albertel 102: #if (&Apache::response::showallfoils()) { $max=$count; }
1.36 albertel 103: return ($count,$max);
1.2 albertel 104: }
105:
106: sub whichfoils {
1.36 albertel 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;
1.56 albertel 114: # if (&Apache::response::showallfoils()) {
115: # $aopt=0;
116: # } else {
1.36 albertel 117: $aopt=int(&Math::Random::random_uniform() * ($#names+1));
1.56 albertel 118: # }
1.36 albertel 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;
1.2 albertel 125: }
126:
1.40 albertel 127: sub prep_image {
1.42 albertel 128: my ($image,$mode,$name)=@_;
1.40 albertel 129: my $part=$Apache::inputtags::part;
1.41 albertel 130: my $respid=$Apache::inputtags::response['-1'];
131: my $id=&Apache::loncommon::get_cgi_id();
1.49 albertel 132: my (%x,$i);
1.47 albertel 133: $x{"cgi.$id.BGIMG"}=&Apache::lonnet::escape($image);
1.41 albertel 134: my ($x,$y)=split(/:/,$Apache::lonhomework::history{"resource.$part.$respid.submission"});
1.40 albertel 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
1.75 ! albertel 137: if (defined($x) && $x =~/\S/
! 138: && defined($y) && $y =~/\S/
! 139: && !&Apache::response::show_answer()
! 140: && $mode ne 'answeronly') {
1.40 albertel 141: my $length = 6;
142: my $width = 1;
143: my $extrawidth = 2;
1.48 albertel 144: my $xmin=($x-$length);
145: my $xmax=($x+$length);
146: my $ymin=($y-$length);
147: my $ymax=($y+$length);
148:
1.49 albertel 149: $x{"cgi.$id.OBJTYPE"}.='LINE:';
150: $i=$x{"cgi.$id.OBJCOUNT"}++;
1.58 albertel 151: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
1.49 albertel 152: "FFFFFF",($width+$extrawidth)));
153: $x{"cgi.$id.OBJTYPE"}.='LINE:';
154: $i=$x{"cgi.$id.OBJCOUNT"}++;
1.58 albertel 155: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
1.49 albertel 156: "FFFFFF",($width+$extrawidth)));
157: $x{"cgi.$id.OBJTYPE"}.='LINE:';
158: $i=$x{"cgi.$id.OBJCOUNT"}++;
1.58 albertel 159: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
1.49 albertel 160: "FF0000",($width)));
161: $x{"cgi.$id.OBJTYPE"}.='LINE:';
162: $i=$x{"cgi.$id.OBJCOUNT"}++;
1.58 albertel 163: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
1.49 albertel 164: "FF0000",($width)));
1.40 albertel 165: }
1.75 ! albertel 166: if ($mode eq 'answer' || $mode eq 'answeronly') {
1.42 albertel 167: my $width = 1;
168: my $extrawidth = 2;
169: my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
170: foreach my $area (@areas) {
1.43 albertel 171: if ($area=~/^rectangle:/) {
1.49 albertel 172: $x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
173: $i=$x{"cgi.$id.OBJCOUNT"}++;
1.43 albertel 174: my ($x1,$y1,$x2,$y2)=
175: ($area=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
1.49 albertel 176: $x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"FFFFFF",
1.43 albertel 177: ($width+$extrawidth)));
1.49 albertel 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));
1.43 albertel 181: } elsif ($area=~/^polygon:(.*)/) {
1.49 albertel 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;
1.43 albertel 190: }
1.42 albertel 191: }
192: }
1.41 albertel 193: &Apache::lonnet::appenv(%x);
194: return $id;
1.40 albertel 195: }
196:
1.2 albertel 197: sub displayfoils {
1.36 albertel 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");
1.40 albertel 208: if ( $target eq 'web' && $image !~ /^http:/ ) {
1.47 albertel 209: $image=&clean_up_image($image);
210: }
1.40 albertel 211: &Apache::lonxml::debug("image is $image");
1.36 albertel 212: if ( &Apache::response::show_answer() ) {
213: if ($target eq 'tex') {
214: $result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
215: } else {
1.42 albertel 216: my $token=&prep_image($image,'answer',$name);
1.40 albertel 217: $result.="<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
1.36 albertel 218: }
219: } else {
220: if ($target eq 'tex') {
221: $result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
222: } else {
1.40 albertel 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";
1.36 albertel 228: }
229: }
230: $temp++;
231: }
232: return $result;
1.47 albertel 233: }
234:
1.75 ! albertel 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:
1.47 albertel 254: sub clean_up_image {
255: my ($image)=@_;
256: if ($image =~ /\s*<img\s*/) {
1.50 albertel 257: ($image) = ($image =~ /src\s*=\s*[\"\']([^\"\']+)[\"\']/i);
1.47 albertel 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);
1.66 foxr 266: &Apache::lonxml::debug("repcopying: $image");
1.64 raeburn 267: if (&Apache::lonnet::repcopy($image) ne 'ok') {
1.47 albertel 268: $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
269: }
270: }
271: return $image;
1.2 albertel 272: }
273:
1.3 albertel 274: sub gradefoils {
1.36 albertel 275: my (@whichopt) = @_;
1.74 albertel 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:
1.36 albertel 285: my $x;
286: my $y;
287: my $result;
288: my $temp=1;
289: foreach my $name (@whichopt) {
1.65 albertel 290: $x=$env{"form.HWVAL_$id:$temp.x"};
291: $y=$env{"form.HWVAL_$id:$temp.y"};
1.36 albertel 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);
1.43 albertel 303: } elsif ($1 eq 'polygon') {
304: $grade=&grade_polygon($area,$x,$y);
1.36 albertel 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++;
1.9 albertel 316: }
1.36 albertel 317: }
1.60 albertel 318: my $responsestr="$x:$y";
319: my $part=$Apache::inputtags::part;
320: my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
1.56 albertel 321: if ($result
322: && $Apache::lonhomework::type eq 'survey') { $result='SUBMITTED'; }
1.60 albertel 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;
1.74 albertel 326: return;
1.3 albertel 327: }
328:
1.1 albertel 329: sub end_foilgroup {
1.36 albertel 330: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
331: my $result='';
332: my @whichopt;
1.75 ! albertel 333:
1.38 albertel 334: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75 ! albertel 335: $target eq 'analyze' || $target eq 'answer') {
! 336:
1.36 albertel 337: my ($count,$max) = &getfoilcounts($parstack,$safeeval);
338: if ($count>$max) { $count=$max }
339: &Apache::lonxml::debug("Count is $count from $max");
1.75 ! albertel 340:
1.36 albertel 341: @whichopt = &whichfoils($max);
1.75 ! albertel 342:
1.36 albertel 343: if ($target eq 'web' || $target eq 'tex') {
344: $result=&displayfoils($target,@whichopt);
345: } elsif ($target eq 'grade') {
1.65 albertel 346: if ( defined $env{'form.submitted'}) { &gradefoils(@whichopt); }
1.37 albertel 347: } elsif ( $target eq 'analyze') {
348: &Apache::response::analyze_store_foilgroup(\@whichopt,
349: ['text','image','area']);
1.75 ! albertel 350: } elsif ($target eq 'answer') {
! 351: $result=&display_answers($target,\@whichopt);
1.37 albertel 352: }
1.75 ! albertel 353:
1.36 albertel 354: } elsif ($target eq 'edit') {
355: $result=&Apache::edit::end_table();
356: }
1.53 albertel 357: &Apache::response::poprandomnumber();
1.36 albertel 358: return $result;
1.1 albertel 359: }
360:
361: sub start_conceptgroup {
1.36 albertel 362: $Apache::imageresponse::conceptgroup=1;
363: %Apache::response::conceptgroup=();
364: return '';
1.1 albertel 365: }
366:
367: sub end_conceptgroup {
1.36 albertel 368: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
369: $Apache::imageresponse::conceptgroup=0;
370: my $result;
1.37 albertel 371: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
372: $target eq 'analyze') {
373: &Apache::response::pick_foil_for_concept($target,
374: ['area','text','image'],
375: \%Apache::hint::image,
376: $parstack,$safeeval);
1.36 albertel 377: } elsif ($target eq 'edit') {
378: $result=&Apache::edit::end_table();
379: }
380: return $result;
1.31 albertel 381: }
382:
383: sub insert_foil {
384: return '
385: <foil>
386: <image></image>
387: <text></text>
388: <rectangle></rectangle>
389: </foil>
390: ';
1.1 albertel 391: }
392:
1.12 albertel 393: $Apache::imageresponse::curname='';
1.1 albertel 394: sub start_foil {
1.36 albertel 395: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.67 albertel 396: my $result;
1.37 albertel 397: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75 ! albertel 398: $target eq 'analyze' || $target eq 'answer') {
1.36 albertel 399: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.72 albertel 400: if ($name eq "") {
1.71 albertel 401: &Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
1.59 albertel 402: $name=$Apache::lonxml::curdepth;
403: }
1.51 albertel 404: if (defined($Apache::response::foilnames{$name})) {
405: &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
406: }
1.52 albertel 407: $Apache::response::foilnames{$name}++;
1.36 albertel 408: if ( $Apache::imageresponse::conceptgroup
1.56 albertel 409: #&& !&Apache::response::showallfoils()
410: ) {
1.36 albertel 411: push(@{ $Apache::response::conceptgroup{'names'} }, $name);
412: } else {
413: push(@{ $Apache::response::foilgroup{'names'} }, $name);
414: }
415: $Apache::imageresponse::curname=$name;
1.67 albertel 416: } elsif ($target eq 'edit') {
417: $result = &Apache::edit::tag_start($target,$token);
418: $result .= &Apache::edit::text_arg('Name:','name',$token);
419: $result .= &Apache::edit::end_row().
420: &Apache::edit::start_spanning_row();
421: } elsif ($target eq 'modified') {
422: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
423: $safeeval,'name');
424: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
425: }
426: return $result;;
1.1 albertel 427: }
428:
429: sub end_foil {
1.26 albertel 430: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
431: my $result;
432: if ($target eq 'edit') {
433: $result=&Apache::edit::end_table();
434: }
435: return $result;
1.1 albertel 436: }
437:
438: sub start_text {
1.36 albertel 439: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
440: my $result='';
1.75 ! albertel 441: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
! 442: || $target eq 'answer') {
1.36 albertel 443: &Apache::lonxml::startredirection;
444: } elsif ($target eq 'edit') {
1.73 albertel 445: my $descr=&Apache::lonxml::get_all_text('/text',$parser,$style);
1.36 albertel 446: $result=&Apache::edit::tag_start($target,$token,'Task Description').
447: &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
448: &Apache::edit::end_row();
449: } elsif ($target eq "modified") {
1.39 albertel 450: $result=$token->[4].&Apache::edit::modifiedfield('/text',$parser);
1.36 albertel 451: }
452: return $result;
1.1 albertel 453: }
454:
455: sub end_text {
1.36 albertel 456: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
457: my $result;
1.75 ! albertel 458: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
! 459: || $target eq 'answer') {
1.36 albertel 460: my $name = $Apache::imageresponse::curname;
461: if ( $Apache::imageresponse::conceptgroup
1.56 albertel 462: #&& !&Apache::response::showallfoils()
463: ) {
1.36 albertel 464: $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
465: } else {
466: $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
467: }
468: } elsif ($target eq 'edit') {
469: $result=&Apache::edit::end_table();
470: }
471: return $result;
1.1 albertel 472: }
473:
474: sub start_image {
1.36 albertel 475: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
476: my $result='';
1.75 ! albertel 477: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
! 478: || $target eq 'answer') {
1.36 albertel 479: &Apache::lonxml::startredirection;
480: } elsif ($target eq 'edit') {
1.73 albertel 481: my $bgimg=&Apache::lonxml::get_all_text('/image',$parser,$style);
1.36 albertel 482: $Apache::edit::bgimgsrc=$bgimg;
483: $Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
484:
485: $result=&Apache::edit::tag_start($target,$token,'Clickable Image').
486: &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
487: $result.=&Apache::edit::browse(undef,'textnode').' ';
488: $result.=&Apache::edit::search(undef,'textnode').
489: &Apache::edit::end_row();
490: } elsif ($target eq "modified") {
1.39 albertel 491: $result=$token->[4].&Apache::edit::modifiedfield('/image',$parser);
1.36 albertel 492: }
493: return $result;
1.1 albertel 494: }
495:
496: sub end_image {
1.36 albertel 497: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
498: my $result;
499: my $name = $Apache::imageresponse::curname;
1.75 ! albertel 500: if ($target eq 'web' || $target eq 'answer') {
1.62 albertel 501: my $image = &Apache::lonxml::endredirection();
1.36 albertel 502: &Apache::lonxml::debug("original image is $image");
1.37 albertel 503: if ( $Apache::imageresponse::conceptgroup
1.56 albertel 504: #&& !&Apache::response::showallfoils()
505: ) {
1.37 albertel 506: $Apache::response::conceptgroup{"$name.image"} = $image;
507: } else {
508: $Apache::response::foilgroup{"$name.image"} = $image;
509: }
510: } elsif ($target eq 'analyze') {
1.62 albertel 511: my $image = &Apache::lonxml::endredirection();
1.36 albertel 512: if ( $Apache::imageresponse::conceptgroup
1.56 albertel 513: #&& !&Apache::response::showallfoils()
514: ) {
1.36 albertel 515: $Apache::response::conceptgroup{"$name.image"} = $image;
516: } else {
517: $Apache::response::foilgroup{"$name.image"} = $image;
518: }
519: } elsif ($target eq 'edit') {
520: $result=&Apache::edit::end_table();
521: } elsif ($target eq 'tex') {
1.62 albertel 522: my $src = &Apache::lonxml::endredirection();
1.68 foxr 523:
524: # There may be all sorts of whitespace on fore and aft:
525:
1.69 foxr 526: $src =~ s/\s+$//s;
527: $src =~ s/^\s+//s;
1.68 foxr 528:
529: #
530: # Gnuplot e.g. just generates the latex to put inplace.
531: #
532: my $graphinclude;
1.70 foxr 533: if ($src =~ /^%DYNAMICIMAGE/) {
534: # This is needed because the newline is not always passed -> tex.
535: # At present we don't care about the sizing info.
536:
537: my ($commentline, $restofstuff) = split(/\n/, $src);
1.68 foxr 538: $graphinclude = $src;
1.70 foxr 539: $graphinclude =~ s/^$commentline//;
1.68 foxr 540: } else {
541: my ($path,$file) = &Apache::londefdef::get_eps_image($src);
542: my ($height_param,$width_param)=
543: &Apache::londefdef::image_size($src,0.3,$parstack,$safeeval);
544: $graphinclude = '\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'}';
545: }
546: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent '.$graphinclude;
1.36 albertel 547: }
548: return $result;
1.1 albertel 549: }
550:
551: sub start_rectangle {
1.36 albertel 552: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
553: my $result='';
1.38 albertel 554: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75 ! albertel 555: $target eq 'analyze' || $target eq 'answer') {
1.36 albertel 556: &Apache::lonxml::startredirection;
557: } elsif ($target eq 'edit') {
1.73 albertel 558: my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser,$style);
1.36 albertel 559: $result=&Apache::edit::tag_start($target,$token,'Rectangle').
560: &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
1.44 albertel 561: &Apache::edit::entercoord(undef,'textnode',undef,undef,'box').
1.36 albertel 562: &Apache::edit::end_row();
563: } elsif ($target eq "modified") {
1.44 albertel 564: &Apache::edit::deletecoorddata();
1.39 albertel 565: $result=$token->[4].&Apache::edit::modifiedfield('/rectangle',$parser);
1.36 albertel 566: }
567: return $result;
1.1 albertel 568: }
569:
1.3 albertel 570: sub grade_rectangle {
1.36 albertel 571: my ($spec,$x,$y) = @_;
572: &Apache::lonxml::debug("Spec is $spec");
1.43 albertel 573: my ($x1,$y1,$x2,$y2)=($spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
1.36 albertel 574: &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
575: if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
576: if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
577: if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
578: return 'APPROX_ANS';
579: }
580: return 'INCORRECT';
1.3 albertel 581: }
582:
1.1 albertel 583: sub end_rectangle {
1.36 albertel 584: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
585: my $result;
1.38 albertel 586: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75 ! albertel 587: $target eq 'analyze' || $target eq 'answer') {
1.36 albertel 588: my $name = $Apache::imageresponse::curname;
589: my $area = &Apache::lonxml::endredirection;
1.54 albertel 590: $area=~s/\s//g;
1.36 albertel 591: &Apache::lonxml::debug("out is $area for $name");
592: if ( $Apache::imageresponse::conceptgroup
1.56 albertel 593: #&& !&Apache::response::showallfoils()
594: ) {
1.36 albertel 595: push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
596: } else {
597: push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
1.43 albertel 598: }
599: } elsif ($target eq 'edit') {
600: $result=&Apache::edit::end_table();
601: }
602: return $result;
603: }
604:
605: sub start_polygon {
606: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
607: my $result='';
608: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75 ! albertel 609: $target eq 'analyze' || $target eq 'answer') {
1.43 albertel 610: &Apache::lonxml::startredirection;
611: } elsif ($target eq 'edit') {
1.73 albertel 612: my $coords=&Apache::lonxml::get_all_text('/polygon',$parser,$style);
1.43 albertel 613: $result=&Apache::edit::tag_start($target,$token,'Polygon').
614: &Apache::edit::editline($token->[1],$coords,'Coordinate list',40).
1.44 albertel 615: &Apache::edit::entercoord(undef,'textnode',undef,undef,'polygon').
1.43 albertel 616: &Apache::edit::end_row();
617: } elsif ($target eq "modified") {
618: $result=$token->[4].&Apache::edit::modifiedfield('/polygon',$parser);
619: }
620: return $result;
621: }
622:
623: sub grade_polygon {
624: my ($spec,$x,$y) = @_;
625: &Apache::lonxml::debug("Spec is $spec");
626: $spec=~s/^polygon://;
627: my @polygon;
628: foreach my $coord (split('-',$spec)) {
629: my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
630: &Apache::lonxml::debug("x $x y $y");
631: push @polygon, {'x'=>$x,'y'=>$y};
632: }
633: #make end point start point
634: push @polygon, $polygon[0];
635: # cribbed from
636: # http://geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm
637: my $crossing = 0; # the crossing number counter
638:
639: # loop through all edges of the polygon
640: for (my $i=0; $i<$#polygon; $i++) { # edge from V[i] to V[i+1]
641: if ((($polygon[$i]->{'y'} <= $y)
642: && ($polygon[$i+1]->{'y'} > $y)) # an upward crossing
643: ||
644: (($polygon[$i]->{'y'} > $y)
645: && ($polygon[$i+1]->{'y'} <= $y))) { # a downward crossing
646: # compute the actual edge-ray intersect x-coordinate
647: my $vt = ($y - $polygon[$i]->{'y'})
648: / ($polygon[$i+1]->{'y'} - $polygon[$i]->{'y'});
649: if ($x < $polygon[$i]->{'x'} + $vt *
650: ($polygon[$i+1]->{'x'} - $polygon[$i]->{'x'})) { # x<intersect
651: $crossing++; # a valid crossing of y=P.y right of P.x
652: }
653: }
654: }
655:
656: # 0 if even (out), and 1 if odd (in)
657: if ($crossing%2) {
658: return 'APPROX_ANS';
659: } else {
660: return 'INCORRECT';
661: }
662: }
663:
664: sub end_polygon {
665: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
666: my $result;
667: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75 ! albertel 668: $target eq 'analyze' || $target eq 'answer') {
1.43 albertel 669: my $name = $Apache::imageresponse::curname;
670: my $area = &Apache::lonxml::endredirection;
1.48 albertel 671: $area=~s/\s*//g;
1.43 albertel 672: &Apache::lonxml::debug("out is $area for $name");
673: if ( $Apache::imageresponse::conceptgroup
1.56 albertel 674: #&& !&Apache::response::showallfoils()
675: ) {
1.43 albertel 676: push @{ $Apache::response::conceptgroup{"$name.area"} },"polygon:$area";
677: } else {
678: push @{ $Apache::response::foilgroup{"$name.area"} },"polygon:$area";
1.36 albertel 679: }
680: } elsif ($target eq 'edit') {
681: $result=&Apache::edit::end_table();
682: }
683: return $result;
1.1 albertel 684: }
685: 1;
686: __END__
687:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>