1 |
efrain |
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 |
* Privacy provider.
|
|
|
19 |
*
|
|
|
20 |
* File provider.php
|
|
|
21 |
* Encoding UTF-8
|
|
|
22 |
*
|
|
|
23 |
* @package tool_usersuspension
|
|
|
24 |
*
|
|
|
25 |
* @copyright Sebsoft.nl
|
|
|
26 |
* @author R.J. van Dongen <rogier@sebsoft.nl>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
|
|
|
30 |
namespace tool_usersuspension\privacy;
|
|
|
31 |
|
|
|
32 |
use core_privacy\local\metadata\collection;
|
|
|
33 |
use core_privacy\local\request\contextlist;
|
|
|
34 |
use core_privacy\local\request\approved_contextlist;
|
|
|
35 |
use core_privacy\local\request\transform;
|
|
|
36 |
use core_privacy\local\request\writer;
|
|
|
37 |
use core_privacy\local\request\userlist;
|
|
|
38 |
use core_privacy\local\request\approved_userlist;
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Privacy provider.
|
|
|
42 |
*
|
|
|
43 |
* @package tool_usersuspension
|
|
|
44 |
*
|
|
|
45 |
* @copyright Sebsoft.nl
|
|
|
46 |
* @author R.J. van Dongen <rogier@sebsoft.nl>
|
|
|
47 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
48 |
*/
|
|
|
49 |
class provider implements
|
|
|
50 |
\core_privacy\local\metadata\provider,
|
|
|
51 |
\core_privacy\local\request\plugin\provider,
|
|
|
52 |
\core_privacy\local\request\core_userlist_provider {
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Provides meta data that is stored about a user with tool_usersuspension
|
|
|
56 |
*
|
|
|
57 |
* @param collection $collection A collection of meta data items to be added to.
|
|
|
58 |
* @return collection Returns the collection of metadata.
|
|
|
59 |
*/
|
|
|
60 |
public static function get_metadata(collection $collection) : collection {
|
|
|
61 |
$collection->add_database_table(
|
|
|
62 |
'tool_usersuspension_excl',
|
|
|
63 |
[
|
|
|
64 |
'type' => 'privacy:metadata:tool_usersuspension:type',
|
|
|
65 |
'refid' => 'privacy:metadata:tool_usersuspension:userid',
|
|
|
66 |
'timecreated' => 'privacy:metadata:tool_usersuspension:timecreated',
|
|
|
67 |
],
|
|
|
68 |
'privacy:metadata:tool_usersuspension_excl'
|
|
|
69 |
);
|
|
|
70 |
$collection->add_database_table(
|
|
|
71 |
'tool_usersuspension_status',
|
|
|
72 |
[
|
|
|
73 |
'userid' => 'privacy:metadata:tool_usersuspension:userid',
|
|
|
74 |
'status' => 'privacy:metadata:tool_usersuspension:status',
|
|
|
75 |
'mailsent' => 'privacy:metadata:tool_usersuspension:mailsent',
|
|
|
76 |
'mailedto' => 'privacy:metadata:tool_usersuspension:mailedto',
|
|
|
77 |
'timecreated' => 'privacy:metadata:tool_usersuspension:timecreated',
|
|
|
78 |
],
|
|
|
79 |
'privacy:metadata:tool_usersuspension_status'
|
|
|
80 |
);
|
|
|
81 |
$collection->add_database_table(
|
|
|
82 |
'tool_usersuspension_log',
|
|
|
83 |
[
|
|
|
84 |
'userid' => 'privacy:metadata:tool_usersuspension:userid',
|
|
|
85 |
'status' => 'privacy:metadata:tool_usersuspension:status',
|
|
|
86 |
'mailsent' => 'privacy:metadata:tool_usersuspension:mailsent',
|
|
|
87 |
'mailedto' => 'privacy:metadata:tool_usersuspension:mailedto',
|
|
|
88 |
'timecreated' => 'privacy:metadata:tool_usersuspension:timecreated',
|
|
|
89 |
],
|
|
|
90 |
'privacy:metadata:tool_usersuspension_log'
|
|
|
91 |
);
|
|
|
92 |
return $collection;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Get the list of contexts that contain user information for the specified user.
|
|
|
97 |
*
|
|
|
98 |
* @param int $userid The user to search.
|
|
|
99 |
* @return contextlist $contextlist The list of contexts used in this plugin.
|
|
|
100 |
*/
|
|
|
101 |
public static function get_contexts_for_userid(int $userid) : contextlist {
|
|
|
102 |
$contextlist = new \core_privacy\local\request\contextlist();
|
|
|
103 |
// Since this system works on a global level (it hooks into the authentication system), the only context is CONTEXT_SYSTEM.
|
|
|
104 |
$contextlist->add_system_context();
|
|
|
105 |
return $contextlist;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Export all user data for the specified user, in the specified contexts, using the supplied exporter instance.
|
|
|
110 |
*
|
|
|
111 |
* @param approved_contextlist $contextlist The approved contexts to export information for.
|
|
|
112 |
*/
|
|
|
113 |
public static function export_user_data(approved_contextlist $contextlist) {
|
|
|
114 |
global $DB;
|
|
|
115 |
if (empty($contextlist->count())) {
|
|
|
116 |
return;
|
|
|
117 |
}
|
|
|
118 |
$user = $contextlist->get_user();
|
|
|
119 |
|
|
|
120 |
foreach ($contextlist->get_contexts() as $context) {
|
|
|
121 |
if ($context->contextlevel != CONTEXT_SYSTEM) {
|
|
|
122 |
continue;
|
|
|
123 |
}
|
|
|
124 |
$contextid = $context->id;
|
|
|
125 |
// Add suspension status records.
|
|
|
126 |
$sql = "SELECT ss.* FROM {tool_usersuspension_status} ss WHERE ss.userid = :userid";
|
|
|
127 |
$params = ['userid' => $user->id];
|
|
|
128 |
$alldata = [];
|
|
|
129 |
$statuses = $DB->get_recordset_sql($sql, $params);
|
|
|
130 |
foreach ($statuses as $status) {
|
|
|
131 |
$alldata[$contextid][] = (object)[
|
|
|
132 |
'userid' => $status->userid,
|
|
|
133 |
'restored' => transform::yesno($status->restored),
|
|
|
134 |
'mailsent' => transform::yesno($status->mailsent),
|
|
|
135 |
'mailedto' => $status->mailedto,
|
|
|
136 |
'timecreated' => transform::datetime($status->timecreated),
|
|
|
137 |
];
|
|
|
138 |
}
|
|
|
139 |
$statuses->close();
|
|
|
140 |
|
|
|
141 |
// The data is organised in: {? }/hammering.json.
|
|
|
142 |
// where X is the attempt number.
|
|
|
143 |
array_walk($alldata, function($statusdata, $contextid) {
|
|
|
144 |
$context = \context::instance_by_id($contextid);
|
|
|
145 |
writer::with_context($context)->export_related_data(
|
|
|
146 |
['tool_usersuspension'],
|
|
|
147 |
'statuses',
|
|
|
148 |
(object)['status' => $statusdata]
|
|
|
149 |
);
|
|
|
150 |
});
|
|
|
151 |
|
|
|
152 |
// Add suspension log records.
|
|
|
153 |
$sql = "SELECT ul.* FROM {tool_usersuspension_log} ul WHERE ul.userid = :userid";
|
|
|
154 |
$params = ['userid' => $user->id];
|
|
|
155 |
$alldata = [];
|
|
|
156 |
$statuslogs = $DB->get_recordset_sql($sql, $params);
|
|
|
157 |
foreach ($statuslogs as $statuslog) {
|
|
|
158 |
$alldata[$contextid][] = (object)[
|
|
|
159 |
'userid' => $statuslog->userid,
|
|
|
160 |
'mailedto' => $statuslog->mailedto,
|
|
|
161 |
'timecreated' => transform::datetime($statuslog->timecreated),
|
|
|
162 |
];
|
|
|
163 |
}
|
|
|
164 |
$statuslogs->close();
|
|
|
165 |
|
|
|
166 |
// The data is organised in: {?}/hammerlogs.json.
|
|
|
167 |
// where X is the attempt number.
|
|
|
168 |
array_walk($alldata, function($statuslog, $contextid) {
|
|
|
169 |
$context = \context::instance_by_id($contextid);
|
|
|
170 |
writer::with_context($context)->export_related_data(
|
|
|
171 |
['tool_usersuspension'],
|
|
|
172 |
'statuslogs',
|
|
|
173 |
(object)['statuslog' => $statuslog]
|
|
|
174 |
);
|
|
|
175 |
});
|
|
|
176 |
|
|
|
177 |
// Add suspension exception records.
|
|
|
178 |
$sql = "SELECT ue.* FROM {tool_usersuspension_excl} ue WHERE ue.refid = :userid AND ue.type = :type";
|
|
|
179 |
$params = ['userid' => $user->id, 'type' => 'user'];
|
|
|
180 |
$alldata = [];
|
|
|
181 |
$exclusions = $DB->get_recordset_sql($sql, $params);
|
|
|
182 |
foreach ($exclusions as $exclusion) {
|
|
|
183 |
$alldata[$contextid][] = (object)[
|
|
|
184 |
'userid' => $exclusion->refid,
|
|
|
185 |
'type' => $exclusion->type,
|
|
|
186 |
'timecreated' => transform::datetime($exclusion->timecreated),
|
|
|
187 |
];
|
|
|
188 |
}
|
|
|
189 |
$exclusions->close();
|
|
|
190 |
|
|
|
191 |
// The data is organised in: {?}/hammerlogs.json.
|
|
|
192 |
// where X is the attempt number.
|
|
|
193 |
array_walk($alldata, function($exclusion, $contextid) {
|
|
|
194 |
$context = \context::instance_by_id($contextid);
|
|
|
195 |
writer::with_context($context)->export_related_data(
|
|
|
196 |
['tool_usersuspension'],
|
|
|
197 |
'exclusions',
|
|
|
198 |
(object)['exclusion' => $exclusion]
|
|
|
199 |
);
|
|
|
200 |
});
|
|
|
201 |
}
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
/**
|
|
|
205 |
* Delete all use data which matches the specified context.
|
|
|
206 |
*
|
|
|
207 |
* @param context $context The module context.
|
|
|
208 |
*/
|
|
|
209 |
public static function delete_data_for_all_users_in_context(\context $context) {
|
|
|
210 |
global $DB;
|
|
|
211 |
if ($context->contextlevel != CONTEXT_SYSTEM) {
|
|
|
212 |
return;
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
// Delete exclusion records.
|
|
|
216 |
$DB->delete_records('tool_usersuspension_excl');
|
|
|
217 |
// Delete status records.
|
|
|
218 |
$DB->delete_records('tool_usersuspension_status');
|
|
|
219 |
// Delete log records.
|
|
|
220 |
$DB->delete_records('tool_usersuspension_log');
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
/**
|
|
|
224 |
* Delete all user data for the specified user, in the specified contexts.
|
|
|
225 |
*
|
|
|
226 |
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
|
|
|
227 |
*/
|
|
|
228 |
public static function delete_data_for_user(approved_contextlist $contextlist) {
|
|
|
229 |
global $DB;
|
|
|
230 |
|
|
|
231 |
if (empty($contextlist->count())) {
|
|
|
232 |
return;
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
foreach ($contextlist->get_contexts() as $context) {
|
|
|
236 |
if ($context->contextlevel != CONTEXT_SYSTEM) {
|
|
|
237 |
continue;
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
$user = $contextlist->get_user();
|
|
|
241 |
// Delete exclusion records.
|
|
|
242 |
$DB->delete_records('tool_usersuspension_excl', ['type' => 'user', 'refid' => $user->id]);
|
|
|
243 |
// Delete status records.
|
|
|
244 |
$DB->delete_records('tool_usersuspension_status', ['userid' => $user->id]);
|
|
|
245 |
// Delete log records.
|
|
|
246 |
$DB->delete_records('tool_usersuspension_log', ['userid' => $user->id]);
|
|
|
247 |
}
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
/**
|
|
|
251 |
* Get the list of users who have data within a context.
|
|
|
252 |
*
|
|
|
253 |
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
|
|
|
254 |
*/
|
|
|
255 |
public static function get_users_in_context(userlist $userlist) {
|
|
|
256 |
global $DB;
|
|
|
257 |
$context = $userlist->get_context();
|
|
|
258 |
if ($context->contextlevel != CONTEXT_SYSTEM) {
|
|
|
259 |
return;
|
|
|
260 |
}
|
|
|
261 |
// I'm unsure if we should also include the course contexts.
|
|
|
262 |
// I'm also unsure if we should include the cohort linked contexts.
|
|
|
263 |
// If we should, we'll implement those too.
|
|
|
264 |
// For now, include "all".
|
|
|
265 |
$userids1 = $DB->get_fieldset_sql('SELECT DISTINCT refid FROM {tool_usersuspension_excl} WHERE type = ?', ['user']);
|
|
|
266 |
$userids2 = $DB->get_fieldset_sql('SELECT DISTINCT userid FROM {tool_usersuspension_status}');
|
|
|
267 |
$userids3 = $DB->get_fieldset_sql('SELECT DISTINCT userid FROM {tool_usersuspension_log}');
|
|
|
268 |
$userids = array_unique(array_merge($userids1, $userids2, $userids3));
|
|
|
269 |
$userlist->add_users($userids);
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
/**
|
|
|
273 |
* Delete multiple users within a single context.
|
|
|
274 |
*
|
|
|
275 |
* @param approved_userlist $userlist The approved context and user information to delete information for.
|
|
|
276 |
*/
|
|
|
277 |
public static function delete_data_for_users(approved_userlist $userlist) {
|
|
|
278 |
global $DB;
|
|
|
279 |
$context = $userlist->get_context();
|
|
|
280 |
if ($context->contextlevel != CONTEXT_SYSTEM) {
|
|
|
281 |
return;
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
foreach ($userlist->get_userids() as $userid) {
|
|
|
285 |
$DB->delete_records('tool_usersuspension_excl', ['type' => 'user', 'refid' => $userid]);
|
|
|
286 |
$DB->delete_records('tool_usersuspension_status', ['userid' => $userid]);
|
|
|
287 |
$DB->delete_records('tool_usersuspension_log', ['userid' => $userid]);
|
|
|
288 |
}
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
}
|