You are here

function quiz_start_check in Quiz 7.6

Same name and namespace in other branches
  1. 8.4 quiz.module \quiz_start_check()
  2. 6.4 quiz.module \quiz_start_check()
  3. 7 quiz.module \quiz_start_check()
  4. 7.4 quiz.module \quiz_start_check()

Actions to take place at the start of a quiz.

This is called when the quiz node is viewed for the first time. It ensures that the quiz can be taken at this time.

Parameters

$quiz: The quiz node.

Return value

Return quiz_node_results result_id, or FALSE if there is an error.

Related topics

1 call to quiz_start_check()
quiz_take_page in ./quiz.module
Page to either resume a quiz or display a start quiz form.

File

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

Code

function quiz_start_check($quiz) {
  global $user;
  $user_is_admin = node_access('update', $quiz);

  // Make sure this is available.
  if ($quiz->quiz_always != 1) {

    // Compare current GMT time to the open and close dates (which should still
    // be in GMT time).
    $now = REQUEST_TIME;
    if ($now >= $quiz->quiz_close || $now < $quiz->quiz_open) {
      if ($user_is_admin) {
        drupal_set_message(t('You are marked as an administrator or owner for this @quiz. While you can take this @quiz, the open/close times prohibit other users from taking this @quiz.', array(
          '@quiz' => QUIZ_NAME,
        )), 'status');
      }
      else {
        drupal_set_message(t('This @quiz is not currently available.', array(
          '@quiz' => QUIZ_NAME,
        )), 'status');

        // Can't take quiz.
        return FALSE;
      }
    }
  }

  // Check to see if this user is allowed to take the quiz again:
  if ($quiz->takes > 0) {
    $taken = db_query("SELECT COUNT(*) AS takes FROM {quiz_node_results} WHERE uid = :uid AND nid = :nid", array(
      ':uid' => $user->uid,
      ':nid' => $quiz->nid,
    ))
      ->fetchField();
    $allowed_times = format_plural($quiz->takes, '1 time', '@count times');
    $taken_times = format_plural($taken, '1 time', '@count times');

    // The user has already taken this quiz.
    if ($taken) {
      if ($user_is_admin) {
        drupal_set_message(t('You have taken this @quiz already. You are marked as an owner or administrator for this quiz, so you can take this quiz as many times as you would like.', array(
          '@quiz' => QUIZ_NAME,
        )), 'status');
      }
      elseif ($taken >= $quiz->takes) {
        drupal_set_message(t('You have already taken this @quiz @really. You may not take it again.', array(
          '@quiz',
          QUIZ_NAME,
          '@really' => $taken_times,
        )), 'error');
        return FALSE;
      }
      elseif ($quiz->show_attempt_stats) {
        drupal_set_message(t("You can only take this @quiz @allowed. You have taken it @really.", array(
          '@quiz' => QUIZ_NAME,
          '@allowed' => $allowed_times,
          '@really' => $taken_times,
        )), 'status');
      }
    }
  }

  // Check to see if the user is registered, and user alredy passed this quiz.
  if ($quiz->show_passed && $user->uid && quiz_is_passed($user->uid, $quiz->nid, $quiz->vid)) {
    drupal_set_message(t('You have already passed this @quiz.', array(
      '@quiz' => QUIZ_NAME,
    )), 'status', FALSE);
  }
  return TRUE;
}