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_external\external_api;
20
use core_external\external_function_parameters;
21
use core_external\external_single_structure;
22
use core_external\external_value;
23
 
24
/**
25
 * Web Service to control the order of a provider instance.
26
 *
27
 * @package   core_ai
28
 * @category  external
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_provider_order 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(PARAM_INT, ' The provider instance ID', VALUE_REQUIRED),
41
            'direction' => new external_value(PARAM_INT, 'The direction to move', VALUE_REQUIRED),
42
        ]);
43
    }
44
 
45
    /**
46
     * Set the provider instance order.
47
     *
48
     * @param int $providerid The provider instance ID
49
     * @param int $direction The direction to move the provider instance
50
     * @return array
51
     */
52
    public static function execute(
53
        int $providerid,
54
        int $direction,
55
    ): array {
56
        [
57
            'plugin' => $providerid,
58
            'direction' => $direction,
59
        ] = self::validate_parameters(self::execute_parameters(), [
60
            'plugin' => $providerid,
61
            'direction' => $direction,
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
        if ($aiprovider) {
72
            $manager->change_provider_order($providerid, $direction);
73
        }
74
 
75
        $directionstring = $direction === \core\plugininfo\aiprovider::MOVE_UP
76
                            ? \core\plugininfo\aiprovider::UP
77
                            : \core\plugininfo\aiprovider::DOWN;
78
        $message = get_string('providermoved' . $directionstring, 'ai', $aiprovider->name);
79
        $messagetype = \core\notification::SUCCESS;
80
 
81
        \core\notification::add($message, $messagetype);
82
 
83
        return [];
84
    }
85
 
86
    /**
87
     * Describe the return structure of the external service.
88
     *
89
     * @return external_single_structure
90
     */
91
    public static function execute_returns(): external_single_structure {
92
        return new external_single_structure([]);
93
    }
94
}