function _quiz_resume_existing_quiz in Quiz 7
Same name and namespace in other branches
- 8.4 quiz.module \_quiz_resume_existing_quiz()
- 6.6 quiz.module \_quiz_resume_existing_quiz()
- 6.3 quiz.module \_quiz_resume_existing_quiz()
- 6.4 quiz.module \_quiz_resume_existing_quiz()
- 6.5 quiz.module \_quiz_resume_existing_quiz()
- 7.4 quiz.module \_quiz_resume_existing_quiz()
Resume an in-progress quiz.
This sets the user's session back to the state it was in when the quiz was aborted.
This function should only be called if the quiz needs resuming. Outside logic needs to check that, though.
Parameters
$quiz: The current quiz(node).
$uid: The ID of the current user.
$rid: The result ID found for the current quiz.
1 call to _quiz_resume_existing_quiz()
- quiz_take_quiz in ./
quiz.module - Handles quiz taking.
File
- ./
quiz.module, line 3012 - Quiz Module
Code
function _quiz_resume_existing_quiz($quiz, $uid, $rid) {
// Create question list.
$questions = quiz_build_question_list($quiz);
$already_answered = array();
// Now we need to make sure to set previous questions to be correct.
// This includes corrections for cases where questions were shuffled.
$answered_questions = db_query('SELECT question_nid AS nid, question_vid AS vid, number
FROM {quiz_node_results_answers}
WHERE result_id = :rid
ORDER BY number, answer_timestamp', array(
':rid' => $rid,
));
//while ($answered = db_fetch_object($answered_questions)) {
foreach ($answered_questions as $answered) {
foreach ($questions as $question) {
if ($question['vid'] == $answered->vid) {
$already_answered[] = $answered->vid;
// we found it, no need to continue the foreach loop
continue;
}
}
// This question was answered, but wasn't in $questions, so we need
// to replace an unanswered question in $questions with it.
if (!in_array($answered->vid, $already_answered)) {
foreach ($questions as $key => $question) {
if (!in_array($question['vid'], $already_answered) && $question['random'] === TRUE) {
$questions[$key]['vid'] = $answered->vid;
$questions[$key]['nid'] = $answered->nid;
$questions[$key]['rid'] = $rid;
$already_answered[] = $answered->vid;
continue;
}
}
}
}
// Adding data to the session.
reset($questions);
$_SESSION['quiz_' . $quiz->nid]['previous_quiz_questions'] = array();
$_SESSION['quiz_' . $quiz->nid]['quiz_questions'] = array();
$next_number = count($already_answered) + 1;
foreach ($questions as $question) {
if (in_array($question['vid'], $already_answered)) {
$question['rid'] = $rid;
$_SESSION['quiz_' . $quiz->nid]['previous_quiz_questions'][] = $question;
}
else {
$question['number'] = $next_number;
$next_number++;
$_SESSION['quiz_' . $quiz->nid]['quiz_questions'][] = $question;
}
}
$_SESSION['quiz_' . $quiz->nid]['result_id'] = $rid;
$_SESSION['quiz_' . $quiz->nid]['question_number'] = count($_SESSION['quiz_' . $quiz->nid]['previous_quiz_questions']);
// Timed quizzes are likely to have expired by this point. But we let
// the quiz_take_quiz handler handle that.
$_SESSION['quiz_' . $quiz->nid]['question_start_time'] = REQUEST_TIME;
$_SESSION['quiz_' . $quiz->nid]['question_duration'] = $quiz->time_limit;
drupal_set_message(t('Resuming a previous quiz in-progress.'), 'status');
}