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