You are here

function quiz_start_actions in Quiz 6.2

Same name and namespace in other branches
  1. 5.2 quiz.module \quiz_start_actions()
  2. 5 quiz.module \quiz_start_actions()
  3. 6.6 quiz.module \quiz_start_actions()
  4. 6.3 quiz.module \quiz_start_actions()
  5. 6.5 quiz.module \quiz_start_actions()

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

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

1 call to quiz_start_actions()
quiz_take_quiz in ./quiz.module
Handles quiz taking.

File

./quiz.module, line 996
Quiz Module

Code

function quiz_start_actions($quiz) {
  global $user;
  $user_is_admin = user_access('create 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 = gmmktime();
    if ($now >= $quiz->quiz_close || $now < $quiz->quiz_open) {
      drupal_set_message(t('This @quiz is not currently available.', array(
        '@quiz' => QUIZ_NAME,
      )), 'status');
      if (!$user_is_admin) {

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

  // Check to see if this user is allowed to take the quiz again:
  if ($quiz->takes > 0) {
    $query = "SELECT COUNT(*) AS takes FROM {quiz_node_results} WHERE uid = %s AND nid = %s AND vid = %s";
    $taken = db_result(db_query($query, $user->uid, $quiz->nid, $quiz->vid));
    $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 (nid/vid combo).
    if ($taken) {

      // If the user has already taken this quiz too many times, stop the user.
      if ($taken >= $quiz->takes) {
        drupal_set_message(t('You have already taken this quiz @really. You may not take it again.', array(
          '@really' => $taken_times,
        )), 'error');
        if (!$user_is_admin) {
          return FALSE;
        }
      }
      else {
        if (variable_get('quiz_show_allowed_times', TRUE)) {
          drupal_set_message(t("You can only take this quiz @allowed. You have taken it @really.", array(
            '@allowed' => $allowed_times,
            '@really' => $taken_times,
          )), 'status');
        }
      }
    }
  }

  // Check to see if the (a) user is registered, and (b) user alredy passed this quiz.
  if ($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');
  }

  // On error, we want to return before here to avoid creating an empty entry in quiz_node_results.
  // Otherwise, we get fairly clutter Quiz Results.
  // Insert quiz_node_results record.
  $result = db_query("INSERT INTO {quiz_node_results} (result_id, nid, vid, uid, time_start) VALUES (%d, %d, %d, %d, %d)", $rid, $quiz->nid, $quiz->vid, $user->uid, time());
  if ($result) {

    // Return the last RID.
    return db_last_insert_id('quiz_node_results', 'result_id');
  }
  else {
    drupal_set_message(t('There was a problem starting the @quiz. Please try again later.', array(
      '@quiz' => QUIZ_NAME,
    ), 'error'));
    return FALSE;
  }
}