Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
65.71% covered (warning)
65.71%
23 / 35
28.57% covered (danger)
28.57%
2 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
channels
65.71% covered (warning)
65.71%
23 / 35
28.57% covered (danger)
28.57%
2 / 7
33.55
0.00% covered (danger)
0.00%
0 / 1
 outputs
28.57% covered (danger)
28.57%
2 / 7
0.00% covered (danger)
0.00%
0 / 1
14.11
 ordered
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
3.00
 active
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 is_off
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 set_on
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 move
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
6.10
 split
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
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
17namespace message_notifyhub\local;
18
19/**
20 * The channels the hub can use, in the admin's order.
21 *
22 * A channel is any message output that is on for the site, except popup (every notification is already kept for
23 * the bell) and the hub itself. Channels missing from the saved order are added before email, so a new output is
24 * tried before the catch-all.
25 *
26 * @package    message_notifyhub
27 * @copyright  2026 DevLMS
28 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 */
30final class channels {
31    /** @var string[] Outputs the hub never routes to. */
32    public const EXCLUDED = ['notifyhub', 'popup'];
33
34    /**
35     * Every usable output, keyed by name.
36     *
37     * @return \message_output[]
38     */
39    public static function outputs(): array {
40        if (router::$outputs !== null) {
41            return router::$outputs;
42        }
43        $outputs = [];
44        foreach (get_message_processors(true) as $name => $processor) {
45            if (!in_array($name, self::EXCLUDED, true) && !empty($processor->object)) {
46                $outputs[$name] = $processor->object;
47            }
48        }
49        return $outputs;
50    }
51
52    /**
53     * Channel names in the order the hub tries them, including those switched off in the hub.
54     *
55     * @return string[]
56     */
57    public static function ordered(): array {
58        $available = array_keys(self::outputs());
59        $saved = array_values(array_intersect(self::split(get_config('message_notifyhub', 'order')), $available));
60        $new = array_diff($available, $saved);
61        sort($new);
62        $emailat = array_search('email', $saved, true);
63        if ($emailat === false) {
64            // Email stays the last resort.
65            $new = array_values(array_diff($new, ['email']));
66            $order = array_merge($saved, $new);
67            if (in_array('email', $available, true)) {
68                $order[] = 'email';
69            }
70            return $order;
71        }
72        array_splice($saved, $emailat, 0, $new);
73        return $saved;
74    }
75
76    /**
77     * Channels the hub may use, in order.
78     *
79     * @return string[]
80     */
81    public static function active(): array {
82        $off = self::split(get_config('message_notifyhub', 'off'));
83        return array_values(array_diff(self::ordered(), $off));
84    }
85
86    /**
87     * Whether a channel is switched off in the hub.
88     *
89     * @param string $name
90     * @return bool
91     */
92    public static function is_off(string $name): bool {
93        return in_array($name, self::split(get_config('message_notifyhub', 'off')), true);
94    }
95
96    /**
97     * Switch a channel on or off in the hub.
98     *
99     * @param string $name
100     * @param bool $on
101     */
102    public static function set_on(string $name, bool $on): void {
103        $off = array_diff(self::split(get_config('message_notifyhub', 'off')), [$name]);
104        if (!$on) {
105            $off[] = $name;
106        }
107        set_config('off', implode(',', $off), 'message_notifyhub');
108    }
109
110    /**
111     * Move a channel up (-1) or down (+1).
112     *
113     * @param string $name
114     * @param int $step
115     */
116    public static function move(string $name, int $step): void {
117        $order = self::ordered();
118        $at = array_search($name, $order, true);
119        $to = $at === false ? false : $at + ($step < 0 ? -1 : 1);
120        if ($to === false || $to < 0 || $to >= count($order)) {
121            return;
122        }
123        [$order[$at], $order[$to]] = [$order[$to], $order[$at]];
124        set_config('order', implode(',', $order), 'message_notifyhub');
125    }
126
127    /**
128     * Comma list to array.
129     *
130     * @param mixed $value
131     * @return string[]
132     */
133    private static function split($value): array {
134        return array_values(array_filter(array_map('trim', explode(',', (string) $value))));
135    }
136}