You are here

function quiz_take_quiz in Quiz 7.5

Same name and namespace in other branches
  1. 8.4 quiz.module \quiz_take_quiz()
  2. 5.2 quiz.module \quiz_take_quiz()
  3. 5 quiz.module \quiz_take_quiz()
  4. 6.6 quiz.module \quiz_take_quiz()
  5. 6.2 quiz.module \quiz_take_quiz()
  6. 6.3 quiz.module \quiz_take_quiz()
  7. 6.4 quiz.module \quiz_take_quiz()
  8. 6.5 quiz.module \quiz_take_quiz()
  9. 7.6 quiz.module \quiz_take_quiz()
  10. 7 quiz.module \quiz_take_quiz()
  11. 7.4 quiz.module \quiz_take_quiz()

Find a resumable result for this quiz and the current user.

Parameters

$quiz: The quiz node.

Return value

QuizResult Active quiz result.

Related topics

3 calls to quiz_take_quiz()
quiz_quiztake_pane_content_type_render in plugins/content_types/quiztake_pane.inc
Run-time rendering of the body of the block (content type).
quiz_take_page in ./quiz.module
Page to either resume a quiz or display a start quiz form.
quiz_view in ./quiz.module
Implements hook_view().

File

./quiz.module, line 1840
quiz.module Main file for the Quiz module.

Code

function quiz_take_quiz($quiz) {
  global $user;

  // Make sure we use the same revision of the quiz throughout the quiz taking
  // session.
  $result_id = !empty($_SESSION['quiz'][$quiz->nid]['result_id']) ? $_SESSION['quiz'][$quiz->nid]['result_id'] : NULL;
  if ($result_id && ($quiz_result = quiz_result_load($result_id))) {
    return $quiz_result;
  }
  else {

    // User doesn't have attempt in session. If we allow resuming we can load it
    // from the database.
    if ($quiz->allow_resume && !user_is_anonymous()) {
      if ($result_id = _quiz_active_result_id($user->uid, $quiz->nid, $quiz->vid)) {

        // Put the result in the user's session.
        $_SESSION['quiz'][$quiz->nid]['result_id'] = $result_id;
        $_SESSION['quiz'][$quiz->nid]['current'] = 1;

        // Now advance the user to after the last answered question.
        $quiz_result = quiz_result_load($result_id);

        // Layout has already been ordered.
        $prev = NULL;
        foreach ($quiz_result
          ->getLayout() as $question) {
          $qra = entity_load_single('quiz_result_answer', $question['result_answer_id']);
          if ($prev) {
            if (!$qra->answer_timestamp && $prev->answer_timestamp) {

              // This question has not been answered, but the previous
              // question has.
              $_SESSION['quiz'][$quiz->nid]['current'] = $question['number'];
            }
          }
          $prev = clone $qra;
        }

        // Resume a quiz from the database.
        $quiz_result->resume = 1;
        return $quiz_result;
      }
    }
  }
  return FALSE;
}