Annotation of loncom/homework/imageresponse.pm, revision 1.32

1.12      albertel    1: # The LearningOnline Network with CAPA
1.14      albertel    2: # image click response style
                      3: #
1.32    ! ng          4: # $Id: imageresponse.pm,v 1.31 2003/06/09 22:42:49 albertel Exp $
1.14      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.1       albertel   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.21      harris41   34: use Image::Magick;
1.1       albertel   35: 
1.16      harris41   36: BEGIN {
1.12      albertel   37:   &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
1.1       albertel   38: }
                     39: 
                     40: sub start_imageresponse {
1.12      albertel   41:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.13      albertel   42:   my $result;
1.12      albertel   43:   #when in a radiobutton response use these
                     44:   &Apache::lonxml::register('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
                     45:   push (@Apache::lonxml::namespace,'imageresponse');
                     46:   my $id = &Apache::response::start_response($parstack,$safeeval);
1.13      albertel   47:   if ($target eq 'meta') {
                     48:     $result=&Apache::response::meta_package_write('imageresponse');
                     49:   }
                     50:   return $result;
1.1       albertel   51: }
                     52: 
                     53: sub end_imageresponse {
1.30      albertel   54:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     55:     &Apache::response::end_response;
                     56:     pop @Apache::lonxml::namespace;
                     57:     &Apache::lonxml::deregister('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
                     58:     my $result;
                     59:     if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
                     60:     return $result;
1.1       albertel   61: }
                     62: 
1.20      albertel   63: %Apache::response::foilgroup=();
1.1       albertel   64: sub start_foilgroup {
1.20      albertel   65:   %Apache::response::foilgroup=();
1.12      albertel   66:   $Apache::imageresponse::conceptgroup=0;
                     67:   &Apache::response::setrandomnumber();
                     68:   return '';
1.1       albertel   69: }
                     70: 
1.2       albertel   71: sub getfoilcounts {
1.12      albertel   72:   my ($parstack,$safeeval)=@_;
                     73: 
                     74:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
                     75:   # +1 since instructors will count from 1
                     76:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
1.18      albertel   77:   if (&Apache::response::showallfoils()) { $max=$count; }
1.12      albertel   78:   return ($count,$max);
1.2       albertel   79: }
                     80: 
                     81: sub whichfoils {
1.12      albertel   82:   my ($max)=@_;
                     83:   if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
                     84:   my @names = @{ $Apache::response::foilgroup{'names'} };
                     85:   my @whichopt =();
                     86:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
                     87:     &Apache::lonxml::debug("Have $#whichopt max is $max");
1.18      albertel   88:     my $aopt;
                     89:     if (&Apache::response::showallfoils()) {
                     90:       $aopt=0;
                     91:     } else {
1.22      albertel   92:       $aopt=int(&Math::Random::random_uniform() * ($#names+1));
1.18      albertel   93:     }
1.12      albertel   94:     &Apache::lonxml::debug("From $#names elms, picking $aopt");
                     95:     $aopt=splice(@names,$aopt,1);
                     96:     &Apache::lonxml::debug("Picked $aopt");
                     97:     push (@whichopt,$aopt);
                     98:   }
                     99:   return @whichopt;
1.2       albertel  100: }
                    101: 
                    102: sub displayfoils {
1.24      sakharuk  103:   my ($target,@whichopt) = @_;
1.12      albertel  104:   my $result ='';
                    105:   my $name;
                    106:   my $temp=1;
                    107:   foreach $name (@whichopt) {
1.24      sakharuk  108:     $result.=$Apache::response::foilgroup{"$name.text"};
1.28      albertel  109:     &Apache::lonxml::debug("Text is $result");
1.24      sakharuk  110:     if ($target eq 'tex') {$result.="\\vskip 0 mm \n";} else {$result.="<br />\n";}
1.12      albertel  111:     my $image=$Apache::response::foilgroup{"$name.image"};
1.28      albertel  112:     &Apache::lonxml::debug("image is $image");
1.12      albertel  113:     if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
1.24      sakharuk  114:       if ($target eq 'tex') {
                    115: 	$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
                    116:       } else {
                    117:         $result.="<img src=\"$image\"/> <br />\n";
                    118:       }
1.12      albertel  119:     } else {
1.24      sakharuk  120:       if ($target eq 'tex') {
                    121: 	$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
                    122:       } else {
                    123:         $result.="<input type=\"image\" name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\" src=\"$image\"/> <br />\n";
                    124:       }
1.12      albertel  125:     }
                    126:     $temp++;
                    127:   }
                    128:   return $result;
1.2       albertel  129: }
                    130: 
1.3       albertel  131: sub gradefoils {
1.12      albertel  132:   my (@whichopt) = @_;
                    133:   my $x;
                    134:   my $y;
                    135:   my $result;
                    136:   my $id=$Apache::inputtags::response['-1'];
                    137:   my $temp=1;
                    138:   foreach my $name (@whichopt) {
                    139:     $x=$ENV{"form.HWVAL_$id:$temp.x"};
                    140:     $y=$ENV{"form.HWVAL_$id:$temp.y"};
                    141:     &Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
1.29      albertel  142:     if (defined($x) && defined($y) &&
                    143: 	defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
1.12      albertel  144:       my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
                    145:       my $grade="INCORRECT";
                    146:       foreach my $area (@areas) {
                    147: 	&Apache::lonxml::debug("Area is $area for $name");
                    148: 	$area =~ m/([a-z]*):/;
                    149: 	&Apache::lonxml::debug("Area of type $1");
                    150: 	if ($1 eq 'rectangle') {
                    151: 	  $grade=&grade_rectangle($area,$x,$y);
                    152: 	} else {
                    153: 	  &Apache::lonxml::error("Unknown area style $area");
1.9       albertel  154: 	}
1.12      albertel  155: 	&Apache::lonxml::debug("Area said $grade");
                    156: 	if ($grade eq 'APPROX_ANS') { last; }
                    157:       }
                    158:       &Apache::lonxml::debug("Foil was $grade");
                    159:       if ($grade eq 'INCORRECT') { $result= 'INCORRECT'; }
                    160:       if (($grade eq 'APPROX_ANS') && ($result ne 'APPROX_ANS')) { $result=$grade; }
                    161:       &Apache::lonxml::debug("Question is $result");
                    162:       $temp++;
                    163:     }
                    164:   }
                    165:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}="$x:$y";
                    166:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}=$result;
                    167:   return '';
1.3       albertel  168: }
                    169: 
