![]() ![]() | ![]() |
- fixed auto-checking of first radio button
1: # The LearningOnline Network with CAPA 2: # mutliple choice style responses 3: # 2/21 Guy 4: 5: package Apache::radiobuttonresponse; 6: use strict; 7: 8: sub BEGIN { 9: &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse')); 10: } 11: 12: sub start_radiobuttonresponse { 13: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_; 14: #when in a radiobutton response use these 15: &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil')); 16: my $id = &Apache::response::start_response($parstack,$safeeval); 17: return ''; 18: } 19: 20: sub end_radiobuttonresponse { 21: &Apache::response::end_response; 22: return ''; 23: } 24: 25: %Apache::response::foilgroup={}; 26: sub start_foilgroup { 27: %Apache::response::foilgroup={}; 28: $Apache::optionresponse::conceptgroup=0; 29: &setrandomnumber(); 30: return ''; 31: } 32: 33: sub setrandomnumber { 34: my $rndseed=&Apache::lonnet::rndseed(); 35: $rndseed=unpack("%32i",$rndseed); 36: $rndseed=$rndseed 37: +&Apache::lonnet::numval($Apache::inputtags::part) 38: +&Apache::lonnet::numval($Apache::inputtags::response['-1']); 39: srand($rndseed); 40: return ''; 41: } 42: 43: sub storesurvey { 44: if ( defined $ENV{'form.submitted'}) { 45: my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']}; 46: &Apache::lonxml::debug("Here I am!:$response:"); 47: if ( $response =~ /[^\s]/) { 48: my $id = $Apache::inputtags::response['-1']; 49: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response; 50: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='SUBMITTED'; 51: &Apache::lonxml::debug("submitted a $response<br />\n"); 52: } 53: } 54: return ''; 55: } 56: 57: sub end_foilgroup { 58: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_; 59: 60: my $result; 61: if ($target ne 'meta') { 62: my $rrargs =''; 63: if ( $#$parstack > 0 ) { $rrargs=$$parstack['-2']; } 64: my $style = &Apache::run::run("{$rrargs;".'return $style}',$safeeval); 65: if ( $style eq 'survey' ) { 66: if ($target eq 'web') { 67: $result=&displayallfoils(); 68: } elsif ( $target eq 'grade' ) { 69: $result=&storesurvey(); 70: } 71: } else { 72: my $name; 73: my ($truecnt,$falsecnt,$max) = &getfoilcounts($parstack,$safeeval); 74: my $count=0; 75: # we will add in 1 of the true statements 76: if (($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; } 77: my $answer = int(rand ($count)); 78: &Apache::lonxml::debug("Answer is $answer, $count from $max, $falsecnt"); 79: if ($target eq 'web') { 80: $result=&displayfoils($max,$answer); 81: } elsif ( $target eq 'grade') { 82: if ( defined $ENV{'form.submitted'}) { 83: my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']}; 84: if ( $response =~ /[^\s]/) { 85: my $id = $Apache::inputtags::response['-1']; 86: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response; 87: &Apache::lonxml::debug("submitted a $response<br />\n"); 88: if ($response == $answer) { 89: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS'; 90: } else { 91: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT'; 92: } 93: } 94: } 95: } 96: } 97: } 98: return $result; 99: } 100: 101: sub getfoilcounts { 102: my ($parstack,$safeeval)=@_; 103: my $rrargs =''; 104: if ( $#$parstack > 0 ) { $rrargs=$$parstack['-2']; } 105: my $max = &Apache::run::run("{$rrargs;".'return $max}',$safeeval); 106: my @names = @{ $Apache::response::foilgroup{'names'} }; 107: my $truecnt=0; 108: my $falsecnt=0; 109: my $name; 110: 111: foreach $name (@names) { 112: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 113: $truecnt++; 114: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') { 115: $falsecnt++; 116: } 117: } 118: return ($truecnt,$falsecnt,$max); 119: } 120: 121: sub displayallfoils { 122: my $result; 123: &Apache::lonxml::debug("survey style display"); 124: my @names = @{ $Apache::response::foilgroup{'names'} }; 125: my $temp=0; 126: my $id=$Apache::inputtags::response['-1']; 127: my $part=$Apache::inputtags::part; 128: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"}; 129: foreach my $name (@names) { 130: if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') { 131: $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" "; 132: if ($lastresponse eq $temp) { $result .= 'checked="on"'; } 133: $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n"; 134: $temp++; 135: } 136: } 137: return $result; 138: } 139: 140: sub displayfoils { 141: my ($max,$answer)=@_; 142: my @names = @{ $Apache::response::foilgroup{'names'} }; 143: my @truelist; 144: my @falselist; 145: my $result; 146: my $name; 147: 148: foreach $name (@names) { 149: #result.="<br /><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>"; 150: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 151: push (@truelist,$name); 152: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') { 153: push (@falselist,$name); 154: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') { 155: } else { 156: &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>"); 157: } 158: } 159: my $whichtrue = rand $#truelist; 160: &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue"); 161: my @whichfalse =(); 162: while ((($#whichfalse+1) < $max) && ($#falselist > -1)) { 163: my $afalse=rand $#falselist; 164: &Apache::lonxml::debug("From $#falselist elms, picking $afalse"); 165: $afalse=splice(@falselist,$afalse,1); 166: &Apache::lonxml::debug("Picked $afalse"); 167: push (@whichfalse,$afalse); 168: } 169: splice(@whichfalse,$answer,0,$truelist[$whichtrue]); 170: &Apache::lonxml::debug("the true statement is $answer"); 171: if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) { 172: foreach $name (@whichfalse) { 173: $result.="<br />"; 174: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 175: $result.='Correct'; 176: } else { 177: $result.='Incorrect'; 178: } 179: $result.=":".$Apache::response::foilgroup{$name.'.text'}."</input>\n"; 180: } 181: } else { 182: my $temp=0; 183: my $id=$Apache::inputtags::response['-1']; 184: my $part=$Apache::inputtags::part; 185: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"}; 186: foreach $name (@whichfalse) { 187: $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" "; 188: if ($lastresponse eq $temp) { $result .= 'checked="on"'; } 189: $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n"; 190: $temp++; 191: } 192: } 193: return $result."<br />"; 194: } 195: 196: sub start_conceptgroup { 197: $Apache::optionresponse::conceptgroup=1; 198: %Apache::response::conceptgroup={}; 199: return ''; 200: } 201: 202: sub end_conceptgroup { 203: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_; 204: $Apache::optionresponse::conceptgroup=0; 205: if ($target eq 'web' || $target eq 'grade') { 206: my @names = @{ $Apache::response::conceptgroup{'names'} }; 207: my $pick=int rand $#names+1; 208: my $name=$names[$pick]; 209: push @{ $Apache::response::foilgroup{'names'} }, $name; 210: $Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"}; 211: $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"}; 212: my $args; 213: if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; } 214: my $concept = &Apache::run::run("{$args;".'return $concept}',$safeeval); 215: $Apache::response::foilgroup{"$name.concept"} = $concept; 216: &Apache::lonxml::debug("Selecting $name in $concept"); 217: } 218: return ''; 219: } 220: 221: sub start_foil { 222: &Apache::lonxml::startredirection; 223: return ''; 224: } 225: 226: sub end_foil { 227: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_; 228: my $foil=&Apache::lonxml::endredirection; 229: if ($target eq 'web' || $target eq 'grade') { 230: my $args =''; 231: if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; } 232: my $name = &Apache::run::run("{$args;".'return $name}',$safeeval); 233: push @{ $Apache::response::foilgroup{'names'} }, $name; 234: my $value = &Apache::run::run("{$args;".'return $value}',$safeeval); 235: $Apache::response::foilgroup{"$name.value"} = $value; 236: $Apache::response::foilgroup{"$name.text"} = $foil; 237: } 238: return ''; 239: } 240: 241: 1; 242: __END__ 243: