| 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\task;
|
|
|
18 |
|
|
|
19 |
use core\task\scheduled_task;
|
|
|
20 |
use core_customfield\category_controller;
|
|
|
21 |
use core_customfield\field_controller;
|
|
|
22 |
use customfield_number\provider_base;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Scheduled task for customfield_number to recalculate automatically populated fields.
|
|
|
26 |
*
|
|
|
27 |
* @package customfield_number
|
|
|
28 |
* @author 2024 Marina Glancy
|
|
|
29 |
* @copyright 2024 Moodle Pty Ltd <support@moodle.com>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
*/
|
|
|
32 |
class cron extends scheduled_task {
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Get a descriptive name for the task (shown to admins)
|
|
|
36 |
*
|
|
|
37 |
* @return string
|
|
|
38 |
*/
|
|
|
39 |
public function get_name(): string {
|
|
|
40 |
return get_string('crontaskname', 'customfield_number');
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Recalculate automatically populated number fields.
|
|
|
45 |
*
|
|
|
46 |
* Throw exceptions on errors (the job will be retried).
|
|
|
47 |
*/
|
|
|
48 |
public function execute(): void {
|
|
|
49 |
global $DB;
|
|
|
50 |
// Get all number custom fields.
|
|
|
51 |
$sql = "SELECT f.*, c.component, c.area, c.itemid, c.contextid
|
|
|
52 |
FROM {customfield_field} f
|
|
|
53 |
JOIN {customfield_category} c ON f.categoryid = c.id
|
|
|
54 |
WHERE f.type = ?";
|
|
|
55 |
$res = $DB->get_records_sql($sql, ['number']);
|
|
|
56 |
foreach ($res as $row) {
|
|
|
57 |
$cat = (object)[
|
|
|
58 |
'id' => $row->categoryid,
|
|
|
59 |
'component' => $row->component,
|
|
|
60 |
'area' => $row->area,
|
|
|
61 |
'itemid' => $row->itemid,
|
|
|
62 |
'contextid' => $row->contextid,
|
|
|
63 |
];
|
|
|
64 |
unset($row->component, $row->area, $row->itemid, $row->contextid);
|
|
|
65 |
$category = category_controller::create(0, $cat);
|
|
|
66 |
// Create an instance of field controller for each field and recalculate the value if field provider is available.
|
|
|
67 |
$field = field_controller::create(0, $row, $category);
|
|
|
68 |
if ($provider = provider_base::instance($field)) {
|
|
|
69 |
if ($provider->is_available()) {
|
|
|
70 |
$provider->recalculate();
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
}
|