You are here

function quiz_start_actions in Quiz 5.2

Same name and namespace in other branches
  1. 5 quiz.module \quiz_start_actions()
  2. 6.6 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.

Parameters

$uid: User ID

$nid: Quiz node ID

Return value

integer 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 1161

Code

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

  // Get the results.
  global $user;

  //$results = _quiz_get_results($quiz->nid, $user->uid);

  // Check to see if the user alredy passed this quiz,
  // but only perform this check if it is a registered user.
  if ($user->uid) {
    $passed = db_result(db_query("SELECT result_id " . "FROM {quiz_node_results} qnrs " . "INNER JOIN {quiz_node_properties} USING (vid, nid) " . "WHERE qnrs.vid = %d AND qnrs.nid = %d AND qnrs.uid =%d " . "AND score >= pass_rate", $quiz->vid, $quiz->nid, $user->uid));
    if ($passed) {
      drupal_set_message(t('You have already passed this @quiz.', array(
        '@quiz' => QUIZ_NAME,
      )), 'status');
    }
  }

  // Insert quiz_node_results record.
  $rid = db_next_id('{quiz_node_results}_result_id');
  $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 $rid;
  }
  else {
    drupal_set_message(t('There was a problem starting the @quiz. Please try again later.', array(
      '@quiz' => QUIZ_NAME,
    ), 'error'));
    return FALSE;
  }
}