1.1       albertel  170: sub end_foilgroup {
1.12      albertel  171:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    172:   my $result='';
                    173:   my @whichopt;
1.24      sakharuk  174:   if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
1.12      albertel  175:     my ($count,$max) = &getfoilcounts($parstack,$safeeval);
                    176:     if ($count>$max) { $count=$max }
                    177:     &Apache::lonxml::debug("Count is $count from $max");
                    178:     @whichopt = &whichfoils($max);
1.28      albertel  179:     if ($target eq 'web' || $target eq 'tex') {
                    180: 	$result=&displayfoils($target,@whichopt);
                    181:     } elsif ($target eq 'grade') {
                    182: 	if ( defined $ENV{'form.submitted'}) { &gradefoils(@whichopt); }
                    183:     } 
1.26      albertel  184:   } elsif ($target eq 'edit') {
                    185:       $result=&Apache::edit::end_table();
1.12      albertel  186:   }
                    187:   return $result;
1.1       albertel  188: }
                    189: 
                    190: sub start_conceptgroup {
1.12      albertel  191:   $Apache::imageresponse::conceptgroup=1;
1.20      albertel  192:   %Apache::response::conceptgroup=();
1.12      albertel  193:   return '';
1.1       albertel  194: }
                    195: 
                    196: sub end_conceptgroup {
1.12      albertel  197:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.26      albertel  198:   $Apache::imageresponse::conceptgroup=0;
                    199:   my $result;
1.24      sakharuk  200:   if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
1.12      albertel  201:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
                    202:       my @names = @{ $Apache::response::conceptgroup{'names'} };
1.22      albertel  203:       my $pick=int(&Math::Random::random_uniform() * ($#names+1));
1.12      albertel  204:       my $name=$names[$pick];
                    205:       if (defined(@{ $Apache::response::conceptgroup{"$name.area"} })) {
                    206: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
                    207: 	$Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
                    208: 	$Apache::response::foilgroup{"$name.image"} = $Apache::response::conceptgroup{"$name.image"};
                    209: 	push(@{ $Apache::response::foilgroup{"$name.area"} }, @{ $Apache::response::conceptgroup{"$name.area"} });
                    210: 	my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    211: 	$Apache::response::foilgroup{"$name.concept"} = $concept;
                    212: 	&Apache::lonxml::debug("Selecting $name in $concept");
                    213:       }
1.9       albertel  214:     }
1.26      albertel  215:   } elsif ($target eq 'edit') {
                    216:       $result=&Apache::edit::end_table();
1.12      albertel  217:   }
1.26      albertel  218:   return $result;
1.31      albertel  219: }
                    220: 
                    221: sub insert_foil {
                    222:     return '
                    223:        <foil>
                    224:            <image></image>
                    225:            <text></text>
                    226:            <rectangle></rectangle>
                    227:        </foil>
                    228: ';
1.1       albertel  229: }
                    230: 
