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 core_ai\external;
18
 
19
use core\context\system;
20
use core_ai\manager;
21
use core_external\external_api;
22
use core_external\external_function_parameters;
23
use core_external\external_value;
24
 
25
/**
26
 * External API to set provider action enabled.
27
 *
28
 * @package    core_ai
29
 * @copyright  Meirza <meirza.arson@moodle.com>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class set_action extends external_api {
33
    /**
34
     * Returns description of method parameters.
35
     *
36
     * @return external_function_parameters
37
     */
38
    public static function execute_parameters(): external_function_parameters {
39
        return new external_function_parameters([
40
            'plugin' => new external_value(
41
                PARAM_TEXT,
42
                'The name of the plugin and the action to change state for',
43
                VALUE_REQUIRED,
44
            ),
45
            'state' => new external_value(
46
                PARAM_INT,
47
                'The target state',
48
                VALUE_REQUIRED,
49
            ),
50
            'providerid' => new external_value(
51
                PARAM_INT,
52
                'The provider id',
53
                VALUE_DEFAULT,
54
 
55
            ),
56
        ]);
57
    }
58
 
59
    /**
60
     * Set the providers action state.
61
     *
62
     * @param string $plugin The name of the plugin and the action to change state for.
63
     * @param int $state The target state.
64
     * @param int $providerid The provider id.
65
     * @return array
66
     */
67
    public static function execute(
68
        string $plugin,
69
        int $state,
70
        int $providerid = 0
71
    ): array {
72
        // Parameter validation.
73
        [
74
            'plugin' => $plugin,
75
            'state' => $state,
76
            'providerid' => $providerid,
77
        ] = self::validate_parameters(self::execute_parameters(), [
78
            'plugin' => $plugin,
79
            'state' => $state,
80
            'providerid' => $providerid,
81
        ]);
82
 
83
        $context = system::instance();
84
        self::validate_context($context);
85
        require_capability('moodle/site:config', $context);
86
 
87
        [$plugin, $action] = explode('-', $plugin);
88
        $actionname = get_string("action_$action", 'core_ai');
89
 
90
        if (!empty($state)) {
91
            \core\notification::add(
92
                get_string('plugin_enabled', 'core_admin', $actionname),
93
                \core\notification::SUCCESS
94
            );
95
        } else {
96
            \core\notification::add(
97
                get_string('plugin_disabled', 'core_admin', $actionname),
98
                \core\notification::SUCCESS
99
            );
100
        }
101
 
102
        $manager = \core\di::get(manager::class);
103
        $manager->set_action_state(
104
            plugin: $plugin,
105
            actionbasename: $action,
106
            enabled: $state,
107
            instanceid: $providerid
108
        );
109
 
110
        return [];
111
    }
112
 
113
    /**
114
     * Describe the return structure of the external service.
115
     *
116
     * @return external_function_parameters
117
     */
118
    public static function execute_returns(): external_function_parameters {
119
        return new external_function_parameters([]);
120
    }
121
}