Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
57.69% covered (warning)
57.69%
15 / 26
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
log
57.69% covered (warning)
57.69%
15 / 26
50.00% covered (danger)
50.00%
2 / 4
12.85
0.00% covered (danger)
0.00%
0 / 1
 add
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 recent
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
3.21
 counts
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 prune
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
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 * Delivery attempts: who, which notification, which channel, and whether it went. Never the text, which can hold
21 * sign-in codes.
22 *
23 * @package    message_notifyhub
24 * @copyright  2026 DevLMS
25 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27final class log {
28    /** @var string The channel confirmed delivery. */
29    public const SENT = 'sent';
30    /** @var string The channel was tried and did not deliver. */
31    public const FAILED = 'failed';
32    /** @var string No channel delivered it. */
33    public const UNDELIVERED = 'undelivered';
34    /** @var string Moodle could not send an email (any email, not only the hub's). */
35    public const EMAILFAILED = 'emailfailed';
36    /** @var string Table name. */
37    public const TABLE = 'message_notifyhub_log';
38
39    /**
40     * Add a row.
41     *
42     * @param int $userid
43     * @param string $component
44     * @param string $name
45     * @param string $channel
46     * @param string $status
47     * @param int|null $time
48     */
49    public static function add(
50        int $userid,
51        string $component,
52        string $name,
53        string $channel,
54        string $status,
55        ?int $time = null
56    ): void {
57        global $DB;
58        $DB->insert_record(self::TABLE, (object) [
59            'userid' => $userid,
60            'component' => \core_text::substr($component, 0, 100),
61            'name' => \core_text::substr($name, 0, 100),
62            'channel' => \core_text::substr($channel, 0, 100),
63            'status' => $status,
64            'timecreated' => $time ?? time(),
65        ]);
66    }
67
68    /**
69     * Newest rows first.
70     *
71     * @param int $limit
72     * @param string $channel Only this channel when set.
73     * @param string $status Only this status when set.
74     * @return \stdClass[]
75     */
76    public static function recent(int $limit, string $channel = '', string $status = ''): array {
77        global $DB;
78        $where = [];
79        $params = [];
80        if ($channel !== '') {
81            $where['channel'] = $channel;
82        }
83        if ($status !== '') {
84            $where['status'] = $status;
85        }
86        return $DB->get_records(self::TABLE, $where, 'timecreated DESC, id DESC', '*', 0, $limit);
87    }
88
89    /**
90     * Counts per channel and status since a time.
91     *
92     * @param int $since
93     * @return array channel => [status => count]
94     */
95    public static function counts(int $since): array {
96        global $DB;
97        $rows = $DB->get_recordset_sql(
98            'SELECT channel, status, COUNT(1) AS n FROM {' . self::TABLE . '} WHERE timecreated >= ? GROUP BY channel, status',
99            [$since]
100        );
101        $counts = [];
102        foreach ($rows as $row) {
103            $counts[$row->channel][$row->status] = (int) $row->n;
104        }
105        $rows->close();
106        return $counts;
107    }
108
109    /**
110     * Remove rows older than the retention setting.
111     */
112    public static function prune(): void {
113        global $DB;
114        $days = max(1, (int) (get_config('message_notifyhub', 'keepdays') ?: 30));
115        $DB->delete_records_select(self::TABLE, 'timecreated < ?', [time() - $days * DAYSECS]);
116    }
117}