mirrored from git://git.moodle.org/moodle.git
/
provider.php
399 lines (328 loc) · 15.3 KB
/
provider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Privacy Subsystem implementation for core_group.
*
* @package core_group
* @category privacy
* @copyright 2018 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_group\privacy;
defined('MOODLE_INTERNAL') || die();
use core_privacy\local\metadata\collection;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\approved_userlist;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\transform;
use core_privacy\local\request\userlist;
/**
* Privacy Subsystem implementation for core_group.
*
* @copyright 2018 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
// Groups store user data.
\core_privacy\local\metadata\provider,
// The group subsystem contains user's group memberships.
\core_privacy\local\request\subsystem\provider,
// The group subsystem can provide information to other plugins.
\core_privacy\local\request\subsystem\plugin_provider,
// This plugin is capable of determining which users have data within it.
\core_privacy\local\request\core_userlist_provider,
\core_privacy\local\request\shared_userlist_provider
{
/**
* Returns meta data about this system.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection) : collection {
$collection->add_database_table('groups_members', [
'groupid' => 'privacy:metadata:groups:groupid',
'userid' => 'privacy:metadata:groups:userid',
'timeadded' => 'privacy:metadata:groups:timeadded',
], 'privacy:metadata:groups');
$collection->link_subsystem('core_message', 'privacy:metadata:core_message');
return $collection;
}
/**
* Writes user data to the writer for the user to download.
*
* @param \context $context The context to export data for.
* @param string $component The component that is calling this function. Empty string means no component.
* @param array $subcontext The sub-context in which to export this data.
* @param int $itemid Optional itemid associated with component.
*/
public static function export_groups(\context $context, string $component, array $subcontext = [], int $itemid = 0) {
global $DB, $USER;
if (!$context instanceof \context_course) {
return;
}
$subcontext[] = get_string('groups', 'core_group');
$sql = "SELECT gm.id, gm.timeadded, gm.userid, g.name, gm.groupid
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = :courseid
AND gm.component = :component
AND gm.userid = :userid";
$params = [
'courseid' => $context->instanceid,
'component' => $component,
'userid' => $USER->id
];
if ($itemid) {
$sql .= ' AND gm.itemid = :itemid';
$params['itemid'] = $itemid;
}
$groups = $DB->get_records_sql($sql, $params);
$groupstoexport = array_map(function($group) {
return (object) [
'name' => format_string($group->name),
'timeadded' => transform::datetime($group->timeadded),
];
}, $groups);
if (!empty($groups)) {
\core_privacy\local\request\writer::with_context($context)
->export_data($subcontext, (object) [
'groups' => $groupstoexport,
]);
foreach ($groups as $group) {
// Export associated conversations to this group.
\core_message\privacy\provider::export_conversations($USER->id, 'core_group', 'groups',
$context, [], $group->groupid);
}
}
}
/**
* Deletes all group memberships for a specified context and component.
*
* @param \context $context Details about which context to delete group memberships for.
* @param string $component Component to delete. Empty string means no component (manual group memberships).
* @param int $itemid Optional itemid associated with component.
*/
public static function delete_groups_for_all_users(\context $context, string $component, int $itemid = 0) {
global $DB;
if (!$context instanceof \context_course) {
return;
}
if (!$DB->record_exists('groups', ['courseid' => $context->instanceid])) {
return;
}
$select = "component = :component AND groupid IN (SELECT g.id FROM {groups} g WHERE courseid = :courseid)";
$params = ['component' => $component, 'courseid' => $context->instanceid];
if ($itemid) {
$select .= ' AND itemid = :itemid';
$params['itemid'] = $itemid;
}
// Delete the group conversations.
$groups = $DB->get_records_select('groups_members', $select, $params);
foreach ($groups as $group) {
\core_message\privacy\provider::delete_conversations_for_all_users($context, 'core_group', 'groups', $group->groupid);
}
// Remove members from the group.
$DB->delete_records_select('groups_members', $select, $params);
// Purge the group and grouping cache for users.
\cache_helper::purge_by_definition('core', 'user_group_groupings');
}
/**
* Deletes all records for a user from a list of approved contexts.
*
* @param approved_contextlist $contextlist Contains the user ID and a list of contexts to be deleted from.
* @param string $component Component to delete from. Empty string means no component (manual memberships).
* @param int $itemid Optional itemid associated with component.
*/
public static function delete_groups_for_user(approved_contextlist $contextlist, string $component, int $itemid = 0) {
global $DB;
$userid = $contextlist->get_user()->id;
$contextids = $contextlist->get_contextids();
if (!$contextids) {
return;
}
list($contextsql, $contextparams) = $DB->get_in_or_equal($contextids, SQL_PARAMS_NAMED);
$contextparams += ['contextcourse' => CONTEXT_COURSE];
$groupselect = "SELECT g.id
FROM {groups} g
JOIN {context} ctx ON g.courseid = ctx.instanceid AND ctx.contextlevel = :contextcourse
WHERE ctx.id $contextsql";
if (!$DB->record_exists_sql($groupselect, $contextparams)) {
return;
}
$select = "userid = :userid AND component = :component AND groupid IN ({$groupselect})";
$params = ['userid' => $userid, 'component' => $component] + $contextparams;
if ($itemid) {
$select .= ' AND itemid = :itemid';
$params['itemid'] = $itemid;
}
// Delete the group conversations.
$groups = $DB->get_records_select('groups_members', $select, $params);
foreach ($groups as $group) {
\core_message\privacy\provider::delete_conversations_for_user($contextlist, 'core_group', 'groups', $group->groupid);
}
// Remove members from the group.
$DB->delete_records_select('groups_members', $select, $params);
// Invalidate the group and grouping cache for the user.
\cache_helper::invalidate_by_definition('core', 'user_group_groupings', array(), array($userid));
}
/**
* Add the list of users who are members of some groups in the specified constraints.
*
* @param userlist $userlist The userlist to add the users to.
* @param string $component The component to check.
* @param int $itemid Optional itemid associated with component.
*/
public static function get_group_members_in_context(userlist $userlist, string $component, int $itemid = 0) {
$context = $userlist->get_context();
if (!$context instanceof \context_course) {
return;
}
// Group members in the given context.
$sql = "SELECT gm.userid
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = :courseid AND gm.component = :component";
$params = [
'courseid' => $context->instanceid,
'component' => $component
];
if ($itemid) {
$sql .= ' AND gm.itemid = :itemid';
$params['itemid'] = $itemid;
}
$userlist->add_from_sql('userid', $sql, $params);
// Get the users with some group conversation in this context.
\core_message\privacy\provider::add_conversations_in_context($userlist, 'core_group', 'groups', $itemid);
}
/**
* Deletes all records for multiple users within a single context.
*
* @param approved_userlist $userlist The approved context and user information to delete information for.
* @param string $component Component to delete from. Empty string means no component (manual memberships).
* @param int $itemid Optional itemid associated with component.
*/
public static function delete_groups_for_users(approved_userlist $userlist, string $component, int $itemid = 0) {
global $DB;
$context = $userlist->get_context();
$userids = $userlist->get_userids();
list($usersql, $userparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
$groupselect = "SELECT g.id
FROM {groups} g
JOIN {context} ctx ON g.courseid = ctx.instanceid AND ctx.contextlevel = :contextcourse
WHERE ctx.id = :contextid";
$groupparams = ['contextid' => $context->id, 'contextcourse' => CONTEXT_COURSE];
$select = "component = :component AND userid {$usersql} AND groupid IN ({$groupselect})";
$params = ['component' => $component] + $groupparams + $userparams;
if ($itemid) {
$select .= ' AND itemid = :itemid';
$params['itemid'] = $itemid;
}
// Delete the group conversations for these users.
$groups = $DB->get_records_select('groups_members', $select, $params);
foreach ($groups as $group) {
\core_message\privacy\provider::delete_conversations_for_users($userlist, 'core_group', 'groups', $group->groupid);
}
$DB->delete_records_select('groups_members', $select, $params);
// Invalidate the group and grouping cache for the user.
\cache_helper::invalidate_by_definition('core', 'user_group_groupings', array(), $userids);
}
/**
* Get the list of contexts that contain group membership for the specified user.
*
* @param int $userid The user to search.
* @param string $component The component to check.
* @param int $itemid Optional itemid associated with component.
* @return contextlist The contextlist containing the list of contexts.
*/
public static function get_contexts_for_group_member(int $userid, string $component, int $itemid = 0) {
$contextlist = new contextlist();
$sql = "SELECT ctx.id
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
JOIN {context} ctx ON g.courseid = ctx.instanceid AND ctx.contextlevel = :contextcourse
WHERE gm.userid = :userid AND gm.component = :component";
$params = [
'contextcourse' => CONTEXT_COURSE,
'userid' => $userid,
'component' => $component
];
if ($itemid) {
$sql .= ' AND gm.itemid = :itemid';
$params['itemid'] = $itemid;
}
$contextlist->add_from_sql($sql, $params);
// Get the contexts where the userid has group conversations.
\core_message\privacy\provider::add_contexts_for_conversations($contextlist, $userid, 'core_group', 'groups', $itemid);
return $contextlist;
}
/**
* Get the list of users who have data within a context.
*
* @param int $userid The user to search.
* @return contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid(int $userid) : contextlist {
return static::get_contexts_for_group_member($userid, '');
}
/**
* Get the list of users who have data within a context.
*
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
*/
public static function get_users_in_context(userlist $userlist) {
$context = $userlist->get_context();
if (!$context instanceof \context_course) {
return;
}
static::get_group_members_in_context($userlist, '');
}
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
$contexts = $contextlist->get_contexts();
foreach ($contexts as $context) {
static::export_groups($context, '');
}
}
/**
* Delete all data for all users in the specified context.
*
* @param context $context The specific context to delete data for.
*/
public static function delete_data_for_all_users_in_context(\context $context) {
static::delete_groups_for_all_users($context, '');
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
static::delete_groups_for_user($contextlist, '');
}
/**
* Delete multiple users within a single context.
*
* @param approved_userlist $userlist The approved context and user information to delete information for.
*/
public static function delete_data_for_users(approved_userlist $userlist) {
static::delete_groups_for_users($userlist, '');
}
}