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