Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 coding_exception;
20
use core\task\adhoc_task;
21
use core_customfield\field_controller;
22
use customfield_number\provider_base;
23
 
24
/**
25
 * Recalculates data for the given number field with a provider
26
 *
27
 * @since      Moodle 4.5.1
28
 * @package    customfield_number
29
 * @copyright  Marina Glancy
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class recalculate extends adhoc_task {
33
    #[\Override]
34
    public function execute() {
35
        global $DB;
36
        $customdata = $this->get_custom_data();
37
        $fieldid = clean_param($customdata->fieldid ?? null, PARAM_INT);
38
        $fieldtype = $customdata->fieldtype ?? null;
39
 
40
        // Find all fields that we need to recalculate (either by 'fieldid' or 'fieldtype').
41
        $fields = [];
42
        if ($fieldid) {
43
            try {
44
                $fields[] = field_controller::create($fieldid);
45
            } catch (\Exception $e) {
46
                // Could be a race condition when the field was already deleted by the time ad-hoc task runs.
47
                return;
48
            }
49
        } else if ($fieldtype) {
50
            $records = $DB->get_records('customfield_field', ['type' => 'number']);
51
            foreach ($records as $record) {
52
                $configdata = @json_decode($record->configdata, true);
53
                if (($configdata['fieldtype'] ?? '') === $fieldtype) {
54
                    $fields[] = field_controller::create(0, $record);
55
                }
56
            }
57
        }
58
 
59
        // Schedule recalculate for each field, checking component, area and the presense of provider.
60
        $instanceid = clean_param($customdata->instanceid ?? null, PARAM_INT);
61
        foreach ($fields as $field) {
62
            if ($this->field_is_scheduled($field) && ($provider = provider_base::instance($field))) {
63
                $provider->recalculate($instanceid ?: null);
64
            }
65
        }
66
    }
67
 
68
    /**
69
     * Helper method validating that the field should be recalculated
70
     *
71
     * @param \core_customfield\field_controller $field
72
     * @return bool
73
     */
74
    protected function field_is_scheduled(field_controller $field): bool {
75
        $customdata = $this->get_custom_data();
76
        if (!empty($customdata->component) && $field->get_handler()->get_component() !== $customdata->component) {
77
            return false;
78
        }
79
        if (!empty($customdata->area) && $field->get_handler()->get_area() !== $customdata->area) {
80
            return false;
81
        }
82
        return true;
83
    }
84
 
85
    /**
86
     * Schedule recalculation for the given number custom field (and optionally, instanceid)
87
     *
88
     * @param int $fieldid in of the custom field
89
     * @param int|null $instanceid if specified, only recalculates for the given instance id
90
     * @return void
91
     */
92
    public static function schedule_for_field(int $fieldid, ?int $instanceid = null) {
93
        $task = new static();
94
        $task->set_custom_data(['fieldid' => $fieldid, 'instanceid' => $instanceid]);
95
        \core\task\manager::queue_adhoc_task($task, true);
96
    }
97
 
98
    /**
99
     * Schedule recalculation for all number custom fields that use the provider (optionally with instanceid)
100
     *
101
     * @param string $fieldtype name of the class extending provider_base
102
     * @param string|null $component
103
     * @param string|null $area
104
     * @param int|null $instanceid
105
     * @return void
106
     */
107
    public static function schedule_for_fieldtype(string $fieldtype, ?string $component = null, ?string $area = null,
108
            ?int $instanceid = null) {
109
        $task = new static();
110
        $task->set_custom_data([
111
            'fieldtype' => $fieldtype,
112
            'component' => $component,
113
            'area' => $area,
114
            'instanceid' => $instanceid,
115
        ]);
116
        \core\task\manager::queue_adhoc_task($task, true);
117
    }
118
}