You are here

function quiz_take_quiz in Quiz 5.2

Same name and namespace in other branches
  1. 8.4 quiz.module \quiz_take_quiz()
  2. 5 quiz.module \quiz_take_quiz()
  3. 6.6 quiz.module \quiz_take_quiz()
  4. 6.2 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.

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 875

Code

function quiz_take_quiz() {
  global $user;
  if (arg(0) == 'node' && is_numeric(arg(1)) && user_access('access quiz')) {
    if ($quiz = node_load(arg(1))) {

      // If anonymous user and no unique hash, refresh with a unique string to prevent caching.
      if (!$user->uid && arg(4) == NULL) {
        drupal_goto('node/' . $quiz->nid . '/quiz/start/' . md5(mt_rand() . time()));
      }
      if ($user->uid && $quiz->takes != 0) {
        $times = db_num_rows(db_query("SELECT result_id " . "FROM {quiz_node_results} " . "WHERE nid = %d AND vid = %d AND uid = %d", $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 '';
        }
      }
      if ($_POST['op'] == t('Back')) {
        unset($_POST['tries']);
        array_unshift($_SESSION['quiz_' . $quiz->nid]['quiz_questions'], $_SESSION['quiz_' . $quiz->nid]['previous_quiz_questions']);
      }

      // 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']);
          $_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'],
          ));
          $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']);
        return $output;
      }
      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;
      }
    }
  }

  // If we got this far, the quiz does not exist.
  drupal_not_found();
}