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

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

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