File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.75: download - view: text, annotated - select for diffs
Thu Mar 9 01:33:57 2006 UTC (18 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- answer mode works for image response

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

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