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

1.58      albertel    1: 
1.12      albertel    2: # The LearningOnline Network with CAPA
1.14      albertel    3: # image click response style
                      4: #
1.83    ! banghart    5: # $Id: imageresponse.pm,v 1.82 2007/04/18 01:06:22 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.51      albertel   34: use Image::Magick();
                     35: use Apache::randomlylabel();
                     36: use Apache::londefdef();
1.40      albertel   37: use Apache::Constants qw(:common :http);
1.51      albertel   38: use Apache::lonlocal;
1.65      albertel   39: use Apache::lonnet;
1.78      www        40: use lib '/home/httpd/lib/perl/';
                     41: use LONCAPA;
                     42:  
1.1       albertel   43: 
1.16      harris41   44: BEGIN {
1.36      albertel   45:     &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
1.1       albertel   46: }
                     47: 
                     48: sub start_imageresponse {
1.36      albertel   49:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     50:     my $result;
                     51:     #when in a radiobutton response use these
1.43      albertel   52:     &Apache::lonxml::register('Apache::imageresponse',
                     53: 			      ('foilgroup','foil','text','image','rectangle',
                     54: 			       'polygon','conceptgroup'));
1.36      albertel   55:     push (@Apache::lonxml::namespace,'imageresponse');
                     56:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.51      albertel   57:     undef(%Apache::response::foilnames);
1.36      albertel   58:     if ($target eq 'meta') {
                     59: 	$result=&Apache::response::meta_package_write('imageresponse');
1.37      albertel   60:     } elsif ($target eq 'analyze') {
                     61: 	my $part_id="$Apache::inputtags::part.$id";
                     62: 	push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.36      albertel   63:     }
                     64:     return $result;
1.1       albertel   65: }
                     66: 
                     67: sub end_imageresponse {
1.30      albertel   68:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.74      albertel   69: 
                     70:     pop(@Apache::lonxml::namespace);
1.30      albertel   71:     &Apache::lonxml::deregister('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
1.74      albertel   72: 
1.30      albertel   73:     my $result;
1.74      albertel   74:     if ($target eq 'edit') {
                     75: 	$result=&Apache::edit::end_table();
1.77      albertel   76:     } elsif ($target eq 'tex'
                     77: 	     && $Apache::lonhomework::type eq 'exam') {
1.74      albertel   78: 	$result=&Apache::inputtags::exam_score_line($target);
                     79:     }
                     80: 
1.51      albertel   81:     undef(%Apache::response::foilnames);
1.74      albertel   82:     
                     83:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
                     84: 	$target eq 'tex' || $target eq 'analyze') {
                     85: 	&Apache::lonxml::increment_counter(&Apache::response::repetition());
                     86:     }
                     87:     &Apache::response::end_response();
                     88: 
1.30      albertel   89:     return $result;
1.1       albertel   90: }
                     91: 
1.20      albertel   92: %Apache::response::foilgroup=();
1.1       albertel   93: sub start_foilgroup {
1.36      albertel   94:     %Apache::response::foilgroup=();
                     95:     $Apache::imageresponse::conceptgroup=0;
1.53      albertel   96:     &Apache::response::pushrandomnumber();
1.36      albertel   97:     return '';
1.1       albertel   98: }
                     99: 
