File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.18: download - view: text, annotated - select for diffs
Fri May 3 20:44:46 2002 UTC (22 years ago) by albertel
Branches: MAIN
CVS tags: version_0_4, stable_2002_july, STABLE, HEAD
- implements BUG#238 for image response (showallfoils)

    1: # The LearningOnline Network with CAPA
    2: # image click response style
    3: #
    4: # $Id: imageresponse.pm,v 1.18 2002/05/03 20:44:46 albertel Exp $
    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: #
   28: 
   29: #FIXME assumes multiple possbile submissions but only one is possible currently
   30: 
   31: package Apache::imageresponse;
   32: use strict;
   33: 
   34: BEGIN {
   35:   &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
   36: }
   37: 
   38: sub start_imageresponse {
   39:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   40:   my $result;
   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);
   45:   if ($target eq 'meta') {
   46:     $result=&Apache::response::meta_package_write('imageresponse');
   47:   }
   48:   return $result;
   49: }
   50: 
   51: sub end_imageresponse {
   52:   &Apache::response::end_response;
   53:   pop @Apache::lonxml::namespace;
   54:   &Apache::lonxml::deregister('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
   55:   return '';
   56: }
   57: 
   58: %Apache::response::foilgroup={};
   59: sub start_foilgroup {
   60:   %Apache::response::foilgroup={};
   61:   $Apache::imageresponse::conceptgroup=0;
   62:   &Apache::response::setrandomnumber();
   63:   return '';
   64: }
   65: 
   66: sub getfoilcounts {
   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:   if (&Apache::response::showallfoils()) { $max=$count; }
   73:   return ($count,$max);
   74: }
   75: 
   76: sub whichfoils {
   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");
   83:     my $aopt;
   84:     if (&Apache::response::showallfoils()) {
   85:       $aopt=0;
   86:     } else {
   87:       $aopt=int(rand($#names+1));
   88:     }
   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;
   95: }
   96: 
   97: sub displayfoils {
   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;
  113: }
  114: 
  115: sub gradefoils {
  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");
  138: 	}
  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 '';
  152: }
  153: 
  154: sub end_foilgroup {
  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);
  170:     }
  171:   }
  172:   return $result;
  173: }
  174: 
  175: sub start_conceptgroup {
  176:   $Apache::imageresponse::conceptgroup=1;
  177:   %Apache::response::conceptgroup={};
  178:   return '';
  179: }
  180: 
  181: sub end_conceptgroup {
  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:       }
  198:     }
  199:   }
  200:   return '';
  201: }
  202: 
  203: $Apache::imageresponse::curname='';
  204: sub start_foil {
  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; }
  209:     if ( $Apache::imageresponse::conceptgroup
  210: 	 && !&Apache::response::showallfoils()) {
  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 '';
  218: }
  219: 
  220: sub end_foil {
  221:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  222:   return '';
  223: }
  224: 
  225: sub start_text {
  226:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  227:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
  228:   return '';
  229: }
  230: 
  231: sub end_text {
  232:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  233:   if ($target eq 'web') {
  234:     my $name = $Apache::imageresponse::curname;
  235:     if ( $Apache::imageresponse::conceptgroup
  236:        && !&Apache::response::showallfoils() ) {
  237:       $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
  238:     } else {
  239:       $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
  240:     }
  241:   }
  242:   return '';
  243: }
  244: 
  245: sub start_image {
  246:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  247:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
  248:   return '';
  249: }
  250: 
  251: sub end_image {
  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");
  257:     if ( $Apache::imageresponse::conceptgroup
  258: 	 && !&Apache::response::showallfoils()) {
  259:       $Apache::response::conceptgroup{"$name.image"} = $image;
  260:     } else {
  261:       $Apache::response::foilgroup{"$name.image"} = $image;
  262:     }
  263:   }
  264:   return '';
  265: }
  266: 
  267: sub start_rectangle {
  268:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  269:   if ($target eq 'web' || $target eq 'grade') { &Apache::lonxml::startredirection; }
  270:   return '';
  271: }
  272: 
  273: sub grade_rectangle {
  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; }
  284:   if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
  285:     return 'APPROX_ANS';
  286:   }
  287:   return 'INCORRECT';
  288: }
  289: 
  290: sub end_rectangle {
  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");
  296:     if ( $Apache::imageresponse::conceptgroup
  297: 	 && !&Apache::response::showallfoils()) {
  298:       push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
  299:     } else {
  300:       push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
  301:     }
  302:   }
  303:   return '';
  304: }
  305: 1;
  306: __END__
  307:  

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