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