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

1.89      foxr        1: #
1.12      albertel    2: # The LearningOnline Network with CAPA
1.14      albertel    3: # image click response style
                      4: #
1.105   ! raeburn     5: # $Id: imageresponse.pm,v 1.104 2012/10/12 12:45:46 raeburn 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.95      jms        32: 
                     33: =head1 NAME
                     34: 
                     35: Apache::imageresponse
                     36: 
                     37: =head1 SYNOPSIS
                     38: 
                     39: Coordinates the response to clicking an image.
                     40: 
                     41: This is part of the LearningOnline Network with CAPA project
                     42: described at http://www.lon-capa.org.
                     43: 
1.96      jms        44: =head1 SUBROUTINES
1.95      jms        45: 
                     46: =over
                     47: 
1.96      jms        48: =item start_imageresponse()
1.95      jms        49: 
1.96      jms        50: =item end_imageresponse()
1.95      jms        51: 
1.96      jms        52: =item start_foilgroup()
1.95      jms        53: 
1.96      jms        54: =item getfoilcounts()
1.95      jms        55: 
1.96      jms        56: =item whichfoils()
1.95      jms        57: 
1.96      jms        58: =item prep_image()
1.95      jms        59: 
1.96      jms        60: =item draw_image()
1.95      jms        61: 
1.96      jms        62: =item displayfoils()
1.95      jms        63: 
1.96      jms        64: =item format_prior_response()
1.95      jms        65: 
1.96      jms        66: =item display_answers()
1.95      jms        67: 
1.96      jms        68: =item clean_up_image()
1.95      jms        69: 
1.96      jms        70: =item gradefoils()
1.95      jms        71: 
1.96      jms        72: =item stringify_submission()
1.95      jms        73: 
1.96      jms        74: =item get_submission()
1.95      jms        75: 
1.96      jms        76: =item end_foilgroup()
1.95      jms        77: 
1.96      jms        78: =item start_conceptgroup()
1.95      jms        79: 
1.96      jms        80: =item end_conceptgroup()
1.95      jms        81: 
1.96      jms        82: =item insert_foil()
1.95      jms        83: 
1.96      jms        84: =item start_foil()
1.95      jms        85: 
1.96      jms        86: =item end_foil()
1.95      jms        87: 
1.96      jms        88: =item start_text()
1.95      jms        89: 
1.96      jms        90: =item end_text()
1.95      jms        91: 
1.96      jms        92: =item start_image()
1.95      jms        93: 
1.96      jms        94: =item end_image()
1.95      jms        95: 
1.96      jms        96: =item start_rectangle()
1.95      jms        97: 
1.96      jms        98: =item grade_rectangle()
1.95      jms        99: 
1.96      jms       100: =item end_rectangle()
1.95      jms       101: 
1.96      jms       102: =item start_polygon()
1.95      jms       103: 
1.96      jms       104: =item grade_polygon()
1.95      jms       105: 
1.96      jms       106: =item end_polygon()
1.95      jms       107: 
                    108: =back
                    109: 
                    110: =cut
                    111: 
                    112: 
1.1       albertel  113: package Apache::imageresponse;
                    114: use strict;
1.51      albertel  115: use Image::Magick();
                    116: use Apache::randomlylabel();
                    117: use Apache::londefdef();
1.40      albertel  118: use Apache::Constants qw(:common :http);
1.51      albertel  119: use Apache::lonlocal;
1.65      albertel  120: use Apache::lonnet;
1.78      www       121: use lib '/home/httpd/lib/perl/';
                    122: use LONCAPA;
                    123:  
1.1       albertel  124: 
1.16      harris41  125: BEGIN {
1.36      albertel  126:     &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
1.1       albertel  127: }
                    128: 
                    129: sub start_imageresponse {
1.36      albertel  130:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    131:     my $result;
                    132:     #when in a radiobutton response use these
1.43      albertel  133:     &Apache::lonxml::register('Apache::imageresponse',
                    134: 			      ('foilgroup','foil','text','image','rectangle',
                    135: 			       'polygon','conceptgroup'));
1.36      albertel  136:     push (@Apache::lonxml::namespace,'imageresponse');
                    137:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.51      albertel  138:     undef(%Apache::response::foilnames);
1.36      albertel  139:     if ($target eq 'meta') {
                    140: 	$result=&Apache::response::meta_package_write('imageresponse');
1.37      albertel  141:     } elsif ($target eq 'analyze') {
                    142: 	my $part_id="$Apache::inputtags::part.$id";
1.92      raeburn   143:         $Apache::lonhomework::analyze{"$part_id.type"} = 'imageresponse';
1.37      albertel  144: 	push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.84      foxr      145: 	push (@{ $Apache::lonhomework::analyze{"$part_id.bubble_lines"} },
                    146: 	      1);
1.88      albertel  147:     } elsif ( $target eq 'edit' ) {
                    148: 	$result .= &Apache::edit::tag_start($target,$token).
                    149: 	    &Apache::edit::text_arg('Max Number Of Shown Foils:',
                    150: 				    'max',$token,'4').
                    151: 	    &Apache::edit::end_row().
                    152: 	    &Apache::edit::start_spanning_row();
                    153:     } elsif ( $target eq 'modified' ) {
                    154: 	my $constructtag=
                    155: 	    &Apache::edit::get_new_args($token,$parstack,$safeeval,'max');
                    156: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.36      albertel  157:     }
                    158:     return $result;
1.1       albertel  159: }
                    160: 
                    161: sub end_imageresponse {
1.30      albertel  162:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.74      albertel  163: 
1.90      foxr      164:     my $part_id     = $Apache::inputtags::part;
                    165:     my $response_id = $Apache::inputtags::response[-1]; 
1.89      foxr      166: 
1.74      albertel  167:     pop(@Apache::lonxml::namespace);
1.30      albertel  168:     &Apache::lonxml::deregister('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
1.74      albertel  169: 
1.30      albertel  170:     my $result;
1.74      albertel  171:     if ($target eq 'edit') {
                    172: 	$result=&Apache::edit::end_table();
1.77      albertel  173:     } elsif ($target eq 'tex'
                    174: 	     && $Apache::lonhomework::type eq 'exam') {
1.74      albertel  175: 	$result=&Apache::inputtags::exam_score_line($target);
                    176:     }
                    177: 
1.51      albertel  178:     undef(%Apache::response::foilnames);
1.74      albertel  179:     
                    180:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
                    181: 	$target eq 'tex' || $target eq 'analyze') {
1.103     raeburn   182:         my $repetition = &Apache::response::repetition();
                    183: 	&Apache::lonxml::increment_counter($repetition, 
1.90      foxr      184: 					   "$part_id.$response_id");
1.89      foxr      185: 	if ($target eq 'analyze') {
                    186: 	    &Apache::lonhomework::set_bubble_lines();
                    187: 	}
                    188: 	
1.74      albertel  189:     }
                    190:     &Apache::response::end_response();
1.30      albertel  191:     return $result;
1.1       albertel  192: }
                    193: 
