File:  [LON-CAPA] / loncom / homework / imageresponse.pm
Revision 1.7: download - view: text, annotated - select for diffs
Thu May 31 22:34:33 2001 UTC (22 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- added calls to get_param to clean up how one accesses data
- fixed the concetpgropu code to actually work
- fixed the off by one errors in selecting and displaying foils.

    1: # The LearningOnline Network with CAPA
    2: # iimage click response style
    3: 
    4: #FIXME assumes multiple possbile submissions but only one is possible currently
    5: 
    6: package Apache::imageresponse;
    7: use strict;
    8: 
    9: sub BEGIN {
   10:   &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
   11: }
   12: 
   13: sub start_imageresponse {
   14:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
   15:   #when in a radiobutton response use these
   16:   &Apache::lonxml::register('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
   17:   push (@Apache::lonxml::namespace,'imageresponse');
   18:   my $id = &Apache::response::start_response($parstack,$safeeval);
   19:   return '';
   20: }
   21: 
   22: sub end_imageresponse {
   23:   &Apache::response::end_response;
   24:   pop @Apache::lonxml::namespace;
   25:   return '';
   26: }
   27: 
   28: %Apache::response::foilgroup={};
   29: sub start_foilgroup {
   30:   %Apache::response::foilgroup={};
   31:   $Apache::imageresponse::conceptgroup=0;
   32:   &Apache::response::setrandomnumber();
   33:   return '';
   34: }
   35: 
   36: sub getfoilcounts {
   37:   my ($parstack,$safeeval)=@_;
   38: 
   39:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
   40:   # +1 since instructors will count from 1
   41:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
   42:   return ($count,$max);
   43: }
   44: 
   45: sub whichfoils {
   46:   my ($max)=@_;
   47:   my @names = @{ $Apache::response::foilgroup{'names'} };
   48:   my @whichopt =();
   49:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
   50:     &Apache::lonxml::debug("Have $#whichopt max is $max");
   51:     my $aopt=int(rand($#names+1));
   52:     &Apache::lonxml::debug("From $#names elms, picking $aopt");
   53:     $aopt=splice(@names,$aopt,1);
   54:     &Apache::lonxml::debug("Picked $aopt");
   55:     push (@whichopt,$aopt);
   56:   }
   57:   return @whichopt;
   58: }
   59: 
   60: sub displayfoils {
   61:   my (@whichopt) = @_;
   62:   my $result ='';
   63:   my $name;
   64:   my $temp=1;
   65:   foreach $name (@whichopt) {
   66:     $result.=$Apache::response::foilgroup{"$name.text"}."<br />\n";
   67:     my $image=$Apache::response::foilgroup{"$name.image"};
   68:     if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
   69:       $result.="<img src=\"$image\"/> <br />\n";
   70:     } else {
   71:       $result.="<input type=\"image\" name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\" src=\"$image\"/> <br />\n";
   72:     }
   73:     $temp++;
   74:   }
   75:   return $result;
   76: }
   77: 
   78: sub gradefoils {
   79:   my (@whichopt) = @_;
   80:   my $result='';
   81:   my $x;
   82:   my $y;
   83:   my $result;
   84:   my $id=$Apache::inputtags::response['-1'];
   85:   my $temp=1;
   86:   foreach my $name (@whichopt) {
   87:     $x=$ENV{"form.HWVAL_$id:$temp.x"};
   88:     $y=$ENV{"form.HWVAL_$id:$temp.y"};
   89:     &Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
   90:     my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
   91:     my $grade="INCORRECT";
   92:     foreach my $area (@areas) {
   93:       &Apache::lonxml::debug("Area is $area for $name");
   94:       $area =~ m/([a-z]*):/;
   95:       &Apache::lonxml::debug("Area of type $1");
   96:       if ($1 eq 'rectangle') {
   97: 	$grade=&grade_rectangle($area,$x,$y);
   98:       } else {
   99: 	&Apache::lonxml::error("Unknown area style $area");
  100:       }
  101:       &Apache::lonxml::debug("Area said $grade");
  102:       if ($grade eq 'APPROX_ANS') { last; }
  103:     }
  104:     &Apache::lonxml::debug("Foil was $grade");
  105:     if ($grade eq 'INCORRECT') { $result= 'INCORRECT'; }
  106:     if (($grade eq 'APPROX_ANS') && ($result ne 'APPROX_ANS')) { $result=$grade; }
  107:     &Apache::lonxml::debug("Question is $result");
  108:     $temp++;
  109:   }
  110:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}="$x:$y";
  111:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}=$result;
  112:   return '';
  113: }
  114: 
  115: sub end_foilgroup {
  116:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  117:   my $result='';
  118:   my @whichopt;
  119:   if ($target eq 'web' || $target eq 'grade') {
  120:     my ($count,$max) = &getfoilcounts($parstack,$safeeval);
  121:     if ($count>$max) { $count=$max }
  122:     &Apache::lonxml::debug("Count is $count from $max");
  123:     @whichopt = &whichfoils($max);
  124:   }
  125:   if ($target eq 'web') {
  126:     $result=&displayfoils(@whichopt);
  127:   }
  128:   if ($target eq 'grade') {
  129:     if ( defined $ENV{'form.submitted'}) {
  130:       &gradefoils(@whichopt);
  131:     }
  132:   }
  133:   return $result;
  134: }
  135: 
  136: sub start_conceptgroup {
  137:   $Apache::imageresponse::conceptgroup=1;
  138:   %Apache::response::conceptgroup={};
  139:   return '';
  140: }
  141: 
  142: sub end_conceptgroup {
  143:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  144:   $Apache::imageresponse::conceptgroup=0;  
  145:   if ($target eq 'web' || $target eq 'grade') {
  146:     my @names = @{ $Apache::response::conceptgroup{'names'} };
  147:     my $pick=int(rand($#names+1));
  148:     my $name=$names[$pick];
  149:     push @{ $Apache::response::foilgroup{'names'} }, $name;
  150:     $Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
  151:     $Apache::response::foilgroup{"$name.image"} = $Apache::response::conceptgroup{"$name.image"};
  152:     push(@{ $Apache::response::foilgroup{"$name.area"} }, @{ $Apache::response::conceptgroup{"$name.area"} });
  153:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
  154:     $Apache::response::foilgroup{"$name.concept"} = $concept;
  155:     &Apache::lonxml::debug("Selecting $name in $concept");
  156:   }
  157:   return '';
  158: }
  159: 
  160: $Apache::imageresponse::curname='';
  161: sub start_foil {
  162:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  163:   if ($target eq 'web' || $target eq 'grade') {
  164:     my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  165:     if ($name eq '') { $name=$Apache::lonxml::curdepth; }
  166:     if ( $Apache::imageresponse::conceptgroup ) {
  167:       push(@{ $Apache::response::conceptgroup{'names'} }, $name);
  168:     } else {
  169:       push(@{ $Apache::response::foilgroup{'names'} }, $name);
  170:     }
  171:     $Apache::imageresponse::curname=$name;
  172:   }
  173:   return '';
  174: }
  175: 
  176: sub end_foil {
  177:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  178:   return '';
  179: }
  180: 
  181: sub start_text {
  182:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  183:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
  184:   return '';
  185: }
  186: 
  187: sub end_text {
  188:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  189:   if ($target eq 'web') {
  190:     my $name = $Apache::imageresponse::curname;
  191:     if ( $Apache::imageresponse::conceptgroup ) {
  192:       $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
  193:     } else {
  194:       $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
  195:     }
  196:   }
  197:   return '';
  198: }
  199: 
  200: sub start_image {
  201:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  202:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
  203:   return '';
  204: }
  205: 
  206: sub end_image {
  207:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  208:   if ($target eq 'web') {
  209:     my $name = $Apache::imageresponse::curname;
  210:     my $image = &Apache::lonxml::endredirection;
  211:     &Apache::lonxml::debug("out is $image");
  212:     if ( $Apache::imageresponse::conceptgroup ) {
  213:       $Apache::response::conceptgroup{"$name.image"} = $image;
  214:     } else {
  215:       $Apache::response::foilgroup{"$name.image"} = $image;
  216:     }
  217:   }
  218:   return '';
  219: }
  220: 
  221: sub start_rectangle {
  222:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  223:   if ($target eq 'web' || $target eq 'grade') { &Apache::lonxml::startredirection; }
  224:   return '';
  225: }
  226: 
  227: sub grade_rectangle {
  228:   my ($spec,$x,$y) = @_;
  229:   &Apache::lonxml::debug("Spec is $spec");
  230:   $spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/;
  231:   my $x1=$1;
  232:   my $y1=$2;
  233:   my $x2=$3;
  234:   my $y2=$4;
  235:   &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
  236:   if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
  237:   if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
  238:   if ($x => $x1) { if ($x <= $x2) { if ($y => $y1) { if ($y <= $y2) { return 'APPROX_ANS'; } } } }
  239:   return 'INCORRECT';
  240: }
  241: 
  242: sub end_rectangle {
  243:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  244:   if ($target eq 'web' || $target eq 'grade') {
  245:     my $name = $Apache::imageresponse::curname;
  246:     my $area = &Apache::lonxml::endredirection;
  247:     &Apache::lonxml::debug("out is $area for $name");
  248:     if ( $Apache::imageresponse::conceptgroup ) {
  249:       push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
  250:     } else {
  251:       push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
  252:     }
  253:   }
  254:   return '';
  255: }
  256: 1;
  257: __END__
  258:  

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