You are here

function quiz_take_quiz in Quiz 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. 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 string reference to 'quiz_take_quiz'
quiz_menu in ./quiz.module
Implementation of hook_menu().

File

./quiz.module, line 611
Quiz Module

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 (!isset($_SESSION['quiz_' . $quiz->nid]['quiz_questions'])) {

        // First time running through quiz
        if ($rid = quiz_start_actions($user->uid, $quiz->nid)) {

          // Create question list
          $questions = quiz_build_question_list($quiz->nid);
          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,
            )), 'error');
            return '';
          }

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

      // 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 {
          $former_question = node_load(array(
            'nid' => array_shift($_SESSION['quiz_' . $quiz->nid]['quiz_questions']),
          ));
          $result = module_invoke($former_question->type, 'evaluate_question', $former_question->nid);
          db_query("REPLACE {quiz_question_results} VALUES(%d, %d, '%s')", $_SESSION['quiz_' . $quiz->nid]['rid'], $former_question->nid, serialize($result));
        }
      }

      // 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],
        ));
        return theme('quiz_take_question', $quiz, $question_node);
      }
      else {

        //First - update the result to show we have finished.
        $now = time();
        db_query("UPDATE {quiz_result} SET time_end = %d WHERE rid = %d", $now, $_SESSION['quiz_' . $quiz->nid]['rid']);

        //Get the results and summary text for this quiz
        $questions = _quiz_get_answers($_SESSION['quiz_' . $quiz->nid]['rid']);
        $score = quiz_calculate_score($_SESSION['quiz_' . $quiz->nid]['rid']);
        $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
        return $output;
      }
    }
  }

  // If we got down here then the quiz does not exist.
  drupal_not_found();
}