1.12      albertel  231: $Apache::imageresponse::curname='';
1.1       albertel  232: sub start_foil {
1.12      albertel  233:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.24      sakharuk  234:   if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
1.12      albertel  235:     my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    236:     if ($name eq '') { $name=$Apache::lonxml::curdepth; }
1.18      albertel  237:     if ( $Apache::imageresponse::conceptgroup
                    238: 	 && !&Apache::response::showallfoils()) {
1.12      albertel  239:       push(@{ $Apache::response::conceptgroup{'names'} }, $name);
                    240:     } else {
                    241:       push(@{ $Apache::response::foilgroup{'names'} }, $name);
                    242:     }
                    243:     $Apache::imageresponse::curname=$name;
                    244:   }
                    245:   return '';
1.1       albertel  246: }
                    247: 
                    248: sub end_foil {
1.26      albertel  249:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    250:     my $result;
                    251:     if ($target eq 'edit') {
                    252: 	$result=&Apache::edit::end_table();
                    253:     }
                    254:     return $result;
1.1       albertel  255: }
                    256: 
                    257: sub start_text {
1.12      albertel  258:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.25      www       259:   my $result='';
                    260:   if ($target eq 'web' || $target eq 'tex') { 
                    261:      &Apache::lonxml::startredirection; 
                    262:   } elsif ($target eq 'edit') {
1.26      albertel  263:     my $descr=&Apache::lonxml::get_all_text('/text',$parser);
1.25      www       264:     $result=&Apache::edit::tag_start($target,$token,'Task Description').
                    265: 	&Apache::edit::editfield($token->[1],$descr,'Text',60,2).
1.26      albertel  266:         &Apache::edit::end_row();
1.25      www       267:   } elsif ($target eq "modified") {
1.26      albertel  268:     my $descr=&Apache::lonxml::get_all_text('/text',$parser);
1.25      www       269:     $result=$token->[4].&Apache::edit::modifiedfield($token);
                    270:     &Apache::lonxml::debug($result);
                    271:   }
                    272:   return $result;
1.1       albertel  273: }
                    274: 
                    275: sub end_text {
1.12      albertel  276:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.26      albertel  277:   my $result;
1.24      sakharuk  278:   if ($target eq 'web' || $target eq 'tex') {
1.12      albertel  279:     my $name = $Apache::imageresponse::curname;
1.18      albertel  280:     if ( $Apache::imageresponse::conceptgroup
                    281:        && !&Apache::response::showallfoils() ) {
1.12      albertel  282:       $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
                    283:     } else {
                    284:       $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
1.7       albertel  285:     }
1.26      albertel  286:   } elsif ($target eq 'edit') {
                    287:       $result=&Apache::edit::end_table();
1.12      albertel  288:   }
1.26      albertel  289:   return $result;
1.1       albertel  290: }
                    291: 
                    292: sub start_image {
1.12      albertel  293:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.25      www       294:   my $result='';
                    295:   if ($target eq 'web' || $target eq 'tex') { 
                    296:       &Apache::lonxml::startredirection; 
                    297:   } elsif ($target eq 'edit') {
1.26      albertel  298:     my $bgimg=&Apache::lonxml::get_all_text('/image',$parser);
1.27      www       299:     $Apache::edit::bgimgsrc=$bgimg;
                    300:     $Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
                    301: 
1.25      www       302:     $result=&Apache::edit::tag_start($target,$token,'Clickable Image').
                    303: 	&Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
                    304:     $result.=&Apache::edit::browse(undef,'textnode').' ';
                    305:     $result.=&Apache::edit::search(undef,'textnode').
1.26      albertel  306:         &Apache::edit::end_row();
1.25      www       307:   } elsif ($target eq "modified") {
1.26      albertel  308:     my $bgimg=&Apache::lonxml::get_all_text('/image',$parser);
1.25      www       309:     $result=$token->[4].&Apache::edit::modifiedfield($token);
                    310:     &Apache::lonxml::debug($result);
                    311:   }
                    312:   return $result;
1.1       albertel  313: }
                    314: 
                    315: sub end_image {
1.26      albertel  316:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    317:   my $result;
1.24      sakharuk  318:   my $name = $Apache::imageresponse::curname;
1.12      albertel  319:   if ($target eq 'web') {
                    320:     my $image = &Apache::lonxml::endredirection;
1.32    ! ng        321:     &Apache::lonxml::debug("original image is $image");
        !           322:     my $id=$Apache::inputtags::response['-1'];
        !           323:     my $temp=1;
        !           324:     my $x=$ENV{"form.HWVAL_$id:$temp.x"};
        !           325:     my $y=$ENV{"form.HWVAL_$id:$temp.y"};
        !           326:     if (defined ($x) && defined ($y)) {
        !           327: 	&Apache::lonxml::debug("x and y defined as $x,$y");
        !           328: 	my $magickImage = Image::Magick->new;
        !           329: 	my $currentImage = $magickImage->Read('/home/httpd/html'.$image);
        !           330: 	my $length = 6;
        !           331: 	$currentImage = $magickImage->Draw(primitive=>'line',
        !           332: 					   stroke=>'red',
        !           333: 					   points=>($x-$length).','.($y-$length).' '.
        !           334: 					   ($x+$length).','.($y+$length));
        !           335: 	$currentImage = $magickImage->Draw(primitive=>'line',
        !           336: 					   stroke=>'red',
        !           337: 					   points=>($x-$length).','.($y+$length).' '.
        !           338: 					   ($x+$length).','.($y-$length));
        !           339: 	binmode STDOUT;
        !           340: 	my ($graphExt) = ($image =~ /.*\.(.*)$/);
        !           341: 	&Apache::lonxml::debug("graph mime type $graphExt");
        !           342: 	my $webImageName = "/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_".(time).'.'.$graphExt; #needs to be more random
        !           343: 	my $newImageName = '/home/httpd'.$webImageName;
        !           344: 
        !           345: 	$currentImage = $magickImage->Write($graphExt.':'.$newImageName);
        !           346: 	undef $magickImage;
        !           347: 	$image = $webImageName;
        !           348:     }
        !           349:     &Apache::lonxml::debug("out image is $image");
1.18      albertel  350:     if ( $Apache::imageresponse::conceptgroup
                    351: 	 && !&Apache::response::showallfoils()) {
1.12      albertel  352:       $Apache::response::conceptgroup{"$name.image"} = $image;
                    353:     } else {
                    354:       $Apache::response::foilgroup{"$name.image"} = $image;
1.7       albertel  355:     }
1.26      albertel  356:   } elsif ($target eq 'edit') {
                    357:       $result=&Apache::edit::end_table();
1.19      sakharuk  358:   } elsif ($target eq 'tex') {
                    359:     my $src = &Apache::lonxml::endredirection;
                    360:     $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
                    361:     my $width_param = '';
                    362:     my $height_param = '';
                    363:     my $scaling = .3;
                    364:     my $image = Image::Magick->new;
                    365:     my $current_figure = $image->Read($src);
                    366:     $width_param = $image->Get('width') * $scaling;;
                    367:     $height_param = $image->Get('height') * $scaling;;
                    368:     undef $image;
                    369:     my $epssrc = $src;
                    370:     $epssrc =~ s/(\.gif|\.jpg)$/\.eps/i;
                    371:     if (not -e $epssrc) {
                    372: 	my $localfile = $epssrc;
                    373: 	$localfile =~ s/.*(\/res)/$1/;	
                    374: 	my $file;
                    375: 	my $path;	
                    376: 	if ($localfile =~ m!(.*)/([^/]*)$!) {
                    377: 	    $file = $2;
                    378: 	    $path = $1.'/'; 
                    379: 	}	
                    380: 	my $signal_eps = 0;
                    381: 	my @content_directory = &Apache::lonnet::dirlist($path);
                    382: 	for (my $iy=0;$iy<=$#content_directory;$iy++) {
                    383: 	    my @tempo_array = split(/&/,$content_directory[$iy]);
                    384: 	    $content_directory[$iy] = $tempo_array[0];
                    385: 	    if ($file eq $tempo_array[0]) {
                    386: 		$signal_eps = 1;
                    387: 		last;
                    388: 	    }
                    389: 	}
                    390: 	if ($signal_eps) {
                    391: 	    my $eps_file = &Apache::lonnet::getfile($localfile);
                    392: 	} else {
                    393: 	    $localfile = $src;
                    394: 	    $localfile =~ s/.*(\/res)/$1/;	
                    395: 	    my $as = &Apache::lonnet::getfile($src);		      
                    396: 	}
                    397:     }
                    398:     my $file;
                    399:     my $path;	
                    400:     if ($src =~ m!(.*)/([^/]*)$!) {
                    401: 	$file = $2;
                    402: 	$path = $1.'/'; 
                    403:     }
                    404:     my $newsrc = $src;
1.27      www       405:     $newsrc =~ s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
                    406:     $file=~s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
1.19      sakharuk  407:     #do we have any specified size of the picture?
                    408:     my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval); 
                    409:     my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval); 
                    410:     my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval);
                    411:     if ($TeXwidth ne '') { 
                    412: 	$width_param = $TeXwidth; 
                    413:     } elsif ($TeXheight ne '') { 
                    414: 	$width_param = $TeXheight/$height_param*$width_param;
                    415:     } elsif ($width ne '') {
                    416: 	$width_param = $width*$scaling;      
                    417:     }
                    418:     #where can we find the picture?
                    419:     if (-e $newsrc) {
                    420: 	if ($path) {
1.24      sakharuk  421: 	  $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
1.19      sakharuk  422: 	}
                    423:     } else {
                    424: 	my $temp_file;
                    425: 	my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
                    426: 	$temp_file = Apache::File->new('>>'.$filename); 
                    427: 	print $temp_file "$src\n";
1.24      sakharuk  428: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \graphicspath{{/home/httpd/prtspool/}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
1.19      sakharuk  429:     }
1.24      sakharuk  430:   } 
1.26      albertel  431:   return $result;
1.1       albertel  432: }
                    433: 
                    434: sub start_rectangle {
1.12      albertel  435:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.25      www       436:   my $result='';
                    437:   if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') { 
                    438:      &Apache::lonxml::startredirection; 
                    439:   } elsif ($target eq 'edit') {
1.26      albertel  440:     my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser);
1.25      www       441:     $result=&Apache::edit::tag_start($target,$token,'Rectangle').
                    442: 	&Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
