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

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

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