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

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

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