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