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

1.89      foxr        1: #
1.12      albertel    2: # The LearningOnline Network with CAPA
1.14      albertel    3: # image click response style
                      4: #
1.94.2.1! raeburn     5: # $Id: imageresponse.pm,v 1.94 2008/08/08 16:37:13 bisitz 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.93      raeburn   226:     &Apache::lonnet::appenv(\%x);
1.41      albertel  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') 
1.94.2.1! raeburn   242: 	     && $image !~ /^https?\:/ ) {
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')
1.94.2.1! raeburn   308: 	     && $image !~ /^https?\:/ ) {
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.94.2.1! raeburn   323: 	if ($image !~ /^https?\:/) {
1.47      albertel  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 {
1.94      bisitz    370: 		    &Apache::lonxml::error(&mt('Unknown area style [_1]',$area));
1.36      albertel  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.94      bisitz    497: 	    &Apache::lonxml::warning(&mt('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})) {
1.94      bisitz    501: 	    &Apache::lonxml::error(&mt("Foil name [_1] appears more than once. Foil names need to be unique."
                    502:                                       ,'<b><tt>'.$name.'</tt></b>'));
1.51      albertel  503: 	}
1.52      albertel  504: 	$Apache::response::foilnames{$name}++;
1.36      albertel  505: 	if ( $Apache::imageresponse::conceptgroup
1.88      albertel  506: 	     && !&Apache::response::showallfoils()
1.56      albertel  507: 	     ) {
1.36      albertel  508: 	    push(@{ $Apache::response::conceptgroup{'names'} }, $name);
                    509: 	} else {
                    510: 	    push(@{ $Apache::response::foilgroup{'names'} }, $name);
                    511: 	}
                    512: 	$Apache::imageresponse::curname=$name;
1.67      albertel  513:     } elsif ($target eq 'edit') {
                    514: 	$result  = &Apache::edit::tag_start($target,$token);
                    515: 	$result .= &Apache::edit::text_arg('Name:','name',$token);
                    516: 	$result .= &Apache::edit::end_row().
                    517: 	    &Apache::edit::start_spanning_row();
                    518:     } elsif ($target eq 'modified') {
                    519: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    520: 						     $safeeval,'name');
                    521: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    522:     } 
                    523:     return $result;;
1.1       albertel  524: }
                    525: 
                    526: sub end_foil {
1.26      albertel  527:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    528:     my $result;
                    529:     if ($target eq 'edit') {
                    530: 	$result=&Apache::edit::end_table();
                    531:     }
                    532:     return $result;
1.1       albertel  533: }
                    534: 
                    535: sub start_text {
1.36      albertel  536:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    537:     my $result='';
1.75      albertel  538:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
                    539: 	|| $target eq 'answer') { 
1.36      albertel  540: 	&Apache::lonxml::startredirection; 
                    541:     } elsif ($target eq 'edit') {
1.73      albertel  542: 	my $descr=&Apache::lonxml::get_all_text('/text',$parser,$style);
1.36      albertel  543: 	$result=&Apache::edit::tag_start($target,$token,'Task Description').
                    544: 	    &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
                    545: 	    &Apache::edit::end_row();
                    546:     } elsif ($target eq "modified") {
1.39      albertel  547: 	$result=$token->[4].&Apache::edit::modifiedfield('/text',$parser);
1.36      albertel  548:     }
                    549:     return $result;
1.1       albertel  550: }
                    551: 
                    552: sub end_text {
1.36      albertel  553:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    554:     my $result;
1.75      albertel  555:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
                    556: 	|| $target eq 'answer') {
1.36      albertel  557: 	my $name = $Apache::imageresponse::curname;
                    558: 	if ( $Apache::imageresponse::conceptgroup
1.88      albertel  559: 	     && !&Apache::response::showallfoils()
1.56      albertel  560: 	     ) {
1.36      albertel  561: 	    $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
                    562: 	} else {
                    563: 	    $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
                    564: 	}
                    565:     } elsif ($target eq 'edit') {
                    566: 	$result=&Apache::edit::end_table();
                    567:     }
                    568:     return $result;
1.1       albertel  569: }
                    570: 
                    571: sub start_image {
1.36      albertel  572:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    573:     my $result='';
1.83      banghart  574:     my $only = join(',',&Apache::loncommon::filecategorytypes('Pictures'));
1.75      albertel  575:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze'
                    576: 	|| $target eq 'answer') { 
1.36      albertel  577: 	&Apache::lonxml::startredirection; 
                    578:     } elsif ($target eq 'edit') {
1.73      albertel  579: 	my $bgimg=&Apache::lonxml::get_all_text('/image',$parser,$style);
1.36      albertel  580: 	$Apache::edit::bgimgsrc=$bgimg;
                    581: 	$Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
                    582: 
                    583: 	$result=&Apache::edit::tag_start($target,$token,'Clickable Image').
                    584: 	    &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
1.83      banghart  585: 	$result.=&Apache::edit::browse(undef,'textnode',undef,$only).' ';
1.36      albertel  586: 	$result.=&Apache::edit::search(undef,'textnode').
                    587: 	    &Apache::edit::end_row();
                    588:     } elsif ($target eq "modified") {
1.39      albertel  589: 	$result=$token->[4].&Apache::edit::modifiedfield('/image',$parser);
1.36      albertel  590:     }
                    591:     return $result;
