You are here

quiz.pages.inc in Quiz 8.6

User pages.

File

quiz.pages.inc
View source
<?php

/**
 * @file
 * User pages.
 */

/**
 * Show result page for a given result id.
 *
 * @param $result_id
 *  Result id.
 */
function quiz_user_results($result_id) {
  $quiz_result = QuizResult::load($result_id);
  $view = entity_view('quiz_result', array(
    $quiz_result,
  ));
  if ($quiz_result->is_invalid) {
    \Drupal::messenger()
      ->addWarning(t('Your previous score on this @quiz was equal or better. This result will not be saved.', array(
      '@quiz' => _quiz_get_quiz_name(),
    )));
  }
  return $view;
}

/**
 * Helper function to remove the message saying the quiz haven't been scored.
 */
function _quiz_remove_unscored_message() {
  if (!empty($_SESSION['messages']['warning'])) {

    // Search for the message, and remove it if we find it.
    foreach ($_SESSION['messages']['warning'] as $key => $val) {
      if ($val == t('This @quiz has not been scored yet.', array(
        '@quiz' => _quiz_get_quiz_name(),
      ))) {
        unset($_SESSION['messages']['warning'][$key]);
      }
    }

    // Clean up if the message array was left empty.
    if (empty($_SESSION['messages']['warning'])) {
      unset($_SESSION['messages']['warning']);
      if (empty($_SESSION['messages'])) {
        unset($_SESSION['messages']);
      }
    }
  }
}

/**
 * Returns an array of score information for a quiz.
 *
 * @param unknown_type $result_id
 * @param unknown_type $quiz_vid
 * @param unknown_type $is_evaluated
 */
function quiz_get_score_array($result_id, $quiz_vid, $is_evaluated) {
  $properties = db_query('SELECT max_score, number_of_random_questions
          FROM {quiz_node_properties}
          WHERE vid = :vid', array(
    ':vid' => $quiz_vid,
  ))
    ->fetchObject();
  $total_score = db_query('SELECT SUM(points_awarded)
          FROM {quiz_node_results_answers}
          WHERE result_id = :result_id', array(
    ':result_id' => $result_id,
  ))
    ->fetchField();
  return array(
    'question_count' => $properties->number_of_random_questions + quiz_get_number_of_questions($quiz_vid, $result_id),
    'possible_score' => $properties->max_score,
    'numeric_score' => $total_score,
    'percentage_score' => $properties->max_score == 0 ? 0 : round($total_score * 100 / $properties->max_score),
    'is_evaluated' => $is_evaluated,
  );
}

Functions

Namesort descending Description
quiz_get_score_array Returns an array of score information for a quiz.
quiz_user_results Show result page for a given result id.
_quiz_remove_unscored_message Helper function to remove the message saying the quiz haven't been scored.