Annotation of loncom/homework/imageresponse.pm, revision 1.53
1.43 albertel 1:
1.12 albertel 2: # The LearningOnline Network with CAPA
1.14 albertel 3: # image click response style
4: #
1.53 ! albertel 5: # $Id: imageresponse.pm,v 1.52 2004/03/15 22:09:52 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.1 albertel 39:
1.16 harris41 40: BEGIN {
1.36 albertel 41: &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
1.1 albertel 42: }
43:
44: sub start_imageresponse {
1.36 albertel 45: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
46: my $result;
47: #when in a radiobutton response use these
1.43 albertel 48: &Apache::lonxml::register('Apache::imageresponse',
49: ('foilgroup','foil','text','image','rectangle',
50: 'polygon','conceptgroup'));
1.36 albertel 51: push (@Apache::lonxml::namespace,'imageresponse');
52: my $id = &Apache::response::start_response($parstack,$safeeval);
1.51 albertel 53: undef(%Apache::response::foilnames);
1.36 albertel 54: if ($target eq 'meta') {
55: $result=&Apache::response::meta_package_write('imageresponse');
1.37 albertel 56: } elsif ($target eq 'analyze') {
57: my $part_id="$Apache::inputtags::part.$id";
58: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.36 albertel 59: }
60: return $result;
1.1 albertel 61: }
62:
63: sub end_imageresponse {
1.30 albertel 64: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
65: &Apache::response::end_response;
66: pop @Apache::lonxml::namespace;
67: &Apache::lonxml::deregister('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
68: my $result;
69: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1.51 albertel 70: undef(%Apache::response::foilnames);
1.30 albertel 71: return $result;
1.1 albertel 72: }
73:
1.20 albertel 74: %Apache::response::foilgroup=();
1.1 albertel 75: sub start_foilgroup {
1.36 albertel 76: %Apache::response::foilgroup=();
77: $Apache::imageresponse::conceptgroup=0;
1.53 ! albertel 78: &Apache::response::pushrandomnumber();
1.36 albertel 79: return '';
1.1 albertel 80: }
81:
1.2 albertel 82: sub getfoilcounts {
1.36 albertel 83: my ($parstack,$safeeval)=@_;
1.12 albertel 84:
1.36 albertel 85: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
86: # +1 since instructors will count from 1
87: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
88: if (&Apache::response::showallfoils()) { $max=$count; }
89: return ($count,$max);
1.2 albertel 90: }
91:
92: sub whichfoils {
1.36 albertel 93: my ($max)=@_;
94: if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
95: my @names = @{ $Apache::response::foilgroup{'names'} };
96: my @whichopt =();
97: while ((($#whichopt+1) < $max) && ($#names > -1)) {
98: &Apache::lonxml::debug("Have $#whichopt max is $max");
99: my $aopt;
100: if (&Apache::response::showallfoils()) {
101: $aopt=0;
102: } else {
103: $aopt=int(&Math::Random::random_uniform() * ($#names+1));
104: }
105: &Apache::lonxml::debug("From $#names elms, picking $aopt");
106: $aopt=splice(@names,$aopt,1);
107: &Apache::lonxml::debug("Picked $aopt");
108: push (@whichopt,$aopt);
109: }
110: return @whichopt;
1.2 albertel 111: }
112:
1.40 albertel 113: sub prep_image {
1.42 albertel 114: my ($image,$mode,$name)=@_;
1.40 albertel 115: my $part=$Apache::inputtags::part;
1.41 albertel 116: my $respid=$Apache::inputtags::response['-1'];
117: my $id=&Apache::loncommon::get_cgi_id();
1.49 albertel 118: my (%x,$i);
1.47 albertel 119: $x{"cgi.$id.BGIMG"}=&Apache::lonnet::escape($image);
1.41 albertel 120: my ($x,$y)=split(/:/,$Apache::lonhomework::history{"resource.$part.$respid.submission"});
1.40 albertel 121: #draws 2 xs on the image at the clicked location
122: #one in white and then one in red on top of the one in white
1.48 albertel 123: if (defined($x) && $x=~/\S/ && defined($y) && $y =~/\S/) {
1.40 albertel 124: my $length = 6;
125: my $width = 1;
126: my $extrawidth = 2;
1.48 albertel 127: my $xmin=($x-$length);
128: my $xmax=($x+$length);
129: my $ymin=($y-$length);
130: my $ymax=($y+$length);
131:
1.49 albertel 132: $x{"cgi.$id.OBJTYPE"}.='LINE:';
133: $i=$x{"cgi.$id.OBJCOUNT"}++;
134: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
135: "FFFFFF",($width+$extrawidth)));
136: $x{"cgi.$id.OBJTYPE"}.='LINE:';
137: $i=$x{"cgi.$id.OBJCOUNT"}++;
138: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
139: "FFFFFF",($width+$extrawidth)));
140: $x{"cgi.$id.OBJTYPE"}.='LINE:';
141: $i=$x{"cgi.$id.OBJCOUNT"}++;
142: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
143: "FF0000",($width)));
144: $x{"cgi.$id.OBJTYPE"}.='LINE:';
145: $i=$x{"cgi.$id.OBJCOUNT"}++;
146: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
147: "FF0000",($width)));
1.40 albertel 148: }
1.42 albertel 149: if ($mode eq 'answer') {
150: my $width = 1;
151: my $extrawidth = 2;
152: my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
153: foreach my $area (@areas) {
1.43 albertel 154: if ($area=~/^rectangle:/) {
1.49 albertel 155: $x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
156: $i=$x{"cgi.$id.OBJCOUNT"}++;
1.43 albertel 157: my ($x1,$y1,$x2,$y2)=
158: ($area=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
1.49 albertel 159: $x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"FFFFFF",
1.43 albertel 160: ($width+$extrawidth)));
1.49 albertel 161: $x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
162: $i=$x{"cgi.$id.OBJCOUNT"}++;
163: $x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"00FF00",$width));
1.43 albertel 164: } elsif ($area=~/^polygon:(.*)/) {
1.49 albertel 165: $x{"cgi.$id.OBJTYPE"}.='POLYGON:';
166: $i=$x{"cgi.$id.OBJCOUNT"}++;
167: $x{"cgi.$id.OBJ$i"}=join(':',("FFFFFF",($width+$extrawidth)));
168: $x{"cgi.$id.OBJEXTRA$i"}=$1;
169: $x{"cgi.$id.OBJTYPE"}.='POLYGON:';
170: $i=$x{"cgi.$id.OBJCOUNT"}++;
171: $x{"cgi.$id.OBJ$i"}=join(':',("00FF00",$width));
172: $x{"cgi.$id.OBJEXTRA$i"}=$1;
1.43 albertel 173: }
1.42 albertel 174: }
175: }
1.41 albertel 176: &Apache::lonnet::appenv(%x);
177: return $id;
1.40 albertel 178: }
179:
1.2 albertel 180: sub displayfoils {
1.36 albertel 181: my ($target,@whichopt) = @_;
182: my $result ='';
183: my $name;
184: my $temp=1;
185: foreach $name (@whichopt) {
186: $result.=$Apache::response::foilgroup{"$name.text"};
187: &Apache::lonxml::debug("Text is $result");
188: if ($target eq 'tex') {$result.="\\vskip 0 mm \n";} else {$result.="<br />\n";}
189: my $image=$Apache::response::foilgroup{"$name.image"};
190: &Apache::lonxml::debug("image is $image");
1.40 albertel 191: if ( $target eq 'web' && $image !~ /^http:/ ) {
1.47 albertel 192: $image=&clean_up_image($image);
193: }
1.40 albertel 194: &Apache::lonxml::debug("image is $image");
1.36 albertel 195: if ( &Apache::response::show_answer() ) {
196: if ($target eq 'tex') {
197: $result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
198: } else {
1.42 albertel 199: my $token=&prep_image($image,'answer',$name);
1.40 albertel 200: $result.="<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
1.36 albertel 201: }
202: } else {
203: if ($target eq 'tex') {
204: $result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
205: } else {
1.40 albertel 206: my $id=$Apache::inputtags::response['-1'];
207: my $token=&prep_image($image);
208: my $temp=1;
209: $result.="<input type=\"image\" name=\"HWVAL_$id:$temp\" ".
210: "src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
1.36 albertel 211: }
212: }
213: $temp++;
214: }
215: return $result;
1.47 albertel 216: }
217:
218: sub clean_up_image {
219: my ($image)=@_;
220: if ($image =~ /\s*<img\s*/) {
1.50 albertel 221: ($image) = ($image =~ /src\s*=\s*[\"\']([^\"\']+)[\"\']/i);
1.47 albertel 222: if ($image !~ /^http:/) {
223: $image=&Apache::lonnet::hreflocation('',$image);
224: }
225: if (!$image) {
226: $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
227: }
228: } else {
229: $image=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$image);
230: if (&Apache::lonnet::repcopy($image) ne OK) {
231: $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
232: }
233: }
234: return $image;
1.2 albertel 235: }
236:
1.3 albertel 237: sub gradefoils {
1.36 albertel 238: my (@whichopt) = @_;
239: my $x;
240: my $y;
241: my $result;
242: my $id=$Apache::inputtags::response['-1'];
243: my $temp=1;
244: foreach my $name (@whichopt) {
245: $x=$ENV{"form.HWVAL_$id:$temp.x"};
246: $y=$ENV{"form.HWVAL_$id:$temp.y"};
247: &Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
248: if (defined($x) && defined($y) &&
249: defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
250: my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
251: my $grade="INCORRECT";
252: foreach my $area (@areas) {
253: &Apache::lonxml::debug("Area is $area for $name");
254: $area =~ m/([a-z]*):/;
255: &Apache::lonxml::debug("Area of type $1");
256: if ($1 eq 'rectangle') {
257: $grade=&grade_rectangle($area,$x,$y);
1.43 albertel 258: } elsif ($1 eq 'polygon') {
259: $grade=&grade_polygon($area,$x,$y);
1.36 albertel 260: } else {
261: &Apache::lonxml::error("Unknown area style $area");
262: }
263: &Apache::lonxml::debug("Area said $grade");
264: if ($grade eq 'APPROX_ANS') { last; }
265: }
266: &Apache::lonxml::debug("Foil was $grade");
267: if ($grade eq 'INCORRECT') { $result= 'INCORRECT'; }
268: if (($grade eq 'APPROX_ANS') && ($result ne 'APPROX_ANS')) { $result=$grade; }
269: &Apache::lonxml::debug("Question is $result");
270: $temp++;
1.9 albertel 271: }
1.36 albertel 272: }
273: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}="$x:$y";
274: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}=$result;
275: return '';
1.3 albertel 276: }
277:
1.1 albertel 278: sub end_foilgroup {
1.36 albertel 279: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
280: my $result='';
281: my @whichopt;
1.38 albertel 282: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
283: $target eq 'analyze') {
1.36 albertel 284: my ($count,$max) = &getfoilcounts($parstack,$safeeval);
285: if ($count>$max) { $count=$max }
286: &Apache::lonxml::debug("Count is $count from $max");
287: @whichopt = &whichfoils($max);
288: if ($target eq 'web' || $target eq 'tex') {
289: $result=&displayfoils($target,@whichopt);
290: } elsif ($target eq 'grade') {
291: if ( defined $ENV{'form.submitted'}) { &gradefoils(@whichopt); }
1.37 albertel 292: } elsif ( $target eq 'analyze') {
293: &Apache::response::analyze_store_foilgroup(\@whichopt,
294: ['text','image','area']);
295: }
1.36 albertel 296: } elsif ($target eq 'edit') {
297: $result=&Apache::edit::end_table();
298: }
1.53 ! albertel 299: &Apache::response::poprandomnumber();
1.36 albertel 300: return $result;
1.1 albertel 301: }
302:
303: sub start_conceptgroup {
1.36 albertel 304: $Apache::imageresponse::conceptgroup=1;
305: %Apache::response::conceptgroup=();
306: return '';
1.1 albertel 307: }
308:
309: sub end_conceptgroup {
1.36 albertel 310: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
311: $Apache::imageresponse::conceptgroup=0;
312: my $result;
1.37 albertel 313: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
314: $target eq 'analyze') {
315: &Apache::response::pick_foil_for_concept($target,
316: ['area','text','image'],
317: \%Apache::hint::image,
318: $parstack,$safeeval);
1.36 albertel 319: } elsif ($target eq 'edit') {
320: $result=&Apache::edit::end_table();
321: }
322: return $result;
1.31 albertel 323: }
324:
325: sub insert_foil {
326: return '
327: <foil>
328: <image></image>
329: <text></text>
330: <rectangle></rectangle>
331: </foil>
332: ';
1.1 albertel 333: }
334:
1.12 albertel 335: $Apache::imageresponse::curname='';
1.1 albertel 336: sub start_foil {
1.36 albertel 337: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37 albertel 338: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
339: $target eq 'analyze') {
1.36 albertel 340: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
341: if ($name eq '') { $name=$Apache::lonxml::curdepth; }
1.51 albertel 342: if (defined($Apache::response::foilnames{$name})) {
343: &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
344: }
1.52 albertel 345: $Apache::response::foilnames{$name}++;
1.36 albertel 346: if ( $Apache::imageresponse::conceptgroup
347: && !&Apache::response::showallfoils()) {
348: push(@{ $Apache::response::conceptgroup{'names'} }, $name);
349: } else {
350: push(@{ $Apache::response::foilgroup{'names'} }, $name);
351: }
352: $Apache::imageresponse::curname=$name;
353: }
354: return '';
1.1 albertel 355: }
356:
357: sub end_foil {
1.26 albertel 358: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
359: my $result;
360: if ($target eq 'edit') {
361: $result=&Apache::edit::end_table();
362: }
363: return $result;
1.1 albertel 364: }
365:
366: sub start_text {
1.36 albertel 367: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
368: my $result='';
1.37 albertel 369: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.36 albertel 370: &Apache::lonxml::startredirection;
371: } elsif ($target eq 'edit') {
372: my $descr=&Apache::lonxml::get_all_text('/text',$parser);
373: $result=&Apache::edit::tag_start($target,$token,'Task Description').
374: &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
375: &Apache::edit::end_row();
376: } elsif ($target eq "modified") {
1.39 albertel 377: $result=$token->[4].&Apache::edit::modifiedfield('/text',$parser);
1.36 albertel 378: }
379: return $result;
1.1 albertel 380: }
381:
382: sub end_text {
1.36 albertel 383: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
384: my $result;
1.37 albertel 385: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.36 albertel 386: my $name = $Apache::imageresponse::curname;
387: if ( $Apache::imageresponse::conceptgroup
388: && !&Apache::response::showallfoils() ) {
389: $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
390: } else {
391: $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
392: }
393: } elsif ($target eq 'edit') {
394: $result=&Apache::edit::end_table();
395: }
396: return $result;
1.1 albertel 397: }
398:
399: sub start_image {
1.36 albertel 400: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
401: my $result='';
1.37 albertel 402: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.36 albertel 403: &Apache::lonxml::startredirection;
404: } elsif ($target eq 'edit') {
405: my $bgimg=&Apache::lonxml::get_all_text('/image',$parser);
406: $Apache::edit::bgimgsrc=$bgimg;
407: $Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
408:
409: $result=&Apache::edit::tag_start($target,$token,'Clickable Image').
410: &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
411: $result.=&Apache::edit::browse(undef,'textnode').' ';
412: $result.=&Apache::edit::search(undef,'textnode').
413: &Apache::edit::end_row();
414: } elsif ($target eq "modified") {
1.39 albertel 415: $result=$token->[4].&Apache::edit::modifiedfield('/image',$parser);
1.36 albertel 416: }
417: return $result;
1.1 albertel 418: }
419:
420: sub end_image {
1.36 albertel 421: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
422: my $result;
423: my $name = $Apache::imageresponse::curname;
424: if ($target eq 'web') {
425: my $image = &Apache::lonxml::endredirection;
426: &Apache::lonxml::debug("original image is $image");
1.37 albertel 427: if ( $Apache::imageresponse::conceptgroup
428: && !&Apache::response::showallfoils()) {
429: $Apache::response::conceptgroup{"$name.image"} = $image;
430: } else {
431: $Apache::response::foilgroup{"$name.image"} = $image;
432: }
433: } elsif ($target eq 'analyze') {
434: my $image = &Apache::lonxml::endredirection;
1.36 albertel 435: if ( $Apache::imageresponse::conceptgroup
436: && !&Apache::response::showallfoils()) {
437: $Apache::response::conceptgroup{"$name.image"} = $image;
438: } else {
439: $Apache::response::foilgroup{"$name.image"} = $image;
440: }
441: } elsif ($target eq 'edit') {
442: $result=&Apache::edit::end_table();
443: } elsif ($target eq 'tex') {
444: my $src = &Apache::lonxml::endredirection;
445: $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
446: my $width_param = '';
447: my $height_param = '';
448: my $scaling = .3;
449: my $image = Image::Magick->new;
450: my $current_figure = $image->Read($src);
451: $width_param = $image->Get('width') * $scaling;;
452: $height_param = $image->Get('height') * $scaling;;
453: undef $image;
454: my $epssrc = $src;
455: $epssrc =~ s/(\.gif|\.jpg)$/\.eps/i;
456: if (not -e $epssrc) {
457: my $localfile = $epssrc;
458: $localfile =~ s/.*(\/res)/$1/;
459: my $file;
460: my $path;
461: if ($localfile =~ m!(.*)/([^/]*)$!) {
462: $file = $2;
463: $path = $1.'/';
464: }
465: my $signal_eps = 0;
466: my @content_directory = &Apache::lonnet::dirlist($path);
467: for (my $iy=0;$iy<=$#content_directory;$iy++) {
468: my @tempo_array = split(/&/,$content_directory[$iy]);
469: $content_directory[$iy] = $tempo_array[0];
470: if ($file eq $tempo_array[0]) {
471: $signal_eps = 1;
472: last;
473: }
474: }
475: if ($signal_eps) {
476: my $eps_file = &Apache::lonnet::getfile($localfile);
477: } else {
478: $localfile = $src;
479: $localfile =~ s/.*(\/res)/$1/;
480: my $as = &Apache::lonnet::getfile($src);
481: }
482: }
1.19 sakharuk 483: my $file;
484: my $path;
1.36 albertel 485: if ($src =~ m!(.*)/([^/]*)$!) {
1.19 sakharuk 486: $file = $2;
487: $path = $1.'/';
1.36 albertel 488: }
489: my $newsrc = $src;
490: $newsrc =~ s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
491: $file=~s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
492: #do we have any specified size of the picture?
493: my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval);
494: my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval);
495: my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval);
496: if ($TeXwidth ne '') {
497: $width_param = $TeXwidth;
498: } elsif ($TeXheight ne '') {
499: $width_param = $TeXheight/$height_param*$width_param;
500: } elsif ($width ne '') {
501: $width_param = $width*$scaling;
502: }
503: #where can we find the picture?
504: if (-e $newsrc) {
505: if ($path) {
506: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
1.19 sakharuk 507: }
508: } else {
1.46 sakharuk 509: #care about eps dynamical generation
510: $Apache::response::foilgroup{"$name.image"}='\vskip 0 mm '.&Apache::londefdef::eps_generation($src,$file,$width_param);
1.19 sakharuk 511: }
1.36 albertel 512: }
513: return $result;
1.1 albertel 514: }
515:
516: sub start_rectangle {
1.36 albertel 517: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
518: my $result='';
1.38 albertel 519: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
520: $target eq 'analyze') {
1.36 albertel 521: &Apache::lonxml::startredirection;
522: } elsif ($target eq 'edit') {
523: my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser);
524: $result=&Apache::edit::tag_start($target,$token,'Rectangle').
525: &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
1.44 albertel 526: &Apache::edit::entercoord(undef,'textnode',undef,undef,'box').
1.36 albertel 527: &Apache::edit::end_row();
528: } elsif ($target eq "modified") {
1.44 albertel 529: &Apache::edit::deletecoorddata();
1.39 albertel 530: $result=$token->[4].&Apache::edit::modifiedfield('/rectangle',$parser);
1.36 albertel 531: }
532: return $result;
1.1 albertel 533: }
534:
1.3 albertel 535: sub grade_rectangle {
1.36 albertel 536: my ($spec,$x,$y) = @_;
537: &Apache::lonxml::debug("Spec is $spec");
1.43 albertel 538: my ($x1,$y1,$x2,$y2)=($spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
1.36 albertel 539: &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
540: if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
541: if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
542: if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
543: return 'APPROX_ANS';
544: }
545: return 'INCORRECT';
1.3 albertel 546: }
547:
1.1 albertel 548: sub end_rectangle {
1.36 albertel 549: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
550: my $result;
1.38 albertel 551: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
552: $target eq 'analyze') {
1.36 albertel 553: my $name = $Apache::imageresponse::curname;
554: my $area = &Apache::lonxml::endredirection;
555: &Apache::lonxml::debug("out is $area for $name");
556: if ( $Apache::imageresponse::conceptgroup
557: && !&Apache::response::showallfoils()) {
558: push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
559: } else {
560: push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
1.43 albertel 561: }
562: } elsif ($target eq 'edit') {
563: $result=&Apache::edit::end_table();
564: }
565: return $result;
566: }
567:
568: sub start_polygon {
569: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
570: my $result='';
571: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
572: $target eq 'analyze') {
573: &Apache::lonxml::startredirection;
574: } elsif ($target eq 'edit') {
575: my $coords=&Apache::lonxml::get_all_text('/polygon',$parser);
576: $result=&Apache::edit::tag_start($target,$token,'Polygon').
577: &Apache::edit::editline($token->[1],$coords,'Coordinate list',40).
1.44 albertel 578: &Apache::edit::entercoord(undef,'textnode',undef,undef,'polygon').
1.43 albertel 579: &Apache::edit::end_row();
580: } elsif ($target eq "modified") {
581: $result=$token->[4].&Apache::edit::modifiedfield('/polygon',$parser);
582: }
583: return $result;
584: }
585:
586: sub grade_polygon {
587: my ($spec,$x,$y) = @_;
588: &Apache::lonxml::debug("Spec is $spec");
589: $spec=~s/^polygon://;
590: my @polygon;
591: foreach my $coord (split('-',$spec)) {
592: my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
593: &Apache::lonxml::debug("x $x y $y");
594: push @polygon, {'x'=>$x,'y'=>$y};
595: }
596: #make end point start point
597: push @polygon, $polygon[0];
598: # cribbed from
599: # http://geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm
600: my $crossing = 0; # the crossing number counter
601:
602: # loop through all edges of the polygon
603: for (my $i=0; $i<$#polygon; $i++) { # edge from V[i] to V[i+1]
604: if ((($polygon[$i]->{'y'} <= $y)
605: && ($polygon[$i+1]->{'y'} > $y)) # an upward crossing
606: ||
607: (($polygon[$i]->{'y'} > $y)
608: && ($polygon[$i+1]->{'y'} <= $y))) { # a downward crossing
609: # compute the actual edge-ray intersect x-coordinate
610: my $vt = ($y - $polygon[$i]->{'y'})
611: / ($polygon[$i+1]->{'y'} - $polygon[$i]->{'y'});
612: if ($x < $polygon[$i]->{'x'} + $vt *
613: ($polygon[$i+1]->{'x'} - $polygon[$i]->{'x'})) { # x<intersect
614: $crossing++; # a valid crossing of y=P.y right of P.x
615: }
616: }
617: }
618:
619: # 0 if even (out), and 1 if odd (in)
620: if ($crossing%2) {
621: return 'APPROX_ANS';
622: } else {
623: return 'INCORRECT';
624: }
625: }
626:
627: sub end_polygon {
628: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
629: my $result;
630: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
631: $target eq 'analyze') {
632: my $name = $Apache::imageresponse::curname;
633: my $area = &Apache::lonxml::endredirection;
1.48 albertel 634: $area=~s/\s*//g;
1.43 albertel 635: &Apache::lonxml::debug("out is $area for $name");
636: if ( $Apache::imageresponse::conceptgroup
637: && !&Apache::response::showallfoils()) {
638: push @{ $Apache::response::conceptgroup{"$name.area"} },"polygon:$area";
639: } else {
640: push @{ $Apache::response::foilgroup{"$name.area"} },"polygon:$area";
1.36 albertel 641: }
642: } elsif ($target eq 'edit') {
643: $result=&Apache::edit::end_table();
644: }
645: return $result;
1.1 albertel 646: }
647: 1;
648: __END__
649:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>