You are here

function quiz_update_max_score_properties in Quiz 7.4

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 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 2758
Quiz Module

Code

function quiz_update_max_score_properties($quizzes_to_update) {
  if (empty($quizzes_to_update)) {
    return;
  }
  db_update('quiz_node_properties')
    ->expression('max_score', 'max_score_for_random * number_of_random_questions + (
      SELECT COALESCE(SUM(max_score), 0)
      FROM {quiz_node_relationship} qnr
      WHERE qnr.question_status = ' . QUESTION_ALWAYS . '
      AND parent_vid = {quiz_node_properties}.vid)')
    ->condition('vid', $quizzes_to_update, 'IN')
    ->execute();
  db_update('quiz_node_properties')
    ->expression('max_score', '(SELECT COALESCE(SUM(qt.max_score * qt.number), 0)
      FROM {quiz_terms} qt
      WHERE qt.nid = {quiz_node_properties}.nid AND qt.vid = {quiz_node_properties}.vid)')
    ->condition('randomization', 3)
    ->condition('vid', $quizzes_to_update, 'IN')
    ->execute();
  $results_to_update = db_query('SELECT vid FROM {quiz_node_properties} WHERE vid IN (:vid) AND max_score <> :max_score', array(
    ':vid' => $quizzes_to_update,
    ':max_score' => 0,
  ))
    ->fetchCol();
  if (!empty($results_to_update)) {
    db_update('quiz_node_results')
      ->expression('score', 'ROUND(
        100 * (
          SELECT COALESCE (SUM(a.points_awarded), 0)
          FROM {quiz_node_results_answers} a
          WHERE a.result_id = {quiz_node_results}.result_id
        ) / (
          SELECT max_score
          FROM {quiz_node_properties} qnp
          WHERE qnp.vid = {quiz_node_results}.vid
        )
      )')
      ->condition('vid', $results_to_update, 'IN')
      ->execute();
  }
}