You are here

function quiz_quiz_access in Quiz 7.5

Same name and namespace in other branches
  1. 8.6 quiz.module \quiz_quiz_access()
  2. 8.5 quiz.module \quiz_quiz_access()
  3. 6.x quiz.module \quiz_quiz_access()

Can a user take this quiz?

Related topics

File

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

Code

function quiz_quiz_access($op = 'take', $quiz, $account) {
  $hooks = array();
  $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).
    $quiz_open = REQUEST_TIME >= $quiz->quiz_open;
    $quiz_closed = REQUEST_TIME >= $quiz->quiz_close;
    if (!$quiz_open || $quiz_closed) {
      if ($user_is_admin) {
        $hooks['admin_ignore_date'] = array(
          'success' => TRUE,
          '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,
          )),
        );
      }
      else {
        if ($quiz_closed) {
          $hooks['closed'] = array(
            'success' => FALSE,
            'message' => t('This @quiz is closed.', array(
              '@quiz' => QUIZ_NAME,
            )),
          );
        }
        if (!$quiz_open) {
          $hooks['not_open'] = array(
            'success' => FALSE,
            'message' => t('This @quiz is not yet open.', array(
              '@quiz' => QUIZ_NAME,
            )),
          );
        }
      }
    }
  }

  // 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' => $account->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) {
        $hooks['owner_limit'] = array(
          'success' => TRUE,
          '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,
          )),
        );
      }
      elseif ($taken >= $quiz->takes) {
        if ($quiz->allow_resume && _quiz_active_result_id($account->uid, $quiz->nid, $quiz->vid)) {

          // Quiz is resumable and there is an active attempt, so we should
          // allow them to finish it as it won't be creating a new attempt. This
          // is the blocker, so we do nothing here. The resume handles in the
          // take function.
        }
        elseif (!isset($_SESSION['quiz'][$quiz->nid])) {

          // If result is in session, don't check the attempt limit. @todo would
          // be to split up "take" into something like "start" and "continue" an
          // attempt.
          $hooks['attempt_limit'] = array(
            'success' => FALSE,
            'message' => t('You have already taken this @quiz @really. You may not take it again.', array(
              '@quiz' => QUIZ_NAME,
              '@really' => $taken_times,
            )),
          );
        }
      }
      elseif ($quiz->show_attempt_stats) {
        $hooks['attempt_limit'] = array(
          'success' => TRUE,
          'message' => t("You can only take this @quiz @allowed. You have taken it @really.", array(
            '@quiz' => QUIZ_NAME,
            '@allowed' => $allowed_times,
            '@really' => $taken_times,
          )),
          'weight' => -10,
        );
      }
    }
  }

  // Check to see if the user is registered, and user alredy passed this quiz.
  if ($quiz->show_passed && $account->uid && quiz_is_passed($account->uid, $quiz->nid, $quiz->vid)) {
    $hooks['already_passed'] = array(
      'success' => TRUE,
      'message' => t('You have already passed this @quiz.', array(
        '@quiz' => QUIZ_NAME,
      )),
      'weight' => 10,
    );
  }

  // Check permission and node access.
  if (!user_access('access quiz') || !node_access('view', $quiz)) {
    $hooks['node_perm_access'] = array(
      'success' => FALSE,
      'message' => t('You are not allowed to take this @quiz.', array(
        '@quiz' => QUIZ_NAME,
      )),
    );
  }
  return $hooks;
}