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 - https://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 <https://www.gnu.org/licenses/>.
16
 
17
namespace core_ai\external;
18
 
19
use context_system;
20
use core_external\external_api;
21
use core_external\external_function_parameters;
22
use core_external\external_single_structure;
23
use core_external\external_value;
24
 
25
/**
26
 * Webservice to enable or disable AI provider.
27
 *
28
 * @package    core_ai
29
 * @copyright  2024 Matt Porritt <matt.porritt@moodle.com>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class set_provider_status extends external_api {
33
    /**
34
     * Set provider status parameters.
35
     *
36
     * @since  Moodle 4.5
37
     * @return external_function_parameters
38
     */
39
    public static function execute_parameters(): external_function_parameters {
40
        return new external_function_parameters([
41
            'plugin' => new external_value(PARAM_INT, 'Provider ID', VALUE_REQUIRED),
42
            'state' => new external_value(PARAM_INT, 'Enabled or disabled', VALUE_REQUIRED),
43
        ]);
44
    }
45
 
46
    /**
47
     * Set a provider status.
48
     *
49
     * @since  Moodle 4.5
50
     * @param int $plugin The provider ID.
51
     * @param int $state The state of the provider.
52
     * @return array The generated content.
53
     */
54
    public static function execute(int $plugin, int $state): array {
55
        // Parameter validation.
56
        [
57
            'plugin' => $providerid,
58
            'state' => $state,
59
        ] = self::validate_parameters(self::execute_parameters(), [
60
            'plugin' => $plugin,
61
            'state' => $state,
62
        ]);
63
 
64
        $context = context_system::instance();
65
        self::validate_context($context);
66
        require_capability('moodle/site:config', $context);
67
 
68
        $manager = \core\di::get(\core_ai\manager::class);
69
        $aiproviders = $manager->get_provider_instances(['id' => $providerid]);
70
        $aiprovider = reset($aiproviders);
71
 
72
        if (!$aiprovider) {
73
            return [
74
                'result' => false,
75
                'message' => get_string('notfound', 'error'),
76
                'messagetype' => \core\notification::ERROR,
77
            ];
78
        }
79
 
80
        if (!empty($state)) {
81
            $manager->enable_provider_instance(provider: $aiprovider);
82
            $message = get_string('plugin_enabled', 'core_admin', $aiprovider->name);
83
            $messagetype = \core\notification::SUCCESS;
84
        } else {
85
            $providerresult = $manager->disable_provider_instance(provider: $aiprovider);
86
            if ($providerresult->enabled) {
87
                $message = get_string('providerinstancedisablefailed', 'core_ai');
88
                $messagetype = \core\notification::ERROR;
89
            } else {
90
                $message = get_string('plugin_disabled', 'core_admin', $aiprovider->name);
91
                $messagetype = \core\notification::SUCCESS;
92
            }
93
        }
94
 
95
        \core\notification::add($message, $messagetype);
96
 
97
        return [
98
            'result' => $messagetype === \core\notification::SUCCESS ? true : false,
99
            'message' => $message,
100
            'messagetype' => $messagetype,
101
        ];
102
    }
103
 
104
    /**
105
     * Generate content return value.
106
     *
107
     * @since  Moodle 4.5
108
     * @return external_single_structure
109
     */
110
    public static function execute_returns(): external_single_structure {
111
        return new external_single_structure(
112
            [
113
                'result' => new external_value(PARAM_BOOL, 'Whether the status was changed, true or false'),
114
                'message' => new external_value(PARAM_TEXT, 'Messages'),
115
                'messagetype' => new external_value(PARAM_TEXT, 'Message type'),
116
            ]
117
        );
118
    }
119
 
120
}