File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.48: download - view: text, annotated - select for diffs
Wed Feb 18 00:23:01 2004 UTC (20 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- supports whitespace in polygons tags and blank submissions withouth bombing

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

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