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