Annotation of loncom/homework/imageresponse.pm, revision 1.74
1.58 albertel 1:
1.12 albertel 2: # The LearningOnline Network with CAPA
1.14 albertel 3: # image click response style
4: #
1.74 ! albertel 5: # $Id: imageresponse.pm,v 1.73 2005/12/01 18:46:31 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.58 albertel 137: if (defined($x) && $x=~/\S/ && defined($y) && $y =~/\S/ && !&Apache::response::show_answer()) {
1.40 albertel 138: my $length = 6;
139: my $width = 1;
140: my $extrawidth = 2;
1.48 albertel 141: my $xmin=($x-$length);
142: my $xmax=($x+$length);
143: my $ymin=($y-$length);
144: my $ymax=($y+$length);
145:
1.49 albertel 146: $x{"cgi.$id.OBJTYPE"}.='LINE:';
147: $i=$x{"cgi.$id.OBJCOUNT"}++;
1.58 albertel 148: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
1.49 albertel 149: "FFFFFF",($width+$extrawidth)));
150: $x{"cgi.$id.OBJTYPE"}.='LINE:';
151: $i=$x{"cgi.$id.OBJCOUNT"}++;
1.58 albertel 152: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
1.49 albertel 153: "FFFFFF",($width+$extrawidth)));
154: $x{"cgi.$id.OBJTYPE"}.='LINE:';
155: $i=$x{"cgi.$id.OBJCOUNT"}++;
1.58 albertel 156: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
1.49 albertel 157: "FF0000",($width)));
158: $x{"cgi.$id.OBJTYPE"}.='LINE:';
159: $i=$x{"cgi.$id.OBJCOUNT"}++;
1.58 albertel 160: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
1.49 albertel 161: "FF0000",($width)));
1.40 albertel 162: }
1.42 albertel 163: if ($mode eq 'answer') {
164: my $width = 1;
165: my $extrawidth = 2;
166: my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
167: foreach my $area (@areas) {
1.43 albertel 168: if ($area=~/^rectangle:/) {
1.49 albertel 169: $x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
170: $i=$x{"cgi.$id.OBJCOUNT"}++;
1.43 albertel 171: my ($x1,$y1,$x2,$y2)=
172: ($area=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
1.49 albertel 173: $x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"FFFFFF",
1.43 albertel 174: ($width+$extrawidth)));
1.49 albertel 175: $x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
176: $i=$x{"cgi.$id.OBJCOUNT"}++;
177: $x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"00FF00",$width));
1.43 albertel 178: } elsif ($area=~/^polygon:(.*)/) {
1.49 albertel 179: $x{"cgi.$id.OBJTYPE"}.='POLYGON:';
180: $i=$x{"cgi.$id.OBJCOUNT"}++;
181: $x{"cgi.$id.OBJ$i"}=join(':',("FFFFFF",($width+$extrawidth)));
182: $x{"cgi.$id.OBJEXTRA$i"}=$1;
183: $x{"cgi.$id.OBJTYPE"}.='POLYGON:';
184: $i=$x{"cgi.$id.OBJCOUNT"}++;
185: $x{"cgi.$id.OBJ$i"}=join(':',("00FF00",$width));
186: $x{"cgi.$id.OBJEXTRA$i"}=$1;
1.43 albertel 187: }
1.42 albertel 188: }
189: }
1.41 albertel 190: &Apache::lonnet::appenv(%x);
191: return $id;
1.40 albertel 192: }
193:
1.2 albertel 194: sub displayfoils {
1.36 albertel 195: my ($target,@whichopt) = @_;
196: my $result ='';
197: my $name;
198: my $temp=1;
199: foreach $name (@whichopt) {
200: $result.=$Apache::response::foilgroup{"$name.text"};
201: &Apache::lonxml::debug("Text is $result");
202: if ($target eq 'tex') {$result.="\\vskip 0 mm \n";} else {$result.="<br />\n";}
203: my $image=$Apache::response::foilgroup{"$name.image"};
204: &Apache::lonxml::debug("image is $image");
1.40 albertel 205: if ( $target eq 'web' && $image !~ /^http:/ ) {
1.47 albertel 206: $image=&clean_up_image($image);
207: }
1.40 albertel 208: &Apache::lonxml::debug("image is $image");
1.36 albertel 209: if ( &Apache::response::show_answer() ) {
210: if ($target eq 'tex') {
211: $result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
212: } else {
1.42 albertel 213: my $token=&prep_image($image,'answer',$name);
1.40 albertel 214: $result.="<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
1.36 albertel 215: }
216: } else {
217: if ($target eq 'tex') {
218: $result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
219: } else {
1.40 albertel 220: my $id=$Apache::inputtags::response['-1'];
221: my $token=&prep_image($image);
222: my $temp=1;
223: $result.="<input type=\"image\" name=\"HWVAL_$id:$temp\" ".
224: "src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
1.36 albertel 225: }
226: }
227: $temp++;
228: }
229: return $result;
1.47 albertel 230: }
231:
232: sub clean_up_image {
233: my ($image)=@_;
234: if ($image =~ /\s*<img\s*/) {
1.50 albertel 235: ($image) = ($image =~ /src\s*=\s*[\"\']([^\"\']+)[\"\']/i);
1.47 albertel 236: if ($image !~ /^http:/) {
237: $image=&Apache::lonnet::hreflocation('',$image);
238: }
239: if (!$image) {
240: $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
241: }
242: } else {
243: $image=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$image);
1.66 foxr 244: &Apache::lonxml::debug("repcopying: $image");
1.64 raeburn 245: if (&Apache::lonnet::repcopy($image) ne 'ok') {
1.47 albertel 246: $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
247: }
248: }
249: return $image;
1.2 albertel 250: }
251:
1.3 albertel 252: sub gradefoils {
1.36 albertel 253: my (@whichopt) = @_;
1.74 ! albertel 254:
! 255: my $partid = $Apache::inputtags::part;
! 256: my $id = $Apache::inputtags::response['-1'];
! 257:
! 258: if ($Apache::lonhomework::type eq 'exam') {
! 259: &Apache::response::scored_response($partid,$id);
! 260: return;
! 261: }
! 262:
1.36 albertel 263: my $x;
264: my $y;
265: my $result;
266: my $temp=1;
267: foreach my $name (@whichopt) {
1.65 albertel 268: $x=$env{"form.HWVAL_$id:$temp.x"};
269: $y=$env{"form.HWVAL_$id:$temp.y"};
1.36 albertel 270: &Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
271: if (defined($x) && defined($y) &&
272: defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
273: my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
274: my $grade="INCORRECT";
275: foreach my $area (@areas) {
276: &Apache::lonxml::debug("Area is $area for $name");
277: $area =~ m/([a-z]*):/;
278: &Apache::lonxml::debug("Area of type $1");
279: if ($1 eq 'rectangle') {
280: $grade=&grade_rectangle($area,$x,$y);
1.43 albertel 281: } elsif ($1 eq 'polygon') {
282: $grade=&grade_polygon($area,$x,$y);
1.36 albertel 283: } else {
284: &Apache::lonxml::error("Unknown area style $area");
285: }
286: &Apache::lonxml::debug("Area said $grade");
287: if ($grade eq 'APPROX_ANS') { last; }
288: }
289: &Apache::lonxml::debug("Foil was $grade");
290: if ($grade eq 'INCORRECT') { $result= 'INCORRECT'; }
291: if (($grade eq 'APPROX_ANS') && ($result ne 'APPROX_ANS')) { $result=$grade; }
292: &Apache::lonxml::debug("Question is $result");
293: $temp++;
1.9 albertel 294: }
1.36 albertel 295: }
1.60 albertel 296: my $responsestr="$x:$y";
297: my $part=$Apache::inputtags::part;
298: my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
1.56 albertel 299: if ($result
300: && $Apache::lonhomework::type eq 'survey') { $result='SUBMITTED'; }
1.60 albertel 301: &Apache::response::handle_previous(\%previous,$result);
302: $Apache::lonhomework::results{"resource.$part.$id.submission"}=$responsestr;
303: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$result;
1.74 ! albertel 304: return;
1.3 albertel 305: }
306:
1.1 albertel 307: sub end_foilgroup {
1.36 albertel 308: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
309: my $result='';
310: my @whichopt;
1.38 albertel 311: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
312: $target eq 'analyze') {
1.36 albertel 313: my ($count,$max) = &getfoilcounts($parstack,$safeeval);
314: if ($count>$max) { $count=$max }
315: &Apache::lonxml::debug("Count is $count from $max");
316: @whichopt = &whichfoils($max);
317: if ($target eq 'web' || $target eq 'tex') {
318: $result=&displayfoils($target,@whichopt);
319: } elsif ($target eq 'grade') {
1.65 albertel 320: if ( defined $env{'form.submitted'}) { &gradefoils(@whichopt); }
1.37 albertel 321: } elsif ( $target eq 'analyze') {
322: &Apache::response::analyze_store_foilgroup(\@whichopt,
323: ['text','image','area']);
324: }
1.36 albertel 325: } elsif ($target eq 'edit') {
326: $result=&Apache::edit::end_table();
327: }
1.53 albertel 328: &Apache::response::poprandomnumber();
1.36 albertel 329: return $result;
1.1 albertel 330: }
331:
332: sub start_conceptgroup {
1.36 albertel 333: $Apache::imageresponse::conceptgroup=1;
334: %Apache::response::conceptgroup=();
335: return '';
1.1 albertel 336: }
337:
338: sub end_conceptgroup {
1.36 albertel 339: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
340: $Apache::imageresponse::conceptgroup=0;
341: my $result;
1.37 albertel 342: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
343: $target eq 'analyze') {
344: &Apache::response::pick_foil_for_concept($target,
345: ['area','text','image'],
346: \%Apache::hint::image,
347: $parstack,$safeeval);
1.36 albertel 348: } elsif ($target eq 'edit') {
349: $result=&Apache::edit::end_table();
350: }
351: return $result;
1.31 albertel 352: }
353:
354: sub insert_foil {
355: return '
356: <foil>
357: <image></image>
358: <text></text>
359: <rectangle></rectangle>
360: </foil>
361: ';
1.1 albertel 362: }
363:
1.12 albertel 364: $Apache::imageresponse::curname='';
1.1 albertel 365: sub start_foil {
1.36 albertel 366: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.67 albertel 367: my $result;
1.37 albertel 368: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
369: $target eq 'analyze') {
1.36 albertel 370: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.72 albertel 371: if ($name eq "") {
1.71 albertel 372: &Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
1.59 albertel 373: $name=$Apache::lonxml::curdepth;
374: }
1.51 albertel 375: if (defined($Apache::response::foilnames{$name})) {
376: &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
377: }
1.52 albertel 378: $Apache::response::foilnames{$name}++;
1.36 albertel 379: if ( $Apache::imageresponse::conceptgroup
1.56 albertel 380: #&& !&Apache::response::showallfoils()
381: ) {
1.36 albertel 382: push(@{ $Apache::response::conceptgroup{'names'} }, $name);
383: } else {
384: push(@{ $Apache::response::foilgroup{'names'} }, $name);
385: }
386: $Apache::imageresponse::curname=$name;
1.67 albertel 387: } elsif ($target eq 'edit') {
388: $result = &Apache::edit::tag_start($target,$token);
389: $result .= &Apache::edit::text_arg('Name:','name',$token);
390: $result .= &Apache::edit::end_row().
391: &Apache::edit::start_spanning_row();
392: } elsif ($target eq 'modified') {
393: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
394: $safeeval,'name');
395: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
396: }
397: return $result;;
1.1 albertel 398: }
399:
400: sub end_foil {
1.26 albertel 401: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
402: my $result;
403: if ($target eq 'edit') {
404: $result=&Apache::edit::end_table();
405: }
406: return $result;
1.1 albertel 407: }
408:
409: sub start_text {
1.36 albertel 410: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
411: my $result='';
1.37 albertel 412: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.36 albertel 413: &Apache::lonxml::startredirection;
414: } elsif ($target eq 'edit') {
1.73 albertel 415: my $descr=&Apache::lonxml::get_all_text('/text',$parser,$style);
1.36 albertel 416: $result=&Apache::edit::tag_start($target,$token,'Task Description').
417: &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
418: &Apache::edit::end_row();
419: } elsif ($target eq "modified") {
1.39 albertel 420: $result=$token->[4].&Apache::edit::modifiedfield('/text',$parser);
1.36 albertel 421: }
422: return $result;
1.1 albertel 423: }
424:
425: sub end_text {
1.36 albertel 426: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
427: my $result;
1.37 albertel 428: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.36 albertel 429: my $name = $Apache::imageresponse::curname;
430: if ( $Apache::imageresponse::conceptgroup
1.56 albertel 431: #&& !&Apache::response::showallfoils()
432: ) {
1.36 albertel 433: $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
434: } else {
435: $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
436: }
437: } elsif ($target eq 'edit') {
438: $result=&Apache::edit::end_table();
439: }
440: return $result;
1.1 albertel 441: }
442:
443: sub start_image {
1.36 albertel 444: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
445: my $result='';
1.37 albertel 446: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.36 albertel 447: &Apache::lonxml::startredirection;
448: } elsif ($target eq 'edit') {
1.73 albertel 449: my $bgimg=&Apache::lonxml::get_all_text('/image',$parser,$style);
1.36 albertel 450: $Apache::edit::bgimgsrc=$bgimg;
451: $Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
452:
453: $result=&Apache::edit::tag_start($target,$token,'Clickable Image').
454: &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
455: $result.=&Apache::edit::browse(undef,'textnode').' ';
456: $result.=&Apache::edit::search(undef,'textnode').
457: &Apache::edit::end_row();
458: } elsif ($target eq "modified") {
1.39 albertel 459: $result=$token->[4].&Apache::edit::modifiedfield('/image',$parser);
1.36 albertel 460: }
461: return $result;
1.1 albertel 462: }
463:
464: sub end_image {
1.36 albertel 465: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
466: my $result;
467: my $name = $Apache::imageresponse::curname;
468: if ($target eq 'web') {
1.62 albertel 469: my $image = &Apache::lonxml::endredirection();
1.36 albertel 470: &Apache::lonxml::debug("original image is $image");
1.37 albertel 471: if ( $Apache::imageresponse::conceptgroup
1.56 albertel 472: #&& !&Apache::response::showallfoils()
473: ) {
1.37 albertel 474: $Apache::response::conceptgroup{"$name.image"} = $image;
475: } else {
476: $Apache::response::foilgroup{"$name.image"} = $image;
477: }
478: } elsif ($target eq 'analyze') {
1.62 albertel 479: my $image = &Apache::lonxml::endredirection();
1.36 albertel 480: if ( $Apache::imageresponse::conceptgroup
1.56 albertel 481: #&& !&Apache::response::showallfoils()
482: ) {
1.36 albertel 483: $Apache::response::conceptgroup{"$name.image"} = $image;
484: } else {
485: $Apache::response::foilgroup{"$name.image"} = $image;
486: }
487: } elsif ($target eq 'edit') {
488: $result=&Apache::edit::end_table();
489: } elsif ($target eq 'tex') {
1.62 albertel 490: my $src = &Apache::lonxml::endredirection();
1.68 foxr 491:
492: # There may be all sorts of whitespace on fore and aft:
493:
1.69 foxr 494: $src =~ s/\s+$//s;
495: $src =~ s/^\s+//s;
1.68 foxr 496:
497: #
498: # Gnuplot e.g. just generates the latex to put inplace.
499: #
500: my $graphinclude;
1.70 foxr 501: if ($src =~ /^%DYNAMICIMAGE/) {
502: # This is needed because the newline is not always passed -> tex.
503: # At present we don't care about the sizing info.
504:
505: my ($commentline, $restofstuff) = split(/\n/, $src);
1.68 foxr 506: $graphinclude = $src;
1.70 foxr 507: $graphinclude =~ s/^$commentline//;
1.68 foxr 508: } else {
509: my ($path,$file) = &Apache::londefdef::get_eps_image($src);
510: my ($height_param,$width_param)=
511: &Apache::londefdef::image_size($src,0.3,$parstack,$safeeval);
512: $graphinclude = '\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'}';
513: }
514: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent '.$graphinclude;
1.36 albertel 515: }
516: return $result;
1.1 albertel 517: }
518:
519: sub start_rectangle {
1.36 albertel 520: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
521: my $result='';
1.38 albertel 522: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
523: $target eq 'analyze') {
1.36 albertel 524: &Apache::lonxml::startredirection;
525: } elsif ($target eq 'edit') {
1.73 albertel 526: my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser,$style);
1.36 albertel 527: $result=&Apache::edit::tag_start($target,$token,'Rectangle').
528: &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
1.44 albertel 529: &Apache::edit::entercoord(undef,'textnode',undef,undef,'box').
1.36 albertel 530: &Apache::edit::end_row();
531: } elsif ($target eq "modified") {
1.44 albertel 532: &Apache::edit::deletecoorddata();
1.39 albertel 533: $result=$token->[4].&Apache::edit::modifiedfield('/rectangle',$parser);
1.36 albertel 534: }
535: return $result;
1.1 albertel 536: }
537:
1.3 albertel 538: sub grade_rectangle {
1.36 albertel 539: my ($spec,$x,$y) = @_;
540: &Apache::lonxml::debug("Spec is $spec");
1.43 albertel 541: my ($x1,$y1,$x2,$y2)=($spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
1.36 albertel 542: &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
543: if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
544: if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
545: if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
546: return 'APPROX_ANS';
547: }
548: return 'INCORRECT';
1.3 albertel 549: }
550:
1.1 albertel 551: sub end_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' ||
555: $target eq 'analyze') {
1.36 albertel 556: my $name = $Apache::imageresponse::curname;
557: my $area = &Apache::lonxml::endredirection;
1.54 albertel 558: $area=~s/\s//g;
1.36 albertel 559: &Apache::lonxml::debug("out is $area for $name");
560: if ( $Apache::imageresponse::conceptgroup
1.56 albertel 561: #&& !&Apache::response::showallfoils()
562: ) {
1.36 albertel 563: push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
564: } else {
565: push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
1.43 albertel 566: }
567: } elsif ($target eq 'edit') {
568: $result=&Apache::edit::end_table();
569: }
570: return $result;
571: }
572:
573: sub start_polygon {
574: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
575: my $result='';
576: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
577: $target eq 'analyze') {
578: &Apache::lonxml::startredirection;
579: } elsif ($target eq 'edit') {
1.73 albertel 580: my $coords=&Apache::lonxml::get_all_text('/polygon',$parser,$style);
1.43 albertel 581: $result=&Apache::edit::tag_start($target,$token,'Polygon').
582: &Apache::edit::editline($token->[1],$coords,'Coordinate list',40).
1.44 albertel 583: &Apache::edit::entercoord(undef,'textnode',undef,undef,'polygon').
1.43 albertel 584: &Apache::edit::end_row();
585: } elsif ($target eq "modified") {
586: $result=$token->[4].&Apache::edit::modifiedfield('/polygon',$parser);
587: }
588: return $result;
589: }
590:
591: sub grade_polygon {
592: my ($spec,$x,$y) = @_;
593: &Apache::lonxml::debug("Spec is $spec");
594: $spec=~s/^polygon://;
595: my @polygon;
596: foreach my $coord (split('-',$spec)) {
597: my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
598: &Apache::lonxml::debug("x $x y $y");
599: push @polygon, {'x'=>$x,'y'=>$y};
600: }
601: #make end point start point
602: push @polygon, $polygon[0];
603: # cribbed from
604: # http://geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm
605: my $crossing = 0; # the crossing number counter
606:
607: # loop through all edges of the polygon
608: for (my $i=0; $i<$#polygon; $i++) { # edge from V[i] to V[i+1]
609: if ((($polygon[$i]->{'y'} <= $y)
610: && ($polygon[$i+1]->{'y'} > $y)) # an upward crossing
611: ||
612: (($polygon[$i]->{'y'} > $y)
613: && ($polygon[$i+1]->{'y'} <= $y))) { # a downward crossing
614: # compute the actual edge-ray intersect x-coordinate
615: my $vt = ($y - $polygon[$i]->{'y'})
616: / ($polygon[$i+1]->{'y'} - $polygon[$i]->{'y'});
617: if ($x < $polygon[$i]->{'x'} + $vt *
618: ($polygon[$i+1]->{'x'} - $polygon[$i]->{'x'})) { # x<intersect
619: $crossing++; # a valid crossing of y=P.y right of P.x
620: }
621: }
622: }
623:
624: # 0 if even (out), and 1 if odd (in)
625: if ($crossing%2) {
626: return 'APPROX_ANS';
627: } else {
628: return 'INCORRECT';
629: }
630: }
631:
632: sub end_polygon {
633: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
634: my $result;
635: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
636: $target eq 'analyze') {
637: my $name = $Apache::imageresponse::curname;
638: my $area = &Apache::lonxml::endredirection;
1.48 albertel 639: $area=~s/\s*//g;
1.43 albertel 640: &Apache::lonxml::debug("out is $area for $name");
641: if ( $Apache::imageresponse::conceptgroup
1.56 albertel 642: #&& !&Apache::response::showallfoils()
643: ) {
1.43 albertel 644: push @{ $Apache::response::conceptgroup{"$name.area"} },"polygon:$area";
645: } else {
646: push @{ $Apache::response::foilgroup{"$name.area"} },"polygon:$area";
1.36 albertel 647: }
648: } elsif ($target eq 'edit') {
649: $result=&Apache::edit::end_table();
650: }
651: return $result;
1.1 albertel 652: }
653: 1;
654: __END__
655:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>