1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
namespace quiz_statistics;
|
|
|
17 |
|
|
|
18 |
use core\dml\sql_join;
|
|
|
19 |
use mod_quiz\hook\attempt_state_changed;
|
|
|
20 |
use mod_quiz\hook\structure_modified;
|
|
|
21 |
use mod_quiz\quiz_attempt;
|
|
|
22 |
use quiz_statistics\task\recalculate;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Hook callbacks
|
|
|
26 |
*
|
|
|
27 |
* @package quiz_statistics
|
|
|
28 |
* @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net}
|
|
|
29 |
* @author Mark Johnson <mark.johnson@catalyst-eu.net>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
*/
|
|
|
32 |
class hook_callbacks {
|
|
|
33 |
/**
|
|
|
34 |
* Clear the statistics cache for the quiz where the structure was modified.
|
|
|
35 |
*
|
|
|
36 |
* @param structure_modified $hook The structure_modified hook containing the new structure.
|
|
|
37 |
* @return void
|
|
|
38 |
*/
|
|
|
39 |
public static function quiz_structure_modified(structure_modified $hook) {
|
|
|
40 |
global $CFG;
|
|
|
41 |
require_once($CFG->dirroot . '/mod/quiz/report/statistics/statisticslib.php');
|
|
|
42 |
require_once($CFG->dirroot . '/mod/quiz/report/statistics/report.php');
|
|
|
43 |
$quiz = $hook->get_structure()->get_quiz();
|
|
|
44 |
$qubaids = quiz_statistics_qubaids_condition(
|
|
|
45 |
$quiz->id,
|
|
|
46 |
new sql_join(),
|
|
|
47 |
$quiz->grademethod
|
|
|
48 |
);
|
|
|
49 |
|
|
|
50 |
$report = new \quiz_statistics_report();
|
|
|
51 |
$report->clear_cached_data($qubaids);
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Queue a statistics recalculation when an attempt is submitted or deleting.
|
|
|
56 |
*
|
|
|
57 |
* @param attempt_state_changed $hook
|
|
|
58 |
* @return bool True if a task was queued.
|
|
|
59 |
*/
|
|
|
60 |
public static function quiz_attempt_submitted_or_deleted(attempt_state_changed $hook): bool {
|
|
|
61 |
$originalattempt = $hook->get_original_attempt();
|
|
|
62 |
$updatedattempt = $hook->get_updated_attempt();
|
|
|
63 |
if (is_null($updatedattempt) || $updatedattempt->state === quiz_attempt::FINISHED) {
|
|
|
64 |
// Only recalculate on deletion or submission.
|
|
|
65 |
return recalculate::queue_future_run($originalattempt->quiz);
|
|
|
66 |
}
|
|
|
67 |
return false;
|
|
|
68 |
}
|
|
|
69 |
}
|