1.2       albertel  100: sub getfoilcounts {
1.36      albertel  101:     my ($parstack,$safeeval)=@_;
1.12      albertel  102: 
1.36      albertel  103:     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
                    104:     # +1 since instructors will count from 1
                    105:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
1.56      albertel  106:     #if (&Apache::response::showallfoils()) { $max=$count; }
1.36      albertel  107:     return ($count,$max);
1.2       albertel  108: }
                    109: 
                    110: sub whichfoils {
1.36      albertel  111:     my ($max)=@_;
                    112:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
                    113:     my @names = @{ $Apache::response::foilgroup{'names'} };
                    114:     my @whichopt =();
                    115:     while ((($#whichopt+1) < $max) && ($#names > -1)) {
                    116: 	&Apache::lonxml::debug("Have $#whichopt max is $max");
                    117: 	my $aopt;
1.56      albertel  118: #	if (&Apache::response::showallfoils()) {
                    119: #	    $aopt=0;
                    120: #	} else {
1.36      albertel  121: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
1.56      albertel  122: #	}
1.36      albertel  123: 	&Apache::lonxml::debug("From $#names elms, picking $aopt");
                    124: 	$aopt=splice(@names,$aopt,1);
                    125: 	&Apache::lonxml::debug("Picked $aopt");
                    126: 	push (@whichopt,$aopt);
                    127:     }
                    128:     return @whichopt;
1.2       albertel  129: }
                    130: 
1.40      albertel  131: sub prep_image {
1.42      albertel  132:     my ($image,$mode,$name)=@_;
1.40      albertel  133:     my $part=$Apache::inputtags::part;
1.41      albertel  134:     my $respid=$Apache::inputtags::response['-1'];
1.82      albertel  135:     my ($x,$y)=split(/:/,$Apache::lonhomework::history{"resource.$part.$respid.submission"});
                    136:     &draw_image($mode,$image,$x,$y,$Apache::response::foilgroup{"$name.area"});
                    137: }
                    138: 
                    139: sub draw_image {
                    140:     my ($mode,$image,$x,$y,$areas) = @_;
                    141: 
1.41      albertel  142:     my $id=&Apache::loncommon::get_cgi_id();
1.82      albertel  143: 
1.49      albertel  144:     my (%x,$i);
1.78      www       145:     $x{"cgi.$id.BGIMG"}=&escape($image);
1.82      albertel  146: 
1.40      albertel  147:     #draws 2 xs on the image at the clicked location
                    148:     #one in white and then one in red on top of the one in white
1.82      albertel  149: 
1.75      albertel  150:     if (defined($x)    && $x =~/\S/ 
                    151: 	&& defined($y) && $y =~/\S/ 
1.82      albertel  152: 	&& ($mode eq 'submission' || !&Apache::response::show_answer())
1.75      albertel  153: 	&& $mode ne 'answeronly') {
1.40      albertel  154: 	my $length = 6;
                    155: 	my $width = 1;
                    156: 	my $extrawidth = 2;
1.48      albertel  157: 	my $xmin=($x-$length);
                    158: 	my $xmax=($x+$length); 
                    159: 	my $ymin=($y-$length);
                    160: 	my $ymax=($y+$length);
                    161: 
1.49      albertel  162: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
                    163: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
1.58      albertel  164: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
1.49      albertel  165: 				      "FFFFFF",($width+$extrawidth)));
                    166: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
                    167: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
1.58      albertel  168: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
1.49      albertel  169: 				      "FFFFFF",($width+$extrawidth)));
                    170: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
                    171: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
1.58      albertel  172: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
1.49      albertel  173: 				      "FF0000",($width)));
                    174: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
                    175: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
1.58      albertel  176: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
1.49      albertel  177: 				      "FF0000",($width)));
1.40      albertel  178:     }
1.75      albertel  179:     if ($mode eq 'answer' || $mode eq 'answeronly') {
1.42      albertel  180: 	my $width = 1;
                    181: 	my $extrawidth = 2;
1.82      albertel  182: 	foreach my $area (@{ $areas }) {
1.43      albertel  183: 	    if ($area=~/^rectangle:/) {
1.49      albertel  184: 		$x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
                    185: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
1.43      albertel  186: 		my ($x1,$y1,$x2,$y2)=
                    187: 		    ($area=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
1.49      albertel  188: 		$x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"FFFFFF",
1.43      albertel  189: 					      ($width+$extrawidth)));
1.49      albertel  190: 		$x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
                    191: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
                    192: 		$x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"00FF00",$width));
1.43      albertel  193: 	    } elsif ($area=~/^polygon:(.*)/) {
1.49      albertel  194: 		$x{"cgi.$id.OBJTYPE"}.='POLYGON:';
                    195: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
                    196: 		$x{"cgi.$id.OBJ$i"}=join(':',("FFFFFF",($width+$extrawidth)));
                    197: 		$x{"cgi.$id.OBJEXTRA$i"}=$1;
                    198: 		$x{"cgi.$id.OBJTYPE"}.='POLYGON:';
                    199: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
                    200: 		$x{"cgi.$id.OBJ$i"}=join(':',("00FF00",$width));
                    201: 		$x{"cgi.$id.OBJEXTRA$i"}=$1;
1.43      albertel  202: 	    }
1.42      albertel  203: 	}
                    204:     }