1.20      albertel  194: %Apache::response::foilgroup=();
1.1       albertel  195: sub start_foilgroup {
1.100     raeburn   196:     my ($target) = @_;
1.36      albertel  197:     %Apache::response::foilgroup=();
                    198:     $Apache::imageresponse::conceptgroup=0;
1.100     raeburn   199:     &Apache::response::pushrandomnumber(undef,$target);
1.36      albertel  200:     return '';
1.1       albertel  201: }
                    202: 
1.2       albertel  203: sub getfoilcounts {
1.36      albertel  204:     my ($parstack,$safeeval)=@_;
1.12      albertel  205: 
1.36      albertel  206:     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
                    207:     # +1 since instructors will count from 1
                    208:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
1.88      albertel  209:     if (&Apache::response::showallfoils()) { $max=$count; }
1.36      albertel  210:     return ($count,$max);
1.2       albertel  211: }
                    212: 
                    213: sub whichfoils {
1.36      albertel  214:     my ($max)=@_;
1.104     raeburn   215:     my @names;
                    216:     if (ref($Apache::response::foilgroup{'names'}) eq 'ARRAY') {
                    217:         @names = @{ $Apache::response::foilgroup{'names'} };
                    218:     }
                    219:     return if (!@names);
1.88      albertel  220:     my @whichopt;
1.36      albertel  221:     while ((($#whichopt+1) < $max) && ($#names > -1)) {
                    222: 	&Apache::lonxml::debug("Have $#whichopt max is $max");
                    223: 	my $aopt;
1.88      albertel  224: 	if (&Apache::response::showallfoils()) {
                    225: 	    $aopt=0;
                    226: 	} else {
1.36      albertel  227: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
1.88      albertel  228: 	}
1.36      albertel  229: 	&Apache::lonxml::debug("From $#names elms, picking $aopt");
                    230: 	$aopt=splice(@names,$aopt,1);
                    231: 	&Apache::lonxml::debug("Picked $aopt");
1.88      albertel  232: 	push(@whichopt,$aopt);
1.36      albertel  233:     }
                    234:     return @whichopt;
1.2       albertel  235: }
                    236: 
1.40      albertel  237: sub prep_image {
1.42      albertel  238:     my ($image,$mode,$name)=@_;
1.85      albertel  239: 
                    240:     my ($x,$y)= &get_submission($name);
                    241:     &Apache::lonxml::debug("for $name drawing click at $x and $y");
1.82      albertel  242:     &draw_image($mode,$image,$x,$y,$Apache::response::foilgroup{"$name.area"});
                    243: }
                    244: 
                    245: sub draw_image {
                    246:     my ($mode,$image,$x,$y,$areas) = @_;
                    247: 
1.41      albertel  248:     my $id=&Apache::loncommon::get_cgi_id();
1.82      albertel  249: 
1.49      albertel  250:     my (%x,$i);
1.78      www       251:     $x{"cgi.$id.BGIMG"}=&escape($image);
1.82      albertel  252: 
1.40      albertel  253:     #draws 2 xs on the image at the clicked location
                    254:     #one in white and then one in red on top of the one in white
1.82      albertel  255: 
1.75      albertel  256:     if (defined($x)    && $x =~/\S/ 
                    257: 	&& defined($y) && $y =~/\S/ 
1.82      albertel  258: 	&& ($mode eq 'submission' || !&Apache::response::show_answer())
1.75      albertel  259: 	&& $mode ne 'answeronly') {
1.40      albertel  260: 	my $length = 6;
                    261: 	my $width = 1;
                    262: 	my $extrawidth = 2;
1.48      albertel  263: 	my $xmin=($x-$length);
                    264: 	my $xmax=($x+$length); 
                    265: 	my $ymin=($y-$length);
                    266: 	my $ymax=($y+$length);
                    267: 
1.49      albertel  268: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
                    269: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
1.58      albertel  270: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
1.49      albertel  271: 				      "FFFFFF",($width+$extrawidth)));
                    272: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
                    273: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
1.58      albertel  274: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
1.49      albertel  275: 				      "FFFFFF",($width+$extrawidth)));
                    276: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
                    277: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
1.58      albertel  278: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
1.49      albertel  279: 				      "FF0000",($width)));
                    280: 	$x{"cgi.$id.OBJTYPE"}.='LINE:';
                    281: 	$i=$x{"cgi.$id.OBJCOUNT"}++;
