MDL-80099 quiz: Replace quiz_structure_modified callback with hook

This adds a new structure_modified hook to mod_quiz, deprecating the
quiz_stucture_modified class callback used in
recompute_quiz_sumgrades(). The callback will be removed in Moodle 4.8.

The quiz_statsitics plugin which used this callback now defines a hook
callback instead.
This commit is contained in:
Mark Johnson 2023-12-05 12:00:12 +00:00
parent 788556f652
commit 9a2f82a709
No known key found for this signature in database
GPG Key ID: EB30E1468CFAE242
4 changed files with 97 additions and 12 deletions

View File

@ -18,6 +18,7 @@ namespace mod_quiz;
use coding_exception;
use mod_quiz\event\quiz_grade_updated;
use mod_quiz\hook\structure_modified;
use question_engine_data_mapper;
use stdClass;
@ -92,10 +93,14 @@ class grade_calculator {
self::update_quiz_maximum_grade(0);
}
// This class callback is deprecated, and will be removed in Moodle 4.8 (MDL-80327).
// Use the structure_modified hook instead.
$callbackclasses = \core_component::get_plugin_list_with_class('quiz', 'quiz_structure_modified');
foreach ($callbackclasses as $callbackclass) {
component_class_callback($callbackclass, 'callback', [$quiz->id]);
component_class_callback($callbackclass, 'callback', [$quiz->id], null, true);
}
\core\hook\manager::get_instance()->dispatch(new structure_modified($this->quizobj->get_structure()));
}
/**

View File

@ -0,0 +1,51 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz\hook;
use core\attribute;
use mod_quiz\structure;
/**
* The quiz structure has been modified
*
* @package mod_quiz
* @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net}
* @author Mark Johnson <mark.johnson@catalyst-eu.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
#[attribute\label('The quiz structure has been modified')]
#[attribute\tags('quiz', 'structure')]
#[attribute\hook\replaces_callbacks('quiz_structure_modified::callback')]
class structure_modified {
/**
* Create a new hook with the modified structure.
*
* @param structure $structure The new structure.
*/
public function __construct(
protected structure $structure
) {
}
/**
* Returns the new structure of the quiz.
*
* @return structure The structure object.
*/
public function get_structure(): structure {
return $this->structure;
}
}

View File

@ -13,34 +13,31 @@
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace quiz_statistics;
use core\dml\sql_join;
use mod_quiz\hook\structure_modified;
/**
* Clear the statistics cache when the quiz structure is modified.
* Hook callbacks
*
* @package quiz_statistics
* @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net}
* @author Mark Johnson <mark.johnson@catalyst-eu.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quiz_structure_modified {
class hook_callbacks {
/**
* Clear the statistics cache.
* Clear the statistics cache for the quiz where the structure was modified.
*
* @param int $quizid The quiz to clear the cache for.
* @param structure_modified $hook The structure_modified hook containing the new structure.
* @return void
*/
public static function callback(int $quizid): void {
global $DB, $CFG;
public static function quiz_structure_modified(structure_modified $hook) {
global $CFG;
require_once($CFG->dirroot . '/mod/quiz/report/statistics/statisticslib.php');
require_once($CFG->dirroot . '/mod/quiz/report/statistics/report.php');
$quiz = $DB->get_record('quiz', ['id' => $quizid]);
if (!$quiz) {
throw new \coding_exception('Could not find quiz with ID ' . $quizid . '.');
}
$quiz = $hook->get_structure()->get_quiz();
$qubaids = quiz_statistics_qubaids_condition(
$quiz->id,
new sql_join(),

View File

@ -0,0 +1,32 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Hook callback definitions for quiz_statistics
*
* @package quiz_statistics
* @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net}
* @author Mark Johnson <mark.johnson@catalyst-eu.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$callbacks = [
[
'hook' => mod_quiz\hook\structure_modified::class,
'callback' => quiz_statistics\hook_callbacks::class . '::quiz_structure_modified',
'priority' => 500,
],
];