File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.8: download - view: text, annotated - select for diffs
Mon Jan 15 21:44:21 2001 UTC (23 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- look for the hidden submission "submitted" rather then the buttonpress

    1: # The LearningOnline Network with CAPA
    2: # mutliple choice style responses
    3: 
    4: # 11/23,11/24,11/28 Gerd Kortemeyer
    5: 
    6: package Apache::radiobuttonresponse;
    7: use strict;
    8: 
    9: sub BEGIN {
   10:   &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
   11: }
   12: 
   13: sub start_radiobuttonresponse {
   14:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
   15:   #when in a radiobutton response use these
   16:   &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil'));
   17:   my $id = &Apache::response::start_response($parstack,$safeeval);
   18:   return '';
   19: }
   20: 
   21: sub end_radiobuttonresponse {
   22:   &Apache::response::end_response;
   23:   return '';
   24: }
   25: 
   26: %Apache::response::foilgroup={};
   27: sub start_foilgroup {
   28:   %Apache::response::foilgroup={};
   29:   return '';
   30: }
   31: 
   32: sub setrandomnumber {
   33:   my $rndseed=&Apache::lonnet::rndseed();
   34:   $rndseed=unpack("%32i",$rndseed);
   35:   $rndseed=$rndseed
   36:     +&Apache::lonnet::numval($Apache::inputtags::part)
   37:       +&Apache::lonnet::numval($Apache::inputtags::response['-1']);
   38:   srand($rndseed);
   39:   return '';
   40: }
   41: 
   42: #FIXME needs to stablely do random picks
   43: sub end_foilgroup {
   44:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
   45:   
   46:   my $result;
   47:   if ($target ne 'meta') {
   48:     my $name;
   49:     &setrandomnumber();
   50:     my ($truecnt,$falsecnt,$max) = &getfoilcounts($parstack,$safeeval);
   51:     my $count=0;
   52:     # we will add in 1 of the true statements
   53:     if (($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; }
   54:     my $answer = int(rand ($count));
   55:     &Apache::lonxml::debug("Answer is $answer, $count from $max, $falsecnt");
   56:     if ($target eq 'web') {
   57:       $result=&displayfoils($max,$answer);
   58:     } elsif ( $target eq 'grade') {
   59:       if ( defined $ENV{'form.submitted'}) {
   60: 	my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
   61: 	if ( $response =~ /[^\s]/) {
   62: 	  my $id = $Apache::inputtags::response['-1'];
   63: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response;
   64: 	  &Apache::lonxml::debug("submitted a $response<br>\n");
   65: 	  if ($response == $answer) {
   66: 	    $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
   67: 	  } else {
   68: 	    $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
   69: 	  }
   70: 	}
   71:       }
   72:     }
   73:   }
   74:   return $result;
   75: }
   76: 
   77: sub getfoilcounts {
   78:   my ($parstack,$safeeval)=@_;
   79:   my $rrargs ='';
   80:   if ( $#$parstack > 0 ) { $rrargs=$$parstack['-2']; }
   81:   my $max = &Apache::run::run("{$rrargs;".'return $max}',$safeeval);
   82:   my @names = @{ $Apache::response::foilgroup{'names'} };
   83:   my $truecnt=0;
   84:   my $falsecnt=0;
   85:   my $name;
   86: 
   87:   foreach $name (@names) {
   88:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
   89:       $truecnt++;
   90:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
   91:       $falsecnt++;
   92:     }
   93:   }
   94:   return ($truecnt,$falsecnt,$max);
   95: }
   96: 
   97: sub displayfoils {
   98:   my ($max,$answer)=@_;
   99:   my @names = @{ $Apache::response::foilgroup{'names'} };
  100:   my @truelist;
  101:   my @falselist;
  102:   my $result;
  103:   my $name;
  104: 
  105:   foreach $name (@names) {
  106:     #result.="<br><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
  107:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  108:       push (@truelist,$name);
  109:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  110:       push (@falselist,$name);
  111:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
  112:     } else {
  113:       &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
  114:     }
  115:   }
  116:   my $whichtrue = rand $#truelist;
  117:   &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
  118:   my @whichfalse =();
  119:   while ((($#whichfalse+2) < $max) && ($#falselist > -1)) {
  120:     my $afalse=rand $#falselist;
  121:     &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
  122:     $afalse=splice(@falselist,$afalse,1);
  123:     &Apache::lonxml::debug("Picked $afalse");
  124:     push (@whichfalse,$afalse);
  125:   }
  126:   splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
  127:   my $temp=0;
  128:   &Apache::lonxml::debug("the true statement is $answer");
  129:   foreach $name (@whichfalse) {
  130:     $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\">".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  131:     $temp++;
  132:   }
  133:   return $result."<br />";
  134: }
  135: 
  136: sub start_foil {
  137:   $Apache::lonxml::redirection--;
  138:   return '';
  139: }
  140: 
  141: sub end_foil {
  142:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  143:   if ($target eq 'web' || $target eq 'grade') {
  144:     my $args ='';
  145:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  146:     my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
  147:     push @{ $Apache::response::foilgroup{'names'} }, $name;
  148:     my $value = &Apache::run::run("{$args;".'return $value}',$safeeval);
  149:     $Apache::response::foilgroup{"$name.value"} = $value;
  150:     $Apache::response::foilgroup{"$name.text"} = $Apache::lonxml::outputstack;
  151:   }
  152:   
  153:   $Apache::lonxml::redirection++;
  154:   if ($Apache::lonxml::redirection == 1) { 
  155:     $Apache::lonxml::outputstack=''; 
  156:   }
  157:   return '';
  158: }
  159: 
  160: 1;
  161: __END__
  162:  

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