1.58      albertel  282: 	$x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
1.49      albertel  283: 				      "FF0000",($width)));
1.40      albertel  284:     }
1.75      albertel  285:     if ($mode eq 'answer' || $mode eq 'answeronly') {
1.42      albertel  286: 	my $width = 1;
                    287: 	my $extrawidth = 2;
1.82      albertel  288: 	foreach my $area (@{ $areas }) {
1.43      albertel  289: 	    if ($area=~/^rectangle:/) {
1.49      albertel  290: 		$x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
                    291: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
1.43      albertel  292: 		my ($x1,$y1,$x2,$y2)=
                    293: 		    ($area=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
1.49      albertel  294: 		$x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"FFFFFF",
1.43      albertel  295: 					      ($width+$extrawidth)));
1.49      albertel  296: 		$x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
                    297: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
                    298: 		$x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"00FF00",$width));
1.43      albertel  299: 	    } elsif ($area=~/^polygon:(.*)/) {
1.49      albertel  300: 		$x{"cgi.$id.OBJTYPE"}.='POLYGON:';
                    301: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
                    302: 		$x{"cgi.$id.OBJ$i"}=join(':',("FFFFFF",($width+$extrawidth)));
                    303: 		$x{"cgi.$id.OBJEXTRA$i"}=$1;
                    304: 		$x{"cgi.$id.OBJTYPE"}.='POLYGON:';
                    305: 		$i=$x{"cgi.$id.OBJCOUNT"}++;
                    306: 		$x{"cgi.$id.OBJ$i"}=join(':',("00FF00",$width));
                    307: 		$x{"cgi.$id.OBJEXTRA$i"}=$1;
1.43      albertel  308: 	    }
1.42      albertel  309: 	}
                    310:     }
1.93      raeburn   311:     &Apache::lonnet::appenv(\%x);
1.41      albertel  312:     return $id;
1.40      albertel  313: }
                    314: 
1.2       albertel  315: sub displayfoils {
1.36      albertel  316:     my ($target,@whichopt) = @_;
                    317:     my $result ='';
                    318:     my $temp=1;
1.82      albertel  319:     my @images;
1.85      albertel  320:     foreach my $name (@whichopt) {
1.36      albertel  321: 	$result.=$Apache::response::foilgroup{"$name.text"};
                    322: 	&Apache::lonxml::debug("Text is $result");
                    323: 	if ($target eq 'tex') {$result.="\\vskip 0 mm \n";} else {$result.="<br />\n";}
                    324: 	my $image=$Apache::response::foilgroup{"$name.image"};
                    325: 	&Apache::lonxml::debug("image is $image");
1.91      albertel  326: 	if ( ($target eq 'web' || $target eq 'answer') 
1.97      raeburn   327: 	     && $image !~ /^https?\:/ ) {
1.47      albertel  328: 	    $image=&clean_up_image($image);
1.82      albertel  329: 	}
                    330: 	push(@images,$image);
1.40      albertel  331: 	&Apache::lonxml::debug("image is $image");
1.36      albertel  332: 	if ( &Apache::response::show_answer() ) {
                    333: 	    if ($target eq 'tex') {
                    334: 		$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
                    335: 	    } else {
1.42      albertel  336: 		my $token=&prep_image($image,'answer',$name);
1.40      albertel  337: 		$result.="<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
1.36      albertel  338: 	    }
                    339: 	} else {
                    340: 	    if ($target eq 'tex') {
                    341: 		$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
                    342: 	    } else {
1.85      albertel  343: 		my $respid=$Apache::inputtags::response['-1'];
                    344: 		my $token=&prep_image($image,'submission',$name);
1.87      albertel  345: 		my $input_id = "HWVAL_$respid:$temp";
                    346: 		my $id = $env{'form.request.prefix'}.$input_id;
1.85      albertel  347: 		$result.='<img onclick="image_response_click(\''.$id.'\',event);"
                    348: 	                       src="/adm/randomlabel.png?token='.$token.'" 
1.87      albertel  349:                                id="'.$id.'_imageresponse"
1.85      albertel  350:                                 />'.
                    351: 			       '<br />'.
1.87      albertel  352: 			       '<input type="hidden" name="'.$input_id.'_token" value="'.$token.'" />'.
                    353: 			       '<input type="hidden" name="'.$input_id.'" value="'.
1.85      albertel  354: 			       join(':',&get_submission($name)).'" />';
1.36      albertel  355: 	    }
                    356: 	}
                    357: 	$temp++;
                    358:     }
1.82      albertel  359:     if ($target eq 'web') {
1.100     raeburn   360:         &get_prior_options(\@images,\@whichopt);
1.82      albertel  361:     }
1.36      albertel  362:     return $result;
1.47      albertel  363: }
                    364: 