1.1       albertel  592: }
                    593: 
                    594: sub end_image {
1.36      albertel  595:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    596:     my $result;
                    597:     my $name = $Apache::imageresponse::curname;
1.75      albertel  598:     if ($target eq 'web' || $target eq 'answer') {
1.62      albertel  599: 	my $image = &Apache::lonxml::endredirection();
1.36      albertel  600: 	&Apache::lonxml::debug("original image is $image");
1.37      albertel  601: 	if ( $Apache::imageresponse::conceptgroup
1.88      albertel  602: 	     && !&Apache::response::showallfoils()
1.56      albertel  603: 	     ) {
1.37      albertel  604: 	    $Apache::response::conceptgroup{"$name.image"} = $image;
                    605: 	} else {
                    606: 	    $Apache::response::foilgroup{"$name.image"} = $image;
                    607: 	}
                    608:     } elsif ($target eq 'analyze') {
1.62      albertel  609: 	my $image = &Apache::lonxml::endredirection();
1.36      albertel  610: 	if ( $Apache::imageresponse::conceptgroup
1.88      albertel  611: 	     && !&Apache::response::showallfoils()
1.56      albertel  612: 	     ) {
1.36      albertel  613: 	    $Apache::response::conceptgroup{"$name.image"} = $image;
                    614: 	} else {
                    615: 	    $Apache::response::foilgroup{"$name.image"} = $image;
                    616: 	}
                    617:     } elsif ($target eq 'edit') {
                    618: 	$result=&Apache::edit::end_table();
                    619:     } elsif ($target eq 'tex') {
1.62      albertel  620: 	my $src = &Apache::lonxml::endredirection();
1.68      foxr      621: 
                    622: 	#  There may be all sorts of whitespace on fore and aft:
                    623: 
1.69      foxr      624: 	$src =~ s/\s+$//s;
                    625: 	$src =~ s/^\s+//s;
1.68      foxr      626: 
                    627: 	#
                    628: 	#  Gnuplot e.g. just generates the latex to put inplace.
                    629: 	#
                    630: 	my $graphinclude;
1.70      foxr      631: 	if ($src =~ /^%DYNAMICIMAGE/) {
                    632: 	    # This is needed because the newline is not always passed -> tex.
                    633: 	    # At present we don't care about the sizing info.
                    634: 
                    635: 	    my ($commentline, $restofstuff) = split(/\n/, $src);
1.68      foxr      636: 	    $graphinclude = $src;
1.70      foxr      637: 	    $graphinclude =~ s/^$commentline//;
1.68      foxr      638: 	} else {
                    639: 	    my ($path,$file) = &Apache::londefdef::get_eps_image($src);
                    640: 	    my ($height_param,$width_param)=
                    641: 		&Apache::londefdef::image_size($src,0.3,$parstack,$safeeval);
                    642: 	    $graphinclude = '\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'}';
                    643: 	}
                    644: 	$Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent '.$graphinclude;
1.36      albertel  645:     } 
                    646:     return $result;
1.1       albertel  647: }
                    648: 
                    649: sub start_rectangle {
1.36      albertel  650:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    651:     my $result='';
1.38      albertel  652:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  653: 	$target eq 'analyze' || $target eq 'answer') { 
1.36      albertel  654: 	&Apache::lonxml::startredirection; 
                    655:     } elsif ($target eq 'edit') {
1.73      albertel  656: 	my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser,$style);
1.36      albertel  657: 	$result=&Apache::edit::tag_start($target,$token,'Rectangle').
                    658: 	    &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
1.44      albertel  659: 	    &Apache::edit::entercoord(undef,'textnode',undef,undef,'box').
1.36      albertel  660: 	    &Apache::edit::end_row();
                    661:     } elsif ($target eq "modified") {
1.44      albertel  662: 	&Apache::edit::deletecoorddata();
1.39      albertel  663: 	$result=$token->[4].&Apache::edit::modifiedfield('/rectangle',$parser);
1.36      albertel  664:     }
                    665:     return $result;
1.1       albertel  666: }
                    667: 
1.3       albertel  668: sub grade_rectangle {
1.36      albertel  669:     my ($spec,$x,$y) = @_;
                    670:     &Apache::lonxml::debug("Spec is $spec");
1.43      albertel  671:     my ($x1,$y1,$x2,$y2)=($spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
1.36      albertel  672:     &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
                    673:     if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
                    674:     if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
                    675:     if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
                    676: 	return 'APPROX_ANS';
                    677:     }
                    678:     return 'INCORRECT';
1.3       albertel  679: }
                    680: 