1.41      albertel  205:     &Apache::lonnet::appenv(%x);
                    206:     return $id;
1.40      albertel  207: }
                    208: 
1.2       albertel  209: sub displayfoils {
1.36      albertel  210:     my ($target,@whichopt) = @_;
                    211:     my $result ='';
                    212:     my $name;
                    213:     my $temp=1;
1.82      albertel  214:     my @images;
1.36      albertel  215:     foreach $name (@whichopt) {
                    216: 	$result.=$Apache::response::foilgroup{"$name.text"};
                    217: 	&Apache::lonxml::debug("Text is $result");
                    218: 	if ($target eq 'tex') {$result.="\\vskip 0 mm \n";} else {$result.="<br />\n";}
                    219: 	my $image=$Apache::response::foilgroup{"$name.image"};
                    220: 	&Apache::lonxml::debug("image is $image");
1.40      albertel  221: 	if ( $target eq 'web' && $image !~ /^http:/ ) {
1.47      albertel  222: 	    $image=&clean_up_image($image);
1.82      albertel  223: 	}
                    224: 	push(@images,$image);
1.40      albertel  225: 	&Apache::lonxml::debug("image is $image");
1.36      albertel  226: 	if ( &Apache::response::show_answer() ) {
                    227: 	    if ($target eq 'tex') {
                    228: 		$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
                    229: 	    } else {
1.42      albertel  230: 		my $token=&prep_image($image,'answer',$name);
1.40      albertel  231: 		$result.="<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
1.36      albertel  232: 	    }
                    233: 	} else {
                    234: 	    if ($target eq 'tex') {
                    235: 		$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
                    236: 	    } else {
1.40      albertel  237: 		my $id=$Apache::inputtags::response['-1'];
                    238: 		my $token=&prep_image($image);
                    239: 		my $temp=1;
                    240: 		$result.="<input type=\"image\" name=\"HWVAL_$id:$temp\" ".
                    241: 		    "src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
1.36      albertel  242: 	    }
                    243: 	}
                    244: 	$temp++;
                    245:     }
1.82      albertel  246:     if ($target eq 'web') {
                    247: 	&Apache::response::setup_prior_tries_hash(\&format_prior_response,
                    248: 						  [\@images]);
                    249:     }
1.36      albertel  250:     return $result;
1.47      albertel  251: }
                    252: 
1.82      albertel  253: sub format_prior_response {
                    254:     my ($mode,$answer,$other_data) = @_;
                    255:     my ($x,$y)=split(/:/,$answer);
                    256:     my $images = $other_data->[0];
                    257: 
                    258:     my $token = &draw_image('submission',$images->[0],$x,$y);
                    259: 
                    260:     return "<img class=\"LC_prior_image\" src=\"/adm/randomlabel.png?token=$token\" />";
                    261:     
                    262: }
                    263: 
1.75      albertel  264: sub display_answers {
                    265:     my ($target,$whichopt)=@_;
                    266: 
                    267:     my $result;
                    268:     foreach my $name (@$whichopt) {
                    269: 	my $image=$Apache::response::foilgroup{"$name.image"};
                    270: 	&Apache::lonxml::debug("image is $image");
                    271: 	if ( $target eq 'web' && $image !~ /^http:/ ) {
                    272: 	    $image = &clean_up_image($image);
                    273: 	} 
                    274: 	my $token=&prep_image($image,'answeronly',$name);
                    275: 
                    276: 	$result.=&Apache::response::answer_header('imageresponse');
                    277: 	$result.=&Apache::response::answer_part('imageresponse',"<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n");
                    278: 	$result.=&Apache::response::answer_footer('imageresponse');
                    279:     }
                    280:     return $result;
                    281: }
                    282: 
