Annotation of loncom/homework/radiobuttonresponse.pm, revision 1.8

1.1       albertel    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 {
1.3       albertel   10:   &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
1.1       albertel   11: }
                     12: 
                     13: sub start_radiobuttonresponse {
1.4       albertel   14:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.3       albertel   15:   #when in a radiobutton response use these
                     16:   &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil'));
1.4       albertel   17:   my $id = &Apache::response::start_response($parstack,$safeeval);
                     18:   return '';
1.1       albertel   19: }
                     20: 
                     21: sub end_radiobuttonresponse {
1.4       albertel   22:   &Apache::response::end_response;
                     23:   return '';
1.1       albertel   24: }
                     25: 
                     26: %Apache::response::foilgroup={};
                     27: sub start_foilgroup {
                     28:   %Apache::response::foilgroup={};
1.6       albertel   29:   return '';
1.1       albertel   30: }
                     31: 
1.5       albertel   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: 
1.4       albertel   42: #FIXME needs to stablely do random picks
1.1       albertel   43: sub end_foilgroup {
                     44:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.7       albertel   45:   
1.1       albertel   46:   my $result;
1.7       albertel   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') {
1.8     ! albertel   59:       if ( defined $ENV{'form.submitted'}) {
1.7       albertel   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: 	  }
1.5       albertel   70: 	}
                     71:       }
                     72:     }
                     73:   }
                     74:   return $result;
1.6       albertel   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);
1.5       albertel   95: }
                     96: 
                     97: sub displayfoils {
                     98:   my ($max,$answer)=@_;
1.3       albertel   99:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    100:   my @truelist;
                    101:   my @falselist;
1.5       albertel  102:   my $result;
                    103:   my $name;
                    104: 
1.3       albertel  105:   foreach $name (@names) {
1.5       albertel  106:     #result.="<br><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
1.3       albertel  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 {
1.5       albertel  113:       &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
1.3       albertel  114:     }
1.1       albertel  115:   }
1.3       albertel  116:   my $whichtrue = rand $#truelist;
1.4       albertel  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:   }
1.5       albertel  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++;
1.4       albertel  132:   }
1.5       albertel  133:   return $result."<br />";
1.1       albertel  134: }
                    135: 
                    136: sub start_foil {
1.4       albertel  137:   $Apache::lonxml::redirection--;
1.5       albertel  138:   return '';
1.1       albertel  139: }
                    140: 
                    141: sub end_foil {
                    142:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.3       albertel  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:   
1.4       albertel  153:   $Apache::lonxml::redirection++;
                    154:   if ($Apache::lonxml::redirection == 1) { 
                    155:     $Apache::lonxml::outputstack=''; 
                    156:   }
1.1       albertel  157:   return '';
                    158: }
                    159: 
                    160: 1;
                    161: __END__
                    162:  

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