File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.47: download - view: text, annotated - select for diffs
Wed Feb 11 21:59:34 2004 UTC (20 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- support <gnuplot> images as defined images

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

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