1.47      albertel  283: sub clean_up_image {
                    284:     my ($image)=@_;
                    285:     if ($image =~ /\s*<img\s*/) {
1.50      albertel  286: 	($image) = ($image =~ /src\s*=\s*[\"\']([^\"\']+)[\"\']/i);
1.47      albertel  287: 	if ($image !~ /^http:/) {
                    288: 	    $image=&Apache::lonnet::hreflocation('',$image);
                    289: 	}
                    290: 	if (!$image) {
                    291: 	    $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
                    292: 	}
                    293:     } else {
                    294: 	$image=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$image);
1.66      foxr      295: 	&Apache::lonxml::debug("repcopying: $image");
1.64      raeburn   296: 	if (&Apache::lonnet::repcopy($image) ne 'ok') {
1.47      albertel  297: 	    $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
                    298: 	}
                    299:     }
                    300:     return $image;
1.2       albertel  301: }
                    302: 
1.3       albertel  303: sub gradefoils {
1.36      albertel  304:     my (@whichopt) = @_;
1.74      albertel  305: 
                    306:     my $partid = $Apache::inputtags::part;
                    307:     my $id     = $Apache::inputtags::response['-1'];
                    308: 
                    309:     if ($Apache::lonhomework::type eq 'exam') {
                    310: 	&Apache::response::scored_response($partid,$id);
                    311: 	return;
                    312:     }
                    313:     
1.36      albertel  314:     my $x;
                    315:     my $y;
                    316:     my $result;
                    317:     my $temp=1;
                    318:     foreach my $name (@whichopt) {
1.65      albertel  319: 	$x=$env{"form.HWVAL_$id:$temp.x"};
                    320: 	$y=$env{"form.HWVAL_$id:$temp.y"};
1.36      albertel  321: 	&Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
                    322: 	if (defined($x) && defined($y) &&
                    323: 	    defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
                    324: 	    my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
                    325: 	    my $grade="INCORRECT";
                    326: 	    foreach my $area (@areas) {
                    327: 		&Apache::lonxml::debug("Area is $area for $name");
                    328: 		$area =~ m/([a-z]*):/;
                    329: 		&Apache::lonxml::debug("Area of type $1");
                    330: 		if ($1 eq 'rectangle') {
                    331: 		    $grade=&grade_rectangle($area,$x,$y);
1.43      albertel  332: 		} elsif ($1 eq 'polygon') {
                    333: 		    $grade=&grade_polygon($area,$x,$y);
1.36      albertel  334: 		} else {
                    335: 		    &Apache::lonxml::error("Unknown area style $area");
                    336: 		}
                    337: 		&Apache::lonxml::debug("Area said $grade");
                    338: 		if ($grade eq 'APPROX_ANS') { last; }
                    339: 	    }
                    340: 	    &Apache::lonxml::debug("Foil was $grade");
                    341: 	    if ($grade eq 'INCORRECT') { $result= 'INCORRECT'; }
                    342: 	    if (($grade eq 'APPROX_ANS') && ($result ne 'APPROX_ANS')) { $result=$grade; }
                    343: 	    &Apache::lonxml::debug("Question is $result");
                    344: 	    $temp++;
1.9       albertel  345: 	}
1.36      albertel  346:     }
1.60      albertel  347:     my $responsestr="$x:$y";
                    348:     my $part=$Apache::inputtags::part;
                    349:     my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
1.56      albertel  350:     if ($result 
                    351: 	&& $Apache::lonhomework::type eq 'survey') { $result='SUBMITTED'; }
1.60      albertel  352:     &Apache::response::handle_previous(\%previous,$result);
                    353:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=$responsestr;
                    354:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$result;
1.74      albertel  355:     return;
1.3       albertel  356: }
                    357: 
1.1       albertel  358: sub end_foilgroup {
1.36      albertel  359:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    360:     my $result='';
                    361:     my @whichopt;
1.75      albertel  362: 
1.38      albertel  363:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  364: 	$target eq 'analyze' || $target eq 'answer') {
                    365: 
1.36      albertel  366: 	my ($count,$max) = &getfoilcounts($parstack,$safeeval);
                    367: 	if ($count>$max) { $count=$max }
                    368: 	&Apache::lonxml::debug("Count is $count from $max");
1.75      albertel  369: 
1.36      albertel  370: 	@whichopt = &whichfoils($max);
1.75      albertel  371: 
1.36      albertel  372: 	if ($target eq 'web' || $target eq 'tex') {
                    373: 	    $result=&displayfoils($target,@whichopt);
1.81      albertel  374: 	    $Apache::lonxml::post_evaluate=0;
1.36      albertel  375: 	} elsif ($target eq 'grade') {
1.65      albertel  376: 	    if ( defined $env{'form.submitted'}) { &gradefoils(@whichopt); }
1.37      albertel  377: 	} elsif ( $target eq 'analyze') {
                    378: 	    &Apache::response::analyze_store_foilgroup(\@whichopt,
                    379: 						      ['text','image','area']);
1.76      albertel  380: 	} elsif ($target eq 'answer'
                    381: 		 && $env{'form.answer_output_mode'} ne 'tex') {
1.75      albertel  382: 	    $result=&display_answers($target,\@whichopt);
1.37      albertel  383: 	}
1.75      albertel  384: 
1.36      albertel  385:     } elsif ($target eq 'edit') {
                    386: 	$result=&Apache::edit::end_table();
                    387:     }
1.53      albertel  388:     &Apache::response::poprandomnumber();
1.36      albertel  389:     return $result;
1.1       albertel  390: }
                    391: 
                    392: sub start_conceptgroup {
1.36      albertel  393:     $Apache::imageresponse::conceptgroup=1;
                    394:     %Apache::response::conceptgroup=();
                    395:     return '';
1.1       albertel  396: }
                    397: 
                    398: sub end_conceptgroup {
1.36      albertel  399:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    400:     $Apache::imageresponse::conceptgroup=0;
                    401:     my $result;
1.37      albertel  402:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
                    403: 	$target eq 'analyze') {
                    404: 	&Apache::response::pick_foil_for_concept($target,
                    405: 						 ['area','text','image'],
                    406: 						 \%Apache::hint::image,
                    407: 						 $parstack,$safeeval);
1.36      albertel  408:     } elsif ($target eq 'edit') {
                    409: 	$result=&Apache::edit::end_table();
                    410:     }
                    411:     return $result;
