From 475225a1c8b894131b250e585739f4e18e607d08 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Tue, 2 Jul 2013 11:08:55 +0100 Subject: [PATCH] MDL-40441 fix obscure notice in the quiz. This can only happen if you have both a user with both a real attempt and a preview, and then edit the quiz settings. Also clean up this code a bit. --- mod/quiz/lib.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index 8c82d00b8fc..ae23d46489b 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -106,6 +106,7 @@ function quiz_add_instance($quiz) { */ function quiz_update_instance($quiz, $mform) { global $CFG, $DB; + require_once($CFG->dirroot . '/mod/quiz/locallib.php'); // Process the options from the form. $result = quiz_process_options($quiz); @@ -113,11 +114,16 @@ function quiz_update_instance($quiz, $mform) { return $result; } + // Get the current value, so we can see what changed. $oldquiz = $DB->get_record('quiz', array('id' => $quiz->instance)); + // We need two values from the existing DB record that are not in the form, + // in some of the function calls below. + $quiz->sumgrades = $oldquiz->sumgrades; + $quiz->grade = $oldquiz->grade; + // Repaginate, if asked to. if (!$quiz->shufflequestions && !empty($quiz->repaginatenow)) { - require_once($CFG->dirroot . '/mod/quiz/locallib.php'); $quiz->questions = quiz_repaginate(quiz_clean_layout($oldquiz->questions, true), $quiz->questionsperpage); } @@ -131,19 +137,15 @@ function quiz_update_instance($quiz, $mform) { quiz_after_add_or_update($quiz); if ($oldquiz->grademethod != $quiz->grademethod) { - require_once($CFG->dirroot . '/mod/quiz/locallib.php'); - $quiz->sumgrades = $oldquiz->sumgrades; - $quiz->grade = $oldquiz->grade; quiz_update_all_final_grades($quiz); quiz_update_grades($quiz); } - $updateattempts = $oldquiz->timelimit != $quiz->timelimit - || $oldquiz->timeclose != $quiz->timeclose - || $oldquiz->graceperiod != $quiz->graceperiod; - if ($updateattempts) { - require_once($CFG->dirroot . '/mod/quiz/locallib.php'); - quiz_update_open_attempts(array('quizid'=>$quiz->id)); + $quizdateschanged = $oldquiz->timelimit != $quiz->timelimit + || $oldquiz->timeclose != $quiz->timeclose + || $oldquiz->graceperiod != $quiz->graceperiod; + if ($quizdateschanged) { + quiz_update_open_attempts(array('quizid' => $quiz->id)); } // Delete any previous preview attempts.