MDL-61984 mod_chat: Implement privacy API
[moodle.git] / mod / chat / classes / privacy / provider.php
CommitLineData
bf3b4f3e
FM
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/**
18 * Data provider.
19 *
20 * @package mod_chat
21 * @copyright 2018 Frédéric Massart
22 * @author Frédéric Massart <fred@branchup.tech>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26namespace mod_chat\privacy;
27defined('MOODLE_INTERNAL') || die();
28
29use context;
30use context_helper;
31use context_module;
32use moodle_recordset;
33use stdClass;
34use core_privacy\local\metadata\collection;
35use core_privacy\local\request\approved_contextlist;
36use core_privacy\local\request\contextlist;
37use core_privacy\local\request\helper;
38use core_privacy\local\request\transform;
39use core_privacy\local\request\writer;
40
41/**
42 * Data provider class.
43 *
44 * @package mod_chat
45 * @copyright 2018 Frédéric Massart
46 * @author Frédéric Massart <fred@branchup.tech>
47 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48 */
49class provider implements
50 \core_privacy\local\metadata\provider,
51 \core_privacy\local\request\plugin\provider {
52
53 /**
54 * Returns metadata.
55 *
56 * @param collection $collection The initialised collection to add items to.
57 * @return collection A listing of user data stored through this system.
58 */
59 public static function get_metadata(collection $collection) : collection {
60
61 $collection->add_database_table('chat_messages', [
62 'userid' => 'privacy:metadata:messages:userid',
63 'message' => 'privacy:metadata:messages:message',
64 'issystem' => 'privacy:metadata:messages:issystem',
65 'timestamp' => 'privacy:metadata:messages:timestamp',
66 ], 'privacy:metadata:messages');
67
68 // The tables chat_messages_current and chat_users are not reported here
69 // because they are considered as short-lived data and are deleted on a
70 // regular basis by cron, or during normal requests. MDL-62006 was raised
71 // to discuss and/or implement support for those tables.
72
73 return $collection;
74 }
75
76 /**
77 * Get the list of contexts that contain user information for the specified user.
78 *
79 * @param int $userid The user to search.
80 * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
81 */
82 public static function get_contexts_for_userid(int $userid) : \core_privacy\local\request\contextlist {
83 $contextlist = new \core_privacy\local\request\contextlist();
84
85 $sql = "
86 SELECT DISTINCT ctx.id
87 FROM {chat} c
88 JOIN {modules} m
89 ON m.name = :chat
90 JOIN {course_modules} cm
91 ON cm.instance = c.id
92 AND cm.module = m.id
93 JOIN {context} ctx
94 ON ctx.instanceid = cm.id
95 AND ctx.contextlevel = :modulelevel
96 JOIN {chat_messages} chm
97 ON chm.chatid = c.id
98 WHERE chm.userid = :userid";
99
100 $params = [
101 'chat' => 'chat',
102 'modulelevel' => CONTEXT_MODULE,
103 'userid' => $userid,
104 ];
105 $contextlist->add_from_sql($sql, $params);
106
107 return $contextlist;
108 }
109
110 /**
111 * Export all user data for the specified user, in the specified contexts.
112 *
113 * @param approved_contextlist $contextlist The approved contexts to export information for.
114 */
115 public static function export_user_data(approved_contextlist $contextlist) {
116 global $DB;
117
118 $user = $contextlist->get_user();
119 $userid = $user->id;
120 $cmids = array_reduce($contextlist->get_contexts(), function($carry, $context) {
121 if ($context->contextlevel == CONTEXT_MODULE) {
122 $carry[] = $context->instanceid;
123 }
124 return $carry;
125 }, []);
126 if (empty($cmids)) {
127 return;
128 }
129
130 $chatidstocmids = static::get_chat_ids_to_cmids_from_cmids($cmids);
131 $chatids = array_keys($chatidstocmids);
132
133 // Export the messages.
134 list($insql, $inparams) = $DB->get_in_or_equal($chatids, SQL_PARAMS_NAMED);
135 $params = array_merge($inparams, ['userid' => $userid]);
136 $recordset = $DB->get_recordset_select('chat_messages', "chatid $insql AND userid = :userid", $params, 'timestamp, id');
137 static::recordset_loop_and_export($recordset, 'chatid', [], function($carry, $record) use ($user, $chatidstocmids) {
138 $message = $record->message;
139 if ($record->issystem) {
140 $message = get_string('message' . $record->message, 'mod_chat', fullname($user));
141 }
142 $carry[] = [
143 'message' => $message,
144 'sent_at' => transform::datetime($record->timestamp),
145 'is_system_generated' => transform::yesno($record->issystem),
146 ];
147 return $carry;
148
149 }, function($chatid, $data) use ($user, $chatidstocmids) {
150 $context = context_module::instance($chatidstocmids[$chatid]);
151 $contextdata = helper::get_context_data($context, $user);
152 $finaldata = (object) array_merge((array) $contextdata, ['messages' => $data]);
153 helper::export_context_files($context, $user);
154 writer::with_context($context)->export_data([], $finaldata);
155 });
156 }
157
158 /**
159 * Delete all data for all users in the specified context.
160 *
161 * @param context $context The specific context to delete data for.
162 */
163 public static function delete_data_for_all_users_in_context(context $context) {
164 global $DB;
165
166 if ($context->contextlevel != CONTEXT_MODULE) {
167 return;
168 }
169
170 $chatid = get_coursemodule_from_id('chat', $context->instanceid, 0, false, MUST_EXIST)->instance;
171 $DB->delete_records_select('chat_messages', 'chatid = :chatid', ['chatid' => $chatid]);
172 $DB->delete_records_select('chat_messages_current', 'chatid = :chatid', ['chatid' => $chatid]);
173 $DB->delete_records_select('chat_users', 'chatid = :chatid', ['chatid' => $chatid]);
174 }
175
176 /**
177 * Delete all user data for the specified user, in the specified contexts.
178 *
179 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
180 */
181 public static function delete_data_for_user(approved_contextlist $contextlist) {
182 global $DB;
183
184 $userid = $contextlist->get_user()->id;
185 $cmids = array_reduce($contextlist->get_contexts(), function($carry, $context) {
186 if ($context->contextlevel == CONTEXT_MODULE) {
187 $carry[] = $context->instanceid;
188 }
189 return $carry;
190 }, []);
191 if (empty($cmids)) {
192 return;
193 }
194
195 $chatidstocmids = static::get_chat_ids_to_cmids_from_cmids($cmids);
196 $chatids = array_keys($chatidstocmids);
197
198 list($insql, $inparams) = $DB->get_in_or_equal($chatids, SQL_PARAMS_NAMED);
199 $sql = "chatid $insql AND userid = :userid";
200 $params = array_merge($inparams, ['userid' => $userid]);
201
202 $DB->delete_records_select('chat_messages', $sql, $params);
203 $DB->delete_records_select('chat_messages_current', $sql, $params);
204 $DB->delete_records_select('chat_users', $sql, $params);
205 }
206
207 /**
208 * Return a dict of chat IDs mapped to their course module ID.
209 *
210 * @param array $cmids The course module IDs.
211 * @return array In the form of [$chatid => $cmid].
212 */
213 protected static function get_chat_ids_to_cmids_from_cmids(array $cmids) {
214 global $DB;
215 list($insql, $inparams) = $DB->get_in_or_equal($cmids, SQL_PARAMS_NAMED);
216 $sql = "
217 SELECT c.id, cm.id AS cmid
218 FROM {chat} c
219 JOIN {modules} m
220 ON m.name = :chat
221 JOIN {course_modules} cm
222 ON cm.instance = c.id
223 AND cm.module = m.id
224 WHERE cm.id $insql";
225 $params = array_merge($inparams, ['chat' => 'chat']);
226 return $DB->get_records_sql_menu($sql, $params);
227 }
228
229 /**
230 * Loop and export from a recordset.
231 *
232 * @param moodle_recordset $recordset The recordset.
233 * @param string $splitkey The record key to determine when to export.
234 * @param mixed $initial The initial data to reduce from.
235 * @param callable $reducer The function to return the dataset, receives current dataset, and the current record.
236 * @param callable $export The function to export the dataset, receives the last value from $splitkey and the dataset.
237 * @return void
238 */
239 protected static function recordset_loop_and_export(moodle_recordset $recordset, $splitkey, $initial,
240 callable $reducer, callable $export) {
241
242 $data = $initial;
243 $lastid = null;
244
245 foreach ($recordset as $record) {
246 if ($lastid && $record->{$splitkey} != $lastid) {
247 $export($lastid, $data);
248 $data = $initial;
249 }
250 $data = $reducer($data, $record);
251 $lastid = $record->{$splitkey};
252 }
253 $recordset->close();
254
255 if (!empty($lastid)) {
256 $export($lastid, $data);
257 }
258 }
259
260}