1.100     raeburn   365: sub get_prior_options {
                    366:     my ($currimages,$curropt) = @_;
                    367:     return unless((ref($curropt) eq 'ARRAY') && 
                    368:                   (ref($currimages) eq 'ARRAY'));
                    369:     my $part = $Apache::inputtags::part;
                    370:     my $respid   = $Apache::inputtags::response[-1];
                    371:     foreach my $i (1..$Apache::lonhomework::history{'version'}) {
                    372:         my $partprefix = "$i:resource.$part";
                    373:         my $sub_key = "$partprefix.$respid.submission";
                    374:         next if (!exists($Apache::lonhomework::history{$sub_key}));
                    375:         my $type_key = "$partprefix.type";
                    376:         my @whichopt = ();
                    377:         my @images = ();
                    378:         if ($Apache::lonhomework::history{$type_key} eq 'randomizetry') {
                    379:             my $order_key = "$partprefix.$respid.foilorder";
                    380:             @whichopt = &Apache::lonnet::str2array($Apache::lonhomework::history{$order_key});
                    381:             if (@whichopt > 0) {
                    382:                 foreach my $name (@whichopt) {
                    383:                     my $image=$Apache::response::foilgroup{"$name.image"};
                    384:                     if ($image !~ /^https?\:/ ) {
                    385:                         $image=&clean_up_image($image);
                    386:                     }
                    387:                     push(@images,$image);
                    388:                 }
                    389:             }
                    390:         } else {
                    391:             @whichopt = @{$curropt};
                    392:             @images = @{$currimages};
                    393:         }
1.105   ! raeburn   394:         my $submission;
        !           395:         if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
        !           396:             (($env{'form.grade_username'} eq $env{'user.name'}) &&
        !           397:              ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
        !           398:             $submission = $Apache::lonhomework::history{$sub_key};
        !           399:         } else {
        !           400:             unless (($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') ||
        !           401:                     ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) {
        !           402:                 $submission = $Apache::lonhomework::history{$sub_key};
        !           403:             }
        !           404:         }
1.100     raeburn   405:         my $output =  &format_prior_response('grade',$submission,
                    406:                                              [\@images,\@whichopt]);
                    407:         if (defined($output)) {
                    408:             $Apache::inputtags::submission_display{$sub_key} = $output;
                    409:         }
                    410:     }
                    411: }
                    412: 
1.82      albertel  413: sub format_prior_response {
                    414:     my ($mode,$answer,$other_data) = @_;
1.85      albertel  415:     
                    416:     my $result;
1.82      albertel  417: 
1.85      albertel  418:     # make a copy of the data in the refs
                    419:     my @images = @{ $other_data->[0] };
                    420:     my @foils = @{ $other_data->[1] };
                    421:     foreach my $name (@foils) {
                    422: 	my $image = pop(@images);
                    423: 	my ($x,$y) = &get_submission($name,$answer);
                    424: 	my $token = &draw_image('submission',$image,$x,$y);
                    425: 	$result .=
                    426: 	    '<img class="LC_prior_image"
                    427:                   src="/adm/randomlabel.png?token='.$token.'" /><br />';
                    428:     }
                    429:     return $result;
1.82      albertel  430: }
                    431: 
1.75      albertel  432: sub display_answers {
                    433:     my ($target,$whichopt)=@_;
                    434: 
1.88      albertel  435:     my $result=&Apache::response::answer_header('imageresponse');
1.75      albertel  436:     foreach my $name (@$whichopt) {
                    437: 	my $image=$Apache::response::foilgroup{"$name.image"};
                    438: 	&Apache::lonxml::debug("image is $image");
1.91      albertel  439: 	if ( ($target eq 'web' || $target eq 'answer')
1.97      raeburn   440: 	     && $image !~ /^https?\:/ ) {
1.75      albertel  441: 	    $image = &clean_up_image($image);
                    442: 	} 
                    443: 	my $token=&prep_image($image,'answeronly',$name);
                    444: 
                    445: 	$result.=&Apache::response::answer_part('imageresponse',"<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n");
                    446:     }
1.88      albertel  447:     $result.=&Apache::response::answer_footer('imageresponse');
1.75      albertel  448:     return $result;
                    449: }
                    450: 
1.47      albertel  451: sub clean_up_image {
                    452:     my ($image)=@_;
                    453:     if ($image =~ /\s*<img\s*/) {
1.50      albertel  454: 	($image) = ($image =~ /src\s*=\s*[\"\']([^\"\']+)[\"\']/i);
1.97      raeburn   455: 	if ($image !~ /^https?\:/) {
1.47      albertel  456: 	    $image=&Apache::lonnet::hreflocation('',$image);
                    457: 	}
                    458: 	if (!$image) {
                    459: 	    $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
                    460: 	}
                    461:     } else {
                    462: 	$image=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$image);
1.66      foxr      463: 	&Apache::lonxml::debug("repcopying: $image");
1.64      raeburn   464: 	if (&Apache::lonnet::repcopy($image) ne 'ok') {
1.47      albertel  465: 	    $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
                    466: 	}
                    467:     }
                    468:     return $image;
1.2       albertel  469: }
                    470: 
1.3       albertel  471: sub gradefoils {
1.36      albertel  472:     my (@whichopt) = @_;
1.74      albertel  473: 
                    474:     my $partid = $Apache::inputtags::part;
                    475:     my $id     = $Apache::inputtags::response['-1'];
                    476: 
                    477:     if ($Apache::lonhomework::type eq 'exam') {
                    478: 	&Apache::response::scored_response($partid,$id);
                    479: 	return;
                    480:     }
                    481:     
1.85      albertel  482:     my @results;
1.36      albertel  483:     my $temp=1;
1.85      albertel  484:     my %response;
1.36      albertel  485:     foreach my $name (@whichopt) {
1.85      albertel  486: 	my ($x,$y) = split(':',$env{"form.HWVAL_$id:$temp"});
                    487: 	$response{$name} = $env{"form.HWVAL_$id:$temp"};
1.36      albertel  488: 	&Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
1.104     raeburn   489:         my @areas; 
                    490:         if (ref($Apache::response::foilgroup{"$name.area"}) eq 'ARRAY') {
                    491:             @areas = @{ $Apache::response::foilgroup{"$name.area"} };
                    492:         }
                    493: 	if (defined($x) && defined($y) && @areas) {
1.36      albertel  494: 	    my $grade="INCORRECT";
                    495: 	    foreach my $area (@areas) {
                    496: 		&Apache::lonxml::debug("Area is $area for $name");
                    497: 		$area =~ m/([a-z]*):/;
                    498: 		&Apache::lonxml::debug("Area of type $1");
                    499: 		if ($1 eq 'rectangle') {
                    500: 		    $grade=&grade_rectangle($area,$x,$y);
1.43      albertel  501: 		} elsif ($1 eq 'polygon') {
                    502: 		    $grade=&grade_polygon($area,$x,$y);
1.36      albertel  503: 		} else {
1.94      bisitz    504: 		    &Apache::lonxml::error(&mt('Unknown area style [_1]',$area));
1.36      albertel  505: 		}
                    506: 		&Apache::lonxml::debug("Area said $grade");
                    507: 		if ($grade eq 'APPROX_ANS') { last; }
                    508: 	    }
                    509: 	    &Apache::lonxml::debug("Foil was $grade");
1.85      albertel  510: 	    push(@results, $grade)
                    511: 	} else {
                    512: 	    push(@results, 'MISSING_ANSWER')
1.9       albertel  513: 	}
1.85      albertel  514: 	$temp++;
1.36      albertel  515:     }
1.100     raeburn   516:     if ($Apache::lonhomework::type eq 'randomizetry') {
                    517:         $Apache::lonhomework::results{"resource.$partid.$id.foilorder"} = &Apache::lonnet::array2str(@whichopt);
                    518:     }
1.85      albertel  519:     my ($result) = &Apache::inputtags::finalizeawards(\@results,[]);
                    520:     &Apache::lonxml::debug("Question is $result");
                    521: 
1.60      albertel  522:     my $part=$Apache::inputtags::part;
1.85      albertel  523:     my %previous=
                    524: 	&Apache::response::check_for_previous(&stringify_submission(\%response),
                    525: 					      $part,$id);
1.99      raeburn   526:     if ($result) { 
                    527: 	if ($Apache::lonhomework::type eq 'survey') { 
                    528:             $result='SUBMITTED';
                    529:         } elsif ($Apache::lonhomework::type eq 'surveycred') { 
                    530:             $result='SUBMITTED_CREDIT'; 
                    531:         } elsif ($Apache::lonhomework::type eq 'anonsurvey') { 
                    532:             $result='ANONYMOUS'; 
                    533:         } elsif ($Apache::lonhomework::type eq 'anonsurveycred') { 
                    534:             $result='ANONYMOUS_CREDIT'; 
                    535:         }
                    536:     }
                    537: 
1.60      albertel  538:     &Apache::response::handle_previous(\%previous,$result);
1.85      albertel  539:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    540: 	&stringify_submission(\%response);
1.60      albertel  541:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$result;
1.74      albertel  542:     return;
1.3       albertel  543: }
                    544: 
1.85      albertel  545: sub stringify_submission {
                    546:     my ($response) = @_;
1.86      albertel  547:     return &Apache::lonnet::hash2str(%{ $response });
1.85      albertel  548: }
                    549: 
                    550: sub get_submission {
                    551:     my ($name,$string) = @_;
                    552: 
                    553:     if (!defined($string)) {
                    554: 	my $part=$Apache::inputtags::part;
                    555: 	my $respid=$Apache::inputtags::response['-1'];
1.105   ! raeburn   556:         my $newvariation;
        !           557:         if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
        !           558:             ($Apache::lonhomework::type eq 'randomizetry')) &&
        !           559:             ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
        !           560:             if ($env{'form.'.$part.'.rndseed'} ne
        !           561:                 $Apache::lonhomework::history{"resource.$part.rndseed"}) {
        !           562:                 $newvariation = 1;
        !           563:             }
        !           564:         }
        !           565:         unless ($newvariation) {
        !           566:             if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
        !           567:                 (($env{'form.grade_username'} eq $env{'user.name'}) &&
        !           568:                  ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
        !           569:                 $string =
        !           570:                     $Apache::lonhomework::history{"resource.$part.$respid.submission"};
        !           571:             } else {
        !           572:                 unless (($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') ||
        !           573:                         ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) {
        !           574:                     $string =
        !           575:                         $Apache::lonhomework::history{"resource.$part.$respid.submission"};
        !           576:                 }
        !           577:             }
        !           578:         }
1.85      albertel  579:     }
                    580: 
1.86      albertel  581:     if ($string !~ /=/) {
1.85      albertel  582: 	return split(':',$string);
                    583:     } else {
1.86      albertel  584: 	my %response = &Apache::lonnet::str2hash($string);
1.85      albertel  585: 	return split(':',$response{$name});
                    586:     }
                    587: }
                    588: 
1.1       albertel  589: sub end_foilgroup {
1.36      albertel  590:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    591:     my $result='';
1.105   ! raeburn   592:     my (@images,@whichopt);
1.75      albertel  593: 
1.38      albertel  594:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  595: 	$target eq 'analyze' || $target eq 'answer') {
                    596: 
1.36      albertel  597: 	my ($count,$max) = &getfoilcounts($parstack,$safeeval);
                    598: 	if ($count>$max) { $count=$max }
                    599: 	&Apache::lonxml::debug("Count is $count from $max");
1.75      albertel  600: 
1.36      albertel  601: 	@whichopt = &whichfoils($max);
1.75      albertel  602: 
1.36      albertel  603: 	if ($target eq 'web' || $target eq 'tex') {
                    604: 	    $result=&displayfoils($target,@whichopt);
1.100     raeburn   605:             if ($Apache::lonhomework::type eq 'randomizetry') {
                    606:                 if ($target eq 'web') {
1.105   ! raeburn   607:                     &get_prior_options(\@images,\@whichopt);
1.100     raeburn   608:                 }
                    609:             }
1.81      albertel  610: 	    $Apache::lonxml::post_evaluate=0;
1.36      albertel  611: 	} elsif ($target eq 'grade') {
1.65      albertel  612: 	    if ( defined $env{'form.submitted'}) { &gradefoils(@whichopt); }
1.37      albertel  613: 	} elsif ( $target eq 'analyze') {
                    614: 	    &Apache::response::analyze_store_foilgroup(\@whichopt,
                    615: 						      ['text','image','area']);
1.76      albertel  616: 	} elsif ($target eq 'answer'
                    617: 		 && $env{'form.answer_output_mode'} ne 'tex') {
1.75      albertel  618: 	    $result=&display_answers($target,\@whichopt);
1.37      albertel  619: 	}
1.75      albertel  620: 
1.36      albertel  621:     } elsif ($target eq 'edit') {
                    622: 	$result=&Apache::edit::end_table();
                    623:     }
1.53      albertel  624:     &Apache::response::poprandomnumber();
1.36      albertel  625:     return $result;
1.1       albertel  626: }
                    627: 
                    628: sub start_conceptgroup {
1.36      albertel  629:     $Apache::imageresponse::conceptgroup=1;
                    630:     %Apache::response::conceptgroup=();
                    631:     return '';
1.1       albertel  632: }
                    633: 
                    634: sub end_conceptgroup {
1.36      albertel  635:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    636:     $Apache::imageresponse::conceptgroup=0;
                    637:     my $result;
1.37      albertel  638:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
                    639: 	$target eq 'analyze') {
                    640: 	&Apache::response::pick_foil_for_concept($target,
                    641: 						 ['area','text','image'],
                    642: 						 \%Apache::hint::image,
                    643: 						 $parstack,$safeeval);
1.36      albertel  644:     } elsif ($target eq 'edit') {
                    645: 	$result=&Apache::edit::end_table();
                    646:     }
                    647:     return $result;
