You are here

function quiz_update_max_score_properties in Quiz 7

Same name and namespace in other branches
  1. 8.4 quiz.module \quiz_update_max_score_properties()
  2. 6.4 quiz.module \quiz_update_max_score_properties()
  3. 7.6 quiz.module \quiz_update_max_score_properties()
  4. 7.4 quiz.module \quiz_update_max_score_properties()
  5. 7.5 quiz.module \quiz_update_max_score_properties()

Updates the max_score property on the specified quizzes

Parameters

$quizzes_to_update: Array with the vid's of the quizzes to update

Related topics

4 calls to quiz_update_max_score_properties()
QuizQuestion::saveRelationships in question_types/quiz_question/quiz_question.core.inc
Handle the add to quiz part of the quiz_question_form
quiz_set_questions in ./quiz.module
Sets the questions that are assigned to a quiz.
quiz_update in ./quiz.module
Implements hook_update().
_quiz_delete_question in question_types/quiz_question/quiz_question.module
Delete the question node from the db, and mark its identifiers in the quiz linking table as "NEVER". This is safer than deleting them and allows for same tracing of what's happened if a question was deleted unintentionally.

File

./quiz.module, line 2506
Quiz Module

Code

function quiz_update_max_score_properties($quizzes_to_update) {
  if (empty($quizzes_to_update)) {
    return;
  }
  $score = db_query('SELECT SUM(qnr.max_score) FROM {quiz_node_relationship} qnr
    JOIN {quiz_node_properties} qnp ON (qnr.parent_vid = qnp.vid)
    WHERE qnr.question_status = :status', array(
    ':status' => QUESTION_ALWAYS,
  ))
    ->fetchField();
  db_update('quiz_node_properties')
    ->expression('max_score', 'max_score_for_random * number_of_random_questions + :score', array(
    ':score' => (int) $score,
  ))
    ->condition('vid', $quizzes_to_update, 'IN')
    ->execute();
  db_update('node_revision')
    ->fields(array(
    'timestamp' => REQUEST_TIME,
  ))
    ->condition('vid', $quizzes_to_update, 'IN')
    ->execute();
  db_update('node')
    ->fields(array(
    'changed' => REQUEST_TIME,
  ))
    ->condition('vid', $quizzes_to_update, 'IN')
    ->execute();
  $points_awarded = (int) db_query('SELECT SUM(a.points_awarded) FROM {quiz_node_results_answers} a
    JOIN {quiz_node_results} r ON (a.result_id = r.result_id)')
    ->fetchField();
  $max_score = (int) db_query('SELECT max_score FROM {quiz_node_properties} qnp
    JOIN {quiz_node_results} r ON (r.vid = qnp.vid)')
    ->fetchField();
  $expression = array(
    ':points_awarded' => $points_awarded,
    ':max_score' => $max_score > 0 ? $max_score : 1,
  );
  db_update('quiz_node_results')
    ->expression('score', 'ROUND(100 * :points_awarded / :max_score)', $expression)
    ->condition('vid', $quizzes_to_update, 'IN')
    ->execute();
}