Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
67.50% covered (warning)
67.50%
27 / 40
42.86% covered (danger)
42.86%
3 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
provider
67.50% covered (warning)
67.50%
27 / 40
42.86% covered (danger)
42.86%
3 / 7
22.72
0.00% covered (danger)
0.00%
0 / 1
 get_metadata
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 get_contexts_for_userid
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 get_users_in_context
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 export_user_data
87.50% covered (warning)
87.50%
14 / 16
0.00% covered (danger)
0.00%
0 / 1
3.02
 delete_data_for_all_users_in_context
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 delete_data_for_user
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 delete_data_for_users
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
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\privacy;
18
19use core_privacy\local\metadata\collection;
20use core_privacy\local\request\approved_contextlist;
21use core_privacy\local\request\approved_userlist;
22use core_privacy\local\request\contextlist;
23use core_privacy\local\request\transform;
24use core_privacy\local\request\userlist;
25use core_privacy\local\request\writer;
26use message_notifyhub\local\log;
27
28/**
29 * Privacy provider: the delivery log, kept in the system context.
30 *
31 * @package    message_notifyhub
32 * @copyright  2026 DevLMS
33 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class provider implements
36    \core_privacy\local\metadata\provider,
37    \core_privacy\local\request\core_userlist_provider,
38    \core_privacy\local\request\plugin\provider {
39    #[\Override]
40    public static function get_metadata(collection $collection): collection {
41        $collection->add_database_table(log::TABLE, [
42            'userid' => 'privacy:metadata:log:userid',
43            'component' => 'privacy:metadata:log:component',
44            'name' => 'privacy:metadata:log:name',
45            'channel' => 'privacy:metadata:log:channel',
46            'status' => 'privacy:metadata:log:status',
47            'timecreated' => 'privacy:metadata:log:timecreated',
48        ], 'privacy:metadata:log');
49        return $collection;
50    }
51
52    #[\Override]
53    public static function get_contexts_for_userid(int $userid): contextlist {
54        global $DB;
55        $contextlist = new contextlist();
56        if ($DB->record_exists(log::TABLE, ['userid' => $userid])) {
57            $contextlist->add_system_context();
58        }
59        return $contextlist;
60    }
61
62    #[\Override]
63    public static function get_users_in_context(userlist $userlist) {
64        if ($userlist->get_context()->contextlevel != CONTEXT_SYSTEM) {
65            return;
66        }
67        $userlist->add_from_sql('userid', 'SELECT userid FROM {' . log::TABLE . '}', []);
68    }
69
70    #[\Override]
71    public static function export_user_data(approved_contextlist $contextlist) {
72        global $DB;
73        $context = \context_system::instance();
74        if (!in_array($context->id, $contextlist->get_contextids())) {
75            return;
76        }
77        $rows = $DB->get_records(log::TABLE, ['userid' => $contextlist->get_user()->id], 'timecreated, id');
78        if (!$rows) {
79            return;
80        }
81        $deliveries = array_map(fn($row) => [
82            'notification' => $row->component . '/' . $row->name,
83            'channel' => $row->channel,
84            'status' => $row->status,
85            'time' => transform::datetime($row->timecreated),
86        ], array_values($rows));
87        writer::with_context($context)->export_data(
88            [get_string('pluginname', 'message_notifyhub')],
89            (object) ['deliveries' => $deliveries]
90        );
91    }
92
93    #[\Override]
94    public static function delete_data_for_all_users_in_context(\context $context) {
95        global $DB;
96        if ($context->contextlevel == CONTEXT_SYSTEM) {
97            $DB->delete_records(log::TABLE);
98        }
99    }
100
101    #[\Override]
102    public static function delete_data_for_user(approved_contextlist $contextlist) {
103        global $DB;
104        if (in_array(\context_system::instance()->id, $contextlist->get_contextids())) {
105            $DB->delete_records(log::TABLE, ['userid' => $contextlist->get_user()->id]);
106        }
107    }
108
109    #[\Override]
110    public static function delete_data_for_users(approved_userlist $userlist) {
111        global $DB;
112        if ($userlist->get_context()->contextlevel != CONTEXT_SYSTEM || !$userlist->get_userids()) {
113            return;
114        }
115        [$in, $params] = $DB->get_in_or_equal($userlist->get_userids());
116        $DB->delete_records_select(log::TABLE, "userid $in", $params);
117    }
118}