File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.73: download - view: text, annotated - select for diffs
Thu Dec 1 18:46:31 2005 UTC (18 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: version_2_1_X, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_0_99_1, HEAD
- finish off the conversion to allowing style tags get_all_text across tags
- BUG#4428

    1: 
    2: # The LearningOnline Network with CAPA
    3: # image click response style
    4: #
    5: # $Id: imageresponse.pm,v 1.73 2005/12/01 18:46:31 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: 	&Apache::lonxml::debug("repcopying: $image");
  232: 	if (&Apache::lonnet::repcopy($image) ne 'ok') {
  233: 	    $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
  234: 	}
  235:     }
  236:     return $image;
  237: }
  238: 
  239: sub gradefoils {
  240:     my (@whichopt) = @_;
  241:     my $x;
  242:     my $y;
  243:     my $result;
  244:     my $id=$Apache::inputtags::response['-1'];
  245:     my $temp=1;
  246:     foreach my $name (@whichopt) {
  247: 	$x=$env{"form.HWVAL_$id:$temp.x"};
  248: 	$y=$env{"form.HWVAL_$id:$temp.y"};
  249: 	&Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
  250: 	if (defined($x) && defined($y) &&
  251: 	    defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
  252: 	    my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
  253: 	    my $grade="INCORRECT";
  254: 	    foreach my $area (@areas) {
  255: 		&Apache::lonxml::debug("Area is $area for $name");
  256: 		$area =~ m/([a-z]*):/;
  257: 		&Apache::lonxml::debug("Area of type $1");
  258: 		if ($1 eq 'rectangle') {
  259: 		    $grade=&grade_rectangle($area,$x,$y);
  260: 		} elsif ($1 eq 'polygon') {
  261: 		    $grade=&grade_polygon($area,$x,$y);
  262: 		} else {
  263: 		    &Apache::lonxml::error("Unknown area style $area");
  264: 		}
  265: 		&Apache::lonxml::debug("Area said $grade");
  266: 		if ($grade eq 'APPROX_ANS') { last; }
  267: 	    }
  268: 	    &Apache::lonxml::debug("Foil was $grade");
  269: 	    if ($grade eq 'INCORRECT') { $result= 'INCORRECT'; }
  270: 	    if (($grade eq 'APPROX_ANS') && ($result ne 'APPROX_ANS')) { $result=$grade; }
  271: 	    &Apache::lonxml::debug("Question is $result");
  272: 	    $temp++;
  273: 	}
  274:     }
  275:     my $responsestr="$x:$y";
  276:     my $part=$Apache::inputtags::part;
  277:     my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
  278:     if ($result 
  279: 	&& $Apache::lonhomework::type eq 'survey') { $result='SUBMITTED'; }
  280:     &Apache::response::handle_previous(\%previous,$result);
  281:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=$responsestr;
  282:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$result;
  283:     return '';
  284: }
  285: 
  286: sub end_foilgroup {
  287:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  288:     my $result='';
  289:     my @whichopt;
  290:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  291: 	$target eq 'analyze') {
  292: 	my ($count,$max) = &getfoilcounts($parstack,$safeeval);
  293: 	if ($count>$max) { $count=$max }
  294: 	&Apache::lonxml::debug("Count is $count from $max");
  295: 	@whichopt = &whichfoils($max);
  296: 	if ($target eq 'web' || $target eq 'tex') {
  297: 	    $result=&displayfoils($target,@whichopt);
  298: 	} elsif ($target eq 'grade') {
  299: 	    if ( defined $env{'form.submitted'}) { &gradefoils(@whichopt); }
  300: 	} elsif ( $target eq 'analyze') {
  301: 	    &Apache::response::analyze_store_foilgroup(\@whichopt,
  302: 						      ['text','image','area']);
  303: 	}
  304:     } elsif ($target eq 'edit') {
  305: 	$result=&Apache::edit::end_table();
  306:     }
  307:     &Apache::response::poprandomnumber();
  308:     return $result;
  309: }
  310: 
  311: sub start_conceptgroup {
  312:     $Apache::imageresponse::conceptgroup=1;
  313:     %Apache::response::conceptgroup=();
  314:     return '';
  315: }
  316: 
  317: sub end_conceptgroup {
  318:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  319:     $Apache::imageresponse::conceptgroup=0;
  320:     my $result;
  321:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  322: 	$target eq 'analyze') {
  323: 	&Apache::response::pick_foil_for_concept($target,
  324: 						 ['area','text','image'],
  325: 						 \%Apache::hint::image,
  326: 						 $parstack,$safeeval);
  327:     } elsif ($target eq 'edit') {
  328: 	$result=&Apache::edit::end_table();
  329:     }
  330:     return $result;
  331: }
  332: 
  333: sub insert_foil {
  334:     return '
  335:        <foil>
  336:            <image></image>
  337:            <text></text>
  338:            <rectangle></rectangle>
  339:        </foil>
  340: ';
  341: }
  342: 
  343: $Apache::imageresponse::curname='';
  344: sub start_foil {
  345:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  346:     my $result;
  347:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  348: 	$target eq 'analyze') {
  349: 	my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  350: 	if ($name eq "") {
  351: 	    &Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
  352: 	    $name=$Apache::lonxml::curdepth;
  353: 	}
  354: 	if (defined($Apache::response::foilnames{$name})) {
  355: 	    &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
  356: 	}
  357: 	$Apache::response::foilnames{$name}++;
  358: 	if ( $Apache::imageresponse::conceptgroup
  359: 	     #&& !&Apache::response::showallfoils()
  360: 	     ) {
  361: 	    push(@{ $Apache::response::conceptgroup{'names'} }, $name);
  362: 	} else {
  363: 	    push(@{ $Apache::response::foilgroup{'names'} }, $name);
  364: 	}
  365: 	$Apache::imageresponse::curname=$name;
  366:     } elsif ($target eq 'edit') {
  367: 	$result  = &Apache::edit::tag_start($target,$token);
  368: 	$result .= &Apache::edit::text_arg('Name:','name',$token);
  369: 	$result .= &Apache::edit::end_row().
  370: 	    &Apache::edit::start_spanning_row();
  371:     } elsif ($target eq 'modified') {
  372: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  373: 						     $safeeval,'name');
  374: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  375:     } 
  376:     return $result;;
  377: }
  378: 
  379: sub end_foil {
  380:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  381:     my $result;
  382:     if ($target eq 'edit') {
  383: 	$result=&Apache::edit::end_table();
  384:     }
  385:     return $result;
  386: }
  387: 
  388: sub start_text {
  389:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  390:     my $result='';
  391:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
  392: 	&Apache::lonxml::startredirection; 
  393:     } elsif ($target eq 'edit') {
  394: 	my $descr=&Apache::lonxml::get_all_text('/text',$parser,$style);
  395: 	$result=&Apache::edit::tag_start($target,$token,'Task Description').
  396: 	    &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
  397: 	    &Apache::edit::end_row();
  398:     } elsif ($target eq "modified") {
  399: 	$result=$token->[4].&Apache::edit::modifiedfield('/text',$parser);
  400:     }
  401:     return $result;
  402: }
  403: 
  404: sub end_text {
  405:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  406:     my $result;
  407:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  408: 	my $name = $Apache::imageresponse::curname;
  409: 	if ( $Apache::imageresponse::conceptgroup
  410: 	     #&& !&Apache::response::showallfoils()
  411: 	     ) {
  412: 	    $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
  413: 	} else {
  414: 	    $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
  415: 	}
  416:     } elsif ($target eq 'edit') {
  417: 	$result=&Apache::edit::end_table();
  418:     }
  419:     return $result;
  420: }
  421: 
  422: sub start_image {
  423:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  424:     my $result='';
  425:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
  426: 	&Apache::lonxml::startredirection; 
  427:     } elsif ($target eq 'edit') {
  428: 	my $bgimg=&Apache::lonxml::get_all_text('/image',$parser,$style);
  429: 	$Apache::edit::bgimgsrc=$bgimg;
  430: 	$Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
  431: 
  432: 	$result=&Apache::edit::tag_start($target,$token,'Clickable Image').
  433: 	    &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
  434: 	$result.=&Apache::edit::browse(undef,'textnode').' ';
  435: 	$result.=&Apache::edit::search(undef,'textnode').
  436: 	    &Apache::edit::end_row();
  437:     } elsif ($target eq "modified") {
  438: 	$result=$token->[4].&Apache::edit::modifiedfield('/image',$parser);
  439:     }
  440:     return $result;
  441: }
  442: 
  443: sub end_image {
  444:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  445:     my $result;
  446:     my $name = $Apache::imageresponse::curname;
  447:     if ($target eq 'web') {
  448: 	my $image = &Apache::lonxml::endredirection();
  449: 	&Apache::lonxml::debug("original image is $image");
  450: 	if ( $Apache::imageresponse::conceptgroup
  451: 	     #&& !&Apache::response::showallfoils()
  452: 	     ) {
  453: 	    $Apache::response::conceptgroup{"$name.image"} = $image;
  454: 	} else {
  455: 	    $Apache::response::foilgroup{"$name.image"} = $image;
  456: 	}
  457:     } elsif ($target eq 'analyze') {
  458: 	my $image = &Apache::lonxml::endredirection();
  459: 	if ( $Apache::imageresponse::conceptgroup
  460: 	     #&& !&Apache::response::showallfoils()
  461: 	     ) {
  462: 	    $Apache::response::conceptgroup{"$name.image"} = $image;
  463: 	} else {
  464: 	    $Apache::response::foilgroup{"$name.image"} = $image;
  465: 	}
  466:     } elsif ($target eq 'edit') {
  467: 	$result=&Apache::edit::end_table();
  468:     } elsif ($target eq 'tex') {
  469: 	my $src = &Apache::lonxml::endredirection();
  470: 
  471: 	#  There may be all sorts of whitespace on fore and aft:
  472: 
  473: 	$src =~ s/\s+$//s;
  474: 	$src =~ s/^\s+//s;
  475: 
  476: 	#
  477: 	#  Gnuplot e.g. just generates the latex to put inplace.
  478: 	#
  479: 	my $graphinclude;
  480: 	if ($src =~ /^%DYNAMICIMAGE/) {
  481: 	    # This is needed because the newline is not always passed -> tex.
  482: 	    # At present we don't care about the sizing info.
  483: 
  484: 	    my ($commentline, $restofstuff) = split(/\n/, $src);
  485: 	    $graphinclude = $src;
  486: 	    $graphinclude =~ s/^$commentline//;
  487: 	} else {
  488: 	    my ($path,$file) = &Apache::londefdef::get_eps_image($src);
  489: 	    my ($height_param,$width_param)=
  490: 		&Apache::londefdef::image_size($src,0.3,$parstack,$safeeval);
  491: 	    $graphinclude = '\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'}';
  492: 	}
  493: 	$Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent '.$graphinclude;
  494:     } 
  495:     return $result;
  496: }
  497: 
  498: sub start_rectangle {
  499:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  500:     my $result='';
  501:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  502: 	$target eq 'analyze') { 
  503: 	&Apache::lonxml::startredirection; 
  504:     } elsif ($target eq 'edit') {
  505: 	my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser,$style);
  506: 	$result=&Apache::edit::tag_start($target,$token,'Rectangle').
  507: 	    &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
  508: 	    &Apache::edit::entercoord(undef,'textnode',undef,undef,'box').
  509: 	    &Apache::edit::end_row();
  510:     } elsif ($target eq "modified") {
  511: 	&Apache::edit::deletecoorddata();
  512: 	$result=$token->[4].&Apache::edit::modifiedfield('/rectangle',$parser);
  513:     }
  514:     return $result;
  515: }
  516: 
  517: sub grade_rectangle {
  518:     my ($spec,$x,$y) = @_;
  519:     &Apache::lonxml::debug("Spec is $spec");
  520:     my ($x1,$y1,$x2,$y2)=($spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
  521:     &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
  522:     if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
  523:     if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
  524:     if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
  525: 	return 'APPROX_ANS';
  526:     }
  527:     return 'INCORRECT';
  528: }
  529: 
  530: sub end_rectangle {
  531:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  532:     my $result;
  533:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  534: 	$target eq 'analyze') {
  535: 	my $name = $Apache::imageresponse::curname;
  536: 	my $area = &Apache::lonxml::endredirection;
  537: 	$area=~s/\s//g;
  538: 	&Apache::lonxml::debug("out is $area for $name");
  539: 	if ( $Apache::imageresponse::conceptgroup
  540: 	     #&& !&Apache::response::showallfoils()
  541: 	     ) {
  542: 	    push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
  543: 	} else {
  544: 	    push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
  545: 	}
  546:     } elsif ($target eq 'edit') {
  547: 	$result=&Apache::edit::end_table();
  548:     }
  549:     return $result;
  550: }
  551: 
  552: sub start_polygon {
  553:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  554:     my $result='';
  555:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  556: 	$target eq 'analyze') { 
  557: 	&Apache::lonxml::startredirection; 
  558:     } elsif ($target eq 'edit') {
  559: 	my $coords=&Apache::lonxml::get_all_text('/polygon',$parser,$style);
  560: 	$result=&Apache::edit::tag_start($target,$token,'Polygon').
  561: 	    &Apache::edit::editline($token->[1],$coords,'Coordinate list',40).
  562: 	    &Apache::edit::entercoord(undef,'textnode',undef,undef,'polygon').
  563: 	    &Apache::edit::end_row();
  564:     } elsif ($target eq "modified") {
  565: 	$result=$token->[4].&Apache::edit::modifiedfield('/polygon',$parser);
  566:     }
  567:     return $result;
  568: }
  569: 
  570: sub grade_polygon {
  571:     my ($spec,$x,$y) = @_;
  572:     &Apache::lonxml::debug("Spec is $spec");
  573:     $spec=~s/^polygon://;
  574:     my @polygon;
  575:     foreach my $coord (split('-',$spec)) {
  576: 	my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
  577: 	&Apache::lonxml::debug("x $x y $y");
  578: 	push @polygon, {'x'=>$x,'y'=>$y};
  579:     }
  580:     #make end point start point
  581:     push @polygon, $polygon[0];
  582:     # cribbed from
  583:     # http://geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm
  584:     my $crossing = 0;    # the crossing number counter
  585: 
  586:     # loop through all edges of the polygon
  587:     for (my $i=0; $i<$#polygon; $i++) {    # edge from V[i] to V[i+1]
  588: 	if ((($polygon[$i]->{'y'} <= $y)
  589: 	     && ($polygon[$i+1]->{'y'} > $y))    # an upward crossing
  590: 	    || 
  591: 	    (($polygon[$i]->{'y'} > $y) 
  592: 	     && ($polygon[$i+1]->{'y'} <= $y))) { # a downward crossing
  593: 	    # compute the actual edge-ray intersect x-coordinate
  594:             my $vt = ($y - $polygon[$i]->{'y'}) 
  595: 		/ ($polygon[$i+1]->{'y'} - $polygon[$i]->{'y'});
  596:             if ($x < $polygon[$i]->{'x'} + $vt * 
  597: 		($polygon[$i+1]->{'x'} - $polygon[$i]->{'x'})) { # x<intersect
  598:                 $crossing++;   # a valid crossing of y=P.y right of P.x
  599: 	    }
  600: 	}
  601:     }
  602: 
  603:     # 0 if even (out), and 1 if odd (in)
  604:     if ($crossing%2) {
  605: 	return 'APPROX_ANS';
  606:     } else {
  607: 	return 'INCORRECT';
  608:     }
  609: }
  610: 
  611: sub end_polygon {
  612:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  613:     my $result;
  614:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  615: 	$target eq 'analyze') {
  616: 	my $name = $Apache::imageresponse::curname;
  617: 	my $area = &Apache::lonxml::endredirection;
  618: 	$area=~s/\s*//g;
  619: 	&Apache::lonxml::debug("out is $area for $name");
  620: 	if ( $Apache::imageresponse::conceptgroup
  621: 	     #&& !&Apache::response::showallfoils()
  622: 	     ) {
  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>