Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 
17
namespace core\check\external;
18
 
19
use admin_root;
20
use admin_setting_check;
21
use core_external\external_api;
22
use core_external\external_function_parameters;
23
use core_external\external_single_structure;
24
use core_external\external_value;
25
use context_system;
26
use invalid_parameter_exception;
27
 
28
/**
29
 * Webservice to get result of a given check.
30
 *
31
 * @package    core
32
 * @category   check
33
 * @copyright  2023 Matthew Hilton <matthewhilton@catalyst-au.net>
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class get_result_admintree extends external_api {
37
    /**
38
     * Defines parameters
39
     * @return external_function_parameters
40
     */
41
    public static function execute_parameters(): external_function_parameters {
42
        return new external_function_parameters([
43
            'admintreeid' => new external_value(PARAM_TEXT, 'ID of node in admintree'),
44
            'settingname' => new external_value(PARAM_TEXT, 'Name of setting'),
45
            'includedetails' => new external_value(PARAM_BOOL, 'If the details should be included in the response.
46
                Depending on the check, details could be slower to return.', VALUE_DEFAULT, false),
47
        ]);
48
    }
49
 
50
    /**
51
     * Gets the result of the check and returns it.
52
     * @param string $admintreeid ID of admin_setting to find check object from
53
     * @param string $settingname Name of admin_setting to find check object from
54
     * @param bool $includedetails If the details should be included in the response.
55
     * @param admin_root|null $admintree Root of admin tree to use (for unit testing)
56
     * @return array returned data
57
     */
58
    public static function execute(string $admintreeid, string $settingname, bool $includedetails,
59
        admin_root $admintree = null): array {
60
        global $OUTPUT, $CFG;
61
 
62
        // Validate parameters.
63
        self::validate_parameters(self::execute_parameters(), [
64
            'admintreeid' => $admintreeid,
65
            'settingname' => $settingname,
66
            'includedetails' => $includedetails,
67
        ]);
68
 
69
        // Context and capability checks.
70
        $context = context_system::instance();
71
        self::validate_context($context);
72
        require_admin();
73
 
74
        require_once($CFG->libdir . '/adminlib.php');
75
 
76
        // Find admin node so we can load the check object.
77
        $check = self::get_check_from_setting($admintreeid, $settingname, $admintree);
78
 
79
        if (empty($check)) {
80
            throw new invalid_parameter_exception("Could not find check object using admin tree.");
81
        }
82
 
83
        // Execute the check and get the result.
84
        $result = $check->get_result();
85
 
86
        // Build the response.
87
        $data = [
88
            'status' => s($result->get_status()),
89
            'summary' => s($result->get_summary()),
90
            'html' => s($OUTPUT->check_full_result($check, $result, $includedetails)),
91
        ];
92
 
93
        // Since details might be slower to obtain, we allow this to be optionally returned.
94
        if ($includedetails) {
95
            $data['details'] = s($result->get_details());
96
        }
97
 
98
        return $data;
99
    }
100
 
101
    /**
102
     * Finds the check from the admin tree.
103
     *
104
     * @param string $settingid ID of the adming_setting
105
     * @param string $settingname Name of the admin_setting
106
     * @param admin_root|null $tree Admin tree to use (for unit testing). Null will default to the admin_get_root()
107
     */
108
    private static function get_check_from_setting(string $settingid, string $settingname, admin_root $tree = null) {
109
        // Since settings do not know exactly who their parents are in the tree, we must search for the setting.
110
        if (empty($tree)) {
111
            $tree = \admin_get_root();
112
        }
113
 
114
        // Search for the setting name.
115
        // To do this, we must search in each category.
116
        $categories = $tree->search($settingname);
117
 
118
        $allsettings = array_map(function($c) {
119
            return $c->settings;
120
        }, array_values($categories));
121
 
122
        // Flatten the array.
123
        $allsettings = array_merge(...$allsettings);
124
 
125
        // Find the one that matches the unique id exactly and are check settings.
126
        $matchingsettings = array_filter($allsettings, function($s) use ($settingid) {
127
            return $s->get_id() == $settingid && $s instanceof admin_setting_check;
128
        });
129
 
130
        // There was either none found or more than one found.
131
        // In this case, we cannot determine which to use so just return null.
132
        if (count($matchingsettings) != 1) {
133
            return null;
134
        }
135
 
136
        $setting = current($matchingsettings);
137
        return $setting->get_check();
138
    }
139
 
140
    /**
141
     * Defines return structure.
142
     * @return external_single_structure
143
     */
144
    public static function execute_returns(): external_single_structure {
145
        return new external_single_structure([
146
            'status' => new external_value(PARAM_TEXT, 'Result status constant'),
147
            'summary' => new external_value(PARAM_TEXT, 'Summary of result'),
148
            'html' => new external_value(PARAM_TEXT, 'Rendered full html result', VALUE_OPTIONAL),
149
            'details' => new external_value(PARAM_TEXT, 'Details of result (if includedetails was enabled)', VALUE_OPTIONAL),
150
        ]);
151
    }
152
}
153