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 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
 * External API to delete a provider instance.
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 delete_provider_instance extends external_api {
33
    /**
34
     * Get provider parameters.
35
     *
36
     * @since  Moodel 5.0
37
     * @return external_function_parameters
38
     */
39
    public static function execute_parameters(): external_function_parameters {
40
        return new external_function_parameters([
41
            'providerid' => new external_value(PARAM_INT, 'Provider ID', VALUE_REQUIRED),
42
        ]);
43
    }
44
 
45
    /**
46
     * Delete a provider instance.
47
     *
48
     * @since  Moodel 5.0
49
     * @param int $providerid The provider ID.
50
     * @return array The generated content.
51
     */
52
    public static function execute(int $providerid): array {
53
        [
54
            'providerid' => $providerid,
55
        ] = self::validate_parameters(self::execute_parameters(), [
56
            'providerid' => $providerid,
57
        ]);
58
 
59
        $context = context_system::instance();
60
        self::validate_context($context);
61
        require_capability('moodle/site:config', $context);
62
 
63
        // Get AI provider instance.
64
        $manager = \core\di::get(\core_ai\manager::class);
65
        $aiproviders = $manager->get_provider_instances(['id' => $providerid]);
66
        $aiprovider = reset($aiproviders);
67
 
68
        if (!$aiprovider) {
69
            return [
70
                'result' => false,
71
                'message' => get_string('notfound', 'error'),
72
                'messagetype' => \core\notification::ERROR,
73
            ];
74
        }
75
 
76
        $providerresult = $manager->delete_provider_instance(provider: $aiprovider);
77
        if (!$providerresult) {
78
            $message = get_string('providerinstancedeletefailed', 'core_ai', $aiprovider->name);
79
            $messagetype = \core\notification::ERROR;
80
        } else {
81
            $message = get_string('providerinstancedeleted', 'core_ai', $aiprovider->name);
82
            $messagetype = \core\notification::SUCCESS;
83
        }
84
 
85
        \core\notification::add($message, $messagetype);
86
 
87
        // Update and return the result array in one place.
88
        return [
89
            'result' => $providerresult,
90
            'message' => $message,
91
            'messagetype' => $messagetype,
92
        ];
93
    }
94
 
95
    /**
96
     * Generate content return value.
97
     *
98
     * @since  Moodel 5.0
99
     * @return external_single_structure
100
     */
101
    public static function execute_returns(): external_single_structure {
102
        return new external_single_structure(
103
            [
104
                'result' => new external_value(PARAM_BOOL, 'Whether the status was changed, true or false'),
105
                'message' => new external_value(PARAM_TEXT, 'Messages'),
106
                'messagetype' => new external_value(PARAM_TEXT, 'Message type'),
107
            ]
108
        );
109
    }
110
 
111
}