File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.77: download - view: text, annotated - select for diffs
Thu May 18 22:02:03 2006 UTC (18 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- only addd the exam score line in exam mode

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

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