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

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

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