1.31      albertel  412: }
                    413: 
                    414: sub insert_foil {
                    415:     return '
                    416:        <foil>
                    417:            <image></image>
                    418:            <text></text>
                    419:            <rectangle></rectangle>
                    420:        </foil>
                    421: ';
1.1       albertel  422: }
                    423: 
1.12      albertel  424: $Apache::imageresponse::curname='';
1.1       albertel  425: sub start_foil {
1.36      albertel  426:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.67      albertel  427:     my $result;
1.37      albertel  428:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  429: 	$target eq 'analyze' || $target eq 'answer') {
1.36      albertel  430: 	my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.72      albertel  431: 	if ($name eq "") {
1.71      albertel  432: 	    &Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
1.59      albertel  433: 	    $name=$Apache::lonxml::curdepth;
                    434: 	}
1.51      albertel  435: 	if (defined($Apache::response::foilnames{$name})) {
                    436: 	    &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
                    437: 	}
1.52      albertel  438: 	$Apache::response::foilnames{$name}++;
1.36      albertel  439: 	if ( $Apache::imageresponse::conceptgroup
1.56      albertel  440: 	     #&& !&Apache::response::showallfoils()
                    441: 	     ) {
1.36      albertel  442: 	    push(@{ $Apache::response::conceptgroup{'names'} }, $name);
                    443: 	} else {
                    444: 	    push(@{ $Apache::response::foilgroup{'names'} }, $name);
                    445: 	}
                    446: 	$Apache::imageresponse::curname=$name;
1.67      albertel  447:     } elsif ($target eq 'edit') {
                    448: 	$result  = &Apache::edit::tag_start($target,$token);
                    449: 	$result .= &Apache::edit::text_arg('Name:','name',$token);
                    450: 	$result .= &Apache::edit::end_row().
                    451: 	    &Apache::edit::start_spanning_row();
                    452:     } elsif ($target eq 'modified') {
                    453: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    454: 						     $safeeval,'name');
                    455: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    456:     } 
                    457:     return $result;;