1.1       albertel  681: sub end_rectangle {
1.36      albertel  682:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    683:     my $result;
1.38      albertel  684:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  685: 	$target eq 'analyze' || $target eq 'answer') {
1.36      albertel  686: 	my $name = $Apache::imageresponse::curname;
                    687: 	my $area = &Apache::lonxml::endredirection;
1.54      albertel  688: 	$area=~s/\s//g;
1.36      albertel  689: 	&Apache::lonxml::debug("out is $area for $name");
                    690: 	if ( $Apache::imageresponse::conceptgroup
1.88      albertel  691: 	     && !&Apache::response::showallfoils()
1.56      albertel  692: 	     ) {
1.36      albertel  693: 	    push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
                    694: 	} else {
                    695: 	    push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
1.43      albertel  696: 	}
                    697:     } elsif ($target eq 'edit') {
                    698: 	$result=&Apache::edit::end_table();
                    699:     }
                    700:     return $result;
                    701: }
                    702: 
                    703: sub start_polygon {
                    704:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    705:     my $result='';
                    706:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  707: 	$target eq 'analyze' || $target eq 'answer') { 
1.43      albertel  708: 	&Apache::lonxml::startredirection; 
                    709:     } elsif ($target eq 'edit') {
1.73      albertel  710: 	my $coords=&Apache::lonxml::get_all_text('/polygon',$parser,$style);
1.43      albertel  711: 	$result=&Apache::edit::tag_start($target,$token,'Polygon').
                    712: 	    &Apache::edit::editline($token->[1],$coords,'Coordinate list',40).
1.44      albertel  713: 	    &Apache::edit::entercoord(undef,'textnode',undef,undef,'polygon').
1.43      albertel  714: 	    &Apache::edit::end_row();
                    715:     } elsif ($target eq "modified") {
                    716: 	$result=$token->[4].&Apache::edit::modifiedfield('/polygon',$parser);
                    717:     }
                    718:     return $result;
                    719: }
                    720: 
                    721: sub grade_polygon {
                    722:     my ($spec,$x,$y) = @_;
                    723:     &Apache::lonxml::debug("Spec is $spec");
                    724:     $spec=~s/^polygon://;
                    725:     my @polygon;
                    726:     foreach my $coord (split('-',$spec)) {
                    727: 	my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
                    728: 	&Apache::lonxml::debug("x $x y $y");
                    729: 	push @polygon, {'x'=>$x,'y'=>$y};
                    730:     }
                    731:     #make end point start point
                    732:     push @polygon, $polygon[0];
                    733:     # cribbed from
                    734:     # http://geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm
                    735:     my $crossing = 0;    # the crossing number counter
                    736: 
                    737:     # loop through all edges of the polygon
                    738:     for (my $i=0; $i<$#polygon; $i++) {    # edge from V[i] to V[i+1]
                    739: 	if ((($polygon[$i]->{'y'} <= $y)
                    740: 	     && ($polygon[$i+1]->{'y'} > $y))    # an upward crossing
                    741: 	    || 
                    742: 	    (($polygon[$i]->{'y'} > $y) 
                    743: 	     && ($polygon[$i+1]->{'y'} <= $y))) { # a downward crossing
                    744: 	    # compute the actual edge-ray intersect x-coordinate
                    745:             my $vt = ($y - $polygon[$i]->{'y'}) 
                    746: 		/ ($polygon[$i+1]->{'y'} - $polygon[$i]->{'y'});
                    747:             if ($x < $polygon[$i]->{'x'} + $vt * 
                    748: 		($polygon[$i+1]->{'x'} - $polygon[$i]->{'x'})) { # x<intersect
                    749:                 $crossing++;   # a valid crossing of y=P.y right of P.x
                    750: 	    }
                    751: 	}
                    752:     }
                    753: 
                    754:     # 0 if even (out), and 1 if odd (in)
                    755:     if ($crossing%2) {
                    756: 	return 'APPROX_ANS';
                    757:     } else {
                    758: 	return 'INCORRECT';
                    759:     }
                    760: }
                    761: 
                    762: sub end_polygon {
                    763:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    764:     my $result;
                    765:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
1.75      albertel  766: 	$target eq 'analyze' || $target eq 'answer') {
1.43      albertel  767: 	my $name = $Apache::imageresponse::curname;
                    768: 	my $area = &Apache::lonxml::endredirection;
1.48      albertel  769: 	$area=~s/\s*//g;
1.43      albertel  770: 	&Apache::lonxml::debug("out is $area for $name");
                    771: 	if ( $Apache::imageresponse::conceptgroup
1.88      albertel  772: 	     && !&Apache::response::showallfoils()
1.56      albertel  773: 	     ) {
1.43      albertel  774: 	    push @{ $Apache::response::conceptgroup{"$name.area"} },"polygon:$area";
                    775: 	} else {
                    776: 	    push @{ $Apache::response::foilgroup{"$name.area"} },"polygon:$area";
1.36      albertel  777: 	}
                    778:     } elsif ($target eq 'edit') {
                    779: 	$result=&Apache::edit::end_table();
                    780:     }
                    781:     return $result;
1.1       albertel  782: }
                    783: 1;
                    784: __END__
                    785:  

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