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

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

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