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