2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
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
26 namespace mod_chat\privacy;
27 defined('MOODLE_INTERNAL') || die();
34 use core_privacy\local\metadata\collection;
35 use core_privacy\local\request\approved_contextlist;
36 use core_privacy\local\request\contextlist;
37 use core_privacy\local\request\helper;
38 use core_privacy\local\request\transform;
39 use core_privacy\local\request\writer;
42 * Data provider class.
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
49 class provider implements
50 \core_privacy\local\metadata\provider,
51 \core_privacy\local\request\plugin\provider {
56 * @param collection $collection The initialised collection to add items to.
57 * @return collection A listing of user data stored through this system.
59 public static function get_metadata(collection $collection) : collection {
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');
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.
77 * Get the list of contexts that contain user information for the specified user.
79 * @param int $userid The user to search.
80 * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
82 public static function get_contexts_for_userid(int $userid) : \core_privacy\local\request\contextlist {
83 $contextlist = new \core_privacy\local\request\contextlist();
86 SELECT DISTINCT ctx.id
90 JOIN {course_modules} cm
94 ON ctx.instanceid = cm.id
95 AND ctx.contextlevel = :modulelevel
96 JOIN {chat_messages} chm
98 WHERE chm.userid = :userid";
102 'modulelevel' => CONTEXT_MODULE,
105 $contextlist->add_from_sql($sql, $params);
111 * Export all user data for the specified user, in the specified contexts.
113 * @param approved_contextlist $contextlist The approved contexts to export information for.
115 public static function export_user_data(approved_contextlist $contextlist) {
118 $user = $contextlist->get_user();
120 $cmids = array_reduce($contextlist->get_contexts(), function($carry, $context) {
121 if ($context->contextlevel == CONTEXT_MODULE) {
122 $carry[] = $context->instanceid;
130 $chatidstocmids = static::get_chat_ids_to_cmids_from_cmids($cmids);
131 $chatids = array_keys($chatidstocmids);
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));
143 'message' => $message,
144 'sent_at' => transform::datetime($record->timestamp),
145 'is_system_generated' => transform::yesno($record->issystem),
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);
159 * Delete all data for all users in the specified context.
161 * @param context $context The specific context to delete data for.
163 public static function delete_data_for_all_users_in_context(context $context) {
166 if ($context->contextlevel != CONTEXT_MODULE) {
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]);
177 * Delete all user data for the specified user, in the specified contexts.
179 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
181 public static function delete_data_for_user(approved_contextlist $contextlist) {
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;
195 $chatidstocmids = static::get_chat_ids_to_cmids_from_cmids($cmids);
196 $chatids = array_keys($chatidstocmids);
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]);
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);
208 * Return a dict of chat IDs mapped to their course module ID.
210 * @param array $cmids The course module IDs.
211 * @return array In the form of [$chatid => $cmid].
213 protected static function get_chat_ids_to_cmids_from_cmids(array $cmids) {
215 list($insql, $inparams) = $DB->get_in_or_equal($cmids, SQL_PARAMS_NAMED);
217 SELECT c.id, cm.id AS cmid
221 JOIN {course_modules} cm
222 ON cm.instance = c.id
225 $params = array_merge($inparams, ['chat' => 'chat']);
226 return $DB->get_records_sql_menu($sql, $params);
230 * Loop and export from a recordset.
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.
239 protected static function recordset_loop_and_export(moodle_recordset $recordset, $splitkey, $initial,
240 callable $reducer, callable $export) {
245 foreach ($recordset as $record) {
246 if ($lastid && $record->{$splitkey} != $lastid) {
247 $export($lastid, $data);
250 $data = $reducer($data, $record);
251 $lastid = $record->{$splitkey};
255 if (!empty($lastid)) {
256 $export($lastid, $data);