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_sms\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 sms gateway.
27
 *
28
 * @package    core_sms
29
 * @copyright  2024 Safat Shahin <safat.shahin@moodle.com>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class sms_gateway_status extends external_api {
33
 
34
    public static function execute_parameters(): external_function_parameters {
35
        return new external_function_parameters([
36
            'plugin' => new external_value(PARAM_INT, 'Gateway ID', VALUE_REQUIRED),
37
            'state' => new external_value(PARAM_INT, 'Enabled or disabled', VALUE_REQUIRED),
38
        ]);
39
    }
40
 
41
    public static function execute(int $plugin, int $state): array {
42
        // Parameter validation.
43
        [
44
            'plugin' => $gatewayid,
45
            'state' => $state,
46
        ] = self::validate_parameters(self::execute_parameters(), [
47
            'plugin' => $plugin,
48
            'state' => $state,
49
        ]);
50
 
51
        $context = context_system::instance();
52
        self::validate_context($context);
53
        require_capability('moodle/site:config', $context);
54
 
55
        $result = [
56
            'result' => true,
57
            'message' => '',
58
            'messagetype' => '',
59
        ];
60
        $manager = \core\di::get(\core_sms\manager::class);
61
        $gatewaymanagers = $manager->get_gateway_instances(['id' => $gatewayid]);
62
        $gatewaymanager = reset($gatewaymanagers);
63
 
64
        if (!$gatewaymanager) {
65
            $result = [
66
                'result' => false,
67
                'message' => 'sms_gateway_not_found',
68
                'messagetype' => 'error'
69
            ];
70
            return $result;
71
        }
72
 
73
        if (!empty($state)) {
74
            $manager->enable_gateway(gateway: $gatewaymanager);
75
            $message = get_string('plugin_enabled', 'core_admin', $gatewaymanager->name);
76
            $messagetype = \core\notification::SUCCESS;
77
        } else {
78
            $gatewayresult = $manager->disable_gateway(gateway: $gatewaymanager);
79
            if ($gatewayresult->enabled) {
80
                $result = [
81
                    'result' => false,
82
                    'message' => 'sms_gateway_disable_failed',
83
                    'messagetype' => 'error'
84
                ];
85
                $message = get_string('sms_gateway_disable_failed', 'core_sms');
86
                $messagetype = \core\notification::ERROR;
87
            } else {
88
                $message = get_string('plugin_disabled', 'core_admin', $gatewaymanager->name);
89
                $messagetype = \core\notification::SUCCESS;
90
            }
91
        }
92
 
93
        \core\notification::add($message, $messagetype);
94
 
95
        return $result;
96
    }
97
 
98
    public static function execute_returns(): external_single_structure {
99
        return new external_single_structure(
100
            [
101
                'result' => new external_value(PARAM_BOOL, 'Whether the status was changed, true or false'),
102
                'message' => new external_value(PARAM_TEXT, 'Messages'),
103
                'messagetype' => new external_value(PARAM_TEXT, 'Message type'),
104
            ]
105
        );
106
    }
107
 
108
}