Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.15% covered (success)
96.15%
25 / 26
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
send_notification_node
96.15% covered (success)
96.15%
25 / 26
75.00% covered (warning)
75.00%
3 / 4
9
0.00% covered (danger)
0.00%
0 / 1
 type
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 params
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 branches
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
95.65% covered (success)
95.65%
22 / 23
0.00% covered (danger)
0.00%
0 / 1
6
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
17namespace tool_autoflow\local\nodes;
18
19use tool_autoflow\local\engine\node_failed;
20use tool_autoflow\local\engine\node_result;
21use tool_autoflow\local\engine\node_type;
22use tool_autoflow\local\engine\param;
23
24/**
25 * Sends a Moodle notification to one user, from the no-reply user.
26 *
27 * It goes through the plugin's own message provider, so users and admins control
28 * its delivery (popup, email) in the normal notification preferences.
29 *
30 * @package    tool_autoflow
31 * @copyright  2026 DevLMS <contact@devlms.com>
32 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34final class send_notification_node implements node_type {
35    /**
36     * Type name.
37     *
38     * @return string
39     */
40    public function type(): string {
41        return 'moodle.send_notification';
42    }
43
44    /**
45     * Parameters.
46     *
47     * @return param[]
48     */
49    public function params(): array {
50        return [new param('userid', 'user', true), new param('subject', 'text', true), new param('message', 'textarea', true)];
51    }
52
53    /**
54     * Branches.
55     *
56     * @return string[]
57     */
58    public function branches(): array {
59        return [];
60    }
61
62    /**
63     * Send it.
64     *
65     * @param array $params userid, subject, message
66     * @param array $context
67     * @return node_result
68     */
69    public function execute(array $params, array $context): node_result {
70        $userid = $params['userid'] ?? 0;
71        $user = is_numeric($userid) ? \core_user::get_user((int) $userid) : false;
72        if (!$user || $user->deleted) {
73            throw new node_failed("Cannot send a notification: no user with id \"$userid\".");
74        }
75        $subject = trim((string) ($params['subject'] ?? ''));
76        if ($subject === '') {
77            throw new node_failed('A notification needs a subject.');
78        }
79        $text = (string) ($params['message'] ?? '');
80
81        $message = new \core\message\message();
82        $message->component = 'tool_autoflow';
83        $message->name = 'notification';
84        $message->userfrom = \core_user::get_noreply_user();
85        $message->userto = $user;
86        $message->subject = $subject;
87        $message->fullmessage = $text;
88        $message->fullmessageformat = FORMAT_PLAIN;
89        $message->fullmessagehtml = text_to_html(s($text));
90        $message->smallmessage = $subject;
91        $message->notification = 1;
92        $messageid = message_send($message);
93        if (!$messageid) {
94            throw new node_failed("The notification to user {$user->id} was not sent.");
95        }
96        return new node_result(['messageid' => (int) $messageid, 'userid' => (int) $user->id]);
97    }
98}