1.1       albertel  458: }
                    459: 
                    460: sub end_foil {
1.26      albertel  461:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    462:     my $result;
                    463:     if ($target eq 'edit') {
                    464: 	$result=&Apache::edit::end_table();
                    465:     }
                    466:     return $result;
1.1       albertel  467: }
                    468: 
                    469: sub start_text {
1.36      albertel  470:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    471:     my $result='';
1.75      albertel  472:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
                    473: 	|| $target eq 'answer') { 
1.36      albertel  474: 	&Apache::lonxml::startredirection; 
                    475:     } elsif ($target eq 'edit') {
1.73      albertel  476: 	my $descr=&Apache::lonxml::get_all_text('/text',$parser,$style);
1.36      albertel  477: 	$result=&Apache::edit::tag_start($target,$token,'Task Description').
                    478: 	    &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
                    479: 	    &Apache::edit::end_row();
                    480:     } elsif ($target eq "modified") {
1.39      albertel  481: 	$result=$token->[4].&Apache::edit::modifiedfield('/text',$parser);
1.36      albertel  482:     }
                    483:     return $result;
1.1       albertel  484: }
                    485: 
                    486: sub end_text {
1.36      albertel  487:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    488:     my $result;
1.75      albertel  489:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
                    490: 	|| $target eq 'answer') {
1.36      albertel  491: 	my $name = $Apache::imageresponse::curname;
                    492: 	if ( $Apache::imageresponse::conceptgroup
1.56      albertel  493: 	     #&& !&Apache::response::showallfoils()
                    494: 	     ) {
1.36      albertel  495: 	    $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
                    496: 	} else {
                    497: 	    $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
                    498: 	}
                    499:     } elsif ($target eq 'edit') {
                    500: 	$result=&Apache::edit::end_table();
                    501:     }
                    502:     return $result;
1.1       albertel  503: }
                    504: 
                    505: sub start_image {
1.36      albertel  506:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    507:     my $result='';
1.83    ! banghart  508:     my $only = join(',',&Apache::loncommon::filecategorytypes('Pictures'));
1.75      albertel  509:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
                    510: 	|| $target eq 'answer') { 
1.36      albertel  511: 	&Apache::lonxml::startredirection; 
                    512:     } elsif ($target eq 'edit') {
1.73      albertel  513: 	my $bgimg=&Apache::lonxml::get_all_text('/image',$parser,$style);
1.36      albertel  514: 	$Apache::edit::bgimgsrc=$bgimg;
                    515: 	$Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
                    516: 
                    517: 	$result=&Apache::edit::tag_start($target,$token,'Clickable Image').
                    518: 	    &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
1.83    ! banghart  519: 	$result.=&Apache::edit::browse(undef,'textnode',undef,$only).' ';
1.36      albertel  520: 	$result.=&Apache::edit::search(undef,'textnode').
                    521: 	    &Apache::edit::end_row();
                    522:     } elsif ($target eq "modified") {
1.39      albertel  523: 	$result=$token->[4].&Apache::edit::modifiedfield('/image',$parser);
1.36      albertel  524:     }
                    525:     return $result;
