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

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

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