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_message\external;
18
 
19
use context_system;
20
use core_external\external_api;
21
use core_external\external_function_parameters;
22
use core_external\external_value;
23
 
24
/**
25
 * Webservice to enable or disable the default notification.
26
 *
27
 * @package    core_message
28
 * @copyright  2024 Raquel Ortega <raquel.ortega@moodle.com>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class message_set_default_notification extends external_api {
32
    /**
33
     * Returns description of method parameters.
34
     *
35
     * @return external_function_parameters
36
     */
37
    public static function execute_parameters(): external_function_parameters {
38
        return new external_function_parameters([
39
            'preference' => new external_value(
40
                PARAM_TEXT,
41
                'The name of the preference',
42
                VALUE_REQUIRED,
43
            ),
44
            'state' => new external_value(
45
                PARAM_INT,
46
                'The target state',
47
                VALUE_REQUIRED,
48
            ),
49
        ]);
50
    }
51
    /**
52
     * Set the default notification action state.
53
     *
54
     * @param string $preference The name of the preference.
55
     * @param int $state The target state.
56
     * @return array
57
     */
58
    public static function execute(
59
        string $preference,
60
        int $state,
61
    ): array {
62
 
63
        global $DB;
64
        // Parameter validation.
65
        [
66
            'preference' => $preference,
67
            'state' => $state,
68
        ] = self::validate_parameters(self::execute_parameters(), [
69
            'preference' => $preference,
70
            'state' => $state,
71
        ]);
72
 
73
        $context = context_system::instance();
74
        self::validate_context($context);
75
        require_capability('moodle/site:config', $context);
76
 
77
        // Fetch all message processors and filter out disabled ones.
78
        $allprocessors = get_message_processors();
79
        $processors = array_filter($allprocessors, function($processor) {
80
            return $processor->enabled;
81
        });
82
        $preferences = get_message_output_default_preferences();
83
 
84
        // Get the provider name and provider component from the given preference.
85
        $providers = get_message_providers();
86
        $providername = '';
87
        $providercomponent = '';
88
        foreach ($providers as $provider) {
89
            if (str_contains($preference, $provider->component . '_' . $provider->name)) {
90
                $providername = $provider->name;
91
                $providercomponent = $provider->component;
92
                break;
93
            }
94
        }
95
        // Initialize variables to store new preferences.
96
        $preferencename = '';
97
        $value = 0;
98
        $successmessage = '';
99
        if (!empty($providername) && !empty($providercomponent)) {
100
            $componentproviderbase = $providercomponent.'_'.$providername;
101
            $providerdisplayname = get_string('messageprovider:'.$providername, $providercomponent);
102
 
103
            // Enabled / disabled toggle for providers.
104
            if ($preference === $componentproviderbase . '_disable') {
105
                // If $state is true, set the preference to disabled; otherwise, enable it.
106
                $preferencename = $preference;
107
                $value = $state ? 0 : 1;
108
                $successmessage = get_string('successproviderupdate', 'message', $providerdisplayname);
109
 
110
            } else {
111
                // Prepare data to use it for the component settings.
112
                $currentprocessorname = '';
113
                $componentprovidersetting = '';
114
                foreach ($processors as $processor) {
115
                    if (str_contains($preference, '[' . $processor->name . ']')) {
116
                        $currentprocessorname = $processor->name;
117
                        $componentprovidersetting = str_replace('[' . $processor->name . ']', '', $preference);
118
                        $labelparams = [
119
                            'provider'  => $providerdisplayname,
120
                            'processor' => get_string('pluginname', 'message_' . $processor->name),
121
                        ];
122
                        break;
123
                    }
124
                }
125
 
126
                // Locked toggle.
127
                if ($preference === $componentproviderbase.'_locked' . '[' . $currentprocessorname . ']') {
128
                    $preferencename = $currentprocessorname.'_provider_'.$componentproviderbase . '_locked';
129
                    $value = $state ? 1 : 0;
130
                    if ($value === 1) {
131
                        $successmessage = get_string('successproviderlocked', 'message', $labelparams);
132
                    } else {
133
                        $successmessage = get_string('successproviderunlocked', 'message', $labelparams);
134
                    }
135
                }
136
                // Enabled toggle.
137
                if ($preference === $componentproviderbase.'_enabled' . '[' . $currentprocessorname . ']') {
138
                    // Fetch the default message output preferences.
139
                    $preferencename = 'message_provider_'.$componentprovidersetting;
140
                    $successmessage = get_string('successproviderenabled', 'message', $labelparams);
141
                    $newsettings = [];
142
                    if (!empty($state)) {
143
                        $newsettings[] = $currentprocessorname;
144
                    }
145
                    // Check if the property exists within the preferences to maintain existing values.
146
                    if (property_exists($preferences, $preferencename)) {
147
                        $newsettings = array_merge($newsettings, explode(',', $preferences->$preferencename));
148
 
149
                        if (in_array($currentprocessorname, $newsettings) && empty($state)) {
150
                            if (($key = array_search($currentprocessorname, $newsettings)) !== false) {
151
                                unset($newsettings[$key]);
152
                            }
153
                        }
154
                    }
155
                    $value = join(',', $newsettings);
156
                    if (empty($value)) {
157
                        $value = null;
158
                    }
159
                }
160
            }
161
        }
162
        // Update preferences.
163
        if (!empty($preferencename)) {
164
            $transaction = $DB->start_delegated_transaction();
165
            $old = isset($preferences->$preferencename) ? $preferences->$preferencename : '';
166
 
167
            if ($old != $value) {
168
                add_to_config_log($preferencename, $old, $value, 'message');
169
            }
170
 
171
            set_config($preferencename, $value, 'message');
172
            $transaction->allow_commit();
173
 
174
            \core_plugin_manager::reset_caches();
175
        }
176
 
177
        return [
178
            'successmessage' => $successmessage,
179
        ];
180
    }
181
 
182
    /**
183
     * Describe the return structure of the external service.
184
     *
185
     * @return external_function_parameters
186
     */
187
    public static function execute_returns(): external_function_parameters {
188
        return new external_function_parameters([
189
            'successmessage' => new external_value(PARAM_TEXT, 'Success notification message.'),
190
        ]);
191
    }
192
}