You are here

function quiz_start_actions in Quiz 6.6

Same name and namespace in other branches
  1. 5.2 quiz.module \quiz_start_actions()
  2. 5 quiz.module \quiz_start_actions()
  3. 6.2 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 1571
Quiz Module

Code

function quiz_start_actions($quiz) {
  global $user;
  $user_is_admin = user_access('administer quiz') || user_access('edit any quiz') || user_access('edit own quiz') && $quiz->uid == $user->uid;

  // 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) {
      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.'), '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) {
    $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 ($user_is_admin) {
        drupal_set_message('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.', 'status');
      }
      elseif ($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');
        return FALSE;
      }
      elseif (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 cluttered 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()
  //   );
  $result = db_query("INSERT INTO {quiz_node_results} (nid, vid, uid, time_start) VALUES (%d, %d, %d, %d)", $quiz->nid, $quiz->vid, $user->uid, time());
  if ($result) {

    // Return the last RID.
    $rid = 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;
  }

  // Call hook_quiz_begin().
  module_invoke_all('quiz_begin', $quiz, $rid);
  return $rid;
}