1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Privacy class for requesting user data.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_stickynotes
|
|
|
21 |
* @copyright 2021 Olivier VALENTIN
|
|
|
22 |
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace mod_stickynotes\privacy;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
|
|
|
29 |
use core_privacy\local\metadata\collection;
|
|
|
30 |
use core_privacy\local\request\approved_contextlist;
|
|
|
31 |
use core_privacy\local\request\approved_userlist;
|
|
|
32 |
use core_privacy\local\request\contextlist;
|
|
|
33 |
use core_privacy\local\request\helper;
|
|
|
34 |
use core_privacy\local\request\userlist;
|
|
|
35 |
use core_privacy\local\request\writer;
|
|
|
36 |
use \core_privacy\local\request\transform;
|
|
|
37 |
|
|
|
38 |
defined('MOODLE_INTERNAL') || die();
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Implementation of the privacy subsystem plugin provider for the Stickynotes activity module.
|
|
|
42 |
*/
|
|
|
43 |
class provider implements
|
|
|
44 |
// This plugin stores personal data.
|
|
|
45 |
\core_privacy\local\metadata\provider,
|
|
|
46 |
|
|
|
47 |
// This plugin is a core_user_data_provider.
|
|
|
48 |
\core_privacy\local\request\plugin\provider,
|
|
|
49 |
|
|
|
50 |
// This plugin is capable of determining which users have data within it.
|
|
|
51 |
\core_privacy\local\request\core_userlist_provider
|
|
|
52 |
{
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Return the fields which contain personal data.
|
|
|
56 |
*
|
|
|
57 |
* @param collection $items a reference to the collection to use to store the metadata.
|
|
|
58 |
* @return collection the updated collection of metadata items.
|
|
|
59 |
*/
|
|
|
60 |
public static function get_metadata(collection $items): collection {
|
|
|
61 |
// 1. The 'stickynotes_note' table stores information about notes created by user.
|
|
|
62 |
$items->add_database_table(
|
|
|
63 |
'stickynotes_note',
|
|
|
64 |
[
|
|
|
65 |
'id' => 'privacy:metadata:stickynotes_note:id',
|
|
|
66 |
'stickyid' => 'privacy:metadata:stickynotes_note:stickyid',
|
|
|
67 |
'stickycolid' => 'privacy:metadata:stickynotes_note:stickycolid',
|
|
|
68 |
'userid' => 'privacy:metadata:stickynotes_note:userid',
|
|
|
69 |
'message' => 'privacy:metadata:stickynotes_note:message',
|
|
|
70 |
'timecreated' => 'privacy:metadata:stickynotes_note:timecreated',
|
|
|
71 |
'timemodified' => 'privacy:metadata:stickynotes_note:timemodified',
|
|
|
72 |
],
|
|
|
73 |
'privacy:metadata:stickynotes_note'
|
|
|
74 |
);
|
|
|
75 |
// 2. The 'stickynotes_vote' table stores information about which notes a user has rated.
|
|
|
76 |
$items->add_database_table(
|
|
|
77 |
'stickynotes_vote',
|
|
|
78 |
[
|
|
|
79 |
'id' => 'privacy:metadata:stickynotes_vote:id',
|
|
|
80 |
'stickyid' => 'privacy:metadata:stickynotes_vote:stickyid',
|
|
|
81 |
'stickynoteid' => 'privacy:metadata:stickynotes_vote:stickynoteid',
|
|
|
82 |
'userid' => 'privacy:metadata:stickynotes_vote:userid',
|
|
|
83 |
'vote' => 'privacy:metadata:stickynotes_vote:vote',
|
|
|
84 |
'timecreated' => 'privacy:metadata:stickynotes_vote:timecreated',
|
|
|
85 |
],
|
|
|
86 |
'privacy:metadata:stickynotes_vote'
|
|
|
87 |
);
|
|
|
88 |
|
|
|
89 |
return $items;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Get the list of contexts that contain user information for the specified user.
|
|
|
94 |
*
|
|
|
95 |
* In the case of forum, that is any forum where the user has made any post, rated any content, or has any preferences.
|
|
|
96 |
*
|
|
|
97 |
* @param int $userid The user to search.
|
|
|
98 |
* @return contextlist $contextlist The list of contexts used in this plugin.
|
|
|
99 |
*/
|
|
|
100 |
public static function get_contexts_for_userid(int $userid) : \core_privacy\local\request\contextlist {
|
|
|
101 |
$contextlist = new \core_privacy\local\request\contextlist();
|
|
|
102 |
|
|
|
103 |
$params = [
|
|
|
104 |
'modname' => 'stickynotes',
|
|
|
105 |
'contextlevel' => CONTEXT_MODULE,
|
|
|
106 |
'userid' => $userid,
|
|
|
107 |
];
|
|
|
108 |
|
|
|
109 |
// Stickynotes notes.
|
|
|
110 |
$sql = "SELECT c.id
|
|
|
111 |
FROM {context} c
|
|
|
112 |
JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = :contextlevel
|
|
|
113 |
JOIN {modules} m ON m.id = cm.module AND m.name = :modname
|
|
|
114 |
JOIN {stickynotes} s ON s.id = cm.instance
|
|
|
115 |
JOIN {stickynotes_column} sc ON sc.stickyid = s.id
|
|
|
116 |
JOIN {stickynotes_note} sn ON sn.stickycolid = sc.id
|
|
|
117 |
WHERE sn.userid = :userid
|
|
|
118 |
";
|
|
|
119 |
$contextlist->add_from_sql($sql, $params);
|
|
|
120 |
|
|
|
121 |
// Stickynotes votes.
|
|
|
122 |
$sql = "SELECT c.id
|
|
|
123 |
FROM {context} c
|
|
|
124 |
JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = :contextlevel
|
|
|
125 |
JOIN {modules} m ON m.id = cm.module AND m.name = :modname
|
|
|
126 |
JOIN {stickynotes} s ON s.id = cm.instance
|
|
|
127 |
JOIN {stickynotes_column} sc ON sc.stickyid = s.id
|
|
|
128 |
JOIN {stickynotes_note} sn ON sn.stickycolid = sc.id
|
|
|
129 |
JOIN {stickynotes_vote} sv ON sv.stickynoteid = sn.id
|
|
|
130 |
WHERE sv.userid = :userid
|
|
|
131 |
";
|
|
|
132 |
$contextlist->add_from_sql($sql, $params);
|
|
|
133 |
|
|
|
134 |
return $contextlist;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
/**
|
|
|
138 |
* Get the list of users who have data within a context.
|
|
|
139 |
*
|
|
|
140 |
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
|
|
|
141 |
*/
|
|
|
142 |
public static function get_users_in_context(userlist $userlist) {
|
|
|
143 |
$context = $userlist->get_context();
|
|
|
144 |
|
|
|
145 |
if (!is_a($context, \context_module::class)) {
|
|
|
146 |
return;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
$params = [
|
|
|
150 |
'instanceid' => $context->instanceid,
|
|
|
151 |
'modulename' => 'stickynotes',
|
|
|
152 |
];
|
|
|
153 |
|
|
|
154 |
// Notes authors.
|
|
|
155 |
$sql = "SELECT sn.userid
|
|
|
156 |
FROM {course_modules} cm
|
|
|
157 |
JOIN {modules} m ON m.id = cm.module AND m.name = :modname
|
|
|
158 |
JOIN {stickynotes} s ON s.id = cm.instance
|
|
|
159 |
JOIN {stickynotes_column} sc ON sc.stickyid = s.id
|
|
|
160 |
JOIN {stickynotes_note} sn ON sn.stickycolid = sc.id
|
|
|
161 |
WHERE cm.id = :instanceid";
|
|
|
162 |
$userlist->add_from_sql('userid', $sql, $params);
|
|
|
163 |
|
|
|
164 |
// Votes.
|
|
|
165 |
$sql = "SELECT sv.userid
|
|
|
166 |
FROM {course_modules} cm
|
|
|
167 |
JOIN {modules} m ON m.id = cm.module AND m.name = :modname
|
|
|
168 |
JOIN {stickynotes} s ON s.id = cm.instance
|
|
|
169 |
JOIN {stickynotes_column} sc ON sc.stickyid = s.id
|
|
|
170 |
JOIN {stickynotes_note} sn ON sn.stickycolid = sc.id
|
|
|
171 |
JOIN {stickynotes_vote} sv ON sv.stickynoteid = sn.id
|
|
|
172 |
WHERE cm.id = :instanceid";
|
|
|
173 |
$userlist->add_from_sql('userid', $sql, $params);
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* Export all user data for the specified user, in the specified contexts, using the supplied exporter instance.
|
|
|
178 |
*
|
|
|
179 |
* @param approved_contextlist $contextlist The approved contexts to export information for.
|
|
|
180 |
*/
|
|
|
181 |
public static function export_user_data(approved_contextlist $contextlist) {
|
|
|
182 |
global $DB, $CFG;
|
|
|
183 |
|
|
|
184 |
$userid = $contextlist->get_user()->id;
|
|
|
185 |
|
|
|
186 |
if (empty($contextlist)) {
|
|
|
187 |
return;
|
|
|
188 |
}
|
|
|
189 |
list($contextsql, $contextparams) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED);
|
|
|
190 |
$sql = "SELECT
|
|
|
191 |
c.id AS contextid,
|
|
|
192 |
s.id,
|
|
|
193 |
cm.id AS cmid
|
|
|
194 |
FROM {context} c
|
|
|
195 |
JOIN {course_modules} cm ON cm.id = c.instanceid
|
|
|
196 |
JOIN {stickynotes} s ON s.id = cm.instance
|
|
|
197 |
WHERE (
|
|
|
198 |
c.id {$contextsql}
|
|
|
199 |
)
|
|
|
200 |
";
|
|
|
201 |
// Keep a mapping of stickyid to contextid.
|
|
|
202 |
$mappings = [];
|
|
|
203 |
|
|
|
204 |
$params = ['modname' => 'stickynotes', 'contextlevel' => CONTEXT_MODULE, 'userid' => $user->id] + $contextparams;
|
|
|
205 |
$stickynotesactivities = $DB->get_recordset_sql($sql, $params);
|
|
|
206 |
|
|
|
207 |
foreach ($stickynotesactivities as $stickynotes) {
|
|
|
208 |
$mappings[$stickynotes->id] = $stickynotes->contextid;
|
|
|
209 |
|
|
|
210 |
$context = \context::instance_by_id($mappings[$stickynotes->id]);
|
|
|
211 |
$subcontext = [
|
|
|
212 |
get_string('pluginname', 'mod_stickynotes'),
|
|
|
213 |
format_string($stickynotes->name),
|
|
|
214 |
$stickynotes->id
|
|
|
215 |
];
|
|
|
216 |
|
|
|
217 |
// Get all notes created by the user.
|
|
|
218 |
$sql1notes = "SELECT id, message, timecreated, timemodified
|
|
|
219 |
from {stickynotes_note}
|
|
|
220 |
where userid = :userid
|
|
|
221 |
and stickyid = :stickynotesid";
|
|
|
222 |
|
|
|
223 |
$query1notes = $DB->get_records_sql($sql1notes, array('userid' => $userid, 'stickynotesid' => $stickynotes->id));
|
|
|
224 |
foreach ($query1notes as $query1note) {
|
|
|
225 |
$key = $query1note->id;
|
|
|
226 |
$notedata[$key] = (object) [
|
|
|
227 |
'id' => $query1note->id,
|
|
|
228 |
'message' => format_string($query1note->message, true),
|
|
|
229 |
'timecreated' => transform::datetime($query1note->timecreated),
|
|
|
230 |
'timemodified' => transform::datetime($query1note->timemodified),
|
|
|
231 |
];
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
// Get all votes of this user.
|
|
|
235 |
$sql2notes = "SELECT sv.id as idvote, sv.vote, sv.timecreated, sn.id as idnote
|
|
|
236 |
from {stickynotes_note} sn
|
|
|
237 |
join {stickynotes_vote} sv on sv.stickynoteid = sn.id
|
|
|
238 |
where sv.userid = :userid
|
|
|
239 |
and sv.stickyid = :stickynotesid";
|
|
|
240 |
|
|
|
241 |
$query2notes = $DB->get_records_sql($sql2notes, array('userid' => $userid, 'stickynotesid' => $stickynotes->id));
|
|
|
242 |
foreach ($query2notes as $query2note) {
|
|
|
243 |
$key = $query2note->idnote;
|
|
|
244 |
$votedata[$key] = (object) [
|
|
|
245 |
'idnote' => $query2note->idnote,
|
|
|
246 |
'vote' => $query2note->vote,
|
|
|
247 |
'timecreated' => transform::datetime($query2note->timecreated),
|
|
|
248 |
];
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
$stickynotes->notes = $notedata;
|
|
|
252 |
$stickynotes->votes = $votedata;
|
|
|
253 |
unset($notedata);
|
|
|
254 |
unset($votedata);
|
|
|
255 |
|
|
|
256 |
writer::with_context($context)->export_data([], $stickynotes);
|
|
|
257 |
}
|
|
|
258 |
$stickynotesactivities->close();
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
/**
|
|
|
262 |
* Delete all data for all users in the specified context.
|
|
|
263 |
*
|
|
|
264 |
* @param \context $context the context to delete in.
|
|
|
265 |
*/
|
|
|
266 |
public static function delete_data_for_all_users_in_context(\context $context) {
|
|
|
267 |
global $DB;
|
|
|
268 |
|
|
|
269 |
if (!$context instanceof \context_module) {
|
|
|
270 |
return;
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
if ($cm = get_coursemodule_from_id('stickynotes', $context->instanceid)) {
|
|
|
274 |
$DB->delete_records('stickynotes_note', ['stickyid' => $cm->instance]);
|
|
|
275 |
$DB->delete_records('stickynotes_vote', ['stickyid' => $cm->instance]);
|
|
|
276 |
}
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
/**
|
|
|
280 |
* Delete all user data for the specified user, in the specified contexts.
|
|
|
281 |
*
|
|
|
282 |
* @param approved_contextlist $contextlist a list of contexts approved for deletion.
|
|
|
283 |
*/
|
|
|
284 |
public static function delete_data_for_user(approved_contextlist $contextlist) {
|
|
|
285 |
global $DB;
|
|
|
286 |
|
|
|
287 |
if (empty($contextlist->count())) {
|
|
|
288 |
return;
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
$userid = $contextlist->get_user()->id;
|
|
|
292 |
foreach ($contextlist->get_contexts() as $context) {
|
|
|
293 |
|
|
|
294 |
if (!$context instanceof \context_module) {
|
|
|
295 |
continue;
|
|
|
296 |
}
|
|
|
297 |
$instanceid = $DB->get_field('course_modules', 'instance', ['id' => $context->instanceid]);
|
|
|
298 |
if (!$instanceid) {
|
|
|
299 |
continue;
|
|
|
300 |
}
|
|
|
301 |
$DB->delete_records('stickynotes_note', ['stickyid' => $instanceid, 'userid' => $userid]);
|
|
|
302 |
$DB->delete_records('stickynotes_vote', ['stickyid' => $instanceid, 'userid' => $userid]);
|
|
|
303 |
}
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
/**
|
|
|
307 |
* Delete multiple users within a single context.
|
|
|
308 |
*
|
|
|
309 |
* @param approved_userlist $userlist The approved context and user information to delete information for.
|
|
|
310 |
*/
|
|
|
311 |
public static function delete_data_for_users(approved_userlist $userlist) {
|
|
|
312 |
global $DB;
|
|
|
313 |
|
|
|
314 |
$context = $userlist->get_context();
|
|
|
315 |
|
|
|
316 |
if (!$context instanceof \context_module) {
|
|
|
317 |
return;
|
|
|
318 |
}
|
|
|
319 |
|
|
|
320 |
$cm = get_coursemodule_from_id('stickynotes', $context->instanceid);
|
|
|
321 |
|
|
|
322 |
if (!$cm) {
|
|
|
323 |
// Only stickynotes module will be handled.
|
|
|
324 |
return;
|
|
|
325 |
}
|
|
|
326 |
|
|
|
327 |
$userids = $userlist->get_userids();
|
|
|
328 |
list($usersql, $userparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
|
|
|
329 |
|
|
|
330 |
$select = "stickyid = :stickyid AND userid $usersql";
|
|
|
331 |
$params = ['stickyid' => $cm->instance] + $userparams;
|
|
|
332 |
$DB->delete_records_select('stickynotes_note', $select, $params);
|
|
|
333 |
$DB->delete_records_select('stickynotes_note', $select, $params);
|
|
|
334 |
}
|
|
|
335 |
}
|