Annotation of loncom/homework/imageresponse.pm, revision 1.87
1.58 albertel 1:
1.12 albertel 2: # The LearningOnline Network with CAPA
1.14 albertel 3: # image click response style
4: #
1.86 albertel 5: # $Id: imageresponse.pm,v 1.85 2007/09/25 23:16:13 albertel 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.85 albertel 135:
136: my ($x,$y)= &get_submission($name);
137: &Apache::lonxml::debug("for $name drawing click at $x and $y");
1.82 albertel 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 $temp=1;
1.82 albertel 215: my @images;
1.85 albertel 216: foreach my $name (@whichopt) {
1.36 albertel 217: $result.=$Apache::response::foilgroup{"$name.text"};
218: &Apache::lonxml::debug("Text is $result");
219: if ($target eq 'tex') {$result.="\\vskip 0 mm \n";} else {$result.="<br />\n";}
220: my $image=$Apache::response::foilgroup{"$name.image"};
221: &Apache::lonxml::debug("image is $image");
1.40 albertel 222: if ( $target eq 'web' && $image !~ /^http:/ ) {
1.47 albertel 223: $image=&clean_up_image($image);
1.82 albertel 224: }
225: push(@images,$image);
1.40 albertel 226: &Apache::lonxml::debug("image is $image");
1.36 albertel 227: if ( &Apache::response::show_answer() ) {
228: if ($target eq 'tex') {
229: $result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
230: } else {
1.42 albertel 231: my $token=&prep_image($image,'answer',$name);
1.40 albertel 232: $result.="<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
1.36 albertel 233: }
234: } else {
235: if ($target eq 'tex') {
236: $result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
237: } else {
1.85 albertel 238: my $respid=$Apache::inputtags::response['-1'];
239: my $token=&prep_image($image,'submission',$name);
1.87 ! albertel 240: my $input_id = "HWVAL_$respid:$temp";
! 241: my $id = $env{'form.request.prefix'}.$input_id;
1.85 albertel 242: $result.='<img onclick="image_response_click(\''.$id.'\',event);"
243: src="/adm/randomlabel.png?token='.$token.'"
1.87 ! albertel 244: id="'.$id.'_imageresponse"
1.85 albertel 245: />'.
246: '<br />'.
1.87 ! albertel 247: '<input type="hidden" name="'.$input_id.'_token" value="'.$token.'" />'.
! 248: '<input type="hidden" name="'.$input_id.'" value="'.
1.85 albertel 249: join(':',&get_submission($name)).'" />';
1.36 albertel 250: }
251: }
252: $temp++;
253: }
1.82 albertel 254: if ($target eq 'web') {
255: &Apache::response::setup_prior_tries_hash(\&format_prior_response,
1.85 albertel 256: [\@images,\@whichopt]);
1.82 albertel 257: }
1.36 albertel 258: return $result;
1.47 albertel 259: }
260:
1.82 albertel 261: sub format_prior_response {
262: my ($mode,$answer,$other_data) = @_;
1.85 albertel 263:
264: my $result;
1.82 albertel 265:
1.85 albertel 266: # make a copy of the data in the refs
267: my @images = @{ $other_data->[0] };
268: my @foils = @{ $other_data->[1] };
269: foreach my $name (@foils) {
270: my $image = pop(@images);
271: my ($x,$y) = &get_submission($name,$answer);
272: my $token = &draw_image('submission',$image,$x,$y);
273: $result .=
274: '<img class="LC_prior_image"
275: src="/adm/randomlabel.png?token='.$token.'" /><br />';
276: }
277: return $result;
1.82 albertel 278: }
279:
1.75 albertel 280: sub display_answers {
281: my ($target,$whichopt)=@_;
282:
283: my $result;
284: foreach my $name (@$whichopt) {
285: my $image=$Apache::response::foilgroup{"$name.image"};
286: &Apache::lonxml::debug("image is $image");
287: if ( $target eq 'web' && $image !~ /^http:/ ) {
288: $image = &clean_up_image($image);
289: }
290: my $token=&prep_image($image,'answeronly',$name);
291:
292: $result.=&Apache::response::answer_header('imageresponse');
293: $result.=&Apache::response::answer_part('imageresponse',"<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n");
294: $result.=&Apache::response::answer_footer('imageresponse');
295: }
296: return $result;
297: }
298:
1.47 albertel 299: sub clean_up_image {
300: my ($image)=@_;
301: if ($image =~ /\s*<img\s*/) {
1.50 albertel 302: ($image) = ($image =~ /src\s*=\s*[\"\']([^\"\']+)[\"\']/i);
1.47 albertel 303: if ($image !~ /^http:/) {
304: $image=&Apache::lonnet::hreflocation('',$image);
305: }
306: if (!$image) {
307: $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
308: }
309: } else {
310: $image=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$image);
1.66 foxr 311: &Apache::lonxml::debug("repcopying: $image");
1.64 raeburn 312: if (&Apache::lonnet::repcopy($image) ne 'ok') {
1.47 albertel 313: $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
314: }
315: }
316: return $image;
1.2 albertel 317: }
318:
1.3 albertel 319: sub gradefoils {
1.36 albertel 320: my (@whichopt) = @_;
1.74 albertel 321:
322: my $partid = $Apache::inputtags::part;
323: my $id = $Apache::inputtags::response['-1'];
324:
325: if ($Apache::lonhomework::type eq 'exam') {
326: &Apache::response::scored_response($partid,$id);
327: return;
328: }
329:
1.85 albertel 330: my @results;
1.36 albertel 331: my $temp=1;
1.85 albertel 332: my %response;
1.36 albertel 333: foreach my $name (@whichopt) {
1.85 albertel 334: my ($x,$y) = split(':',$env{"form.HWVAL_$id:$temp"});
335: $response{$name} = $env{"form.HWVAL_$id:$temp"};
1.36 albertel 336: &Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
337: if (defined($x) && defined($y) &&
338: defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
339: my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
340: my $grade="INCORRECT";
341: foreach my $area (@areas) {
342: &Apache::lonxml::debug("Area is $area for $name");
343: $area =~ m/([a-z]*):/;
344: &Apache::lonxml::debug("Area of type $1");
345: if ($1 eq 'rectangle') {
346: $grade=&grade_rectangle($area,$x,$y);
1.43 albertel 347: } elsif ($1 eq 'polygon') {
348: $grade=&grade_polygon($area,$x,$y);
1.36 albertel 349: } else {
350: &Apache::lonxml::error("Unknown area style $area");
351: }
352: &Apache::lonxml::debug("Area said $grade");
353: if ($grade eq 'APPROX_ANS') { last; }
354: }
355: &Apache::lonxml::debug("Foil was $grade");
1.85 albertel 356: push(@results, $grade)
357: } else {
358: push(@results, 'MISSING_ANSWER')
1.9 albertel 359: }
1.85 albertel 360: $temp++;
1.36 albertel 361: }
1.85 albertel 362: my ($result) = &Apache::inputtags::finalizeawards(\@results,[]);
363: &Apache::lonxml::debug("Question is $result");
364:
1.60 albertel 365: my $part=$Apache::inputtags::part;
1.85 albertel 366: my %previous=
367: &Apache::response::check_for_previous(&stringify_submission(\%response),
368: $part,$id);
1.56 albertel 369: if ($result
370: && $Apache::lonhomework::type eq 'survey') { $result='SUBMITTED'; }
1.60 albertel 371: &Apache::response::handle_previous(\%previous,$result);
1.85 albertel 372: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
373: &stringify_submission(\%response);
1.60 albertel 374: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$result;
1.74 albertel 375: return;
1.3 albertel 376: }
377:
1.85 albertel 378: sub stringify_submission {
379: my ($response) = @_;
1.86 albertel 380: return &Apache::lonnet::hash2str(%{ $response });
381:
1.85 albertel 382:
383: }
384:
385: sub get_submission {
386: my ($name,$string) = @_;
387:
388: if (!defined($string)) {
389: my $part=$Apache::inputtags::part;
390: my $respid=$Apache::inputtags::response['-1'];
391: $string =
392: $Apache::lonhomework::history{"resource.$part.$respid.submission"};
393: }
394:
1.86 albertel 395: if ($string !~ /=/) {
1.85 albertel 396: return split(':',$string);
397: } else {
1.86 albertel 398: my %response = &Apache::lonnet::str2hash($string);
1.85 albertel 399: return split(':',$response{$name});
400: }
401: }
402:
1.1 albertel 403: sub end_foilgroup {
1.36 albertel 404: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
405: my $result='';
406: my @whichopt;
1.75 albertel 407:
1.38 albertel 408: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75 albertel 409: $target eq 'analyze' || $target eq 'answer') {
410:
1.36 albertel 411: my ($count,$max) = &getfoilcounts($parstack,$safeeval);
412: if ($count>$max) { $count=$max }
413: &Apache::lonxml::debug("Count is $count from $max");
1.75 albertel 414:
1.36 albertel 415: @whichopt = &whichfoils($max);
1.75 albertel 416:
1.36 albertel 417: if ($target eq 'web' || $target eq 'tex') {
418: $result=&displayfoils($target,@whichopt);
1.81 albertel 419: $Apache::lonxml::post_evaluate=0;
1.36 albertel 420: } elsif ($target eq 'grade') {
1.65 albertel 421: if ( defined $env{'form.submitted'}) { &gradefoils(@whichopt); }
1.37 albertel 422: } elsif ( $target eq 'analyze') {
423: &Apache::response::analyze_store_foilgroup(\@whichopt,
424: ['text','image','area']);
1.76 albertel 425: } elsif ($target eq 'answer'
426: && $env{'form.answer_output_mode'} ne 'tex') {
1.75 albertel 427: $result=&display_answers($target,\@whichopt);
1.37 albertel 428: }
1.75 albertel 429:
1.36 albertel 430: } elsif ($target eq 'edit') {
431: $result=&Apache::edit::end_table();
432: }
1.53 albertel 433: &Apache::response::poprandomnumber();
1.36 albertel 434: return $result;
1.1 albertel 435: }
436:
437: sub start_conceptgroup {
1.36 albertel 438: $Apache::imageresponse::conceptgroup=1;
439: %Apache::response::conceptgroup=();
440: return '';
1.1 albertel 441: }
442:
443: sub end_conceptgroup {
1.36 albertel 444: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
445: $Apache::imageresponse::conceptgroup=0;
446: my $result;
1.37 albertel 447: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
448: $target eq 'analyze') {
449: &Apache::response::pick_foil_for_concept($target,
450: ['area','text','image'],
451: \%Apache::hint::image,
452: $parstack,$safeeval);
1.36 albertel 453: } elsif ($target eq 'edit') {
454: $result=&Apache::edit::end_table();
455: }
456: return $result;
1.31 albertel 457: }
458:
459: sub insert_foil {
460: return '
461: <foil>
462: <image></image>
463: <text></text>
464: <rectangle></rectangle>
465: </foil>
466: ';
1.1 albertel 467: }
468:
1.12 albertel 469: $Apache::imageresponse::curname='';
1.1 albertel 470: sub start_foil {
1.36 albertel 471: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.67 albertel 472: my $result;
1.37 albertel 473: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75 albertel 474: $target eq 'analyze' || $target eq 'answer') {
1.36 albertel 475: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.72 albertel 476: if ($name eq "") {
1.71 albertel 477: &Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
1.59 albertel 478: $name=$Apache::lonxml::curdepth;
479: }
1.51 albertel 480: if (defined($Apache::response::foilnames{$name})) {
481: &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
482: }
1.52 albertel 483: $Apache::response::foilnames{$name}++;
1.36 albertel 484: if ( $Apache::imageresponse::conceptgroup
1.56 albertel 485: #&& !&Apache::response::showallfoils()
486: ) {
1.36 albertel 487: push(@{ $Apache::response::conceptgroup{'names'} }, $name);
488: } else {
489: push(@{ $Apache::response::foilgroup{'names'} }, $name);
490: }
491: $Apache::imageresponse::curname=$name;
1.67 albertel 492: } elsif ($target eq 'edit') {
493: $result = &Apache::edit::tag_start($target,$token);
494: $result .= &Apache::edit::text_arg('Name:','name',$token);
495: $result .= &Apache::edit::end_row().
496: &Apache::edit::start_spanning_row();
497: } elsif ($target eq 'modified') {
498: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
499: $safeeval,'name');
500: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
501: }
502: return $result;;
1.1 albertel 503: }
504:
505: sub end_foil {
1.26 albertel 506: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
507: my $result;
508: if ($target eq 'edit') {
509: $result=&Apache::edit::end_table();
510: }
511: return $result;
1.1 albertel 512: }
513:
514: sub start_text {
1.36 albertel 515: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
516: my $result='';
1.75 albertel 517: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
518: || $target eq 'answer') {
1.36 albertel 519: &Apache::lonxml::startredirection;
520: } elsif ($target eq 'edit') {
1.73 albertel 521: my $descr=&Apache::lonxml::get_all_text('/text',$parser,$style);
1.36 albertel 522: $result=&Apache::edit::tag_start($target,$token,'Task Description').
523: &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
524: &Apache::edit::end_row();
525: } elsif ($target eq "modified") {
1.39 albertel 526: $result=$token->[4].&Apache::edit::modifiedfield('/text',$parser);
1.36 albertel 527: }
528: return $result;
1.1 albertel 529: }
530:
531: sub end_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: my $name = $Apache::imageresponse::curname;
537: if ( $Apache::imageresponse::conceptgroup
1.56 albertel 538: #&& !&Apache::response::showallfoils()
539: ) {
1.36 albertel 540: $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
541: } else {
542: $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
543: }
544: } elsif ($target eq 'edit') {
545: $result=&Apache::edit::end_table();
546: }
547: return $result;
1.1 albertel 548: }
549:
550: sub start_image {
1.36 albertel 551: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
552: my $result='';
1.83 banghart 553: my $only = join(',',&Apache::loncommon::filecategorytypes('Pictures'));
1.75 albertel 554: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
555: || $target eq 'answer') {
1.36 albertel 556: &Apache::lonxml::startredirection;
557: } elsif ($target eq 'edit') {
1.73 albertel 558: my $bgimg=&Apache::lonxml::get_all_text('/image',$parser,$style);
1.36 albertel 559: $Apache::edit::bgimgsrc=$bgimg;
560: $Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
561:
562: $result=&Apache::edit::tag_start($target,$token,'Clickable Image').
563: &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
1.83 banghart 564: $result.=&Apache::edit::browse(undef,'textnode',undef,$only).' ';
1.36 albertel 565: $result.=&Apache::edit::search(undef,'textnode').
566: &Apache::edit::end_row();
567: } elsif ($target eq "modified") {
1.39 albertel 568: $result=$token->[4].&Apache::edit::modifiedfield('/image',$parser);
1.36 albertel 569: }
570: return $result;
1.1 albertel 571: }
572:
573: sub end_image {
1.36 albertel 574: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
575: my $result;
576: my $name = $Apache::imageresponse::curname;
1.75 albertel 577: if ($target eq 'web' || $target eq 'answer') {
1.62 albertel 578: my $image = &Apache::lonxml::endredirection();
1.36 albertel 579: &Apache::lonxml::debug("original image is $image");
1.37 albertel 580: if ( $Apache::imageresponse::conceptgroup
1.56 albertel 581: #&& !&Apache::response::showallfoils()
582: ) {
1.37 albertel 583: $Apache::response::conceptgroup{"$name.image"} = $image;
584: } else {
585: $Apache::response::foilgroup{"$name.image"} = $image;
586: }
587: } elsif ($target eq 'analyze') {
1.62 albertel 588: my $image = &Apache::lonxml::endredirection();
1.36 albertel 589: if ( $Apache::imageresponse::conceptgroup
1.56 albertel 590: #&& !&Apache::response::showallfoils()
591: ) {
1.36 albertel 592: $Apache::response::conceptgroup{"$name.image"} = $image;
593: } else {
594: $Apache::response::foilgroup{"$name.image"} = $image;
595: }
596: } elsif ($target eq 'edit') {
597: $result=&Apache::edit::end_table();
598: } elsif ($target eq 'tex') {
1.62 albertel 599: my $src = &Apache::lonxml::endredirection();
1.68 foxr 600:
601: # There may be all sorts of whitespace on fore and aft:
602:
1.69 foxr 603: $src =~ s/\s+$//s;
604: $src =~ s/^\s+//s;
1.68 foxr 605:
606: #
607: # Gnuplot e.g. just generates the latex to put inplace.
608: #
609: my $graphinclude;
1.70 foxr 610: if ($src =~ /^%DYNAMICIMAGE/) {
611: # This is needed because the newline is not always passed -> tex.
612: # At present we don't care about the sizing info.
613:
614: my ($commentline, $restofstuff) = split(/\n/, $src);
1.68 foxr 615: $graphinclude = $src;
1.70 foxr 616: $graphinclude =~ s/^$commentline//;
1.68 foxr 617: } else {
618: my ($path,$file) = &Apache::londefdef::get_eps_image($src);
619: my ($height_param,$width_param)=
620: &Apache::londefdef::image_size($src,0.3,$parstack,$safeeval);
621: $graphinclude = '\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'}';
622: }
623: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent '.$graphinclude;
1.36 albertel 624: }
625: return $result;
1.1 albertel 626: }
627:
628: sub start_rectangle {
1.36 albertel 629: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
630: my $result='';
1.38 albertel 631: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75 albertel 632: $target eq 'analyze' || $target eq 'answer') {
1.36 albertel 633: &Apache::lonxml::startredirection;
634: } elsif ($target eq 'edit') {
1.73 albertel 635: my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser,$style);
1.36 albertel 636: $result=&Apache::edit::tag_start($target,$token,'Rectangle').
637: &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
1.44 albertel 638: &Apache::edit::entercoord(undef,'textnode',undef,undef,'box').
1.36 albertel 639: &Apache::edit::end_row();
640: } elsif ($target eq "modified") {
1.44 albertel 641: &Apache::edit::deletecoorddata();
1.39 albertel 642: $result=$token->[4].&Apache::edit::modifiedfield('/rectangle',$parser);
1.36 albertel 643: }
644: return $result;
1.1 albertel 645: }
646:
1.3 albertel 647: sub grade_rectangle {
1.36 albertel 648: my ($spec,$x,$y) = @_;
649: &Apache::lonxml::debug("Spec is $spec");
1.43 albertel 650: my ($x1,$y1,$x2,$y2)=($spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
1.36 albertel 651: &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
652: if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
653: if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
654: if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
655: return 'APPROX_ANS';
656: }
657: return 'INCORRECT';
1.3 albertel 658: }
659:
1.1 albertel 660: sub end_rectangle {
1.36 albertel 661: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
662: my $result;
1.38 albertel 663: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75 albertel 664: $target eq 'analyze' || $target eq 'answer') {
1.36 albertel 665: my $name = $Apache::imageresponse::curname;
666: my $area = &Apache::lonxml::endredirection;
1.54 albertel 667: $area=~s/\s//g;
1.36 albertel 668: &Apache::lonxml::debug("out is $area for $name");
669: if ( $Apache::imageresponse::conceptgroup
1.56 albertel 670: #&& !&Apache::response::showallfoils()
671: ) {
1.36 albertel 672: push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
673: } else {
674: push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
1.43 albertel 675: }
676: } elsif ($target eq 'edit') {
677: $result=&Apache::edit::end_table();
678: }
679: return $result;
680: }
681:
682: sub start_polygon {
683: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
684: my $result='';
685: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75 albertel 686: $target eq 'analyze' || $target eq 'answer') {
1.43 albertel 687: &Apache::lonxml::startredirection;
688: } elsif ($target eq 'edit') {
1.73 albertel 689: my $coords=&Apache::lonxml::get_all_text('/polygon',$parser,$style);
1.43 albertel 690: $result=&Apache::edit::tag_start($target,$token,'Polygon').
691: &Apache::edit::editline($token->[1],$coords,'Coordinate list',40).
1.44 albertel 692: &Apache::edit::entercoord(undef,'textnode',undef,undef,'polygon').
1.43 albertel 693: &Apache::edit::end_row();
694: } elsif ($target eq "modified") {
695: $result=$token->[4].&Apache::edit::modifiedfield('/polygon',$parser);
696: }
697: return $result;
698: }
699:
700: sub grade_polygon {
701: my ($spec,$x,$y) = @_;
702: &Apache::lonxml::debug("Spec is $spec");
703: $spec=~s/^polygon://;
704: my @polygon;
705: foreach my $coord (split('-',$spec)) {
706: my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
707: &Apache::lonxml::debug("x $x y $y");
708: push @polygon, {'x'=>$x,'y'=>$y};
709: }
710: #make end point start point
711: push @polygon, $polygon[0];
712: # cribbed from
713: # http://geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm
714: my $crossing = 0; # the crossing number counter
715:
716: # loop through all edges of the polygon
717: for (my $i=0; $i<$#polygon; $i++) { # edge from V[i] to V[i+1]
718: if ((($polygon[$i]->{'y'} <= $y)
719: && ($polygon[$i+1]->{'y'} > $y)) # an upward crossing
720: ||
721: (($polygon[$i]->{'y'} > $y)
722: && ($polygon[$i+1]->{'y'} <= $y))) { # a downward crossing
723: # compute the actual edge-ray intersect x-coordinate
724: my $vt = ($y - $polygon[$i]->{'y'})
725: / ($polygon[$i+1]->{'y'} - $polygon[$i]->{'y'});
726: if ($x < $polygon[$i]->{'x'} + $vt *
727: ($polygon[$i+1]->{'x'} - $polygon[$i]->{'x'})) { # x<intersect
728: $crossing++; # a valid crossing of y=P.y right of P.x
729: }
730: }
731: }
732:
733: # 0 if even (out), and 1 if odd (in)
734: if ($crossing%2) {
735: return 'APPROX_ANS';
736: } else {
737: return 'INCORRECT';
738: }
739: }
740:
741: sub end_polygon {
742: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
743: my $result;
744: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75 albertel 745: $target eq 'analyze' || $target eq 'answer') {
1.43 albertel 746: my $name = $Apache::imageresponse::curname;
747: my $area = &Apache::lonxml::endredirection;
1.48 albertel 748: $area=~s/\s*//g;
1.43 albertel 749: &Apache::lonxml::debug("out is $area for $name");
750: if ( $Apache::imageresponse::conceptgroup
1.56 albertel 751: #&& !&Apache::response::showallfoils()
752: ) {
1.43 albertel 753: push @{ $Apache::response::conceptgroup{"$name.area"} },"polygon:$area";
754: } else {
755: push @{ $Apache::response::foilgroup{"$name.area"} },"polygon:$area";
1.36 albertel 756: }
757: } elsif ($target eq 'edit') {
758: $result=&Apache::edit::end_table();
759: }
760: return $result;
1.1 albertel 761: }
762: 1;
763: __END__
764:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>