File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.65: download - view: text, annotated - select for diffs
Thu Apr 7 06:56:21 2005 UTC (19 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: version_1_99_0_tmcc, HEAD
- ENV -> env

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

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