Annotation of loncom/homework/imageresponse.pm, revision 1.44

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

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