You are here

function quiz_take_quiz in Quiz 6.2

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.3 quiz.module \quiz_take_quiz()
  6. 6.4 quiz.module \quiz_take_quiz()
  7. 6.5 quiz.module \quiz_take_quiz()
  8. 7.6 quiz.module \quiz_take_quiz()
  9. 7 quiz.module \quiz_take_quiz()
  10. 7.4 quiz.module \quiz_take_quiz()
  11. 7.5 quiz.module \quiz_take_quiz()

Handles quiz taking.

This gets executed when the main quiz node is first loaded.

Parameters

$quiz: The quiz node.

Return value

HTML output for page.

1 call to quiz_take_quiz()
quiz_view in ./quiz.module
Implementation of hook_view().

File

./quiz.module, line 737
Quiz Module

Code

function quiz_take_quiz($quiz) {

  // If no access, fail.
  if (!user_access('access quiz')) {
    drupal_access_denied();
    return;
  }
  if (!isset($quiz)) {
    drupal_not_found();
    return;
  }

  // If anonymous user and no unique hash, refresh with a unique string to prevent caching.
  if (!$quiz->name && arg(4) == NULL) {
    drupal_goto('node/' . $quiz->nid . '/quiz/start/' . md5(mt_rand() . time()));
  }

  /* Moved to quiz_start_actions()
    if ($user->uid && $quiz->takes != 0) {
      $query = "SELECT COUNT(result_id) FROM {quiz_node_results} WHERE nid = %d AND vid = %d AND uid = %d";
      $times = db_result(db_query($query, $quiz->nid, $quiz->vid, $user->uid));
      if ($times >= $quiz->takes) {
        drupal_set_message(t('You have already taken this @quiz %d times.', array('@quiz' => QUIZ_NAME, '%d' => $times)), 'status');
        return;
      }
    }
    */
  if (!isset($_SESSION['quiz_' . $quiz->nid]['quiz_questions'])) {

    // First time running through quiz.
    if ($rid = quiz_start_actions($quiz)) {

      // Create question list.
      $questions = quiz_build_question_list($quiz);
      if ($questions === FALSE) {
        drupal_set_message(t('Not enough random questions were found. Please !add more questions before trying to take this @quiz.', array(
          '@quiz' => QUIZ_NAME,
          '!add more questions' => l('add more questions', 'node/' . arg(1) . '/questions'),
        )), 'error');
        return '';
      }
      if (count($questions) == 0) {
        drupal_set_message(t('No questions were found. Please !assign questions before trying to take this @quiz.', array(
          '@quiz' => QUIZ_NAME,
          '!assign questions' => l('assign questions', 'node/' . arg(1) . '/questions'),
        )), 'error');
        return '';
      }

      // Initialize session variables.
      $_SESSION['quiz_' . $quiz->nid]['quiz_questions'] = $questions;
      $_SESSION['quiz_' . $quiz->nid]['result_id'] = $rid;
      $_SESSION['quiz_' . $quiz->nid]['question_number'] = 0;
    }
    else {
      return '';
    }
  }

  // Navigate backwards
  if ($_POST['op'] == t('Back')) {
    unset($_POST['tries']);

    // We maintain two lists -- previous questions and upcomming questions.
    // When we go backward, we pop one from the previous and prepend it to
    // the upcomming.
    // TODO: This can be maintained more efficiently with a single array of
    // all questions and then a pointer to the current question. That makes
    // rewinding much easier.
    $quiz_id = 'quiz_' . $quiz->nid;
    $last_q = array_pop($_SESSION[$quiz_id]['previous_quiz_questions']);
    array_unshift($_SESSION[$quiz_id]['quiz_questions'], $last_q);
  }

  // Check for answer submission.
  if ($_POST['op'] == t('Submit')) {
    if (!isset($_POST['tries'])) {
      drupal_set_message(t('You must select an answer before you can progress to the next question!'), 'error');
    }
    else {

      //unset($_SESSION['quiz_'. $quiz->nid]['previous_quiz_questions']);

      // Previous quiz questions: Questions that have been asked already. We save a record of all of them
      // so that a user can navigate backward all the way to the beginning of the quiz.
      $_SESSION['quiz_' . $quiz->nid]['previous_quiz_questions'][] = $_SESSION['quiz_' . $quiz->nid]['quiz_questions'][0];
      $former_question_array = array_shift($_SESSION['quiz_' . $quiz->nid]['quiz_questions']);
      $former_question = node_load(array(
        'nid' => $former_question_array['nid'],
      ));

      // Call hook_evaluate_question
      $result = module_invoke($former_question->type, 'evaluate_question', $former_question, $_SESSION['quiz_' . $quiz->nid]['result_id']);
      quiz_store_question_result($former_question_array['nid'], $former_question_array['vid'], $_SESSION['quiz_' . $quiz->nid]['result_id'], $result);

      // Stash feedback in the session, since the $_POST gets cleared.
      if ($quiz->feedback_time == QUIZ_FEEDBACK_QUESTION) {
        $report = module_invoke($former_question->type, 'get_report', $former_question_array['nid'], $former_question_array['vid'], $_SESSION['quiz_' . $quiz->nid]['result_id']);
        $_SESSION['quiz_' . $quiz->nid]['feedback'] = rawurlencode(quiz_get_feedback($quiz, $report));
      }

      // If anonymous user, refresh url with unique hash to prevent caching.
      if (!$user->uid) {
        drupal_goto('node/' . $quiz->nid . '/quiz/start/' . md5(mt_rand() . time()));
      }
    }
  }

  // If we had feedback from the last question.
  if (isset($_SESSION['quiz_' . $quiz->nid]['feedback']) && $quiz->feedback_time == QUIZ_FEEDBACK_QUESTION) {
    $output .= rawurldecode($_SESSION['quiz_' . $quiz->nid]['feedback']);
  }

  // If this quiz is in progress, load the next questions and return it via the theme.
  if (!empty($_SESSION['quiz_' . $quiz->nid]['quiz_questions'])) {
    $question_node = node_load(array(
      'nid' => $_SESSION['quiz_' . $quiz->nid]['quiz_questions'][0]['nid'],
    ));
    $output .= theme('quiz_take_question', $quiz, $question_node);
    unset($_SESSION['quiz_' . $quiz->nid]['feedback']);
  }
  else {
    $score = quiz_end_actions($quiz, $_SESSION['quiz_' . $quiz->nid]['result_id']);
    if ($quiz->feedback_time == QUIZ_FEEDBACK_NEVER) {
      $output = theme('quiz_no_feedback');
    }
    else {

      // Get the results and summary text for this quiz.
      $questions = _quiz_get_answers($_SESSION['quiz_' . $quiz->nid]['result_id']);
      $summary = _quiz_get_summary_text($quiz, $score);

      // Get the themed summary page.
      $output .= theme('quiz_take_summary', $quiz, $questions, $score, $summary);
    }

    // Remove session variables.
    unset($_SESSION['quiz_' . $quiz->nid]);
  }
  return $output;
}