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