You are here

function quiz_quiz_finished in Quiz 7.4

Same name and namespace in other branches
  1. 6.6 quiz.module \quiz_quiz_finished()
  2. 6.3 quiz.module \quiz_quiz_finished()
  3. 6.4 quiz.module \quiz_quiz_finished()
  4. 6.5 quiz.module \quiz_quiz_finished()
  5. 7.6 quiz.module \quiz_quiz_finished()
  6. 7 quiz.module \quiz_quiz_finished()

Implements hook_quiz_finished().

Performs actions like sending quiz results over email at the end of quiz.

Related topics

File

./quiz.module, line 2567
Quiz Module

Code

function quiz_quiz_finished($quiz, $score, $session_data) {
  $rid = $session_data['result_id'];

  // Load data about the quiz taker
  $sql = 'SELECT u.uid, u.mail FROM {users} u JOIN {quiz_node_results} qnr ON u.uid = qnr.uid WHERE result_id = %d';
  $taker = db_query('SELECT u.uid, u.mail FROM {users} u JOIN {quiz_node_results} qnr ON u.uid = qnr.uid WHERE result_id = :result_id', array(
    ':result_id' => $rid,
  ))
    ->fetch();
  if (variable_get('quiz_results_to_quiz_author', 0)) {
    $author_mail = db_query('SELECT mail FROM {users} WHERE uid = :uid', array(
      ':uid' => $quiz->uid,
    ))
      ->fetchField();
    drupal_mail('quiz', 'notice', $author_mail, NULL, array(
      $quiz,
      $score,
      $rid,
      'author',
    ));
  }
  if (variable_get('quiz_email_results', 0) && variable_get('quiz_use_passfail', 1) && $taker->uid != 0 && $score['is_evaluated']) {
    drupal_mail('quiz', 'notice', $taker->mail, NULL, array(
      $quiz,
      $score,
      $rid,
      'taker',
    ));
    drupal_set_message(t('Your results have been sent to your e-mail address.'));
  }

  // Calls userpoints functions to credit user point based on number of correct
  // answers.
  if ($quiz->has_userpoints && $taker->uid != 0 && $score['is_evaluated']) {

    //Looking up the tid of the selected Userpoint vocabulary
    $selected_tid = db_query("SELECT tid FROM {taxonomy_index}\n                WHERE nid = :nid AND tid IN (\n                  SELECT tid\n                  FROM {taxonomy_term_data} t_t_d JOIN {taxonomy_vocabulary} t_v ON t_v.vid = t_t_d.vid\n                  WHERE t_t_d.vid = :vid\n                )", array(
      ':nid' => $quiz->nid,
      ':vid' => $quiz->vid,
      ':vid' => userpoints_get_vid(),
    ))
      ->fetchField();
    $variables = array(
      '@title' => $quiz->title,
      '@quiz' => variable_get('quiz_name', QUIZ_NAME),
      '@time' => date('l jS \\of F Y h:i:s A'),
    );
    $params = array(
      'points' => $score['numeric_score'],
      'description' => t('Attended @title @quiz on @time', $variables),
      'tid' => $selected_tid,
      'uid' => $taker->uid,
    );
    if ($quiz->userpoints_tid != 0) {
      $params['tid'] = $quiz->userpoints_tid;
    }
    userpoints_userpointsapi($params);
  }
}