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

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

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