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

1.12      albertel    1: # The LearningOnline Network with CAPA
1.14      albertel    2: # image click response style
                      3: #
1.18    ! albertel    4: # $Id: imageresponse.pm,v 1.17 2002/02/04 17:30:53 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;
1.18    ! albertel   72:   if (&Apache::response::showallfoils()) { $max=$count; }
1.12      albertel   73:   return ($count,$max);
1.2       albertel   74: }
                     75: 
                     76: sub whichfoils {
1.12      albertel   77:   my ($max)=@_;
                     78:   if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
                     79:   my @names = @{ $Apache::response::foilgroup{'names'} };
                     80:   my @whichopt =();
                     81:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
                     82:     &Apache::lonxml::debug("Have $#whichopt max is $max");
1.18    ! albertel   83:     my $aopt;
        !            84:     if (&Apache::response::showallfoils()) {
        !            85:       $aopt=0;
        !            86:     } else {
        !            87:       $aopt=int(rand($#names+1));
        !            88:     }
1.12      albertel   89:     &Apache::lonxml::debug("From $#names elms, picking $aopt");
                     90:     $aopt=splice(@names,$aopt,1);
                     91:     &Apache::lonxml::debug("Picked $aopt");
                     92:     push (@whichopt,$aopt);
                     93:   }
                     94:   return @whichopt;
1.2       albertel   95: }
                     96: 
                     97: sub displayfoils {
1.12      albertel   98:   my (@whichopt) = @_;
                     99:   my $result ='';
                    100:   my $name;
                    101:   my $temp=1;
                    102:   foreach $name (@whichopt) {
                    103:     $result.=$Apache::response::foilgroup{"$name.text"}."<br />\n";
                    104:     my $image=$Apache::response::foilgroup{"$name.image"};
                    105:     if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
                    106:       $result.="<img src=\"$image\"/> <br />\n";
                    107:     } else {
                    108:       $result.="<input type=\"image\" name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\" src=\"$image\"/> <br />\n";
                    109:     }
                    110:     $temp++;
                    111:   }
                    112:   return $result;
1.2       albertel  113: }
                    114: 
1.3       albertel  115: sub gradefoils {
1.12      albertel  116:   my (@whichopt) = @_;
                    117:   my $result='';
                    118:   my $x;
                    119:   my $y;
                    120:   my $result;
                    121:   my $id=$Apache::inputtags::response['-1'];
                    122:   my $temp=1;
                    123:   foreach my $name (@whichopt) {
                    124:     $x=$ENV{"form.HWVAL_$id:$temp.x"};
                    125:     $y=$ENV{"form.HWVAL_$id:$temp.y"};
                    126:     &Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
                    127:     if (defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
                    128:       my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
                    129:       my $grade="INCORRECT";
                    130:       foreach my $area (@areas) {
                    131: 	&Apache::lonxml::debug("Area is $area for $name");
                    132: 	$area =~ m/([a-z]*):/;
                    133: 	&Apache::lonxml::debug("Area of type $1");
                    134: 	if ($1 eq 'rectangle') {
                    135: 	  $grade=&grade_rectangle($area,$x,$y);
                    136: 	} else {
                    137: 	  &Apache::lonxml::error("Unknown area style $area");
1.9       albertel  138: 	}
1.12      albertel  139: 	&Apache::lonxml::debug("Area said $grade");
                    140: 	if ($grade eq 'APPROX_ANS') { last; }
                    141:       }
                    142:       &Apache::lonxml::debug("Foil was $grade");
                    143:       if ($grade eq 'INCORRECT') { $result= 'INCORRECT'; }
                    144:       if (($grade eq 'APPROX_ANS') && ($result ne 'APPROX_ANS')) { $result=$grade; }
                    145:       &Apache::lonxml::debug("Question is $result");
                    146:       $temp++;
                    147:     }
                    148:   }
                    149:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}="$x:$y";
                    150:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}=$result;
                    151:   return '';
1.3       albertel  152: }
                    153: 
