Annotation of loncom/homework/imageresponse.pm, revision 1.103
1.89 foxr 1: #
1.12 albertel 2: # The LearningOnline Network with CAPA
1.14 albertel 3: # image click response style
4: #
1.103 ! raeburn 5: # $Id: imageresponse.pm,v 1.102 2011/09/16 22:23:54 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.103 ! raeburn 182: my $repetition = &Apache::response::repetition();
! 183: &Apache::lonxml::increment_counter($repetition,
1.90 foxr 184: "$part_id.$response_id");
1.89 foxr 185: if ($target eq 'analyze') {
186: &Apache::lonhomework::set_bubble_lines();
187: }
188:
1.74 albertel 189: }
190: &Apache::response::end_response();
1.30 albertel 191: return $result;
1.1 albertel 192: }
193:
1.20 albertel 194: %Apache::response::foilgroup=();
1.1 albertel 195: sub start_foilgroup {
1.100 raeburn 196: my ($target) = @_;
1.36 albertel 197: %Apache::response::foilgroup=();
198: $Apache::imageresponse::conceptgroup=0;
1.100 raeburn 199: &Apache::response::pushrandomnumber(undef,$target);
1.36 albertel 200: return '';
1.1 albertel 201: }
202:
1.2 albertel 203: sub getfoilcounts {
1.36 albertel 204: my ($parstack,$safeeval)=@_;
1.12 albertel 205:
1.36 albertel 206: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
207: # +1 since instructors will count from 1
208: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
1.88 albertel 209: if (&Apache::response::showallfoils()) { $max=$count; }
1.36 albertel 210: return ($count,$max);
1.2 albertel 211: }
212:
213: sub whichfoils {
1.36 albertel 214: my ($max)=@_;
1.88 albertel 215: return if (!defined(@{ $Apache::response::foilgroup{'names'} }));
1.36 albertel 216: my @names = @{ $Apache::response::foilgroup{'names'} };
1.88 albertel 217: my @whichopt;
1.36 albertel 218: while ((($#whichopt+1) < $max) && ($#names > -1)) {
219: &Apache::lonxml::debug("Have $#whichopt max is $max");
220: my $aopt;
1.88 albertel 221: if (&Apache::response::showallfoils()) {
222: $aopt=0;
223: } else {
1.36 albertel 224: $aopt=int(&Math::Random::random_uniform() * ($#names+1));
1.88 albertel 225: }
1.36 albertel 226: &Apache::lonxml::debug("From $#names elms, picking $aopt");
227: $aopt=splice(@names,$aopt,1);
228: &Apache::lonxml::debug("Picked $aopt");
1.88 albertel 229: push(@whichopt,$aopt);
1.36 albertel 230: }
231: return @whichopt;
1.2 albertel 232: }
233:
1.40 albertel 234: sub prep_image {
1.42 albertel 235: my ($image,$mode,$name)=@_;
1.85 albertel 236:
237: my ($x,$y)= &get_submission($name);
238: &Apache::lonxml::debug("for $name drawing click at $x and $y");
1.82 albertel 239: &draw_image($mode,$image,$x,$y,$Apache::response::foilgroup{"$name.area"});
240: }
241:
242: sub draw_image {
243: my ($mode,$image,$x,$y,$areas) = @_;
244:
1.41 albertel 245: my $id=&Apache::loncommon::get_cgi_id();
1.82 albertel 246:
1.49 albertel 247: my (%x,$i);
1.78 www 248: $x{"cgi.$id.BGIMG"}=&escape($image);
1.82 albertel 249:
1.40 albertel 250: #draws 2 xs on the image at the clicked location
251: #one in white and then one in red on top of the one in white
1.82 albertel 252:
1.75 albertel 253: if (defined($x) && $x =~/\S/
254: && defined($y) && $y =~/\S/
1.82 albertel 255: && ($mode eq 'submission' || !&Apache::response::show_answer())
1.75 albertel 256: && $mode ne 'answeronly') {
1.40 albertel 257: my $length = 6;
258: my $width = 1;
259: my $extrawidth = 2;
1.48 albertel 260: my $xmin=($x-$length);
261: my $xmax=($x+$length);
262: my $ymin=($y-$length);
263: my $ymax=($y+$length);
264:
1.49 albertel 265: $x{"cgi.$id.OBJTYPE"}.='LINE:';
266: $i=$x{"cgi.$id.OBJCOUNT"}++;
1.58 albertel 267: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
1.49 albertel 268: "FFFFFF",($width+$extrawidth)));
269: $x{"cgi.$id.OBJTYPE"}.='LINE:';
270: $i=$x{"cgi.$id.OBJCOUNT"}++;
1.58 albertel 271: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
1.49 albertel 272: "FFFFFF",($width+$extrawidth)));
273: $x{"cgi.$id.OBJTYPE"}.='LINE:';
274: $i=$x{"cgi.$id.OBJCOUNT"}++;
1.58 albertel 275: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
1.49 albertel 276: "FF0000",($width)));
277: $x{"cgi.$id.OBJTYPE"}.='LINE:';
278: $i=$x{"cgi.$id.OBJCOUNT"}++;
1.58 albertel 279: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
1.49 albertel 280: "FF0000",($width)));
1.40 albertel 281: }
1.75 albertel 282: if ($mode eq 'answer' || $mode eq 'answeronly') {
1.42 albertel 283: my $width = 1;
284: my $extrawidth = 2;
1.82 albertel 285: foreach my $area (@{ $areas }) {
1.43 albertel 286: if ($area=~/^rectangle:/) {
1.49 albertel 287: $x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
288: $i=$x{"cgi.$id.OBJCOUNT"}++;
1.43 albertel 289: my ($x1,$y1,$x2,$y2)=
290: ($area=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
1.49 albertel 291: $x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"FFFFFF",
1.43 albertel 292: ($width+$extrawidth)));
1.49 albertel 293: $x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
294: $i=$x{"cgi.$id.OBJCOUNT"}++;
295: $x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"00FF00",$width));
1.43 albertel 296: } elsif ($area=~/^polygon:(.*)/) {
1.49 albertel 297: $x{"cgi.$id.OBJTYPE"}.='POLYGON:';
298: $i=$x{"cgi.$id.OBJCOUNT"}++;
299: $x{"cgi.$id.OBJ$i"}=join(':',("FFFFFF",($width+$extrawidth)));
300: $x{"cgi.$id.OBJEXTRA$i"}=$1;
301: $x{"cgi.$id.OBJTYPE"}.='POLYGON:';
302: $i=$x{"cgi.$id.OBJCOUNT"}++;
303: $x{"cgi.$id.OBJ$i"}=join(':',("00FF00",$width));
304: $x{"cgi.$id.OBJEXTRA$i"}=$1;
1.43 albertel 305: }
1.42 albertel 306: }
307: }
1.93 raeburn 308: &Apache::lonnet::appenv(\%x);
1.41 albertel 309: return $id;
1.40 albertel 310: }
311:
1.2 albertel 312: sub displayfoils {
1.36 albertel 313: my ($target,@whichopt) = @_;
314: my $result ='';
315: my $temp=1;
1.82 albertel 316: my @images;
1.85 albertel 317: foreach my $name (@whichopt) {
1.36 albertel 318: $result.=$Apache::response::foilgroup{"$name.text"};
319: &Apache::lonxml::debug("Text is $result");
320: if ($target eq 'tex') {$result.="\\vskip 0 mm \n";} else {$result.="<br />\n";}
321: my $image=$Apache::response::foilgroup{"$name.image"};
322: &Apache::lonxml::debug("image is $image");
1.91 albertel 323: if ( ($target eq 'web' || $target eq 'answer')
1.97 raeburn 324: && $image !~ /^https?\:/ ) {
1.47 albertel 325: $image=&clean_up_image($image);
1.82 albertel 326: }
327: push(@images,$image);
1.40 albertel 328: &Apache::lonxml::debug("image is $image");
1.36 albertel 329: if ( &Apache::response::show_answer() ) {
330: if ($target eq 'tex') {
331: $result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
332: } else {
1.42 albertel 333: my $token=&prep_image($image,'answer',$name);
1.40 albertel 334: $result.="<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
1.36 albertel 335: }
336: } else {
337: if ($target eq 'tex') {
338: $result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
339: } else {
1.85 albertel 340: my $respid=$Apache::inputtags::response['-1'];
341: my $token=&prep_image($image,'submission',$name);
1.87 albertel 342: my $input_id = "HWVAL_$respid:$temp";
343: my $id = $env{'form.request.prefix'}.$input_id;
1.85 albertel 344: $result.='<img onclick="image_response_click(\''.$id.'\',event);"
345: src="/adm/randomlabel.png?token='.$token.'"
1.87 albertel 346: id="'.$id.'_imageresponse"
1.85 albertel 347: />'.
348: '<br />'.
1.87 albertel 349: '<input type="hidden" name="'.$input_id.'_token" value="'.$token.'" />'.
350: '<input type="hidden" name="'.$input_id.'" value="'.
1.85 albertel 351: join(':',&get_submission($name)).'" />';
1.36 albertel 352: }
353: }
354: $temp++;
355: }
1.82 albertel 356: if ($target eq 'web') {
1.100 raeburn 357: &get_prior_options(\@images,\@whichopt);
1.82 albertel 358: }
1.36 albertel 359: return $result;
1.47 albertel 360: }
361:
1.100 raeburn 362: sub get_prior_options {
363: my ($currimages,$curropt) = @_;
364: return unless((ref($curropt) eq 'ARRAY') &&
365: (ref($currimages) eq 'ARRAY'));
366: my $part = $Apache::inputtags::part;
367: my $respid = $Apache::inputtags::response[-1];
368: foreach my $i (1..$Apache::lonhomework::history{'version'}) {
369: my $partprefix = "$i:resource.$part";
370: my $sub_key = "$partprefix.$respid.submission";
371: next if (!exists($Apache::lonhomework::history{$sub_key}));
372: my $type_key = "$partprefix.type";
373: my @whichopt = ();
374: my @images = ();
375: if ($Apache::lonhomework::history{$type_key} eq 'randomizetry') {
376: my $order_key = "$partprefix.$respid.foilorder";
377: @whichopt = &Apache::lonnet::str2array($Apache::lonhomework::history{$order_key});
378: if (@whichopt > 0) {
379: foreach my $name (@whichopt) {
380: my $image=$Apache::response::foilgroup{"$name.image"};
381: if ($image !~ /^https?\:/ ) {
382: $image=&clean_up_image($image);
383: }
384: push(@images,$image);
385: }
386: }
387: } else {
388: @whichopt = @{$curropt};
389: @images = @{$currimages};
390: }
391: my $submission = $Apache::lonhomework::history{$sub_key};
392: my $output = &format_prior_response('grade',$submission,
393: [\@images,\@whichopt]);
394: if (defined($output)) {
395: $Apache::inputtags::submission_display{$sub_key} = $output;
396: }
397: }
398: }
399:
1.82 albertel 400: sub format_prior_response {
401: my ($mode,$answer,$other_data) = @_;
1.85 albertel 402:
403: my $result;
1.82 albertel 404:
1.85 albertel 405: # make a copy of the data in the refs
406: my @images = @{ $other_data->[0] };
407: my @foils = @{ $other_data->[1] };
408: foreach my $name (@foils) {
409: my $image = pop(@images);
410: my ($x,$y) = &get_submission($name,$answer);
411: my $token = &draw_image('submission',$image,$x,$y);
412: $result .=
413: '<img class="LC_prior_image"
414: src="/adm/randomlabel.png?token='.$token.'" /><br />';
415: }
416: return $result;
1.82 albertel 417: }
418:
1.75 albertel 419: sub display_answers {
420: my ($target,$whichopt)=@_;
421:
1.88 albertel 422: my $result=&Apache::response::answer_header('imageresponse');
1.75 albertel 423: foreach my $name (@$whichopt) {
424: my $image=$Apache::response::foilgroup{"$name.image"};
425: &Apache::lonxml::debug("image is $image");
1.91 albertel 426: if ( ($target eq 'web' || $target eq 'answer')
1.97 raeburn 427: && $image !~ /^https?\:/ ) {
1.75 albertel 428: $image = &clean_up_image($image);
429: }
430: my $token=&prep_image($image,'answeronly',$name);
431:
432: $result.=&Apache::response::answer_part('imageresponse',"<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n");
433: }
1.88 albertel 434: $result.=&Apache::response::answer_footer('imageresponse');
1.75 albertel 435: return $result;
436: }
437:
1.47 albertel 438: sub clean_up_image {
439: my ($image)=@_;
440: if ($image =~ /\s*<img\s*/) {
1.50 albertel 441: ($image) = ($image =~ /src\s*=\s*[\"\']([^\"\']+)[\"\']/i);
1.97 raeburn 442: if ($image !~ /^https?\:/) {
1.47 albertel 443: $image=&Apache::lonnet::hreflocation('',$image);
444: }
445: if (!$image) {
446: $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
447: }
448: } else {
449: $image=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$image);
1.66 foxr 450: &Apache::lonxml::debug("repcopying: $image");
1.64 raeburn 451: if (&Apache::lonnet::repcopy($image) ne 'ok') {
1.47 albertel 452: $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
453: }
454: }
455: return $image;
1.2 albertel 456: }
457:
1.3 albertel 458: sub gradefoils {
1.36 albertel 459: my (@whichopt) = @_;
1.74 albertel 460:
461: my $partid = $Apache::inputtags::part;
462: my $id = $Apache::inputtags::response['-1'];
463:
464: if ($Apache::lonhomework::type eq 'exam') {
465: &Apache::response::scored_response($partid,$id);
466: return;
467: }
468:
1.85 albertel 469: my @results;
1.36 albertel 470: my $temp=1;
1.85 albertel 471: my %response;
1.36 albertel 472: foreach my $name (@whichopt) {
1.85 albertel 473: my ($x,$y) = split(':',$env{"form.HWVAL_$id:$temp"});
474: $response{$name} = $env{"form.HWVAL_$id:$temp"};
1.36 albertel 475: &Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
476: if (defined($x) && defined($y) &&
477: defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
478: my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
479: my $grade="INCORRECT";
480: foreach my $area (@areas) {
481: &Apache::lonxml::debug("Area is $area for $name");
482: $area =~ m/([a-z]*):/;
483: &Apache::lonxml::debug("Area of type $1");
484: if ($1 eq 'rectangle') {
485: $grade=&grade_rectangle($area,$x,$y);
1.43 albertel 486: } elsif ($1 eq 'polygon') {
487: $grade=&grade_polygon($area,$x,$y);
1.36 albertel 488: } else {
1.94 bisitz 489: &Apache::lonxml::error(&mt('Unknown area style [_1]',$area));
1.36 albertel 490: }
491: &Apache::lonxml::debug("Area said $grade");
492: if ($grade eq 'APPROX_ANS') { last; }
493: }
494: &Apache::lonxml::debug("Foil was $grade");
1.85 albertel 495: push(@results, $grade)
496: } else {
497: push(@results, 'MISSING_ANSWER')
1.9 albertel 498: }
1.85 albertel 499: $temp++;
1.36 albertel 500: }
1.100 raeburn 501: if ($Apache::lonhomework::type eq 'randomizetry') {
502: $Apache::lonhomework::results{"resource.$partid.$id.foilorder"} = &Apache::lonnet::array2str(@whichopt);
503: }
1.85 albertel 504: my ($result) = &Apache::inputtags::finalizeawards(\@results,[]);
505: &Apache::lonxml::debug("Question is $result");
506:
1.60 albertel 507: my $part=$Apache::inputtags::part;
1.85 albertel 508: my %previous=
509: &Apache::response::check_for_previous(&stringify_submission(\%response),
510: $part,$id);
1.99 raeburn 511: if ($result) {
512: if ($Apache::lonhomework::type eq 'survey') {
513: $result='SUBMITTED';
514: } elsif ($Apache::lonhomework::type eq 'surveycred') {
515: $result='SUBMITTED_CREDIT';
516: } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
517: $result='ANONYMOUS';
518: } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
519: $result='ANONYMOUS_CREDIT';
520: }
521: }
522:
1.60 albertel 523: &Apache::response::handle_previous(\%previous,$result);
1.85 albertel 524: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
525: &stringify_submission(\%response);
1.60 albertel 526: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$result;
1.74 albertel 527: return;
1.3 albertel 528: }
529:
1.85 albertel 530: sub stringify_submission {
531: my ($response) = @_;
1.86 albertel 532: return &Apache::lonnet::hash2str(%{ $response });
1.85 albertel 533: }
534:
535: sub get_submission {
536: my ($name,$string) = @_;
537:
538: if (!defined($string)) {
539: my $part=$Apache::inputtags::part;
540: my $respid=$Apache::inputtags::response['-1'];
541: $string =
542: $Apache::lonhomework::history{"resource.$part.$respid.submission"};
543: }
544:
1.86 albertel 545: if ($string !~ /=/) {
1.85 albertel 546: return split(':',$string);
547: } else {
1.86 albertel 548: my %response = &Apache::lonnet::str2hash($string);
1.85 albertel 549: return split(':',$response{$name});
550: }
551: }
552:
1.1 albertel 553: sub end_foilgroup {
1.36 albertel 554: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
555: my $result='';
556: my @whichopt;
1.75 albertel 557:
1.38 albertel 558: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75 albertel 559: $target eq 'analyze' || $target eq 'answer') {
560:
1.36 albertel 561: my ($count,$max) = &getfoilcounts($parstack,$safeeval);
562: if ($count>$max) { $count=$max }
563: &Apache::lonxml::debug("Count is $count from $max");
1.75 albertel 564:
1.36 albertel 565: @whichopt = &whichfoils($max);
1.75 albertel 566:
1.36 albertel 567: if ($target eq 'web' || $target eq 'tex') {
568: $result=&displayfoils($target,@whichopt);
1.100 raeburn 569: if ($Apache::lonhomework::type eq 'randomizetry') {
570: if ($target eq 'web') {
571: &get_prior_options($target,@whichopt);
572: }
573: }
1.81 albertel 574: $Apache::lonxml::post_evaluate=0;
1.36 albertel 575: } elsif ($target eq 'grade') {
1.65 albertel 576: if ( defined $env{'form.submitted'}) { &gradefoils(@whichopt); }
1.37 albertel 577: } elsif ( $target eq 'analyze') {
578: &Apache::response::analyze_store_foilgroup(\@whichopt,
579: ['text','image','area']);
1.76 albertel 580: } elsif ($target eq 'answer'
581: && $env{'form.answer_output_mode'} ne 'tex') {
1.75 albertel 582: $result=&display_answers($target,\@whichopt);
1.37 albertel 583: }
1.75 albertel 584:
1.36 albertel 585: } elsif ($target eq 'edit') {
586: $result=&Apache::edit::end_table();
587: }
1.53 albertel 588: &Apache::response::poprandomnumber();
1.36 albertel 589: return $result;
1.1 albertel 590: }
591:
592: sub start_conceptgroup {
1.36 albertel 593: $Apache::imageresponse::conceptgroup=1;
594: %Apache::response::conceptgroup=();
595: return '';
1.1 albertel 596: }
597:
598: sub end_conceptgroup {
1.36 albertel 599: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
600: $Apache::imageresponse::conceptgroup=0;
601: my $result;
1.37 albertel 602: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
603: $target eq 'analyze') {
604: &Apache::response::pick_foil_for_concept($target,
605: ['area','text','image'],
606: \%Apache::hint::image,
607: $parstack,$safeeval);
1.36 albertel 608: } elsif ($target eq 'edit') {
609: $result=&Apache::edit::end_table();
610: }
611: return $result;
1.31 albertel 612: }
613:
614: sub insert_foil {
615: return '
616: <foil>
617: <image></image>
618: <text></text>
619: <rectangle></rectangle>
620: </foil>
621: ';
1.1 albertel 622: }
623:
1.12 albertel 624: $Apache::imageresponse::curname='';
1.1 albertel 625: sub start_foil {
1.36 albertel 626: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.67 albertel 627: my $result;
1.37 albertel 628: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75 albertel 629: $target eq 'analyze' || $target eq 'answer') {
1.36 albertel 630: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.72 albertel 631: if ($name eq "") {
1.94 bisitz 632: &Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
1.59 albertel 633: $name=$Apache::lonxml::curdepth;
634: }
1.51 albertel 635: if (defined($Apache::response::foilnames{$name})) {
1.94 bisitz 636: &Apache::lonxml::error(&mt("Foil name [_1] appears more than once. Foil names need to be unique."
637: ,'<b><tt>'.$name.'</tt></b>'));
1.51 albertel 638: }
1.52 albertel 639: $Apache::response::foilnames{$name}++;
1.36 albertel 640: if ( $Apache::imageresponse::conceptgroup
1.88 albertel 641: && !&Apache::response::showallfoils()
1.56 albertel 642: ) {
1.36 albertel 643: push(@{ $Apache::response::conceptgroup{'names'} }, $name);
644: } else {
645: push(@{ $Apache::response::foilgroup{'names'} }, $name);
646: }
647: $Apache::imageresponse::curname=$name;
1.67 albertel 648: } elsif ($target eq 'edit') {
649: $result = &Apache::edit::tag_start($target,$token);
650: $result .= &Apache::edit::text_arg('Name:','name',$token);
651: $result .= &Apache::edit::end_row().
652: &Apache::edit::start_spanning_row();
653: } elsif ($target eq 'modified') {
654: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
655: $safeeval,'name');
656: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
657: }
658: return $result;;
1.1 albertel 659: }
660:
661: sub end_foil {
1.26 albertel 662: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
663: my $result;
664: if ($target eq 'edit') {
665: $result=&Apache::edit::end_table();
666: }
667: return $result;
1.1 albertel 668: }
669:
670: sub start_text {
1.36 albertel 671: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
672: my $result='';
1.75 albertel 673: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
674: || $target eq 'answer') {
1.36 albertel 675: &Apache::lonxml::startredirection;
676: } elsif ($target eq 'edit') {
1.73 albertel 677: my $descr=&Apache::lonxml::get_all_text('/text',$parser,$style);
1.36 albertel 678: $result=&Apache::edit::tag_start($target,$token,'Task Description').
679: &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
680: &Apache::edit::end_row();
681: } elsif ($target eq "modified") {
1.39 albertel 682: $result=$token->[4].&Apache::edit::modifiedfield('/text',$parser);
1.36 albertel 683: }
684: return $result;
1.1 albertel 685: }
686:
687: sub end_text {
1.36 albertel 688: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
689: my $result;
1.75 albertel 690: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
691: || $target eq 'answer') {
1.36 albertel 692: my $name = $Apache::imageresponse::curname;
693: if ( $Apache::imageresponse::conceptgroup
1.88 albertel 694: && !&Apache::response::showallfoils()
1.56 albertel 695: ) {
1.36 albertel 696: $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
697: } else {
698: $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
699: }
700: } elsif ($target eq 'edit') {
701: $result=&Apache::edit::end_table();
702: }
703: return $result;
1.1 albertel 704: }
705:
706: sub start_image {
1.36 albertel 707: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
708: my $result='';
1.83 banghart 709: my $only = join(',',&Apache::loncommon::filecategorytypes('Pictures'));
1.75 albertel 710: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
711: || $target eq 'answer') {
1.36 albertel 712: &Apache::lonxml::startredirection;
713: } elsif ($target eq 'edit') {
1.73 albertel 714: my $bgimg=&Apache::lonxml::get_all_text('/image',$parser,$style);
1.36 albertel 715: $Apache::edit::bgimgsrc=$bgimg;
716: $Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
717:
718: $result=&Apache::edit::tag_start($target,$token,'Clickable Image').
719: &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
1.83 banghart 720: $result.=&Apache::edit::browse(undef,'textnode',undef,$only).' ';
1.36 albertel 721: $result.=&Apache::edit::search(undef,'textnode').
722: &Apache::edit::end_row();
723: } elsif ($target eq "modified") {
1.39 albertel 724: $result=$token->[4].&Apache::edit::modifiedfield('/image',$parser);
1.36 albertel 725: }
726: return $result;
1.1 albertel 727: }
728:
729: sub end_image {
1.36 albertel 730: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
731: my $result;
732: my $name = $Apache::imageresponse::curname;
1.75 albertel 733: if ($target eq 'web' || $target eq 'answer') {
1.62 albertel 734: my $image = &Apache::lonxml::endredirection();
1.36 albertel 735: &Apache::lonxml::debug("original image is $image");
1.37 albertel 736: if ( $Apache::imageresponse::conceptgroup
1.88 albertel 737: && !&Apache::response::showallfoils()
1.56 albertel 738: ) {
1.37 albertel 739: $Apache::response::conceptgroup{"$name.image"} = $image;
740: } else {
741: $Apache::response::foilgroup{"$name.image"} = $image;
742: }
743: } elsif ($target eq 'analyze') {
1.62 albertel 744: my $image = &Apache::lonxml::endredirection();
1.36 albertel 745: if ( $Apache::imageresponse::conceptgroup
1.88 albertel 746: && !&Apache::response::showallfoils()
1.56 albertel 747: ) {
1.36 albertel 748: $Apache::response::conceptgroup{"$name.image"} = $image;
749: } else {
750: $Apache::response::foilgroup{"$name.image"} = $image;
751: }
752: } elsif ($target eq 'edit') {
753: $result=&Apache::edit::end_table();
754: } elsif ($target eq 'tex') {
1.62 albertel 755: my $src = &Apache::lonxml::endredirection();
1.68 foxr 756:
757: # There may be all sorts of whitespace on fore and aft:
758:
1.69 foxr 759: $src =~ s/\s+$//s;
760: $src =~ s/^\s+//s;
1.68 foxr 761:
762: #
763: # Gnuplot e.g. just generates the latex to put inplace.
764: #
765: my $graphinclude;
1.70 foxr 766: if ($src =~ /^%DYNAMICIMAGE/) {
767: # This is needed because the newline is not always passed -> tex.
768: # At present we don't care about the sizing info.
769:
770: my ($commentline, $restofstuff) = split(/\n/, $src);
1.68 foxr 771: $graphinclude = $src;
1.70 foxr 772: $graphinclude =~ s/^$commentline//;
1.98 foxr 773: } elsif (!($src =~ /\\/)) {
1.68 foxr 774: my ($path,$file) = &Apache::londefdef::get_eps_image($src);
775: my ($height_param,$width_param)=
776: &Apache::londefdef::image_size($src,0.3,$parstack,$safeeval);
777: $graphinclude = '\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'}';
1.98 foxr 778: } else {
779: $graphinclude = $src; # Already fully formed.
1.68 foxr 780: }
781: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent '.$graphinclude;
1.36 albertel 782: }
783: return $result;
1.1 albertel 784: }
785:
786: sub start_rectangle {
1.36 albertel 787: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
788: my $result='';
1.38 albertel 789: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75 albertel 790: $target eq 'analyze' || $target eq 'answer') {
1.36 albertel 791: &Apache::lonxml::startredirection;
792: } elsif ($target eq 'edit') {
1.73 albertel 793: my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser,$style);
1.36 albertel 794: $result=&Apache::edit::tag_start($target,$token,'Rectangle').
795: &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
1.44 albertel 796: &Apache::edit::entercoord(undef,'textnode',undef,undef,'box').
1.36 albertel 797: &Apache::edit::end_row();
798: } elsif ($target eq "modified") {
1.44 albertel 799: &Apache::edit::deletecoorddata();
1.39 albertel 800: $result=$token->[4].&Apache::edit::modifiedfield('/rectangle',$parser);
1.36 albertel 801: }
802: return $result;
1.1 albertel 803: }
804:
1.3 albertel 805: sub grade_rectangle {
1.36 albertel 806: my ($spec,$x,$y) = @_;
807: &Apache::lonxml::debug("Spec is $spec");
1.43 albertel 808: my ($x1,$y1,$x2,$y2)=($spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
1.36 albertel 809: &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
810: if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
811: if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
812: if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
813: return 'APPROX_ANS';
814: }
815: return 'INCORRECT';
1.3 albertel 816: }
817:
1.1 albertel 818: sub end_rectangle {
1.36 albertel 819: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
820: my $result;
1.38 albertel 821: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75 albertel 822: $target eq 'analyze' || $target eq 'answer') {
1.36 albertel 823: my $name = $Apache::imageresponse::curname;
824: my $area = &Apache::lonxml::endredirection;
1.54 albertel 825: $area=~s/\s//g;
1.36 albertel 826: &Apache::lonxml::debug("out is $area for $name");
827: if ( $Apache::imageresponse::conceptgroup
1.88 albertel 828: && !&Apache::response::showallfoils()
1.56 albertel 829: ) {
1.36 albertel 830: push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
831: } else {
832: push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
1.43 albertel 833: }
834: } elsif ($target eq 'edit') {
835: $result=&Apache::edit::end_table();
836: }
837: return $result;
838: }
839:
840: sub start_polygon {
841: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
842: my $result='';
843: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75 albertel 844: $target eq 'analyze' || $target eq 'answer') {
1.43 albertel 845: &Apache::lonxml::startredirection;
846: } elsif ($target eq 'edit') {
1.73 albertel 847: my $coords=&Apache::lonxml::get_all_text('/polygon',$parser,$style);
1.43 albertel 848: $result=&Apache::edit::tag_start($target,$token,'Polygon').
849: &Apache::edit::editline($token->[1],$coords,'Coordinate list',40).
1.44 albertel 850: &Apache::edit::entercoord(undef,'textnode',undef,undef,'polygon').
1.43 albertel 851: &Apache::edit::end_row();
852: } elsif ($target eq "modified") {
853: $result=$token->[4].&Apache::edit::modifiedfield('/polygon',$parser);
854: }
855: return $result;
856: }
857:
858: sub grade_polygon {
859: my ($spec,$x,$y) = @_;
860: &Apache::lonxml::debug("Spec is $spec");
861: $spec=~s/^polygon://;
862: my @polygon;
863: foreach my $coord (split('-',$spec)) {
864: my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
865: &Apache::lonxml::debug("x $x y $y");
866: push @polygon, {'x'=>$x,'y'=>$y};
867: }
868: #make end point start point
869: push @polygon, $polygon[0];
870: # cribbed from
871: # http://geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm
872: my $crossing = 0; # the crossing number counter
873:
874: # loop through all edges of the polygon
875: for (my $i=0; $i<$#polygon; $i++) { # edge from V[i] to V[i+1]
876: if ((($polygon[$i]->{'y'} <= $y)
877: && ($polygon[$i+1]->{'y'} > $y)) # an upward crossing
878: ||
879: (($polygon[$i]->{'y'} > $y)
880: && ($polygon[$i+1]->{'y'} <= $y))) { # a downward crossing
881: # compute the actual edge-ray intersect x-coordinate
882: my $vt = ($y - $polygon[$i]->{'y'})
883: / ($polygon[$i+1]->{'y'} - $polygon[$i]->{'y'});
884: if ($x < $polygon[$i]->{'x'} + $vt *
885: ($polygon[$i+1]->{'x'} - $polygon[$i]->{'x'})) { # x<intersect
886: $crossing++; # a valid crossing of y=P.y right of P.x
887: }
888: }
889: }
890:
891: # 0 if even (out), and 1 if odd (in)
892: if ($crossing%2) {
893: return 'APPROX_ANS';
894: } else {
895: return 'INCORRECT';
896: }
897: }
898:
899: sub end_polygon {
900: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
901: my $result;
902: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75 albertel 903: $target eq 'analyze' || $target eq 'answer') {
1.43 albertel 904: my $name = $Apache::imageresponse::curname;
905: my $area = &Apache::lonxml::endredirection;
1.48 albertel 906: $area=~s/\s*//g;
1.43 albertel 907: &Apache::lonxml::debug("out is $area for $name");
908: if ( $Apache::imageresponse::conceptgroup
1.88 albertel 909: && !&Apache::response::showallfoils()
1.56 albertel 910: ) {
1.43 albertel 911: push @{ $Apache::response::conceptgroup{"$name.area"} },"polygon:$area";
912: } else {
913: push @{ $Apache::response::foilgroup{"$name.area"} },"polygon:$area";
1.36 albertel 914: }
915: } elsif ($target eq 'edit') {
916: $result=&Apache::edit::end_table();
917: }
918: return $result;
1.1 albertel 919: }
920: 1;
921: __END__
922:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>