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