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

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

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