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

1.12      albertel    1: # The LearningOnline Network with CAPA
1.14      albertel    2: # image click response style
                      3: #
1.16    ! harris41    4: # $Id: imageresponse.pm,v 1.15 2001/12/14 23:00:52 albertel Exp $
1.14      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.1       albertel   28: 
1.12      albertel   29: #FIXME assumes multiple possbile submissions but only one is possible currently
1.3       albertel   30: 
1.1       albertel   31: package Apache::imageresponse;
                     32: use strict;
                     33: 
1.16    ! harris41   34: BEGIN {
1.12      albertel   35:   &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
1.1       albertel   36: }
                     37: 
                     38: sub start_imageresponse {
1.12      albertel   39:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.13      albertel   40:   my $result;
1.12      albertel   41:   #when in a radiobutton response use these
                     42:   &Apache::lonxml::register('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
                     43:   push (@Apache::lonxml::namespace,'imageresponse');
                     44:   my $id = &Apache::response::start_response($parstack,$safeeval);
1.13      albertel   45:   if ($target eq 'meta') {
                     46:     $result=&Apache::response::meta_package_write('imageresponse');
                     47:   }
                     48:   return $result;
1.1       albertel   49: }
                     50: 
                     51: sub end_imageresponse {
1.12      albertel   52:   &Apache::response::end_response;
                     53:   pop @Apache::lonxml::namespace;
1.15      albertel   54:   &Apache::lonxml::deregister('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
1.12      albertel   55:   return '';
1.1       albertel   56: }
                     57: 
1.12      albertel   58: %Apache::response::foilgroup={};
1.1       albertel   59: sub start_foilgroup {
1.12      albertel   60:   %Apache::response::foilgroup={};
                     61:   $Apache::imageresponse::conceptgroup=0;
                     62:   &Apache::response::setrandomnumber();
                     63:   return '';
1.1       albertel   64: }
                     65: 
1.2       albertel   66: sub getfoilcounts {
1.12      albertel   67:   my ($parstack,$safeeval)=@_;
                     68: 
                     69:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
                     70:   # +1 since instructors will count from 1
                     71:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
                     72:   return ($count,$max);
1.2       albertel   73: }
                     74: 
                     75: sub whichfoils {
1.12      albertel   76:   my ($max)=@_;
                     77:   if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
                     78:   my @names = @{ $Apache::response::foilgroup{'names'} };
                     79:   my @whichopt =();
                     80:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
                     81:     &Apache::lonxml::debug("Have $#whichopt max is $max");
                     82:     my $aopt=int(rand($#names+1));
                     83:     &Apache::lonxml::debug("From $#names elms, picking $aopt");
                     84:     $aopt=splice(@names,$aopt,1);
                     85:     &Apache::lonxml::debug("Picked $aopt");
                     86:     push (@whichopt,$aopt);
                     87:   }
                     88:   return @whichopt;
1.2       albertel   89: }
                     90: 
                     91: sub displayfoils {
1.12      albertel   92:   my (@whichopt) = @_;
                     93:   my $result ='';
                     94:   my $name;
                     95:   my $temp=1;
                     96:   foreach $name (@whichopt) {
                     97:     $result.=$Apache::response::foilgroup{"$name.text"}."<br />\n";
                     98:     my $image=$Apache::response::foilgroup{"$name.image"};
                     99:     if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
                    100:       $result.="<img src=\"$image\"/> <br />\n";
                    101:     } else {
                    102:       $result.="<input type=\"image\" name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\" src=\"$image\"/> <br />\n";
                    103:     }
                    104:     $temp++;
                    105:   }
                    106:   return $result;
1.2       albertel  107: }
                    108: 
1.3       albertel  109: sub gradefoils {
1.12      albertel  110:   my (@whichopt) = @_;
                    111:   my $result='';
                    112:   my $x;
                    113:   my $y;
                    114:   my $result;
                    115:   my $id=$Apache::inputtags::response['-1'];
                    116:   my $temp=1;
                    117:   foreach my $name (@whichopt) {
                    118:     $x=$ENV{"form.HWVAL_$id:$temp.x"};
                    119:     $y=$ENV{"form.HWVAL_$id:$temp.y"};
                    120:     &Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
                    121:     if (defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
                    122:       my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
                    123:       my $grade="INCORRECT";
                    124:       foreach my $area (@areas) {
                    125: 	&Apache::lonxml::debug("Area is $area for $name");
                    126: 	$area =~ m/([a-z]*):/;
                    127: 	&Apache::lonxml::debug("Area of type $1");
                    128: 	if ($1 eq 'rectangle') {
                    129: 	  $grade=&grade_rectangle($area,$x,$y);
                    130: 	} else {
                    131: 	  &Apache::lonxml::error("Unknown area style $area");
1.9       albertel  132: 	}
1.12      albertel  133: 	&Apache::lonxml::debug("Area said $grade");
                    134: 	if ($grade eq 'APPROX_ANS') { last; }
                    135:       }
                    136:       &Apache::lonxml::debug("Foil was $grade");
                    137:       if ($grade eq 'INCORRECT') { $result= 'INCORRECT'; }
                    138:       if (($grade eq 'APPROX_ANS') && ($result ne 'APPROX_ANS')) { $result=$grade; }
                    139:       &Apache::lonxml::debug("Question is $result");
                    140:       $temp++;
                    141:     }
                    142:   }
                    143:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}="$x:$y";
                    144:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}=$result;
                    145:   return '';
1.3       albertel  146: }
                    147: 
