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