File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.58: download - view: text, annotated - select for diffs
Wed Dec 8 01:00:41 2004 UTC (19 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: version_1_3_X, version_1_3_1, version_1_3_0, version_1_2_99_1, HEAD
 - fix BUG#3589, revert back to using X to mar location, and only show location when the user is incorrect, when corect just show the green area of goodness

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

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