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
declare(strict_types=1);
18
 
19
namespace customfield_number\external;
20
 
21
use core\exception\invalid_parameter_exception;
22
use core_external\external_function_parameters;
23
use core_external\external_single_structure;
24
use core_external\external_api;
25
use core_external\external_value;
26
use customfield_number\provider_base;
27
 
28
/**
29
 * Implementation of web service customfield_number_recalculate_value
30
 *
31
 * @package    customfield_number
32
 * @author     2024 Marina Glancy
33
 * @copyright  2024 Moodle Pty Ltd <support@moodle.com>
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class recalculate extends external_api {
37
 
38
    /**
39
     * Describes the parameters for customfield_number_recalculate_value
40
     *
41
     * @return external_function_parameters
42
     */
43
    public static function execute_parameters(): external_function_parameters {
44
        return new external_function_parameters([
45
            'fieldid' => new external_value(PARAM_INT, 'Field id', VALUE_REQUIRED),
46
            'instanceid' => new external_value(PARAM_INT, 'Instance id', VALUE_REQUIRED),
47
        ]);
48
    }
49
 
50
    /**
51
     * Implementation of web service customfield_number_recalculate_value
52
     *
53
     * @param int $fieldid
54
     * @param int $instanceid
55
     * @return array
56
     */
57
    public static function execute(int $fieldid, int $instanceid): array {
58
        // Parameter validation.
59
        [
60
            'fieldid' => $fieldid,
61
            'instanceid' => $instanceid,
62
        ] = self::validate_parameters(self::execute_parameters(), [
63
            'fieldid' => $fieldid,
64
            'instanceid' => $instanceid,
65
        ]);
66
 
67
        // Access validation.
68
        $context = \context_system::instance();
69
        self::validate_context($context);
70
 
71
        $field = \core_customfield\field_controller::create($fieldid);
72
        $provider = provider_base::instance($field);
73
        if (!$provider) {
74
            throw new invalid_parameter_exception('Invalid parameter');
75
        }
76
 
77
        $handler = $field->get_handler();
78
        if (!$handler->can_edit($field, $instanceid)) {
79
            throw new \moodle_exception('nopermissions', '', '', get_string('update'));
80
        }
81
 
82
        $provider->recalculate($instanceid);
83
 
84
        $data = \core_customfield\api::get_instance_fields_data(
85
            [$fieldid => $field], $instanceid)[$fieldid];
86
 
87
        return ['value' => $data->export_value()];
88
    }
89
 
90
    /**
91
     * Describe the return structure for customfield_number_recalculate_value
92
     *
93
     * @return external_single_structure
94
     */
95
    public static function execute_returns(): external_single_structure {
96
        return new external_single_structure([
97
            'value' => new external_value(PARAM_RAW, 'Recalculated value (prepared for display)'),
98
        ]);
99
    }
100
}