1441 |
ariadna |
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 |
|
|
|
17 |
namespace customfield_number;
|
|
|
18 |
|
|
|
19 |
use customfield_number\local\numberproviders\nofactivities;
|
|
|
20 |
use customfield_number\task\recalculate;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Event observers for customfield_number
|
|
|
24 |
*
|
|
|
25 |
* @package customfield_number
|
|
|
26 |
* @copyright Marina Glancy
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
class observer {
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* When a 'number' custom field was created, schedule recalculation for the field data
|
|
|
33 |
*
|
|
|
34 |
* @param \core_customfield\event\field_created $event
|
|
|
35 |
*/
|
|
|
36 |
public static function field_created(\core_customfield\event\field_created $event): void {
|
|
|
37 |
$field = $event->get_record_snapshot('customfield_field', $event->objectid);
|
|
|
38 |
if ($field->type === 'number') {
|
|
|
39 |
recalculate::schedule_for_field($event->objectid);
|
|
|
40 |
}
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* When a 'number' custom field was updated, schedule recalculation for the field data
|
|
|
45 |
*
|
|
|
46 |
* @param \core_customfield\event\field_updated $event
|
|
|
47 |
*/
|
|
|
48 |
public static function field_updated(\core_customfield\event\field_updated $event): void {
|
|
|
49 |
$field = $event->get_record_snapshot('customfield_field', $event->objectid);
|
|
|
50 |
if ($field->type === 'number') {
|
|
|
51 |
recalculate::schedule_for_field($event->objectid);
|
|
|
52 |
}
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* When a course module was created, schedule recalculation for all 'nofactivities' custom fields
|
|
|
57 |
*
|
|
|
58 |
* @param \core\event\course_module_created $event
|
|
|
59 |
*/
|
|
|
60 |
public static function course_module_created(\core\event\course_module_created $event): void {
|
|
|
61 |
if (self::has_nofactivities_fields()) {
|
|
|
62 |
recalculate::schedule_for_fieldtype(fieldtype: nofactivities::class,
|
|
|
63 |
component: 'core_course', area: 'course', instanceid: $event->courseid);
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* When a course module was deleted, schedule recalculation for all 'nofactivities' custom fields
|
|
|
69 |
*
|
|
|
70 |
* @param \core\event\course_module_deleted $event
|
|
|
71 |
*/
|
|
|
72 |
public static function course_module_deleted(\core\event\course_module_deleted $event): void {
|
|
|
73 |
if (self::has_nofactivities_fields()) {
|
|
|
74 |
recalculate::schedule_for_fieldtype(fieldtype: nofactivities::class,
|
|
|
75 |
component: 'core_course', area: 'course', instanceid: $event->courseid);
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* When a course module was updated, schedule recalculation for all 'nofactivities' custom fields
|
|
|
81 |
*
|
|
|
82 |
* Module visibility may change following an 'updated' event and it will affect the activities count
|
|
|
83 |
*
|
|
|
84 |
* @param \core\event\course_module_updated $event
|
|
|
85 |
*/
|
|
|
86 |
public static function course_module_updated(\core\event\course_module_updated $event): void {
|
|
|
87 |
if (self::has_nofactivities_fields()) {
|
|
|
88 |
recalculate::schedule_for_fieldtype(fieldtype: nofactivities::class,
|
|
|
89 |
component: 'core_course', area: 'course', instanceid: $event->courseid);
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Checks if a 'number' field with 'nofactivities' provider exists in the course fields
|
|
|
95 |
*
|
|
|
96 |
* This method is very fast, it only performs one DB query and only once per request
|
|
|
97 |
*
|
|
|
98 |
* @return bool
|
|
|
99 |
*/
|
|
|
100 |
protected static function has_nofactivities_fields(): bool {
|
|
|
101 |
$handler = \core_course\customfield\course_handler::create();
|
|
|
102 |
foreach ($handler->get_categories_with_fields() as $category) {
|
|
|
103 |
foreach ($category->get_fields() as $field) {
|
|
|
104 |
if ($field->get('type') === 'number' &&
|
|
|
105 |
$field->get_configdata_property('fieldtype') === nofactivities::class) {
|
|
|
106 |
return true;
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
return false;
|
|
|
111 |
}
|
|
|
112 |
}
|