Permalink
Browse files

A better fix for bug 3520

  • Loading branch information...
1 parent 1ab72c8 commit a26a0e25dd52678d278d5c31399d7c054112e7bc gustav_delius committed Jul 8, 2005
Showing with 17 additions and 24 deletions.
  1. +17 −24 mod/quiz/questiontypes/match/questiontype.php
@@ -105,19 +105,15 @@ function create_session_and_responses(&$question, &$state, $quiz, $attempt) {
$answer->fraction = 1.0;
$state->options->subquestions[$key]->options
->answers[$subquestion->id] = clone($answer);
+
+ $state->responses[$key] = '';
}
// Shuffle the answers if required
- $subquestionids = array_values(array_map(create_function('$val',
- 'return $val->id;'), $state->options->subquestions));
if ($quiz->shuffleanswers) {
- $subquestionids = swapshuffle($subquestionids);
- }
- $state->options->order = $subquestionids;
- // Create empty responses
- foreach ($subquestionids as $val) {
- $state->responses[$val] = '';
+ $state->options->subquestions = swapshuffle_assoc($state->options->subquestions);
}
+
return true;
}
@@ -129,20 +125,20 @@ function restore_session_and_responses(&$question, &$state) {
$responses = array_map(create_function('$val',
'return explode("-", $val);'), $responses);
- // Restore the previous responses
- $state->responses = array();
- if ($responses) {
- foreach ($responses as $response) {
- $state->responses[$response[0]] = $response[1];
- }
- }
-
- if (!$state->options->subquestions = get_records('quiz_match_sub',
+ if (!$questions = get_records('quiz_match_sub',
'question', $question->id)) {
notify('Error: Missing subquestions!');
return false;
}
+ // Restore the previous responses and place the questions into the state options
+ $state->responses = array();
+ $state->options->subquestions = array();
+ foreach ($responses as $response) {
+ $state->responses[$response[0]] = $response[1];
+ $state->options->subquestions[$response[0]] = $questions[$response[0]];
+ }
+
foreach ($state->options->subquestions as $key => $subquestion) {
// This seems rather over complicated, but it is useful for the
// randomsamatch questiontype, which can then inherit the print
@@ -162,10 +158,8 @@ function restore_session_and_responses(&$question, &$state) {
function save_session_and_responses(&$question, &$state) {
// Serialize responses
$responses = array();
- foreach ($state->responses as $key => $val) {
- if ($key != '') {
- $responses[] = "$key-$val";
- }
+ foreach ($state->options->subquestions as $key => $subquestion) {
+ $responses[] = $key.'-'.($state->responses[$key] ? $state->responses[$key] : 0);
}
$responses = implode(',', $responses);
@@ -215,8 +209,7 @@ function print_question_formulation_and_controls(&$question, &$state, $quiz, $op
///// Print the input controls //////
echo '<table border="0" cellpadding="10" align="right">';
- foreach ($state->options->order as $key) {
- $subquestion = $subquestions[$key];
+ foreach ($subquestions as $key => $subquestion) {
/// Subquestion text:
echo '<tr><td align="left" valign="top">';
@@ -268,7 +261,7 @@ function grade_responses(&$question, &$state, $quiz) {
$sumgrade = 0;
foreach ($subquestions as $key => $sub) {
- if (isset($sub->options->answers[$responses[$key]])) {
+ if (isset($responses[$key]) and isset($sub->options->answers[$responses[$key]])) {
$sumgrade += $sub->options->answers[$responses[$key]]->fraction;
}
}

0 comments on commit a26a0e2

Please sign in to comment.