diff --git a/mod/quiz/classes/grade_calculator.php b/mod/quiz/classes/grade_calculator.php index 6e3f8dd18e5..5e64077bc13 100644 --- a/mod/quiz/classes/grade_calculator.php +++ b/mod/quiz/classes/grade_calculator.php @@ -16,6 +16,8 @@ namespace mod_quiz; +use question_engine_data_mapper; + /** * This class contains all the logic for computing the grade of a quiz. * @@ -84,4 +86,26 @@ class grade_calculator { quiz_set_grade(0, $quiz); } } + + /** + * Update the sumgrades field of attempts at this quiz. + */ + public function recompute_all_attempt_sumgrades(): void { + global $DB; + $dm = new question_engine_data_mapper(); + $timenow = time(); + + $DB->execute(" + UPDATE {quiz_attempts} + SET timemodified = :timenow, + sumgrades = ( + {$dm->sum_usage_marks_subquery('uniqueid')} + ) + WHERE quiz = :quizid AND state = :finishedstate + ", [ + 'timenow' => $timenow, + 'quizid' => $this->quizobj->get_quizid(), + 'finishedstate' => quiz_attempt::FINISHED + ]); + } } diff --git a/mod/quiz/classes/local/reports/attempts_report.php b/mod/quiz/classes/local/reports/attempts_report.php index ce1f1519ca9..f7f62cc4258 100644 --- a/mod/quiz/classes/local/reports/attempts_report.php +++ b/mod/quiz/classes/local/reports/attempts_report.php @@ -18,6 +18,7 @@ namespace mod_quiz\local\reports; use coding_exception; use context_module; +use mod_quiz\quiz_settings; use moodle_url; use stdClass; use table_sql; @@ -63,6 +64,9 @@ abstract class attempts_report extends report_base { /** @var boolean caches the results of {@see should_show_grades()}. */ protected $showgrades = null; + /** @var quiz_settings|null quiz settings object. Set in the init method. */ + protected $quizobj = null; + /** * Initialise various aspects of this report. * @@ -80,8 +84,8 @@ abstract class attempts_report extends report_base { */ public function init($mode, $formclass, $quiz, $cm, $course): array { $this->mode = $mode; - - $this->context = context_module::instance($cm->id); + $this->quizobj = new quiz_settings($quiz, $cm, $course); + $this->context = $this->quizobj->get_context(); [$currentgroup, $studentsjoins, $groupstudentsjoins, $allowedjoins] = $this->get_students_joins( $cm, $course); diff --git a/mod/quiz/deprecatedlib.php b/mod/quiz/deprecatedlib.php index 2c0f7cfaf64..afe1363d16a 100644 --- a/mod/quiz/deprecatedlib.php +++ b/mod/quiz/deprecatedlib.php @@ -322,3 +322,16 @@ function quiz_update_sumgrades($quiz) { 'Please use a standard grade_calculator::recompute_quiz_sumgrades instead.', DEBUG_DEVELOPER); quiz_settings::create($quiz->id)->get_grade_calculator()->recompute_quiz_sumgrades(); } + +/** + * Update the sumgrades field of the attempts at a quiz. + * + * @param stdClass $quiz a quiz. + * @deprecated since Moodle 4.2. Please use grade_calculator::recompute_all_attempt_sumgrades. + * @todo MDL-76612 Final deprecation in Moodle 4.6 + */ +function quiz_update_all_attempt_sumgrades($quiz) { + debugging('quiz_update_all_attempt_sumgrades is deprecated. ' . + 'Please use a standard grade_calculator::recompute_all_attempt_sumgrades instead.', DEBUG_DEVELOPER); + quiz_settings::create($quiz->id)->get_grade_calculator()->recompute_all_attempt_sumgrades(); +} diff --git a/mod/quiz/edit_rest.php b/mod/quiz/edit_rest.php index e9f7009fd8f..6bb3f775167 100644 --- a/mod/quiz/edit_rest.php +++ b/mod/quiz/edit_rest.php @@ -133,7 +133,7 @@ switch($requestmethod) { // Grade has really changed. quiz_delete_previews($quiz); $gradecalculator->recompute_quiz_sumgrades(); - quiz_update_all_attempt_sumgrades($quiz); + $gradecalculator->recompute_all_attempt_sumgrades(); quiz_update_all_final_grades($quiz); quiz_update_grades($quiz, 0, true); } diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index f0ba1f0e56b..9333df6e097 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -628,27 +628,6 @@ function quiz_has_feedback($quiz) { return $cache[$quiz->id]; } -/** - * Update the sumgrades field of the attempts at a quiz. - * - * @param stdClass $quiz a quiz. - */ -function quiz_update_all_attempt_sumgrades($quiz) { - global $DB; - $dm = new question_engine_data_mapper(); - $timenow = time(); - - $sql = "UPDATE {quiz_attempts} - SET - timemodified = :timenow, - sumgrades = ( - {$dm->sum_usage_marks_subquery('uniqueid')} - ) - WHERE quiz = :quizid AND state = :finishedstate"; - $DB->execute($sql, ['timenow' => $timenow, 'quizid' => $quiz->id, - 'finishedstate' => quiz_attempt::FINISHED]); -} - /** * The quiz grade is the maximum that student's results are marked out of. When it * changes, the corresponding data in quiz_grades and quiz_feedback needs to be diff --git a/mod/quiz/report/overview/report.php b/mod/quiz/report/overview/report.php index 5febc847778..a380d5ef1ee 100644 --- a/mod/quiz/report/overview/report.php +++ b/mod/quiz/report/overview/report.php @@ -25,6 +25,7 @@ use mod_quiz\local\reports\attempts_report; use mod_quiz\question\bank\qbank_helper; use mod_quiz\quiz_attempt; +use mod_quiz\quiz_settings; defined('MOODLE_INTERNAL') || die(); @@ -59,7 +60,7 @@ class quiz_overview_report extends attempts_report { protected $newquestionidsforold = null; public function display($quiz, $cm, $course) { - global $DB, $OUTPUT, $PAGE; + global $DB, $PAGE; list($currentgroup, $studentsjoins, $groupstudentsjoins, $allowedjoins) = $this->init( 'overview', 'quiz_overview_settings_form', $quiz, $cm, $course); @@ -687,7 +688,8 @@ class quiz_overview_report extends attempts_report { * @param stdClass $quiz the quiz settings. */ protected function update_overall_grades($quiz) { - quiz_update_all_attempt_sumgrades($quiz); + $gradecalculator = $this->quizobj->get_grade_calculator(); + $gradecalculator->recompute_all_attempt_sumgrades(); quiz_update_all_final_grades($quiz); quiz_update_grades($quiz); } diff --git a/mod/quiz/upgrade.txt b/mod/quiz/upgrade.txt index f8248e1d60f..402f3fe8e92 100644 --- a/mod/quiz/upgrade.txt +++ b/mod/quiz/upgrade.txt @@ -79,7 +79,8 @@ This files describes API changes in the quiz code. * Various functions related to calculating grades have moved into a new class mod_quiz\grade_calculator. You get that using $quizobj->get_grade_calculator(), then the following old functions have become these new methods. - - recompute_quiz_sumgrades -> recompute_quiz_sumgrades + - quiz_update_sumgrades -> recompute_quiz_sumgrades + - quiz_update_all_attempt_sumgrades -> recompute_all_attempt_sumgrades * Final deprecation (complete removal) of the following functions which were deprecated long ago: - quiz_groups_member_added_handler - deprecated since 2.6 diff --git a/question/engine/datalib.php b/question/engine/datalib.php index 1464c7a8b9e..2f0208a54c4 100644 --- a/question/engine/datalib.php +++ b/question/engine/datalib.php @@ -1173,11 +1173,11 @@ ORDER BY } /** - * Return a subquery that computes the sum of the marks for all the questions - * in a usage. Which useage to compute the sum for is controlled bu the $qubaid + * Return a sub-query that computes the sum of the marks for all the questions + * in a usage. Which usage to compute the sum for is controlled by the $qubaid * parameter. * - * See {@link quiz_update_all_attempt_sumgrades()} for an example of the usage of + * See {@see \mod_quiz\grade_calculator::recompute_all_attempt_sumgrades()} for an example of the usage of * this method. * * This method may be called publicly.