1.31      albertel  648: }
                    649: 
                    650: sub insert_foil {
                    651:     return '
                    652:        <foil>
                    653:            <image></image>
                    654:            <text></text>
                    655:            <rectangle></rectangle>
                    656:        </foil>
                    657: ';
1.1       albertel  658: }
                    659: 
1.12      albertel  660: $Apache::imageresponse::curname='';
1.1       albertel  661: sub start_foil {
1.36      albertel  662:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.67      albertel  663:     my $result;
1.37      albertel  664:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  665: 	$target eq 'analyze' || $target eq 'answer') {
1.36      albertel  666: 	my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.72      albertel  667: 	if ($name eq "") {
1.94      bisitz    668: 	    &Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
1.59      albertel  669: 	    $name=$Apache::lonxml::curdepth;
                    670: 	}
1.51      albertel  671: 	if (defined($Apache::response::foilnames{$name})) {
1.94      bisitz    672: 	    &Apache::lonxml::error(&mt("Foil name [_1] appears more than once. Foil names need to be unique."
                    673:                                       ,'<b><tt>'.$name.'</tt></b>'));
1.51      albertel  674: 	}
1.52      albertel  675: 	$Apache::response::foilnames{$name}++;
1.36      albertel  676: 	if ( $Apache::imageresponse::conceptgroup
1.88      albertel  677: 	     && !&Apache::response::showallfoils()
1.56      albertel  678: 	     ) {
1.36      albertel  679: 	    push(@{ $Apache::response::conceptgroup{'names'} }, $name);
                    680: 	} else {
                    681: 	    push(@{ $Apache::response::foilgroup{'names'} }, $name);
                    682: 	}
                    683: 	$Apache::imageresponse::curname=$name;
1.67      albertel  684:     } elsif ($target eq 'edit') {
                    685: 	$result  = &Apache::edit::tag_start($target,$token);
                    686: 	$result .= &Apache::edit::text_arg('Name:','name',$token);
                    687: 	$result .= &Apache::edit::end_row().
                    688: 	    &Apache::edit::start_spanning_row();
                    689:     } elsif ($target eq 'modified') {
                    690: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    691: 						     $safeeval,'name');
                    692: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    693:     } 
                    694:     return $result;;