1.1       albertel  526: }
                    527: 
                    528: sub end_image {
1.36      albertel  529:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    530:     my $result;
                    531:     my $name = $Apache::imageresponse::curname;
1.75      albertel  532:     if ($target eq 'web' || $target eq 'answer') {
1.62      albertel  533: 	my $image = &Apache::lonxml::endredirection();
1.36      albertel  534: 	&Apache::lonxml::debug("original image is $image");
1.37      albertel  535: 	if ( $Apache::imageresponse::conceptgroup
1.56      albertel  536: 	     #&& !&Apache::response::showallfoils()
                    537: 	     ) {
1.37      albertel  538: 	    $Apache::response::conceptgroup{"$name.image"} = $image;
                    539: 	} else {
                    540: 	    $Apache::response::foilgroup{"$name.image"} = $image;
                    541: 	}
                    542:     } elsif ($target eq 'analyze') {
1.62      albertel  543: 	my $image = &Apache::lonxml::endredirection();
1.36      albertel  544: 	if ( $Apache::imageresponse::conceptgroup
1.56      albertel  545: 	     #&& !&Apache::response::showallfoils()
                    546: 	     ) {
1.36      albertel  547: 	    $Apache::response::conceptgroup{"$name.image"} = $image;
                    548: 	} else {
                    549: 	    $Apache::response::foilgroup{"$name.image"} = $image;
                    550: 	}
                    551:     } elsif ($target eq 'edit') {
                    552: 	$result=&Apache::edit::end_table();
                    553:     } elsif ($target eq 'tex') {
1.62      albertel  554: 	my $src = &Apache::lonxml::endredirection();
1.68      foxr      555: 
                    556: 	#  There may be all sorts of whitespace on fore and aft:
                    557: 
1.69      foxr      558: 	$src =~ s/\s+$//s;
                    559: 	$src =~ s/^\s+//s;
1.68      foxr      560: 
                    561: 	#
                    562: 	#  Gnuplot e.g. just generates the latex to put inplace.
                    563: 	#
                    564: 	my $graphinclude;
1.70      foxr      565: 	if ($src =~ /^%DYNAMICIMAGE/) {
                    566: 	    # This is needed because the newline is not always passed -> tex.
                    567: 	    # At present we don't care about the sizing info.
                    568: 
                    569: 	    my ($commentline, $restofstuff) = split(/\n/, $src);
1.68      foxr      570: 	    $graphinclude = $src;
1.70      foxr      571: 	    $graphinclude =~ s/^$commentline//;
1.68      foxr      572: 	} else {
                    573: 	    my ($path,$file) = &Apache::londefdef::get_eps_image($src);
                    574: 	    my ($height_param,$width_param)=
                    575: 		&Apache::londefdef::image_size($src,0.3,$parstack,$safeeval);
                    576: 	    $graphinclude = '\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'}';
                    577: 	}
                    578: 	$Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent '.$graphinclude;
1.36      albertel  579:     } 
                    580:     return $result;
1.1       albertel  581: }
                    582: 
                    583: sub start_rectangle {
1.36      albertel  584:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    585:     my $result='';
1.38      albertel  586:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  587: 	$target eq 'analyze' || $target eq 'answer') { 
1.36      albertel  588: 	&Apache::lonxml::startredirection; 
                    589:     } elsif ($target eq 'edit') {
1.73      albertel  590: 	my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser,$style);
1.36      albertel  591: 	$result=&Apache::edit::tag_start($target,$token,'Rectangle').
                    592: 	    &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
1.44      albertel  593: 	    &Apache::edit::entercoord(undef,'textnode',undef,undef,'box').
1.36      albertel  594: 	    &Apache::edit::end_row();
                    595:     } elsif ($target eq "modified") {
1.44      albertel  596: 	&Apache::edit::deletecoorddata();
1.39      albertel  597: 	$result=$token->[4].&Apache::edit::modifiedfield('/rectangle',$parser);
1.36      albertel  598:     }
                    599:     return $result;
1.1       albertel  600: }
                    601: 
1.3       albertel  602: sub grade_rectangle {
1.36      albertel  603:     my ($spec,$x,$y) = @_;
                    604:     &Apache::lonxml::debug("Spec is $spec");
1.43      albertel  605:     my ($x1,$y1,$x2,$y2)=($spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
1.36      albertel  606:     &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
                    607:     if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
                    608:     if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
                    609:     if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
                    610: 	return 'APPROX_ANS';
                    611:     }
                    612:     return 'INCORRECT';
1.3       albertel  613: }
                    614: 
