File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.16: download - view: text, annotated - select for diffs
Thu Jan 17 12:23:31 2002 UTC (22 years, 3 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
changing "sub BEGIN" to "BEGIN"

    1: # The LearningOnline Network with CAPA
    2: # image click response style
    3: #
    4: # $Id: imageresponse.pm,v 1.16 2002/01/17 12:23:31 harris41 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:   return ($count,$max);
   73: }
   74: 
   75: sub whichfoils {
   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;
   89: }
   90: 
   91: sub displayfoils {
   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;
  107: }
  108: 
  109: sub gradefoils {
  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");
  132: 	}
  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 '';
  146: }
  147: 
  148: sub end_foilgroup {
  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);
  164:     }
  165:   }
  166:   return $result;
  167: }
  168: 
  169: sub start_conceptgroup {
  170:   $Apache::imageresponse::conceptgroup=1;
  171:   %Apache::response::conceptgroup={};
  172:   return '';
  173: }
  174: 
  175: sub end_conceptgroup {
  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:       }
  192:     }
  193:   }
  194:   return '';
  195: }
  196: 
  197: $Apache::imageresponse::curname='';
  198: sub start_foil {
  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 '';
  211: }
  212: 
  213: sub end_foil {
  214:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  215:   return '';
  216: }
  217: 
  218: sub start_text {
  219:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  220:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
  221:   return '';
  222: }
  223: 
  224: sub end_text {
  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;
  232:     }
  233:   }
  234:   return '';
  235: }
  236: 
  237: sub start_image {
  238:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  239:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
  240:   return '';
  241: }
  242: 
  243: sub end_image {
  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;
  253:     }
  254:   }
  255:   return '';
  256: }
  257: 
  258: sub start_rectangle {
  259:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  260:   if ($target eq 'web' || $target eq 'grade') { &Apache::lonxml::startredirection; }
  261:   return '';
  262: }
  263: 
  264: sub grade_rectangle {
  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';
  277: }
  278: 
  279: sub end_rectangle {
  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";
  289:     }
  290:   }
  291:   return '';
  292: }
  293: 1;
  294: __END__
  295:  

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