1.1       albertel  148: sub end_foilgroup {
1.12      albertel  149:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    150:   my $result='';
                    151:   my @whichopt;
                    152:   if ($target eq 'web' || $target eq 'grade') {
                    153:     my ($count,$max) = &getfoilcounts($parstack,$safeeval);
                    154:     if ($count>$max) { $count=$max }
                    155:     &Apache::lonxml::debug("Count is $count from $max");
                    156:     @whichopt = &whichfoils($max);
                    157:   }
                    158:   if ($target eq 'web') {
                    159:     $result=&displayfoils(@whichopt);
                    160:   }
                    161:   if ($target eq 'grade') {
                    162:     if ( defined $ENV{'form.submitted'}) {
                    163:       &gradefoils(@whichopt);
1.3       albertel  164:     }
1.12      albertel  165:   }
                    166:   return $result;
1.1       albertel  167: }
                    168: 
                    169: sub start_conceptgroup {
1.12      albertel  170:   $Apache::imageresponse::conceptgroup=1;
                    171:   %Apache::response::conceptgroup={};
                    172:   return '';
1.1       albertel  173: }
                    174: 
                    175: sub end_conceptgroup {
1.12      albertel  176:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    177:   $Apache::imageresponse::conceptgroup=0;  
                    178:   if ($target eq 'web' || $target eq 'grade') {
                    179:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
                    180:       my @names = @{ $Apache::response::conceptgroup{'names'} };
                    181:       my $pick=int(rand($#names+1));
                    182:       my $name=$names[$pick];
                    183:       if (defined(@{ $Apache::response::conceptgroup{"$name.area"} })) {
                    184: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
                    185: 	$Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
                    186: 	$Apache::response::foilgroup{"$name.image"} = $Apache::response::conceptgroup{"$name.image"};
                    187: 	push(@{ $Apache::response::foilgroup{"$name.area"} }, @{ $Apache::response::conceptgroup{"$name.area"} });
                    188: 	my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    189: 	$Apache::response::foilgroup{"$name.concept"} = $concept;
                    190: 	&Apache::lonxml::debug("Selecting $name in $concept");
                    191:       }
1.9       albertel  192:     }
1.12      albertel  193:   }
                    194:   return '';
1.1       albertel  195: }
                    196: 
1.12      albertel  197: $Apache::imageresponse::curname='';
1.1       albertel  198: sub start_foil {
1.12      albertel  199:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    200:   if ($target eq 'web' || $target eq 'grade') {
                    201:     my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    202:     if ($name eq '') { $name=$Apache::lonxml::curdepth; }
                    203:     if ( $Apache::imageresponse::conceptgroup ) {
                    204:       push(@{ $Apache::response::conceptgroup{'names'} }, $name);
                    205:     } else {
                    206:       push(@{ $Apache::response::foilgroup{'names'} }, $name);
                    207:     }
                    208:     $Apache::imageresponse::curname=$name;
                    209:   }
                    210:   return '';
1.1       albertel  211: }
                    212: 
                    213: sub end_foil {
1.12      albertel  214:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    215:   return '';
1.1       albertel  216: }
                    217: 
                    218: sub start_text {
1.12      albertel  219:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    220:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
                    221:   return '';
1.1       albertel  222: }
                    223: 
                    224: sub end_text {
1.12      albertel  225:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    226:   if ($target eq 'web') {
                    227:     my $name = $Apache::imageresponse::curname;
                    228:     if ( $Apache::imageresponse::conceptgroup ) {
                    229:       $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
                    230:     } else {
                    231:       $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
1.7       albertel  232:     }
1.12      albertel  233:   }
                    234:   return '';
1.1       albertel  235: }
                    236: 
                    237: sub start_image {
1.12      albertel  238:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    239:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
                    240:   return '';
1.1       albertel  241: }
                    242: 
                    243: sub end_image {
1.12      albertel  244:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    245:   if ($target eq 'web') {
                    246:     my $name = $Apache::imageresponse::curname;
                    247:     my $image = &Apache::lonxml::endredirection;
                    248:     &Apache::lonxml::debug("out is $image");
                    249:     if ( $Apache::imageresponse::conceptgroup ) {
                    250:       $Apache::response::conceptgroup{"$name.image"} = $image;
                    251:     } else {
                    252:       $Apache::response::foilgroup{"$name.image"} = $image;
1.7       albertel  253:     }
1.12      albertel  254:   }
                    255:   return '';
1.1       albertel  256: }
                    257: 
                    258: sub start_rectangle {
1.12      albertel  259:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    260:   if ($target eq 'web' || $target eq 'grade') { &Apache::lonxml::startredirection; }
                    261:   return '';
1.1       albertel  262: }
                    263: 
1.3       albertel  264: sub grade_rectangle {
1.12      albertel  265:   my ($spec,$x,$y) = @_;
                    266:   &Apache::lonxml::debug("Spec is $spec");
                    267:   $spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/;
                    268:   my $x1=$1;
                    269:   my $y1=$2;
                    270:   my $x2=$3;
                    271:   my $y2=$4;
                    272:   &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
                    273:   if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
                    274:   if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
                    275:   if ($x => $x1) { if ($x <= $x2) { if ($y => $y1) { if ($y <= $y2) { return 'APPROX_ANS'; } } } }
                    276:   return 'INCORRECT';
1.3       albertel  277: }
                    278: 
1.1       albertel  279: sub end_rectangle {
1.12      albertel  280:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    281:   if ($target eq 'web' || $target eq 'grade') {
                    282:     my $name = $Apache::imageresponse::curname;
                    283:     my $area = &Apache::lonxml::endredirection;
                    284:     &Apache::lonxml::debug("out is $area for $name");
                    285:     if ( $Apache::imageresponse::conceptgroup ) {
                    286:       push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
                    287:     } else {
                    288:       push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
1.7       albertel  289:     }
1.12      albertel  290:   }
                    291:   return '';
1.1       albertel  292: }
                    293: 1;
                    294: __END__
                    295:  

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