1.1       albertel  695: }
                    696: 
                    697: sub end_foil {
1.26      albertel  698:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    699:     my $result;
                    700:     if ($target eq 'edit') {
                    701: 	$result=&Apache::edit::end_table();
                    702:     }
                    703:     return $result;
1.1       albertel  704: }
                    705: 
                    706: sub start_text {
1.36      albertel  707:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    708:     my $result='';
1.75      albertel  709:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
                    710: 	|| $target eq 'answer') { 
1.36      albertel  711: 	&Apache::lonxml::startredirection; 
                    712:     } elsif ($target eq 'edit') {
1.73      albertel  713: 	my $descr=&Apache::lonxml::get_all_text('/text',$parser,$style);
1.36      albertel  714: 	$result=&Apache::edit::tag_start($target,$token,'Task Description').
                    715: 	    &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
                    716: 	    &Apache::edit::end_row();
                    717:     } elsif ($target eq "modified") {
1.39      albertel  718: 	$result=$token->[4].&Apache::edit::modifiedfield('/text',$parser);
1.36      albertel  719:     }
                    720:     return $result;
1.1       albertel  721: }
                    722: 
                    723: sub end_text {
1.36      albertel  724:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    725:     my $result;
1.75      albertel  726:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
                    727: 	|| $target eq 'answer') {
1.36      albertel  728: 	my $name = $Apache::imageresponse::curname;
                    729: 	if ( $Apache::imageresponse::conceptgroup
1.88      albertel  730: 	     && !&Apache::response::showallfoils()
1.56      albertel  731: 	     ) {
1.36      albertel  732: 	    $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
                    733: 	} else {
                    734: 	    $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
                    735: 	}
                    736:     } elsif ($target eq 'edit') {
                    737: 	$result=&Apache::edit::end_table();
                    738:     }
                    739:     return $result;
