File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.74: download - view: text, annotated - select for diffs
Thu Mar 9 01:11:12 2006 UTC (18 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- consolidating the 'leave blank on scoring form code'
- image response gets blank lines in exam mode
- imageresponse can be scored in exam mode

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

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