Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>.
16
 
17
namespace core_sms\privacy;
18
 
19
use core_privacy\local\metadata\collection;
20
use core_privacy\local\request\approved_userlist;
21
use core_privacy\local\request\userlist;
22
 
23
/**
24
 * Class provider
25
 *
26
 * @package    core_sms
27
 * @copyright  2024 Andrew Lyons <andrew@nicols.co.uk>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class provider implements
31
    \core_privacy\local\request\core_userlist_provider,
32
    \core_privacy\local\metadata\provider,
33
    \core_privacy\local\request\subsystem\provider {
34
    #[\Override]
35
    public static function get_metadata(
36
        collection $collection,
37
    ): collection {
38
        $collection->add_database_table(
39
            'sms_messages',
40
            [
41
                'id' => 'privacy:metadata:sms_messages:id',
42
                'recipient' => 'privacy:metadata:sms_messages:recipient',
43
                'recipientuserid' => 'privacy:metadata:sms_messages:recipientuserid',
44
                'content' => 'privacy:metadata:sms_messages:content',
45
                'status' => 'privacy:metadata:sms_messages:status',
46
                'timecreated' => 'privacy:metadata:sms_messages:timecreated',
47
 
48
            ],
49
            'privacy:metadata:sms_messages'
50
        );
51
        return $collection;
52
    }
53
 
54
    #[\Override]
55
    public static function get_contexts_for_userid(
56
        int $userid,
57
    ): \core_privacy\local\request\contextlist {
58
        $contextlist = new \core_privacy\local\request\contextlist();
59
        $contextlist->add_from_sql(
60
            <<<EOF
61
                SELECT DISTINCT ctx.id
62
                  FROM {context} ctx
63
                  JOIN {sms_messages} m
64
                    ON m.recipientuserid = ctx.instanceid AND ctx.contextlevel = :contextlevel
65
                 WHERE m.recipientuserid = :userid
66
            EOF,
67
            [
68
               'userid' => $userid,
69
               'contextlevel' => CONTEXT_USER,
70
            ]
71
        );
72
 
73
        return $contextlist;
74
    }
75
 
76
    #[\Override]
77
    public static function export_user_data(
78
        \core_privacy\local\request\approved_contextlist $contextlist,
79
    ) {
80
        global $DB;
81
 
82
        foreach ($contextlist as $context) {
83
            // All data is against the recipientuserid and stored in a user context.
84
            if (!$context instanceof \core\context\user) {
85
                return;
86
            }
87
            $messages = array_map(
88
                function ($data) {
89
                    return [
90
                        'recipient' => $data->recipientnumber,
91
                        'content' => $data->issensitive ?
92
                            get_string('privacy:sms:sensitive_not_shown', 'core_sms') : $data->content,
93
                        'messagetype' => $data->messagetype,
94
                        'status' => $data->status,
95
                        'timecreated' => $data->timecreated,
96
                    ];
97
                },
98
                $DB->get_records(
99
                    table: 'sms_messages',
100
                    conditions: [
101
                        'recipientuserid' => $context->instanceid,
102
                    ],
103
                ),
104
            );
105
 
106
            if (!empty($messages)) {
107
                \core_privacy\local\request\writer::with_context($context)->export_data(
108
                    [
109
                        get_string('sms', 'core_sms'),
110
                    ],
111
                    (object) [
112
                        'messages' => $messages,
113
                    ],
114
                );
115
            }
116
        }
117
    }
118
 
119
    #[\Override]
120
    public static function delete_data_for_all_users_in_context(\context $context) {
121
        global $DB;
122
 
123
        if (!$context instanceof \core\context\user) {
124
            return;
125
        }
126
        $DB->delete_records('sms_messages', ['recipientuserid' => $context->instanceid]);
127
    }
128
 
129
    #[\Override]
130
    public static function delete_data_for_user(\core_privacy\local\request\approved_contextlist $contextlist) {
131
        foreach ($contextlist as $context) {
132
            self::delete_data_for_all_users_in_context($context);
133
        }
134
    }
135
 
136
    #[\Override]
137
    public static function get_users_in_context(userlist $userlist) {
138
        $context = $userlist->get_context();
139
        if ($context instanceof \core\context\user) {
140
            $userlist->add_user($context->instanceid);
141
        }
142
    }
143
 
144
    #[\Override]
145
    public static function delete_data_for_users(approved_userlist $userlist) {
146
        $context = $userlist->get_context();
147
 
148
        if (!$context instanceof \core\context\user) {
149
            return;
150
        }
151
 
152
        self::delete_data_for_all_users_in_context($context);
153
    }
154
}