1.1       albertel  740: }
                    741: 
                    742: sub start_image {
1.36      albertel  743:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    744:     my $result='';
1.83      banghart  745:     my $only = join(',',&Apache::loncommon::filecategorytypes('Pictures'));
1.75      albertel  746:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
                    747: 	|| $target eq 'answer') { 
1.36      albertel  748: 	&Apache::lonxml::startredirection; 
                    749:     } elsif ($target eq 'edit') {
1.73      albertel  750: 	my $bgimg=&Apache::lonxml::get_all_text('/image',$parser,$style);
1.36      albertel  751: 	$Apache::edit::bgimgsrc=$bgimg;
                    752: 	$Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
                    753: 
                    754: 	$result=&Apache::edit::tag_start($target,$token,'Clickable Image').
                    755: 	    &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
1.83      banghart  756: 	$result.=&Apache::edit::browse(undef,'textnode',undef,$only).' ';
1.36      albertel  757: 	$result.=&Apache::edit::search(undef,'textnode').
                    758: 	    &Apache::edit::end_row();
                    759:     } elsif ($target eq "modified") {
1.39      albertel  760: 	$result=$token->[4].&Apache::edit::modifiedfield('/image',$parser);
1.36      albertel  761:     }
                    762:     return $result;
1.1       albertel  763: }
                    764: 
                    765: sub end_image {
1.36      albertel  766:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    767:     my $result;
                    768:     my $name = $Apache::imageresponse::curname;
1.75      albertel  769:     if ($target eq 'web' || $target eq 'answer') {
1.62      albertel  770: 	my $image = &Apache::lonxml::endredirection();
1.36      albertel  771: 	&Apache::lonxml::debug("original image is $image");
1.37      albertel  772: 	if ( $Apache::imageresponse::conceptgroup
1.88      albertel  773: 	     && !&Apache::response::showallfoils()
1.56      albertel  774: 	     ) {
1.37      albertel  775: 	    $Apache::response::conceptgroup{"$name.image"} = $image;
                    776: 	} else {
                    777: 	    $Apache::response::foilgroup{"$name.image"} = $image;
                    778: 	}
                    779:     } elsif ($target eq 'analyze') {
1.62      albertel  780: 	my $image = &Apache::lonxml::endredirection();
1.36      albertel  781: 	if ( $Apache::imageresponse::conceptgroup
1.88      albertel  782: 	     && !&Apache::response::showallfoils()
1.56      albertel  783: 	     ) {
1.36      albertel  784: 	    $Apache::response::conceptgroup{"$name.image"} = $image;
                    785: 	} else {
                    786: 	    $Apache::response::foilgroup{"$name.image"} = $image;
                    787: 	}
                    788:     } elsif ($target eq 'edit') {
                    789: 	$result=&Apache::edit::end_table();
                    790:     } elsif ($target eq 'tex') {
1.62      albertel  791: 	my $src = &Apache::lonxml::endredirection();
1.68      foxr      792: 
                    793: 	#  There may be all sorts of whitespace on fore and aft:
                    794: 
1.69      foxr      795: 	$src =~ s/\s+$//s;
                    796: 	$src =~ s/^\s+//s;
1.68      foxr      797: 
                    798: 	#
                    799: 	#  Gnuplot e.g. just generates the latex to put inplace.
                    800: 	#
                    801: 	my $graphinclude;
1.70      foxr      802: 	if ($src =~ /^%DYNAMICIMAGE/) {
                    803: 	    # This is needed because the newline is not always passed -> tex.
                    804: 	    # At present we don't care about the sizing info.
                    805: 
                    806: 	    my ($commentline, $restofstuff) = split(/\n/, $src);
1.68      foxr      807: 	    $graphinclude = $src;
1.70      foxr      808: 	    $graphinclude =~ s/^$commentline//;
1.98      foxr      809: 	} elsif (!($src =~ /\\/)) {
1.68      foxr      810: 	    my ($path,$file) = &Apache::londefdef::get_eps_image($src);
                    811: 	    my ($height_param,$width_param)=
                    812: 		&Apache::londefdef::image_size($src,0.3,$parstack,$safeeval);
                    813: 	    $graphinclude = '\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'}';
1.98      foxr      814: 	} else {
                    815: 	    $graphinclude = $src;   # Already fully formed.
1.68      foxr      816: 	}
                    817: 	$Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent '.$graphinclude;
1.36      albertel  818:     } 
                    819:     return $result;
1.1       albertel  820: }
                    821: 
                    822: sub start_rectangle {
1.36      albertel  823:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    824:     my $result='';
1.38      albertel  825:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  826: 	$target eq 'analyze' || $target eq 'answer') { 
1.36      albertel  827: 	&Apache::lonxml::startredirection; 
                    828:     } elsif ($target eq 'edit') {
1.73      albertel  829: 	my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser,$style);
1.36      albertel  830: 	$result=&Apache::edit::tag_start($target,$token,'Rectangle').
                    831: 	    &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
1.44      albertel  832: 	    &Apache::edit::entercoord(undef,'textnode',undef,undef,'box').
1.36      albertel  833: 	    &Apache::edit::end_row();
                    834:     } elsif ($target eq "modified") {
1.44      albertel  835: 	&Apache::edit::deletecoorddata();
1.39      albertel  836: 	$result=$token->[4].&Apache::edit::modifiedfield('/rectangle',$parser);
1.36      albertel  837:     }
                    838:     return $result;
1.1       albertel  839: }
                    840: 
