File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.49: download - view: text, annotated - select for diffs
Mon Feb 23 23:55:40 2004 UTC (20 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- convertedd for us by the new randomlylabel

    1: 
    2: # The LearningOnline Network with CAPA
    3: # image click response style
    4: #
    5: # $Id: imageresponse.pm,v 1.49 2004/02/23 23:55:40 albertel Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: #FIXME LATER assumes multiple possible submissions but only one is possible 
   30: #currently
   31: 
   32: package Apache::imageresponse;
   33: use strict;
   34: use Image::Magick;
   35: use Apache::randomlylabel;
   36: use Apache::londefdef;
   37: use Apache::Constants qw(:common :http);
   38: 
   39: BEGIN {
   40:     &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
   41: }
   42: 
   43: sub start_imageresponse {
   44:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   45:     my $result;
   46:     #when in a radiobutton response use these
   47:     &Apache::lonxml::register('Apache::imageresponse',
   48: 			      ('foilgroup','foil','text','image','rectangle',
   49: 			       'polygon','conceptgroup'));
   50:     push (@Apache::lonxml::namespace,'imageresponse');
   51:     my $id = &Apache::response::start_response($parstack,$safeeval);
   52:     if ($target eq 'meta') {
   53: 	$result=&Apache::response::meta_package_write('imageresponse');
   54:     } elsif ($target eq 'analyze') {
   55: 	my $part_id="$Apache::inputtags::part.$id";
   56: 	push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
   57:     }
   58:     return $result;
   59: }
   60: 
   61: sub end_imageresponse {
   62:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   63:     &Apache::response::end_response;
   64:     pop @Apache::lonxml::namespace;
   65:     &Apache::lonxml::deregister('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
   66:     my $result;
   67:     if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
   68:     return $result;
   69: }
   70: 
   71: %Apache::response::foilgroup=();
   72: sub start_foilgroup {
   73:     %Apache::response::foilgroup=();
   74:     $Apache::imageresponse::conceptgroup=0;
   75:     &Apache::response::setrandomnumber();
   76:     return '';
   77: }
   78: 
   79: sub getfoilcounts {
   80:     my ($parstack,$safeeval)=@_;
   81: 
   82:     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
   83:     # +1 since instructors will count from 1
   84:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
   85:     if (&Apache::response::showallfoils()) { $max=$count; }
   86:     return ($count,$max);
   87: }
   88: 
   89: sub whichfoils {
   90:     my ($max)=@_;
   91:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
   92:     my @names = @{ $Apache::response::foilgroup{'names'} };
   93:     my @whichopt =();
   94:     while ((($#whichopt+1) < $max) && ($#names > -1)) {
   95: 	&Apache::lonxml::debug("Have $#whichopt max is $max");
   96: 	my $aopt;
   97: 	if (&Apache::response::showallfoils()) {
   98: 	    $aopt=0;
   99: 	} else {
  100: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
  101: 	}
  102: 	&Apache::lonxml::debug("From $#names elms, picking $aopt");
  103: 	$aopt=splice(@names,$aopt,1);
  104: 	&Apache::lonxml::debug("Picked $aopt");
  105: 	push (@whichopt,$aopt);
  106:     }
  107:     return @whichopt;
  108: }
  109: 
  110: sub prep_image {
  111:     my ($image,$mode,$name)=@_;
  112:     my $part=$Apache::inputtags::part;
  113:     my $respid=$Apache::inputtags::response['-1'];
  114:     my $id=&Apache::loncommon::get_cgi_id();
  115:     my (%x,$i);
  116:     $x{"cgi.$id.BGIMG"}=&Apache::lonnet::escape($image);
  117:     my ($x,$y)=split(/:/,$Apache::lonhomework::history{"resource.$part.$respid.submission"});
  118:     #draws 2 xs on the image at the clicked location
  119:     #one in white and then one in red on top of the one in white
  120:     if (defined($x) && $x=~/\S/ && defined($y) && $y =~/\S/) {
  121: 	my $length = 6;
  122: 	my $width = 1;
  123: 	my $extrawidth = 2;
  124: 	my $xmin=($x-$length);
  125: 	my $xmax=($x+$length); 
  126: 	my $ymin=($y-$length);
  127: 	my $ymax=($y+$length);
  128: 
  129: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
  130: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
  131: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
  132: 				      "FFFFFF",($width+$extrawidth)));
  133: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
  134: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
  135: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
  136: 				      "FFFFFF",($width+$extrawidth)));
  137: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
  138: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
  139: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
  140: 				      "FF0000",($width)));
  141: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
  142: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
  143: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
  144: 				      "FF0000",($width)));
  145:     }
  146:     if ($mode eq 'answer') {
  147: 	my $width = 1;
  148: 	my $extrawidth = 2;
  149: 	my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
  150: 	foreach my $area (@areas) {
  151: 	    if ($area=~/^rectangle:/) {
  152: 		$x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
  153: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
  154: 		my ($x1,$y1,$x2,$y2)=
  155: 		    ($area=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
  156: 		$x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"FFFFFF",
  157: 					      ($width+$extrawidth)));
  158: 		$x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
  159: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
  160: 		$x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"00FF00",$width));
  161: 	    } elsif ($area=~/^polygon:(.*)/) {
  162: 		$x{"cgi.$id.OBJTYPE"}.='POLYGON:';
  163: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
  164: 		$x{"cgi.$id.OBJ$i"}=join(':',("FFFFFF",($width+$extrawidth)));
  165: 		$x{"cgi.$id.OBJEXTRA$i"}=$1;
  166: 		$x{"cgi.$id.OBJTYPE"}.='POLYGON:';
  167: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
  168: 		$x{"cgi.$id.OBJ$i"}=join(':',("00FF00",$width));
  169: 		$x{"cgi.$id.OBJEXTRA$i"}=$1;
  170: 	    }
  171: 	}
  172:     }
  173:     &Apache::lonnet::appenv(%x);
  174:     return $id;
  175: }
  176: 
  177: sub displayfoils {
  178:     my ($target,@whichopt) = @_;
  179:     my $result ='';
  180:     my $name;
  181:     my $temp=1;
  182:     foreach $name (@whichopt) {
  183: 	$result.=$Apache::response::foilgroup{"$name.text"};
  184: 	&Apache::lonxml::debug("Text is $result");
  185: 	if ($target eq 'tex') {$result.="\\vskip 0 mm \n";} else {$result.="<br />\n";}
  186: 	my $image=$Apache::response::foilgroup{"$name.image"};
  187: 	&Apache::lonxml::debug("image is $image");
  188: 	if ( $target eq 'web' && $image !~ /^http:/ ) {
  189: 	    $image=&clean_up_image($image);
  190: 	} 
  191: 	&Apache::lonxml::debug("image is $image");
  192: 	if ( &Apache::response::show_answer() ) {
  193: 	    if ($target eq 'tex') {
  194: 		$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
  195: 	    } else {
  196: 		my $token=&prep_image($image,'answer',$name);
  197: 		$result.="<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
  198: 	    }
  199: 	} else {
  200: 	    if ($target eq 'tex') {
  201: 		$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
  202: 	    } else {
  203: 		my $id=$Apache::inputtags::response['-1'];
  204: 		my $token=&prep_image($image);
  205: 		my $temp=1;
  206: 		$result.="<input type=\"image\" name=\"HWVAL_$id:$temp\" ".
  207: 		    "src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
  208: 	    }
  209: 	}
  210: 	$temp++;
  211:     }
  212:     return $result;
  213: }
  214: 
  215: sub clean_up_image {
  216:     my ($image)=@_;
  217:     if ($image =~ /\s*<img\s*/) {
  218: 	($image) = ($image =~ /src\s*=\s*\"([^\"]+)\"/i);
  219: 	if ($image !~ /^http:/) {
  220: 	    $image=&Apache::lonnet::hreflocation('',$image);
  221: 	}
  222: 	if (!$image) {
  223: 	    $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
  224: 	}
  225:     } else {
  226: 	$image=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$image);
  227: 	if (&Apache::lonnet::repcopy($image) ne OK) {
  228: 	    $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
  229: 	}
  230:     }
  231:     return $image;
  232: }
  233: 
  234: sub gradefoils {
  235:     my (@whichopt) = @_;
  236:     my $x;
  237:     my $y;
  238:     my $result;
  239:     my $id=$Apache::inputtags::response['-1'];
  240:     my $temp=1;
  241:     foreach my $name (@whichopt) {
  242: 	$x=$ENV{"form.HWVAL_$id:$temp.x"};
  243: 	$y=$ENV{"form.HWVAL_$id:$temp.y"};
  244: 	&Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
  245: 	if (defined($x) && defined($y) &&
  246: 	    defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
  247: 	    my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
  248: 	    my $grade="INCORRECT";
  249: 	    foreach my $area (@areas) {
  250: 		&Apache::lonxml::debug("Area is $area for $name");
  251: 		$area =~ m/([a-z]*):/;
  252: 		&Apache::lonxml::debug("Area of type $1");
  253: 		if ($1 eq 'rectangle') {
  254: 		    $grade=&grade_rectangle($area,$x,$y);
  255: 		} elsif ($1 eq 'polygon') {
  256: 		    $grade=&grade_polygon($area,$x,$y);
  257: 		} else {
  258: 		    &Apache::lonxml::error("Unknown area style $area");
  259: 		}
  260: 		&Apache::lonxml::debug("Area said $grade");
  261: 		if ($grade eq 'APPROX_ANS') { last; }
  262: 	    }
  263: 	    &Apache::lonxml::debug("Foil was $grade");
  264: 	    if ($grade eq 'INCORRECT') { $result= 'INCORRECT'; }
  265: 	    if (($grade eq 'APPROX_ANS') && ($result ne 'APPROX_ANS')) { $result=$grade; }
  266: 	    &Apache::lonxml::debug("Question is $result");
  267: 	    $temp++;
  268: 	}
  269:     }
  270:     $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}="$x:$y";
  271:     $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}=$result;
  272:     return '';
  273: }
  274: 
  275: sub end_foilgroup {
  276:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  277:     my $result='';
  278:     my @whichopt;
  279:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  280: 	$target eq 'analyze') {
  281: 	my ($count,$max) = &getfoilcounts($parstack,$safeeval);
  282: 	if ($count>$max) { $count=$max }
  283: 	&Apache::lonxml::debug("Count is $count from $max");
  284: 	@whichopt = &whichfoils($max);
  285: 	if ($target eq 'web' || $target eq 'tex') {
  286: 	    $result=&displayfoils($target,@whichopt);
  287: 	} elsif ($target eq 'grade') {
  288: 	    if ( defined $ENV{'form.submitted'}) { &gradefoils(@whichopt); }
  289: 	} elsif ( $target eq 'analyze') {
  290: 	    &Apache::response::analyze_store_foilgroup(\@whichopt,
  291: 						      ['text','image','area']);
  292: 	}
  293:     } elsif ($target eq 'edit') {
  294: 	$result=&Apache::edit::end_table();
  295:     }
  296:     return $result;
  297: }
  298: 
  299: sub start_conceptgroup {
  300:     $Apache::imageresponse::conceptgroup=1;
  301:     %Apache::response::conceptgroup=();
  302:     return '';
  303: }
  304: 
  305: sub end_conceptgroup {
  306:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  307:     $Apache::imageresponse::conceptgroup=0;
  308:     my $result;
  309:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  310: 	$target eq 'analyze') {
  311: 	&Apache::response::pick_foil_for_concept($target,
  312: 						 ['area','text','image'],
  313: 						 \%Apache::hint::image,
  314: 						 $parstack,$safeeval);
  315:     } elsif ($target eq 'edit') {
  316: 	$result=&Apache::edit::end_table();
  317:     }
  318:     return $result;
  319: }
  320: 
  321: sub insert_foil {
  322:     return '
  323:        <foil>
  324:            <image></image>
  325:            <text></text>
  326:            <rectangle></rectangle>
  327:        </foil>
  328: ';
  329: }
  330: 
  331: $Apache::imageresponse::curname='';
  332: sub start_foil {
  333:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  334:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  335: 	$target eq 'analyze') {
  336: 	my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  337: 	if ($name eq '') { $name=$Apache::lonxml::curdepth; }
  338: 	if ( $Apache::imageresponse::conceptgroup
  339: 	     && !&Apache::response::showallfoils()) {
  340: 	    push(@{ $Apache::response::conceptgroup{'names'} }, $name);
  341: 	} else {
  342: 	    push(@{ $Apache::response::foilgroup{'names'} }, $name);
  343: 	}
  344: 	$Apache::imageresponse::curname=$name;
  345:     }
  346:     return '';
  347: }
  348: 
  349: sub end_foil {
  350:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  351:     my $result;
  352:     if ($target eq 'edit') {
  353: 	$result=&Apache::edit::end_table();
  354:     }
  355:     return $result;
  356: }
  357: 
  358: sub start_text {
  359:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  360:     my $result='';
  361:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
  362: 	&Apache::lonxml::startredirection; 
  363:     } elsif ($target eq 'edit') {
  364: 	my $descr=&Apache::lonxml::get_all_text('/text',$parser);
  365: 	$result=&Apache::edit::tag_start($target,$token,'Task Description').
  366: 	    &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
  367: 	    &Apache::edit::end_row();
  368:     } elsif ($target eq "modified") {
  369: 	$result=$token->[4].&Apache::edit::modifiedfield('/text',$parser);
  370:     }
  371:     return $result;
  372: }
  373: 
  374: sub end_text {
  375:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  376:     my $result;
  377:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  378: 	my $name = $Apache::imageresponse::curname;
  379: 	if ( $Apache::imageresponse::conceptgroup
  380: 	     && !&Apache::response::showallfoils() ) {
  381: 	    $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
  382: 	} else {
  383: 	    $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
  384: 	}
  385:     } elsif ($target eq 'edit') {
  386: 	$result=&Apache::edit::end_table();
  387:     }
  388:     return $result;
  389: }
  390: 
  391: sub start_image {
  392:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  393:     my $result='';
  394:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
  395: 	&Apache::lonxml::startredirection; 
  396:     } elsif ($target eq 'edit') {
  397: 	my $bgimg=&Apache::lonxml::get_all_text('/image',$parser);
  398: 	$Apache::edit::bgimgsrc=$bgimg;
  399: 	$Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
  400: 
  401: 	$result=&Apache::edit::tag_start($target,$token,'Clickable Image').
  402: 	    &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
  403: 	$result.=&Apache::edit::browse(undef,'textnode').' ';
  404: 	$result.=&Apache::edit::search(undef,'textnode').
  405: 	    &Apache::edit::end_row();
  406:     } elsif ($target eq "modified") {
  407: 	$result=$token->[4].&Apache::edit::modifiedfield('/image',$parser);
  408:     }
  409:     return $result;
  410: }
  411: 
  412: sub end_image {
  413:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  414:     my $result;
  415:     my $name = $Apache::imageresponse::curname;
  416:     if ($target eq 'web') {
  417: 	my $image = &Apache::lonxml::endredirection;
  418: 	&Apache::lonxml::debug("original image is $image");
  419: 	if ( $Apache::imageresponse::conceptgroup
  420: 	     && !&Apache::response::showallfoils()) {
  421: 	    $Apache::response::conceptgroup{"$name.image"} = $image;
  422: 	} else {
  423: 	    $Apache::response::foilgroup{"$name.image"} = $image;
  424: 	}
  425:     } elsif ($target eq 'analyze') {
  426: 	my $image = &Apache::lonxml::endredirection;
  427: 	if ( $Apache::imageresponse::conceptgroup
  428: 	     && !&Apache::response::showallfoils()) {
  429: 	    $Apache::response::conceptgroup{"$name.image"} = $image;
  430: 	} else {
  431: 	    $Apache::response::foilgroup{"$name.image"} = $image;
  432: 	}
  433:     } elsif ($target eq 'edit') {
  434: 	$result=&Apache::edit::end_table();
  435:     } elsif ($target eq 'tex') {
  436: 	my $src = &Apache::lonxml::endredirection;
  437: 	$src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
  438: 	my $width_param = '';
  439: 	my $height_param = '';
  440: 	my $scaling = .3;
  441: 	my $image = Image::Magick->new;
  442: 	my $current_figure = $image->Read($src);
  443: 	$width_param = $image->Get('width') * $scaling;;
  444: 	$height_param = $image->Get('height') * $scaling;;
  445: 	undef $image;
  446: 	my $epssrc = $src;
  447: 	$epssrc =~ s/(\.gif|\.jpg)$/\.eps/i;
  448: 	if (not -e $epssrc) {
  449: 	    my $localfile = $epssrc;
  450: 	    $localfile =~ s/.*(\/res)/$1/;	
  451: 	    my $file;
  452: 	    my $path;	
  453: 	    if ($localfile =~ m!(.*)/([^/]*)$!) {
  454: 		$file = $2;
  455: 		$path = $1.'/'; 
  456: 	    }	
  457: 	    my $signal_eps = 0;
  458: 	    my @content_directory = &Apache::lonnet::dirlist($path);
  459: 	    for (my $iy=0;$iy<=$#content_directory;$iy++) {
  460: 		my @tempo_array = split(/&/,$content_directory[$iy]);
  461: 		$content_directory[$iy] = $tempo_array[0];
  462: 		if ($file eq $tempo_array[0]) {
  463: 		    $signal_eps = 1;
  464: 		    last;
  465: 		}
  466: 	    }
  467: 	    if ($signal_eps) {
  468: 		my $eps_file = &Apache::lonnet::getfile($localfile);
  469: 	    } else {
  470: 		$localfile = $src;
  471: 		$localfile =~ s/.*(\/res)/$1/;	
  472: 		my $as = &Apache::lonnet::getfile($src);		      
  473: 	    }
  474: 	}
  475: 	my $file;
  476: 	my $path;	
  477: 	if ($src =~ m!(.*)/([^/]*)$!) {
  478: 	    $file = $2;
  479: 	    $path = $1.'/'; 
  480: 	}
  481: 	my $newsrc = $src;
  482: 	$newsrc =~ s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
  483: 	$file=~s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
  484: 	#do we have any specified size of the picture?
  485: 	my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval); 
  486: 	my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval); 
  487: 	my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval);
  488: 	if ($TeXwidth ne '') { 
  489: 	    $width_param = $TeXwidth; 
  490: 	} elsif ($TeXheight ne '') { 
  491: 	    $width_param = $TeXheight/$height_param*$width_param;
  492: 	} elsif ($width ne '') {
  493: 	    $width_param = $width*$scaling;      
  494: 	}
  495: 	#where can we find the picture?
  496: 	if (-e $newsrc) {
  497: 	    if ($path) {
  498: 		$Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
  499: 	    }
  500: 	} else {
  501: 	    #care about eps dynamical generation
  502: 	    $Apache::response::foilgroup{"$name.image"}='\vskip 0 mm '.&Apache::londefdef::eps_generation($src,$file,$width_param);
  503: 	}
  504:     } 
  505:     return $result;
  506: }
  507: 
  508: sub start_rectangle {
  509:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  510:     my $result='';
  511:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  512: 	$target eq 'analyze') { 
  513: 	&Apache::lonxml::startredirection; 
  514:     } elsif ($target eq 'edit') {
  515: 	my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser);
  516: 	$result=&Apache::edit::tag_start($target,$token,'Rectangle').
  517: 	    &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
  518: 	    &Apache::edit::entercoord(undef,'textnode',undef,undef,'box').
  519: 	    &Apache::edit::end_row();
  520:     } elsif ($target eq "modified") {
  521: 	&Apache::edit::deletecoorddata();
  522: 	$result=$token->[4].&Apache::edit::modifiedfield('/rectangle',$parser);
  523:     }
  524:     return $result;
  525: }
  526: 
  527: sub grade_rectangle {
  528:     my ($spec,$x,$y) = @_;
  529:     &Apache::lonxml::debug("Spec is $spec");
  530:     my ($x1,$y1,$x2,$y2)=($spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
  531:     &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
  532:     if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
  533:     if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
  534:     if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
  535: 	return 'APPROX_ANS';
  536:     }
  537:     return 'INCORRECT';
  538: }
  539: 
  540: sub end_rectangle {
  541:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  542:     my $result;
  543:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  544: 	$target eq 'analyze') {
  545: 	my $name = $Apache::imageresponse::curname;
  546: 	my $area = &Apache::lonxml::endredirection;
  547: 	&Apache::lonxml::debug("out is $area for $name");
  548: 	if ( $Apache::imageresponse::conceptgroup
  549: 	     && !&Apache::response::showallfoils()) {
  550: 	    push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
  551: 	} else {
  552: 	    push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
  553: 	}
  554:     } elsif ($target eq 'edit') {
  555: 	$result=&Apache::edit::end_table();
  556:     }
  557:     return $result;
  558: }
  559: 
  560: sub start_polygon {
  561:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  562:     my $result='';
  563:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  564: 	$target eq 'analyze') { 
  565: 	&Apache::lonxml::startredirection; 
  566:     } elsif ($target eq 'edit') {
  567: 	my $coords=&Apache::lonxml::get_all_text('/polygon',$parser);
  568: 	$result=&Apache::edit::tag_start($target,$token,'Polygon').
  569: 	    &Apache::edit::editline($token->[1],$coords,'Coordinate list',40).
  570: 	    &Apache::edit::entercoord(undef,'textnode',undef,undef,'polygon').
  571: 	    &Apache::edit::end_row();
  572:     } elsif ($target eq "modified") {
  573: 	$result=$token->[4].&Apache::edit::modifiedfield('/polygon',$parser);
  574:     }
  575:     return $result;
  576: }
  577: 
  578: sub grade_polygon {
  579:     my ($spec,$x,$y) = @_;
  580:     &Apache::lonxml::debug("Spec is $spec");
  581:     $spec=~s/^polygon://;
  582:     my @polygon;
  583:     foreach my $coord (split('-',$spec)) {
  584: 	my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
  585: 	&Apache::lonxml::debug("x $x y $y");
  586: 	push @polygon, {'x'=>$x,'y'=>$y};
  587:     }
  588:     #make end point start point
  589:     push @polygon, $polygon[0];
  590:     # cribbed from
  591:     # http://geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm
  592:     my $crossing = 0;    # the crossing number counter
  593: 
  594:     # loop through all edges of the polygon
  595:     for (my $i=0; $i<$#polygon; $i++) {    # edge from V[i] to V[i+1]
  596: 	if ((($polygon[$i]->{'y'} <= $y)
  597: 	     && ($polygon[$i+1]->{'y'} > $y))    # an upward crossing
  598: 	    || 
  599: 	    (($polygon[$i]->{'y'} > $y) 
  600: 	     && ($polygon[$i+1]->{'y'} <= $y))) { # a downward crossing
  601: 	    # compute the actual edge-ray intersect x-coordinate
  602:             my $vt = ($y - $polygon[$i]->{'y'}) 
  603: 		/ ($polygon[$i+1]->{'y'} - $polygon[$i]->{'y'});
  604:             if ($x < $polygon[$i]->{'x'} + $vt * 
  605: 		($polygon[$i+1]->{'x'} - $polygon[$i]->{'x'})) { # x<intersect
  606:                 $crossing++;   # a valid crossing of y=P.y right of P.x
  607: 	    }
  608: 	}
  609:     }
  610: 
  611:     # 0 if even (out), and 1 if odd (in)
  612:     if ($crossing%2) {
  613: 	return 'APPROX_ANS';
  614:     } else {
  615: 	return 'INCORRECT';
  616:     }
  617: }
  618: 
  619: sub end_polygon {
  620:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  621:     my $result;
  622:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
  623: 	$target eq 'analyze') {
  624: 	my $name = $Apache::imageresponse::curname;
  625: 	my $area = &Apache::lonxml::endredirection;
  626: 	$area=~s/\s*//g;
  627: 	&Apache::lonxml::debug("out is $area for $name");
  628: 	if ( $Apache::imageresponse::conceptgroup
  629: 	     && !&Apache::response::showallfoils()) {
  630: 	    push @{ $Apache::response::conceptgroup{"$name.area"} },"polygon:$area";
  631: 	} else {
  632: 	    push @{ $Apache::response::foilgroup{"$name.area"} },"polygon:$area";
  633: 	}
  634:     } elsif ($target eq 'edit') {
  635: 	$result=&Apache::edit::end_table();
  636:     }
  637:     return $result;
  638: }
  639: 1;
  640: __END__
  641:  

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