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

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

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