Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| subscribe | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| execute_parameters | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| execute_returns | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 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 message_webpush\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 | use message_webpush\local\subscriptions; |
| 24 | |
| 25 | /** |
| 26 | * Save the browser the user is in as one that gets their notifications. |
| 27 | * |
| 28 | * @package message_webpush |
| 29 | * @copyright 2026 DevLMS |
| 30 | * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 31 | */ |
| 32 | class subscribe extends external_api { |
| 33 | /** |
| 34 | * Parameters: the browser's PushSubscription. |
| 35 | * |
| 36 | * @return external_function_parameters |
| 37 | */ |
| 38 | public static function execute_parameters(): external_function_parameters { |
| 39 | return new external_function_parameters([ |
| 40 | 'endpoint' => new external_value(PARAM_RAW_TRIMMED, 'PushSubscription.endpoint, checked against the push service list'), |
| 41 | 'p256dh' => new external_value(PARAM_ALPHANUMEXT, 'getKey("p256dh"), base64url'), |
| 42 | 'auth' => new external_value(PARAM_ALPHANUMEXT, 'getKey("auth"), base64url'), |
| 43 | 'label' => new external_value(PARAM_NOTAGS, 'Browser and system, for the list', VALUE_DEFAULT, ''), |
| 44 | ]); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Save it. |
| 49 | * |
| 50 | * @param string $endpoint |
| 51 | * @param string $p256dh |
| 52 | * @param string $auth |
| 53 | * @param string $label |
| 54 | * @return array |
| 55 | */ |
| 56 | public static function execute(string $endpoint, string $p256dh, string $auth, string $label = ''): array { |
| 57 | global $USER; |
| 58 | $params = self::validate_parameters( |
| 59 | self::execute_parameters(), |
| 60 | ['endpoint' => $endpoint, 'p256dh' => $p256dh, 'auth' => $auth, 'label' => $label] |
| 61 | ); |
| 62 | $context = \context_system::instance(); |
| 63 | self::validate_context($context); |
| 64 | if (isguestuser()) { |
| 65 | throw new \moodle_exception('noguest'); |
| 66 | } |
| 67 | require_capability('moodle/user:editownmessageprofile', $context); |
| 68 | subscriptions::add($USER->id, $params['endpoint'], $params['p256dh'], $params['auth'], $params['label']); |
| 69 | return ['saved' => true]; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns. |
| 74 | * |
| 75 | * @return external_single_structure |
| 76 | */ |
| 77 | public static function execute_returns(): external_single_structure { |
| 78 | return new external_single_structure([ |
| 79 | 'saved' => new external_value(PARAM_BOOL, 'The browser is saved'), |
| 80 | ]); |
| 81 | } |
| 82 | } |