1.27      www       443:         &Apache::edit::entercoordpair(undef,'textnode').
1.26      albertel  444:         &Apache::edit::end_row();
1.25      www       445:   } elsif ($target eq "modified") {
1.26      albertel  446:     my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser);
1.25      www       447:     $result=$token->[4].&Apache::edit::modifiedfield($token);
                    448:     &Apache::lonxml::debug($result);
                    449:   }
                    450:   return $result;
1.1       albertel  451: }
                    452: 
1.3       albertel  453: sub grade_rectangle {
1.12      albertel  454:   my ($spec,$x,$y) = @_;
                    455:   &Apache::lonxml::debug("Spec is $spec");
                    456:   $spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/;
                    457:   my $x1=$1;
                    458:   my $y1=$2;
                    459:   my $x2=$3;
                    460:   my $y2=$4;
                    461:   &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
                    462:   if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
                    463:   if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
1.17      albertel  464:   if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
                    465:     return 'APPROX_ANS';
                    466:   }
1.12      albertel  467:   return 'INCORRECT';
1.3       albertel  468: }
                    469: 
1.1       albertel  470: sub end_rectangle {
1.12      albertel  471:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.26      albertel  472:   my $result;
1.24      sakharuk  473:   if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
1.12      albertel  474:     my $name = $Apache::imageresponse::curname;
                    475:     my $area = &Apache::lonxml::endredirection;
                    476:     &Apache::lonxml::debug("out is $area for $name");
1.18      albertel  477:     if ( $Apache::imageresponse::conceptgroup
                    478: 	 && !&Apache::response::showallfoils()) {
1.12      albertel  479:       push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
                    480:     } else {
                    481:       push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
1.7       albertel  482:     }
1.26      albertel  483:   } elsif ($target eq 'edit') {
                    484:       $result=&Apache::edit::end_table();
1.12      albertel  485:   }
1.26      albertel  486:   return $result;
1.1       albertel  487: }
                    488: 1;
                    489: __END__
                    490:  

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>