MDL-63666 logging: Add support for removal of context users.
[moodle.git] / admin / tool / log / store / database / classes / privacy / provider.php
CommitLineData
0f3bcbb9
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 logstore_database
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 logstore_database\privacy;
27defined('MOODLE_INTERNAL') || die();
28
29use context;
30use core_privacy\local\metadata\collection;
31use core_privacy\local\request\contextlist;
32
33/**
34 * Data provider class.
35 *
36 * @package logstore_database
37 * @copyright 2018 Frédéric Massart
38 * @author Frédéric Massart <fred@branchup.tech>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class provider implements
42 \core_privacy\local\metadata\provider,
ac7d086d
AG
43 \tool_log\local\privacy\logstore_provider,
44 \tool_log\local\privacy\logstore_userlist_provider {
0f3bcbb9
FM
45
46 use \tool_log\local\privacy\moodle_database_export_and_delete;
47
48 /**
49 * Returns metadata.
50 *
51 * @param collection $collection The initialised collection to add items to.
52 * @return collection A listing of user data stored through this system.
53 */
54 public static function get_metadata(collection $collection) : collection {
55 $collection->add_external_location_link('log', [
56 'eventname' => 'privacy:metadata:log:eventname',
57 'userid' => 'privacy:metadata:log:userid',
58 'relateduserid' => 'privacy:metadata:log:relateduserid',
59 'anonymous' => 'privacy:metadata:log:anonymous',
60 'other' => 'privacy:metadata:log:other',
61 'timecreated' => 'privacy:metadata:log:timecreated',
62 'origin' => 'privacy:metadata:log:origin',
63 'ip' => 'privacy:metadata:log:ip',
64 'realuserid' => 'privacy:metadata:log:realuserid',
65 ], 'privacy:metadata:log');
66 return $collection;
67 }
68
69 /**
70 * Add contexts that contain user information for the specified user.
71 *
72 * @param contextlist $contextlist The contextlist to add the contexts to.
73 * @param int $userid The user to find the contexts for.
74 * @return void
75 */
76 public static function add_contexts_for_userid(contextlist $contextlist, $userid) {
77 list($db, $table) = static::get_database_and_table();
78 if (!$db || !$table) {
79 return;
80 }
81
82 $sql = 'userid = :userid1 OR relateduserid = :userid2 OR realuserid = :userid3';
83 $params = ['userid1' => $userid, 'userid2' => $userid, 'userid3' => $userid];
84 $contextids = $db->get_fieldset_select($table, 'DISTINCT contextid', $sql, $params);
85 if (empty($contextids)) {
86 return;
87 }
88
89 $sql = implode(' UNION ', array_map(function($id) use ($db) {
90 return 'SELECT ' . $id . $db->sql_null_from_clause();
91 }, $contextids));
92 $contextlist->add_from_sql($sql, []);
93 }
94
ac7d086d
AG
95 /**
96 * Add user IDs that contain user information for the specified context.
97 *
98 * @param \core_privacy\local\request\userlist $userlist The userlist to add the users to.
99 * @return void
100 */
101 public static function add_userids_for_context(\core_privacy\local\request\userlist $userlist) {
102 list($db, $table) = static::get_database_and_table();
103 if (!$db || !$table) {
104 return;
105 }
106
107 $userids = [];
108 $records = $db->get_records($table, ['contextid' => $userlist->get_context()->id], '',
109 'id, userid, relateduserid, realuserid');
110 if (empty($records)) {
111 return;
112 }
113
114 foreach ($records as $record) {
115 $userids[] = $record->userid;
116 if (!empty($record->relateduserid)) {
117 $userids[] = $record->relateduserid;
118 }
119 if (!empty($record->realuserid)) {
120 $userids[] = $record->realuserid;
121 }
122 }
123 $userids = array_unique($userids);
124 $userlist->add_users($userids);
125 }
126
0f3bcbb9
FM
127 /**
128 * Get the database object.
129 *
130 * @return array Containing moodle_database, string, or null values.
131 */
132 protected static function get_database_and_table() {
133 $manager = get_log_manager();
134 $store = new \logstore_database\log\store($manager);
135 $db = $store->get_extdb();
136 return $db ? [$db, $store->get_config_value('dbtable')] : [null, null];
137 }
138
139 /**
140 * Get the path to export the logs to.
141 *
142 * @return array
143 */
144 protected static function get_export_subcontext() {
145 return [get_string('privacy:path:logs', 'tool_log'), get_string('pluginname', 'logstore_database')];
146 }
147}