1.1       albertel  154: sub end_foilgroup {
1.12      albertel  155:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    156:   my $result='';
                    157:   my @whichopt;
                    158:   if ($target eq 'web' || $target eq 'grade') {
                    159:     my ($count,$max) = &getfoilcounts($parstack,$safeeval);
                    160:     if ($count>$max) { $count=$max }
                    161:     &Apache::lonxml::debug("Count is $count from $max");
                    162:     @whichopt = &whichfoils($max);
                    163:   }
                    164:   if ($target eq 'web') {
                    165:     $result=&displayfoils(@whichopt);
                    166:   }
                    167:   if ($target eq 'grade') {
                    168:     if ( defined $ENV{'form.submitted'}) {
                    169:       &gradefoils(@whichopt);
1.3       albertel  170:     }
1.12      albertel  171:   }
                    172:   return $result;
1.1       albertel  173: }
                    174: 
                    175: sub start_conceptgroup {
1.12      albertel  176:   $Apache::imageresponse::conceptgroup=1;
                    177:   %Apache::response::conceptgroup={};
                    178:   return '';
1.1       albertel  179: }
                    180: 
                    181: sub end_conceptgroup {
1.12      albertel  182:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    183:   $Apache::imageresponse::conceptgroup=0;  
                    184:   if ($target eq 'web' || $target eq 'grade') {
                    185:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
                    186:       my @names = @{ $Apache::response::conceptgroup{'names'} };
                    187:       my $pick=int(rand($#names+1));
                    188:       my $name=$names[$pick];
                    189:       if (defined(@{ $Apache::response::conceptgroup{"$name.area"} })) {
                    190: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
                    191: 	$Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
                    192: 	$Apache::response::foilgroup{"$name.image"} = $Apache::response::conceptgroup{"$name.image"};
                    193: 	push(@{ $Apache::response::foilgroup{"$name.area"} }, @{ $Apache::response::conceptgroup{"$name.area"} });
                    194: 	my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    195: 	$Apache::response::foilgroup{"$name.concept"} = $concept;
                    196: 	&Apache::lonxml::debug("Selecting $name in $concept");
                    197:       }
1.9       albertel  198:     }
1.12      albertel  199:   }
                    200:   return '';
1.1       albertel  201: }
                    202: 
1.12      albertel  203: $Apache::imageresponse::curname='';
1.1       albertel  204: sub start_foil {
1.12      albertel  205:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    206:   if ($target eq 'web' || $target eq 'grade') {
                    207:     my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    208:     if ($name eq '') { $name=$Apache::lonxml::curdepth; }
1.18    ! albertel  209:     if ( $Apache::imageresponse::conceptgroup
        !           210: 	 && !&Apache::response::showallfoils()) {
1.12      albertel  211:       push(@{ $Apache::response::conceptgroup{'names'} }, $name);
                    212:     } else {
                    213:       push(@{ $Apache::response::foilgroup{'names'} }, $name);
                    214:     }
                    215:     $Apache::imageresponse::curname=$name;
                    216:   }
                    217:   return '';
1.1       albertel  218: }
                    219: 
                    220: sub end_foil {
1.12      albertel  221:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    222:   return '';
1.1       albertel  223: }
                    224: 
                    225: sub start_text {
1.12      albertel  226:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    227:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
                    228:   return '';
1.1       albertel  229: }
                    230: 
                    231: sub end_text {
1.12      albertel  232:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    233:   if ($target eq 'web') {
                    234:     my $name = $Apache::imageresponse::curname;
1.18    ! albertel  235:     if ( $Apache::imageresponse::conceptgroup
        !           236:        && !&Apache::response::showallfoils() ) {
1.12      albertel  237:       $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
                    238:     } else {
                    239:       $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
1.7       albertel  240:     }
1.12      albertel  241:   }
                    242:   return '';
1.1       albertel  243: }
                    244: 
                    245: sub start_image {
1.12      albertel  246:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    247:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
                    248:   return '';
1.1       albertel  249: }
                    250: 
                    251: sub end_image {
1.12      albertel  252:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    253:   if ($target eq 'web') {
                    254:     my $name = $Apache::imageresponse::curname;
                    255:     my $image = &Apache::lonxml::endredirection;
                    256:     &Apache::lonxml::debug("out is $image");
1.18    ! albertel  257:     if ( $Apache::imageresponse::conceptgroup
        !           258: 	 && !&Apache::response::showallfoils()) {
1.12      albertel  259:       $Apache::response::conceptgroup{"$name.image"} = $image;
                    260:     } else {
                    261:       $Apache::response::foilgroup{"$name.image"} = $image;
1.7       albertel  262:     }
1.12      albertel  263:   }
                    264:   return '';
1.1       albertel  265: }
                    266: 
                    267: sub start_rectangle {
1.12      albertel  268:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    269:   if ($target eq 'web' || $target eq 'grade') { &Apache::lonxml::startredirection; }
                    270:   return '';
1.1       albertel  271: }
                    272: 
1.3       albertel  273: sub grade_rectangle {
1.12      albertel  274:   my ($spec,$x,$y) = @_;
                    275:   &Apache::lonxml::debug("Spec is $spec");
                    276:   $spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/;
                    277:   my $x1=$1;
                    278:   my $y1=$2;
                    279:   my $x2=$3;
                    280:   my $y2=$4;
                    281:   &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
                    282:   if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
                    283:   if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
1.17      albertel  284:   if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
                    285:     return 'APPROX_ANS';
                    286:   }
1.12      albertel  287:   return 'INCORRECT';
1.3       albertel  288: }
                    289: 
1.1       albertel  290: sub end_rectangle {
1.12      albertel  291:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    292:   if ($target eq 'web' || $target eq 'grade') {
                    293:     my $name = $Apache::imageresponse::curname;
                    294:     my $area = &Apache::lonxml::endredirection;
                    295:     &Apache::lonxml::debug("out is $area for $name");
1.18    ! albertel  296:     if ( $Apache::imageresponse::conceptgroup
        !           297: 	 && !&Apache::response::showallfoils()) {
1.12      albertel  298:       push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
                    299:     } else {
                    300:       push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
1.7       albertel  301:     }
1.12      albertel  302:   }
                    303:   return '';
1.1       albertel  304: }
                    305: 1;
                    306: __END__
                    307:  

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