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