1.3       albertel  841: sub grade_rectangle {
1.36      albertel  842:     my ($spec,$x,$y) = @_;
                    843:     &Apache::lonxml::debug("Spec is $spec");
1.43      albertel  844:     my ($x1,$y1,$x2,$y2)=($spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
1.36      albertel  845:     &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
                    846:     if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
                    847:     if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
                    848:     if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
                    849: 	return 'APPROX_ANS';
                    850:     }
                    851:     return 'INCORRECT';
1.3       albertel  852: }
                    853: 
1.1       albertel  854: sub end_rectangle {
1.36      albertel  855:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    856:     my $result;
1.38      albertel  857:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  858: 	$target eq 'analyze' || $target eq 'answer') {
1.36      albertel  859: 	my $name = $Apache::imageresponse::curname;
                    860: 	my $area = &Apache::lonxml::endredirection;
1.54      albertel  861: 	$area=~s/\s//g;
1.36      albertel  862: 	&Apache::lonxml::debug("out is $area for $name");
                    863: 	if ( $Apache::imageresponse::conceptgroup
1.88      albertel  864: 	     && !&Apache::response::showallfoils()
1.56      albertel  865: 	     ) {
1.36      albertel  866: 	    push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
                    867: 	} else {
                    868: 	    push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
1.43      albertel  869: 	}
                    870:     } elsif ($target eq 'edit') {
                    871: 	$result=&Apache::edit::end_table();
                    872:     }
                    873:     return $result;
                    874: }
                    875: 
                    876: sub start_polygon {
                    877:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    878:     my $result='';
                    879:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  880: 	$target eq 'analyze' || $target eq 'answer') { 
1.43      albertel  881: 	&Apache::lonxml::startredirection; 
                    882:     } elsif ($target eq 'edit') {
1.73      albertel  883: 	my $coords=&Apache::lonxml::get_all_text('/polygon',$parser,$style);
1.43      albertel  884: 	$result=&Apache::edit::tag_start($target,$token,'Polygon').
                    885: 	    &Apache::edit::editline($token->[1],$coords,'Coordinate list',40).
1.44      albertel  886: 	    &Apache::edit::entercoord(undef,'textnode',undef,undef,'polygon').
1.43      albertel  887: 	    &Apache::edit::end_row();
                    888:     } elsif ($target eq "modified") {
                    889: 	$result=$token->[4].&Apache::edit::modifiedfield('/polygon',$parser);
                    890:     }
                    891:     return $result;
                    892: }
                    893: 
                    894: sub grade_polygon {
                    895:     my ($spec,$x,$y) = @_;
                    896:     &Apache::lonxml::debug("Spec is $spec");
                    897:     $spec=~s/^polygon://;
                    898:     my @polygon;
                    899:     foreach my $coord (split('-',$spec)) {
                    900: 	my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
                    901: 	&Apache::lonxml::debug("x $x y $y");
                    902: 	push @polygon, {'x'=>$x,'y'=>$y};
                    903:     }
                    904:     #make end point start point
                    905:     push @polygon, $polygon[0];
                    906:     # cribbed from
                    907:     # http://geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm
                    908:     my $crossing = 0;    # the crossing number counter
                    909: 
                    910:     # loop through all edges of the polygon
                    911:     for (my $i=0; $i<$#polygon; $i++) {    # edge from V[i] to V[i+1]
                    912: 	if ((($polygon[$i]->{'y'} <= $y)
                    913: 	     && ($polygon[$i+1]->{'y'} > $y))    # an upward crossing
                    914: 	    || 
                    915: 	    (($polygon[$i]->{'y'} > $y) 
                    916: 	     && ($polygon[$i+1]->{'y'} <= $y))) { # a downward crossing
                    917: 	    # compute the actual edge-ray intersect x-coordinate
                    918:             my $vt = ($y - $polygon[$i]->{'y'}) 
                    919: 		/ ($polygon[$i+1]->{'y'} - $polygon[$i]->{'y'});
                    920:             if ($x < $polygon[$i]->{'x'} + $vt * 
                    921: 		($polygon[$i+1]->{'x'} - $polygon[$i]->{'x'})) { # x<intersect
                    922:                 $crossing++;   # a valid crossing of y=P.y right of P.x
                    923: 	    }
                    924: 	}
                    925:     }
                    926: 
                    927:     # 0 if even (out), and 1 if odd (in)
                    928:     if ($crossing%2) {
                    929: 	return 'APPROX_ANS';
                    930:     } else {
                    931: 	return 'INCORRECT';
                    932:     }
                    933: }
                    934: 
                    935: sub end_polygon {
                    936:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    937:     my $result;
                    938:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  939: 	$target eq 'analyze' || $target eq 'answer') {
1.43      albertel  940: 	my $name = $Apache::imageresponse::curname;
                    941: 	my $area = &Apache::lonxml::endredirection;
1.48      albertel  942: 	$area=~s/\s*//g;
1.43      albertel  943: 	&Apache::lonxml::debug("out is $area for $name");
                    944: 	if ( $Apache::imageresponse::conceptgroup
1.88      albertel  945: 	     && !&Apache::response::showallfoils()
1.56      albertel  946: 	     ) {
1.43      albertel  947: 	    push @{ $Apache::response::conceptgroup{"$name.area"} },"polygon:$area";
                    948: 	} else {
                    949: 	    push @{ $Apache::response::foilgroup{"$name.area"} },"polygon:$area";
1.36      albertel  950: 	}
                    951:     } elsif ($target eq 'edit') {
                    952: 	$result=&Apache::edit::end_table();
                    953:     }
                    954:     return $result;
1.1       albertel  955: }
                    956: 1;
                    957: __END__
                    958:  

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