I have a database-driven quiz with ten questions. Each question is followed by four possible answers users can click, except for the second question (fill-in-the-blank) and Question #10, which is a series of check boxes. The first three check boxes are the correct answers, while the last three are incorrect.
The following answer key does a great job of scoring the quiz according to the number of right and wrong checkboxes a user chooses. For example, if you choose the three correct answers, you get 100%, but if you also choose one incorrect answer, your score drops to 97% (assuming you got the other nine questions right).
if (isset($_POST)):
$totalCorrect = 0;
$answers = array(1 => 'A', 2 => 'Jupiter', 3 => 'C', 4 => 'D', 5 => 'A', 6 => 'C', 7 => 'C', 8 => 'C', 9 => 'B', 10 => array('A','B','C'));
foreach ($answers as $num => $answer):
$question = 'q'.$num;
if(is_array($answer) && isset($_POST[$question])){
$ans_cnt = count($answer);
$ans_value = (1 / $ans_cnt);
$post_cnt = count($_POST[$question]);
//find matches between answer array and post array
$matches = array_intersect($answer,$_POST[$question]);
$good_answers = count($matches);
//Get bad answer count, which be be subtracted from overall score
$bad_answers = 0;
foreach($_POST[$question] as $post_answer):
if(!in_array($post_answer,$answer)):
$bad_answers++;
endif;
endforeach;
//Result of good answers minus bad answers
// $result = $good_answers - $bad_answers;
if($good_answers > $bad_answers){
$result = $good_answers - $bad_answers;
}else{
$result = 0;
}
if(($post_cnt != $ans_cnt) || ($post_cnt == $ans_cnt && $ans_cnt != count($matches))){
$result = $result * $ans_value;
$totalCorrect = $totalCorrect + $result;
}else{
$totalCorrect++;
}
}elseif(isset($_POST[$question]) && strtolower($_POST[$question]) === strtolower($answer)){
$totalCorrect++;
}
endforeach;
$pct = round( (($totalCorrect/count($answers)) * 100), 0);
echo $totalCorrect.' correct ('.$pct.'%)';
endif;
Can anyone tell me if there's a way to modify this script so that the answer is either interpreted as right or wrong, with no shades of gray? The only way to get it right is to select the three correct answers and none of the others. If you choose just two of the correct answers, it's wrong.
Here's the tricky part: Some quizzes will have multiple checkbox questions, and the number of right and wrong choices will vary. For example, there could be five checkboxes, with just one of them the correct answer.
Here's a sample of the HTML for a regular multiple-choice question, followed by one with several radio checkboxes.
<li id="q9">
<div class="Question">Bananas are:</div>
<div class="Answer">
<label class="Wide" for="q9-A"><div class="Radio"><input type="radio" name="q9" id="q9-A" value="A" style="display: none;"> A. red</div></label></div>
<div class="Answer">
<label class="Wide" for="q9-B"><div class="Radio"><input type="radio" name="q9" id="q9-B" value="B" style="display: none;"> B. yellow</div></label></div>
<div class="Answer">
<label class="Wide" for="q9-C"><div class="Radio"><input type="radio" name="q9" id="q9-C" value="C" style="display: none;"> C. blue</div></label></div>
<div class="Answer">
<label class="Wide" for="q9-D"><div class="Radio"><input type="radio" name="q9" id="q9-D" value="D" style="display: none;"> D. pink</div></label></div>
</li>
<li id="q10">
<div class="Question">Check each item that is the name of a fish.</div>
<div class="Answer" style="margin-top: 5px; background: #000; color: #fff; text-align: center;">
<label for="q10-A"><input type="checkbox" name="q10[]" id="q10-A" value="A">bass</label>
<label for="q10-B"><input type="checkbox" name="q10[]" id="q10-B" value="B">catfish</label>
<label for="q10-C"><input type="checkbox" name="q10[]" id="q10-C" value="C">trout</label>
<label for="q10-D"><input type="checkbox" name="q10[]" id="q10-D" value="D">rabbit</label>
<label for="q10-E"><input type="checkbox" name="q10[]" id="q10-E" value="E">fox</label>
<label for="q10-F"><input type="checkbox" name="q10[]" id="q10-F" value="F">deer</label>
</div>
</li>
Aucun commentaire:
Enregistrer un commentaire