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 class for requesting user data.
|
|
|
19 |
*
|
|
|
20 |
* @package tool_dataprivacy
|
|
|
21 |
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace tool_dataprivacy\privacy;
|
|
|
26 |
defined('MOODLE_INTERNAL') || die();
|
|
|
27 |
|
|
|
28 |
use coding_exception;
|
|
|
29 |
use context;
|
|
|
30 |
use context_user;
|
|
|
31 |
use core_privacy\local\metadata\collection;
|
|
|
32 |
use core_privacy\local\request\approved_contextlist;
|
|
|
33 |
use \core_privacy\local\request\approved_userlist;
|
|
|
34 |
use core_privacy\local\request\contextlist;
|
|
|
35 |
use core_privacy\local\request\helper;
|
|
|
36 |
use core_privacy\local\request\transform;
|
|
|
37 |
use \core_privacy\local\request\userlist;
|
|
|
38 |
use core_privacy\local\request\writer;
|
|
|
39 |
use dml_exception;
|
|
|
40 |
use stdClass;
|
|
|
41 |
use tool_dataprivacy\api;
|
|
|
42 |
use tool_dataprivacy\local\helper as tool_helper;
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Privacy class for requesting user data.
|
|
|
46 |
*
|
|
|
47 |
* @package tool_dataprivacy
|
|
|
48 |
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
|
|
|
49 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
50 |
*/
|
|
|
51 |
class provider implements
|
|
|
52 |
// This tool stores user data.
|
|
|
53 |
\core_privacy\local\metadata\provider,
|
|
|
54 |
|
|
|
55 |
// This plugin is capable of determining which users have data within it.
|
|
|
56 |
\core_privacy\local\request\core_userlist_provider,
|
|
|
57 |
|
|
|
58 |
// This tool may provide access to and deletion of user data.
|
|
|
59 |
\core_privacy\local\request\plugin\provider,
|
|
|
60 |
|
|
|
61 |
// This plugin has some sitewide user preferences to export.
|
|
|
62 |
\core_privacy\local\request\user_preference_provider {
|
|
|
63 |
/**
|
|
|
64 |
* Returns meta data about this system.
|
|
|
65 |
*
|
|
|
66 |
* @param collection $collection The initialised collection to add items to.
|
|
|
67 |
* @return collection A listing of user data stored through this system.
|
|
|
68 |
*/
|
|
|
69 |
public static function get_metadata(collection $collection): collection {
|
|
|
70 |
$collection->add_database_table(
|
|
|
71 |
'tool_dataprivacy_request',
|
|
|
72 |
[
|
|
|
73 |
'comments' => 'privacy:metadata:request:comments',
|
|
|
74 |
'userid' => 'privacy:metadata:request:userid',
|
|
|
75 |
'requestedby' => 'privacy:metadata:request:requestedby',
|
|
|
76 |
'dpocomment' => 'privacy:metadata:request:dpocomment',
|
|
|
77 |
'timecreated' => 'privacy:metadata:request:timecreated'
|
|
|
78 |
],
|
|
|
79 |
'privacy:metadata:request'
|
|
|
80 |
);
|
|
|
81 |
|
|
|
82 |
// Regarding this block, we are unable to export or purge this data, as
|
|
|
83 |
// it would damage the privacy data across the whole site.
|
|
|
84 |
$collection->add_database_table(
|
|
|
85 |
'tool_dataprivacy_purposerole',
|
|
|
86 |
[
|
|
|
87 |
'usermodified' => 'privacy:metadata:purpose:usermodified',
|
|
|
88 |
],
|
|
|
89 |
'privacy:metadata:purpose'
|
|
|
90 |
);
|
|
|
91 |
|
|
|
92 |
$collection->add_user_preference(tool_helper::PREF_REQUEST_FILTERS,
|
|
|
93 |
'privacy:metadata:preference:tool_dataprivacy_request-filters');
|
|
|
94 |
$collection->add_user_preference(tool_helper::PREF_REQUEST_PERPAGE,
|
|
|
95 |
'privacy:metadata:preference:tool_dataprivacy_request-perpage');
|
|
|
96 |
|
|
|
97 |
return $collection;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Get the list of contexts that contain user information for the specified user.
|
|
|
102 |
*
|
|
|
103 |
* @param int $userid The user to search.
|
|
|
104 |
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
|
|
|
105 |
*/
|
|
|
106 |
public static function get_contexts_for_userid(int $userid): contextlist {
|
|
|
107 |
$sql = "SELECT id
|
|
|
108 |
FROM {context}
|
|
|
109 |
WHERE instanceid = :userid
|
|
|
110 |
AND contextlevel = :contextlevel";
|
|
|
111 |
|
|
|
112 |
$contextlist = new contextlist();
|
|
|
113 |
$contextlist->set_component('tool_dataprivacy');
|
|
|
114 |
$contextlist->add_from_sql($sql, ['userid' => $userid, 'contextlevel' => CONTEXT_USER]);
|
|
|
115 |
return $contextlist;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Get the list of users who have data within a context.
|
|
|
120 |
*
|
|
|
121 |
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
|
|
|
122 |
*
|
|
|
123 |
*/
|
|
|
124 |
public static function get_users_in_context(userlist $userlist) {
|
|
|
125 |
$context = $userlist->get_context();
|
|
|
126 |
|
|
|
127 |
if (!is_a($context, \context_user::class)) {
|
|
|
128 |
return;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
$params = [
|
|
|
132 |
'contextlevel' => CONTEXT_USER,
|
|
|
133 |
'contextid' => $context->id,
|
|
|
134 |
];
|
|
|
135 |
|
|
|
136 |
$sql = "SELECT instanceid AS userid
|
|
|
137 |
FROM {context}
|
|
|
138 |
WHERE id = :contextid
|
|
|
139 |
AND contextlevel = :contextlevel";
|
|
|
140 |
|
|
|
141 |
$userlist->add_from_sql('userid', $sql, $params);
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
/**
|
|
|
145 |
* Export all user data for the specified user, in the specified contexts.
|
|
|
146 |
*
|
|
|
147 |
* @param approved_contextlist $contextlist The approved contexts to export information for.
|
|
|
148 |
* @throws coding_exception
|
|
|
149 |
* @throws dml_exception
|
|
|
150 |
* @throws \moodle_exception
|
|
|
151 |
*/
|
|
|
152 |
public static function export_user_data(approved_contextlist $contextlist) {
|
|
|
153 |
if (empty($contextlist->count())) {
|
|
|
154 |
return;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
$user = $contextlist->get_user();
|
|
|
158 |
$datarequests = api::get_data_requests($user->id);
|
|
|
159 |
$context = context_user::instance($user->id);
|
|
|
160 |
$contextdatatowrite = [];
|
|
|
161 |
foreach ($datarequests as $request) {
|
|
|
162 |
$record = $request->to_record();
|
|
|
163 |
$data = new stdClass();
|
|
|
164 |
|
|
|
165 |
// The user ID that made the request/the request is made for.
|
|
|
166 |
if ($record->requestedby != $record->userid) {
|
|
|
167 |
if ($user->id != $record->requestedby) {
|
|
|
168 |
// This request is done by this user for another user.
|
|
|
169 |
$data->userid = fullname($user);
|
|
|
170 |
} else if ($user->id != $record->userid) {
|
|
|
171 |
// This request was done by another user on behalf of this user.
|
|
|
172 |
$data->requestedby = fullname($user);
|
|
|
173 |
}
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
// Request type.
|
|
|
177 |
$data->type = tool_helper::get_shortened_request_type_string($record->type);
|
|
|
178 |
// Status.
|
|
|
179 |
$data->status = tool_helper::get_request_status_string($record->status);
|
|
|
180 |
// Creation method.
|
|
|
181 |
$data->creationmethod = tool_helper::get_request_creation_method_string($record->creationmethod);
|
|
|
182 |
// Comments.
|
|
|
183 |
$data->comments = $record->comments;
|
|
|
184 |
// The DPO's comment about this request.
|
|
|
185 |
$data->dpocomment = $record->dpocomment;
|
|
|
186 |
// The date and time this request was lodged.
|
|
|
187 |
$data->timecreated = transform::datetime($record->timecreated);
|
|
|
188 |
$contextdatatowrite[] = $data;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
// User context / Privacy and policies / Data requests.
|
|
|
192 |
$subcontext = [
|
|
|
193 |
get_string('privacyandpolicies', 'admin'),
|
|
|
194 |
get_string('datarequests', 'tool_dataprivacy'),
|
|
|
195 |
];
|
|
|
196 |
writer::with_context($context)->export_data($subcontext, (object)$contextdatatowrite);
|
|
|
197 |
|
|
|
198 |
// Write generic module intro files.
|
|
|
199 |
helper::export_context_files($context, $user);
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
/**
|
|
|
203 |
* Delete all data for all users in the specified context.
|
|
|
204 |
*
|
|
|
205 |
* @param context $context The specific context to delete data for.
|
|
|
206 |
*/
|
|
|
207 |
public static function delete_data_for_all_users_in_context(context $context) {
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
/**
|
|
|
211 |
* Delete all user data for the specified user, in the specified contexts.
|
|
|
212 |
*
|
|
|
213 |
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
|
|
|
214 |
*/
|
|
|
215 |
public static function delete_data_for_user(approved_contextlist $contextlist) {
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
/**
|
|
|
219 |
* Delete multiple users within a single context.
|
|
|
220 |
*
|
|
|
221 |
* @param approved_userlist $userlist The approved context and user information to delete information for.
|
|
|
222 |
*
|
|
|
223 |
*/
|
|
|
224 |
public static function delete_data_for_users(approved_userlist $userlist) {
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
/**
|
|
|
228 |
* Export all user preferences for the plugin.
|
|
|
229 |
*
|
|
|
230 |
* @param int $userid The userid of the user whose data is to be exported.
|
|
|
231 |
*/
|
|
|
232 |
public static function export_user_preferences(int $userid) {
|
|
|
233 |
$preffilter = get_user_preferences(tool_helper::PREF_REQUEST_FILTERS, null, $userid);
|
|
|
234 |
if ($preffilter !== null) {
|
|
|
235 |
$filters = json_decode($preffilter);
|
|
|
236 |
$descriptions = [];
|
|
|
237 |
foreach ($filters as $filter) {
|
|
|
238 |
list($category, $value) = explode(':', $filter);
|
|
|
239 |
$option = new stdClass();
|
|
|
240 |
switch($category) {
|
|
|
241 |
case tool_helper::FILTER_TYPE:
|
|
|
242 |
$option->category = get_string('requesttype', 'tool_dataprivacy');
|
|
|
243 |
$option->name = tool_helper::get_shortened_request_type_string($value);
|
|
|
244 |
break;
|
|
|
245 |
case tool_helper::FILTER_STATUS:
|
|
|
246 |
$option->category = get_string('requeststatus', 'tool_dataprivacy');
|
|
|
247 |
$option->name = tool_helper::get_request_status_string($value);
|
|
|
248 |
break;
|
|
|
249 |
case tool_helper::FILTER_CREATION:
|
|
|
250 |
$option->category = get_string('requestcreation', 'tool_dataprivacy');
|
|
|
251 |
$option->name = tool_helper::get_request_creation_method_string($value);
|
|
|
252 |
break;
|
|
|
253 |
}
|
|
|
254 |
$descriptions[] = get_string('filteroption', 'tool_dataprivacy', $option);
|
|
|
255 |
}
|
|
|
256 |
// Export the filter preference as comma-separated values and text descriptions.
|
|
|
257 |
$values = implode(', ', $filters);
|
|
|
258 |
$descriptionstext = implode(', ', $descriptions);
|
|
|
259 |
writer::export_user_preference('tool_dataprivacy', tool_helper::PREF_REQUEST_FILTERS, $values, $descriptionstext);
|
|
|
260 |
}
|
|
|
261 |
|
|
|
262 |
$prefperpage = get_user_preferences(tool_helper::PREF_REQUEST_PERPAGE, null, $userid);
|
|
|
263 |
if ($prefperpage !== null) {
|
|
|
264 |
writer::export_user_preference('tool_dataprivacy', tool_helper::PREF_REQUEST_PERPAGE, $prefperpage,
|
|
|
265 |
get_string('privacy:metadata:preference:tool_dataprivacy_request-perpage', 'tool_dataprivacy'));
|
|
|
266 |
}
|
|
|
267 |
}
|
|
|
268 |
}
|