1.1       albertel  615: sub end_rectangle {
1.36      albertel  616:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    617:     my $result;
1.38      albertel  618:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  619: 	$target eq 'analyze' || $target eq 'answer') {
1.36      albertel  620: 	my $name = $Apache::imageresponse::curname;
                    621: 	my $area = &Apache::lonxml::endredirection;
1.54      albertel  622: 	$area=~s/\s//g;
1.36      albertel  623: 	&Apache::lonxml::debug("out is $area for $name");
                    624: 	if ( $Apache::imageresponse::conceptgroup
1.56      albertel  625: 	     #&& !&Apache::response::showallfoils()
                    626: 	     ) {
1.36      albertel  627: 	    push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
                    628: 	} else {
                    629: 	    push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
1.43      albertel  630: 	}
                    631:     } elsif ($target eq 'edit') {
                    632: 	$result=&Apache::edit::end_table();
                    633:     }
                    634:     return $result;
                    635: }
                    636: 
                    637: sub start_polygon {
                    638:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    639:     my $result='';
                    640:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  641: 	$target eq 'analyze' || $target eq 'answer') { 
1.43      albertel  642: 	&Apache::lonxml::startredirection; 
                    643:     } elsif ($target eq 'edit') {
1.73      albertel  644: 	my $coords=&Apache::lonxml::get_all_text('/polygon',$parser,$style);
1.43      albertel  645: 	$result=&Apache::edit::tag_start($target,$token,'Polygon').
                    646: 	    &Apache::edit::editline($token->[1],$coords,'Coordinate list',40).
1.44      albertel  647: 	    &Apache::edit::entercoord(undef,'textnode',undef,undef,'polygon').
1.43      albertel  648: 	    &Apache::edit::end_row();
                    649:     } elsif ($target eq "modified") {
                    650: 	$result=$token->[4].&Apache::edit::modifiedfield('/polygon',$parser);
                    651:     }
                    652:     return $result;
                    653: }
                    654: 
                    655: sub grade_polygon {
                    656:     my ($spec,$x,$y) = @_;
                    657:     &Apache::lonxml::debug("Spec is $spec");
                    658:     $spec=~s/^polygon://;
                    659:     my @polygon;
                    660:     foreach my $coord (split('-',$spec)) {
                    661: 	my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
                    662: 	&Apache::lonxml::debug("x $x y $y");
                    663: 	push @polygon, {'x'=>$x,'y'=>$y};
                    664:     }
                    665:     #make end point start point
                    666:     push @polygon, $polygon[0];
                    667:     # cribbed from
                    668:     # http://geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm
                    669:     my $crossing = 0;    # the crossing number counter
                    670: 
                    671:     # loop through all edges of the polygon
                    672:     for (my $i=0; $i<$#polygon; $i++) {    # edge from V[i] to V[i+1]
                    673: 	if ((($polygon[$i]->{'y'} <= $y)
                    674: 	     && ($polygon[$i+1]->{'y'} > $y))    # an upward crossing
                    675: 	    || 
                    676: 	    (($polygon[$i]->{'y'} > $y) 
                    677: 	     && ($polygon[$i+1]->{'y'} <= $y))) { # a downward crossing
                    678: 	    # compute the actual edge-ray intersect x-coordinate
                    679:             my $vt = ($y - $polygon[$i]->{'y'}) 
                    680: 		/ ($polygon[$i+1]->{'y'} - $polygon[$i]->{'y'});
                    681:             if ($x < $polygon[$i]->{'x'} + $vt * 
                    682: 		($polygon[$i+1]->{'x'} - $polygon[$i]->{'x'})) { # x<intersect
                    683:                 $crossing++;   # a valid crossing of y=P.y right of P.x
                    684: 	    }
                    685: 	}
                    686:     }
                    687: 
                    688:     # 0 if even (out), and 1 if odd (in)
                    689:     if ($crossing%2) {
                    690: 	return 'APPROX_ANS';
                    691:     } else {
                    692: 	return 'INCORRECT';
                    693:     }
                    694: }
                    695: 
                    696: sub end_polygon {
                    697:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    698:     my $result;
                    699:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  700: 	$target eq 'analyze' || $target eq 'answer') {
1.43      albertel  701: 	my $name = $Apache::imageresponse::curname;
                    702: 	my $area = &Apache::lonxml::endredirection;
1.48      albertel  703: 	$area=~s/\s*//g;
1.43      albertel  704: 	&Apache::lonxml::debug("out is $area for $name");
                    705: 	if ( $Apache::imageresponse::conceptgroup
1.56      albertel  706: 	     #&& !&Apache::response::showallfoils()
                    707: 	     ) {
1.43      albertel  708: 	    push @{ $Apache::response::conceptgroup{"$name.area"} },"polygon:$area";
                    709: 	} else {
                    710: 	    push @{ $Apache::response::foilgroup{"$name.area"} },"polygon:$area";
1.36      albertel  711: 	}
                    712:     } elsif ($target eq 'edit') {
                    713: 	$result=&Apache::edit::end_table();
                    714:     }
                    715:     return $result;
1.1       albertel  716: }
                    717: 1;
                    718: __END__
                    719:  

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