Merge branch 'wip-MDL-62445-master' of git://github.com/marinaglancy/moodle
[moodle.git] / admin / tool / dataprivacy / classes / api.php
blob05efe9c98dbcddfca3ad53ea565375a14916dec3
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.
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/>.
17 /**
18 * Class containing helper methods for processing data requests.
20 * @package tool_dataprivacy
21 * @copyright 2018 Jun Pataleta
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace tool_dataprivacy;
26 use coding_exception;
27 use context_system;
28 use core\invalid_persistent_exception;
29 use core\message\message;
30 use core\task\manager;
31 use core_privacy\local\request\approved_contextlist;
32 use core_privacy\local\request\contextlist_collection;
33 use core_user;
34 use dml_exception;
35 use moodle_exception;
36 use moodle_url;
37 use required_capability_exception;
38 use stdClass;
39 use tool_dataprivacy\external\data_request_exporter;
40 use tool_dataprivacy\local\helper;
41 use tool_dataprivacy\task\initiate_data_request_task;
42 use tool_dataprivacy\task\process_data_request_task;
44 defined('MOODLE_INTERNAL') || die();
46 /**
47 * Class containing helper methods for processing data requests.
49 * @copyright 2018 Jun Pataleta
50 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
52 class api {
54 /** Data export request type. */
55 const DATAREQUEST_TYPE_EXPORT = 1;
57 /** Data deletion request type. */
58 const DATAREQUEST_TYPE_DELETE = 2;
60 /** Other request type. Usually of enquiries to the DPO. */
61 const DATAREQUEST_TYPE_OTHERS = 3;
63 /** Newly submitted and we haven't yet started finding out where they have data. */
64 const DATAREQUEST_STATUS_PENDING = 0;
66 /** Newly submitted and we have started to find the location of data. */
67 const DATAREQUEST_STATUS_PREPROCESSING = 1;
69 /** Metadata ready and awaiting review and approval by the Data Protection officer. */
70 const DATAREQUEST_STATUS_AWAITING_APPROVAL = 2;
72 /** Request approved and will be processed soon. */
73 const DATAREQUEST_STATUS_APPROVED = 3;
75 /** The request is now being processed. */
76 const DATAREQUEST_STATUS_PROCESSING = 4;
78 /** Data request completed. */
79 const DATAREQUEST_STATUS_COMPLETE = 5;
81 /** Data request cancelled by the user. */
82 const DATAREQUEST_STATUS_CANCELLED = 6;
84 /** Data request rejected by the DPO. */
85 const DATAREQUEST_STATUS_REJECTED = 7;
87 /**
88 * Determines whether the user can contact the site's Data Protection Officer via Moodle.
90 * @return boolean True when tool_dataprivacy|contactdataprotectionofficer is enabled.
91 * @throws dml_exception
93 public static function can_contact_dpo() {
94 return get_config('tool_dataprivacy', 'contactdataprotectionofficer') == 1;
97 /**
98 * Check's whether the current user has the capability to manage data requests.
100 * @param int $userid The user ID.
101 * @return bool
102 * @throws coding_exception
103 * @throws dml_exception
105 public static function can_manage_data_requests($userid) {
106 $context = context_system::instance();
108 // A user can manage data requests if he/she has the site DPO role and has the capability to manage data requests.
109 return self::is_site_dpo($userid) && has_capability('tool/dataprivacy:managedatarequests', $context, $userid);
113 * Checks if the current user can manage the data registry at the provided id.
115 * @param int $contextid Fallback to system context id.
116 * @throws \required_capability_exception
117 * @return null
119 public static function check_can_manage_data_registry($contextid = false) {
120 if ($contextid) {
121 $context = \context_helper::instance_by_id($contextid);
122 } else {
123 $context = \context_system::instance();
126 require_capability('tool/dataprivacy:managedataregistry', $context);
130 * Fetches the list of users with the Data Protection Officer role.
132 * @throws dml_exception
134 public static function get_site_dpos() {
135 // Get role(s) that can manage data requests.
136 $dporoles = explode(',', get_config('tool_dataprivacy', 'dporoles'));
138 $dpos = [];
139 $context = context_system::instance();
140 foreach ($dporoles as $roleid) {
141 if (empty($roleid)) {
142 continue;
144 $allnames = get_all_user_name_fields(true, 'u');
145 $fields = 'u.id, u.confirmed, u.username, '. $allnames . ', ' .
146 'u.maildisplay, u.mailformat, u.maildigest, u.email, u.emailstop, u.city, '.
147 'u.country, u.picture, u.idnumber, u.department, u.institution, '.
148 'u.lang, u.timezone, u.lastaccess, u.mnethostid, u.auth, u.suspended, u.deleted, ' .
149 'r.name AS rolename, r.sortorder, '.
150 'r.shortname AS roleshortname, rn.name AS rolecoursealias';
151 // Fetch users that can manage data requests.
152 $dpos += get_role_users($roleid, $context, false, $fields);
155 // If the site has no data protection officer, defer to site admin(s).
156 if (empty($dpos)) {
157 $dpos = get_admins();
159 return $dpos;
163 * Checks whether a given user is a site DPO.
165 * @param int $userid The user ID.
166 * @return bool
167 * @throws dml_exception
169 public static function is_site_dpo($userid) {
170 $dpos = self::get_site_dpos();
171 return array_key_exists($userid, $dpos);
175 * Lodges a data request and sends the request details to the site Data Protection Officer(s).
177 * @param int $foruser The user whom the request is being made for.
178 * @param int $type The request type.
179 * @param string $comments Request comments.
180 * @return data_request
181 * @throws invalid_persistent_exception
182 * @throws coding_exception
184 public static function create_data_request($foruser, $type, $comments = '') {
185 global $USER;
187 $datarequest = new data_request();
188 // The user the request is being made for.
189 $datarequest->set('userid', $foruser);
191 $requestinguser = $USER->id;
192 // Check when the user is making a request on behalf of another.
193 if ($requestinguser != $foruser) {
194 if (self::is_site_dpo($requestinguser)) {
195 // The user making the request is a DPO. Should be fine.
196 $datarequest->set('dpo', $requestinguser);
197 } else {
198 // If not a DPO, only users with the capability to make data requests for the user should be allowed.
199 // (e.g. users with the Parent role, etc).
200 if (!api::can_create_data_request_for_user($foruser)) {
201 $forusercontext = \context_user::instance($foruser);
202 throw new required_capability_exception($forusercontext,
203 'tool/dataprivacy:makedatarequestsforchildren', 'nopermissions', '');
207 // The user making the request.
208 $datarequest->set('requestedby', $requestinguser);
209 // Set status.
210 $datarequest->set('status', self::DATAREQUEST_STATUS_PENDING);
211 // Set request type.
212 $datarequest->set('type', $type);
213 // Set request comments.
214 $datarequest->set('comments', $comments);
216 // Store subject access request.
217 $datarequest->create();
219 // Fire an ad hoc task to initiate the data request process.
220 $task = new initiate_data_request_task();
221 $task->set_custom_data(['requestid' => $datarequest->get('id')]);
222 manager::queue_adhoc_task($task, true);
224 return $datarequest;
228 * Fetches the list of the data requests.
230 * If user ID is provided, it fetches the data requests for the user.
231 * Otherwise, it fetches all of the data requests, provided that the user has the capability to manage data requests.
232 * (e.g. Users with the Data Protection Officer roles)
234 * @param int $userid The User ID.
235 * @return data_request[]
236 * @throws dml_exception
238 public static function get_data_requests($userid = 0) {
239 global $DB, $USER;
240 $results = [];
241 $sort = 'status ASC, timemodified ASC';
242 if ($userid) {
243 // Get the data requests for the user or data requests made by the user.
244 $select = "(userid = :userid OR requestedby = :requestedby)";
245 $params = [
246 'userid' => $userid,
247 'requestedby' => $userid
250 // Build a list of user IDs that the user is allowed to make data requests for.
251 // Of course, the user should be included in this list.
252 $alloweduserids = [$userid];
253 // Get any users that the user can make data requests for.
254 if ($children = helper::get_children_of_user($userid)) {
255 // Get the list of user IDs of the children and merge to the allowed user IDs.
256 $alloweduserids = array_merge($alloweduserids, array_keys($children));
258 list($insql, $inparams) = $DB->get_in_or_equal($alloweduserids, SQL_PARAMS_NAMED);
259 $select .= " AND userid $insql";
260 $params = array_merge($params, $inparams);
262 $results = data_request::get_records_select($select, $params, $sort);
263 } else {
264 // If the current user is one of the site's Data Protection Officers, then fetch all data requests.
265 if (self::is_site_dpo($USER->id)) {
266 $results = data_request::get_records(null, $sort, '');
270 return $results;
274 * Checks whether there is already an existing pending/in-progress data request for a user for a given request type.
276 * @param int $userid The user ID.
277 * @param int $type The request type.
278 * @return bool
279 * @throws coding_exception
280 * @throws dml_exception
282 public static function has_ongoing_request($userid, $type) {
283 global $DB;
285 // Check if the user already has an incomplete data request of the same type.
286 $nonpendingstatuses = [
287 self::DATAREQUEST_STATUS_COMPLETE,
288 self::DATAREQUEST_STATUS_CANCELLED,
289 self::DATAREQUEST_STATUS_REJECTED,
291 list($insql, $inparams) = $DB->get_in_or_equal($nonpendingstatuses, SQL_PARAMS_NAMED);
292 $select = 'type = :type AND userid = :userid AND status NOT ' . $insql;
293 $params = array_merge([
294 'type' => $type,
295 'userid' => $userid
296 ], $inparams);
298 return data_request::record_exists_select($select, $params);
302 * Determines whether a request is active or not based on its status.
304 * @param int $status The request status.
305 * @return bool
307 public static function is_active($status) {
308 // List of statuses which doesn't require any further processing.
309 $finalstatuses = [
310 self::DATAREQUEST_STATUS_COMPLETE,
311 self::DATAREQUEST_STATUS_CANCELLED,
312 self::DATAREQUEST_STATUS_REJECTED,
315 return !in_array($status, $finalstatuses);
319 * Cancels the data request for a given request ID.
321 * @param int $requestid The request identifier.
322 * @param int $status The request status.
323 * @param int $dpoid The user ID of the Data Protection Officer
324 * @param string $comment The comment about the status update.
325 * @return bool
326 * @throws invalid_persistent_exception
327 * @throws coding_exception
329 public static function update_request_status($requestid, $status, $dpoid = 0, $comment = '') {
330 // Update the request.
331 $datarequest = new data_request($requestid);
332 $datarequest->set('status', $status);
333 if ($dpoid) {
334 $datarequest->set('dpo', $dpoid);
336 $datarequest->set('dpocomment', $comment);
337 return $datarequest->update();
341 * Fetches a request based on the request ID.
343 * @param int $requestid The request identifier
344 * @return data_request
346 public static function get_request($requestid) {
347 return new data_request($requestid);
351 * Approves a data request based on the request ID.
353 * @param int $requestid The request identifier
354 * @return bool
355 * @throws coding_exception
356 * @throws dml_exception
357 * @throws invalid_persistent_exception
358 * @throws required_capability_exception
359 * @throws moodle_exception
361 public static function approve_data_request($requestid) {
362 global $USER;
364 // Check first whether the user can manage data requests.
365 if (!self::can_manage_data_requests($USER->id)) {
366 $context = context_system::instance();
367 throw new required_capability_exception($context, 'tool/dataprivacy:managedatarequests', 'nopermissions', '');
370 // Check if request is already awaiting for approval.
371 $request = new data_request($requestid);
372 if ($request->get('status') != self::DATAREQUEST_STATUS_AWAITING_APPROVAL) {
373 throw new moodle_exception('errorrequestnotwaitingforapproval', 'tool_dataprivacy');
376 // Update the status and the DPO.
377 $result = self::update_request_status($requestid, self::DATAREQUEST_STATUS_APPROVED, $USER->id);
379 // Approve all the contexts attached to the request.
380 // Currently, approving the request implicitly approves all associated contexts, but this may change in future, allowing
381 // users to selectively approve certain contexts only.
382 self::update_request_contexts_with_status($requestid, contextlist_context::STATUS_APPROVED);
384 // Fire an ad hoc task to initiate the data request process.
385 $task = new process_data_request_task();
386 $task->set_custom_data(['requestid' => $requestid]);
387 if ($request->get('type') == self::DATAREQUEST_TYPE_EXPORT) {
388 $task->set_userid($request->get('userid'));
390 manager::queue_adhoc_task($task, true);
392 return $result;
396 * Rejects a data request based on the request ID.
398 * @param int $requestid The request identifier
399 * @return bool
400 * @throws coding_exception
401 * @throws dml_exception
402 * @throws invalid_persistent_exception
403 * @throws required_capability_exception
404 * @throws moodle_exception
406 public static function deny_data_request($requestid) {
407 global $USER;
409 if (!self::can_manage_data_requests($USER->id)) {
410 $context = context_system::instance();
411 throw new required_capability_exception($context, 'tool/dataprivacy:managedatarequests', 'nopermissions', '');
414 // Check if request is already awaiting for approval.
415 $request = new data_request($requestid);
416 if ($request->get('status') != self::DATAREQUEST_STATUS_AWAITING_APPROVAL) {
417 throw new moodle_exception('errorrequestnotwaitingforapproval', 'tool_dataprivacy');
420 // Update the status and the DPO.
421 return self::update_request_status($requestid, self::DATAREQUEST_STATUS_REJECTED, $USER->id);
425 * Sends a message to the site's Data Protection Officer about a request.
427 * @param stdClass $dpo The DPO user record
428 * @param data_request $request The data request
429 * @return int|false
430 * @throws coding_exception
431 * @throws dml_exception
432 * @throws moodle_exception
434 public static function notify_dpo($dpo, data_request $request) {
435 global $PAGE, $SITE;
437 $output = $PAGE->get_renderer('tool_dataprivacy');
439 $usercontext = \context_user::instance($request->get('requestedby'));
440 $requestexporter = new data_request_exporter($request, ['context' => $usercontext]);
441 $requestdata = $requestexporter->export($output);
443 // Create message to send to the Data Protection Officer(s).
444 $typetext = null;
445 $typetext = $requestdata->typename;
446 $subject = get_string('datarequestemailsubject', 'tool_dataprivacy', $typetext);
448 $requestedby = $requestdata->requestedbyuser;
449 $datarequestsurl = new moodle_url('/admin/tool/dataprivacy/datarequests.php');
450 $message = new message();
451 $message->courseid = $SITE->id;
452 $message->component = 'tool_dataprivacy';
453 $message->name = 'contactdataprotectionofficer';
454 $message->userfrom = $requestedby->id;
455 $message->replyto = $requestedby->email;
456 $message->replytoname = $requestedby->fullname;
457 $message->subject = $subject;
458 $message->fullmessageformat = FORMAT_HTML;
459 $message->notification = 1;
460 $message->contexturl = $datarequestsurl;
461 $message->contexturlname = get_string('datarequests', 'tool_dataprivacy');
463 // Prepare the context data for the email message body.
464 $messagetextdata = [
465 'requestedby' => $requestedby->fullname,
466 'requesttype' => $typetext,
467 'requestdate' => userdate($requestdata->timecreated),
468 'requestcomments' => $requestdata->messagehtml,
469 'datarequestsurl' => $datarequestsurl
471 $requestingfor = $requestdata->foruser;
472 if ($requestedby->id == $requestingfor->id) {
473 $messagetextdata['requestfor'] = $messagetextdata['requestedby'];
474 } else {
475 $messagetextdata['requestfor'] = $requestingfor->fullname;
478 // Email the data request to the Data Protection Officer(s)/Admin(s).
479 $messagetextdata['dponame'] = fullname($dpo);
480 // Render message email body.
481 $messagehtml = $output->render_from_template('tool_dataprivacy/data_request_email', $messagetextdata);
482 $message->userto = $dpo;
483 $message->fullmessage = html_to_text($messagehtml);
484 $message->fullmessagehtml = $messagehtml;
486 // Send message.
487 return message_send($message);
491 * Checks whether a non-DPO user can make a data request for another user.
493 * @param int $user The user ID of the target user.
494 * @param int $requester The user ID of the user making the request.
495 * @return bool
496 * @throws coding_exception
498 public static function can_create_data_request_for_user($user, $requester = null) {
499 $usercontext = \context_user::instance($user);
500 return has_capability('tool/dataprivacy:makedatarequestsforchildren', $usercontext, $requester);
504 * Creates a new data purpose.
506 * @param stdClass $record
507 * @return \tool_dataprivacy\purpose.
509 public static function create_purpose(stdClass $record) {
510 self::check_can_manage_data_registry();
512 $purpose = new purpose(0, $record);
513 $purpose->create();
515 return $purpose;
519 * Updates an existing data purpose.
521 * @param stdClass $record
522 * @return \tool_dataprivacy\purpose.
524 public static function update_purpose(stdClass $record) {
525 self::check_can_manage_data_registry();
527 if (!isset($record->sensitivedatareasons)) {
528 $record->sensitivedatareasons = '';
531 $purpose = new purpose($record->id);
532 $purpose->from_record($record);
534 $result = $purpose->update();
536 return $purpose;
540 * Deletes a data purpose.
542 * @param int $id
543 * @return bool
545 public static function delete_purpose($id) {
546 self::check_can_manage_data_registry();
548 $purpose = new purpose($id);
549 if ($purpose->is_used()) {
550 throw new \moodle_exception('Purpose with id ' . $id . ' can not be deleted because it is used.');
552 return $purpose->delete();
556 * Get all system data purposes.
558 * @return \tool_dataprivacy\purpose[]
560 public static function get_purposes() {
561 self::check_can_manage_data_registry();
563 return purpose::get_records([], 'name', 'ASC');
567 * Creates a new data category.
569 * @param stdClass $record
570 * @return \tool_dataprivacy\category.
572 public static function create_category(stdClass $record) {
573 self::check_can_manage_data_registry();
575 $category = new category(0, $record);
576 $category->create();
578 return $category;
582 * Updates an existing data category.
584 * @param stdClass $record
585 * @return \tool_dataprivacy\category.
587 public static function update_category(stdClass $record) {
588 self::check_can_manage_data_registry();
590 $category = new category($record->id);
591 $category->from_record($record);
593 $result = $category->update();
595 return $category;
599 * Deletes a data category.
601 * @param int $id
602 * @return bool
604 public static function delete_category($id) {
605 self::check_can_manage_data_registry();
607 $category = new category($id);
608 if ($category->is_used()) {
609 throw new \moodle_exception('Category with id ' . $id . ' can not be deleted because it is used.');
611 return $category->delete();
615 * Get all system data categories.
617 * @return \tool_dataprivacy\category[]
619 public static function get_categories() {
620 self::check_can_manage_data_registry();
622 return category::get_records([], 'name', 'ASC');
626 * Sets the context instance purpose and category.
628 * @param \stdClass $record
629 * @return \tool_dataprivacy\context_instance
631 public static function set_context_instance($record) {
632 self::check_can_manage_data_registry($record->contextid);
634 if ($instance = context_instance::get_record_by_contextid($record->contextid, false)) {
635 // Update.
636 $instance->from_record($record);
638 if (empty($record->purposeid) && empty($record->categoryid)) {
639 // We accept one of them to be null but we delete it if both are null.
640 self::unset_context_instance($instance);
641 return;
644 } else {
645 // Add.
646 $instance = new context_instance(0, $record);
648 $instance->save();
650 return $instance;
654 * Unsets the context instance record.
656 * @param \tool_dataprivacy\context_instance $instance
657 * @return null
659 public static function unset_context_instance(context_instance $instance) {
660 self::check_can_manage_data_registry($instance->get('contextid'));
661 $instance->delete();
665 * Sets the context level purpose and category.
667 * @throws \coding_exception
668 * @param \stdClass $record
669 * @return contextlevel
671 public static function set_contextlevel($record) {
672 global $DB;
674 // Only manager at system level can set this.
675 self::check_can_manage_data_registry();
677 if ($record->contextlevel != CONTEXT_SYSTEM && $record->contextlevel != CONTEXT_USER) {
678 throw new \coding_exception('Only context system and context user can set a contextlevel ' .
679 'purpose and retention');
682 if ($contextlevel = contextlevel::get_record_by_contextlevel($record->contextlevel, false)) {
683 // Update.
684 $contextlevel->from_record($record);
685 } else {
686 // Add.
687 $contextlevel = new contextlevel(0, $record);
689 $contextlevel->save();
691 // We sync with their defaults as we removed these options from the defaults page.
692 $classname = \context_helper::get_class_for_level($record->contextlevel);
693 list($purposevar, $categoryvar) = data_registry::var_names_from_context($classname);
694 set_config($purposevar, $record->purposeid, 'tool_dataprivacy');
695 set_config($categoryvar, $record->categoryid, 'tool_dataprivacy');
697 return $contextlevel;
701 * Returns the effective category given a context instance.
703 * @param \context $context
704 * @param int $forcedvalue Use this categoryid value as if this was this context instance category.
705 * @return category|false
707 public static function get_effective_context_category(\context $context, $forcedvalue=false) {
708 self::check_can_manage_data_registry($context->id);
709 if (!data_registry::defaults_set()) {
710 return false;
713 return data_registry::get_effective_context_value($context, 'category', $forcedvalue);
717 * Returns the effective purpose given a context instance.
719 * @param \context $context
720 * @param int $forcedvalue Use this purposeid value as if this was this context instance purpose.
721 * @return purpose|false
723 public static function get_effective_context_purpose(\context $context, $forcedvalue=false) {
724 self::check_can_manage_data_registry($context->id);
725 if (!data_registry::defaults_set()) {
726 return false;
729 return data_registry::get_effective_context_value($context, 'purpose', $forcedvalue);
733 * Returns the effective category given a context level.
735 * @param int $contextlevel
736 * @param int $forcedvalue Use this categoryid value as if this was this context level category.
737 * @return category|false
739 public static function get_effective_contextlevel_category($contextlevel, $forcedvalue=false) {
740 self::check_can_manage_data_registry(\context_system::instance()->id);
741 if (!data_registry::defaults_set()) {
742 return false;
745 return data_registry::get_effective_contextlevel_value($contextlevel, 'category', $forcedvalue);
749 * Returns the effective purpose given a context level.
751 * @param int $contextlevel
752 * @param int $forcedvalue Use this purposeid value as if this was this context level purpose.
753 * @return purpose|false
755 public static function get_effective_contextlevel_purpose($contextlevel, $forcedvalue=false) {
756 self::check_can_manage_data_registry(\context_system::instance()->id);
757 if (!data_registry::defaults_set()) {
758 return false;
761 return data_registry::get_effective_contextlevel_value($contextlevel, 'purpose', $forcedvalue);
765 * Creates an expired context record for the provided context id.
767 * @param int $contextid
768 * @return \tool_dataprivacy\expired_context
770 public static function create_expired_context($contextid) {
771 self::check_can_manage_data_registry();
773 $record = (object)[
774 'contextid' => $contextid,
775 'status' => expired_context::STATUS_EXPIRED,
777 $expiredctx = new expired_context(0, $record);
778 $expiredctx->save();
780 return $expiredctx;
784 * Deletes an expired context record.
786 * @param int $id The tool_dataprivacy_ctxexpire id.
787 * @return bool True on success.
789 public static function delete_expired_context($id) {
790 self::check_can_manage_data_registry();
792 $expiredcontext = new expired_context($id);
793 return $expiredcontext->delete();
797 * Updates the status of an expired context.
799 * @param \tool_dataprivacy\expired_context $expiredctx
800 * @param int $status
801 * @return null
803 public static function set_expired_context_status(expired_context $expiredctx, $status) {
804 self::check_can_manage_data_registry();
806 $expiredctx->set('status', $status);
807 $expiredctx->save();
811 * Adds the contexts from the contextlist_collection to the request with the status provided.
813 * @param contextlist_collection $clcollection a collection of contextlists for all components.
814 * @param int $requestid the id of the request.
815 * @param int $status the status to set the contexts to.
817 public static function add_request_contexts_with_status(contextlist_collection $clcollection, int $requestid, int $status) {
818 $request = new data_request($requestid);
819 foreach ($clcollection as $contextlist) {
820 // Convert the \core_privacy\local\request\contextlist into a contextlist persistent and store it.
821 $clp = \tool_dataprivacy\contextlist::from_contextlist($contextlist);
822 $clp->create();
823 $contextlistid = $clp->get('id');
825 // Store the associated contexts in the contextlist.
826 foreach ($contextlist->get_contextids() as $contextid) {
827 if ($request->get('type') == static::DATAREQUEST_TYPE_DELETE) {
828 $context = \context::instance_by_id($contextid);
829 if (($purpose = static::get_effective_context_purpose($context)) && !empty($purpose->get('protected'))) {
830 continue;
833 $context = new contextlist_context();
834 $context->set('contextid', $contextid)
835 ->set('contextlistid', $contextlistid)
836 ->set('status', $status)
837 ->create();
840 // Create the relation to the request.
841 $requestcontextlist = request_contextlist::create_relation($requestid, $contextlistid);
842 $requestcontextlist->create();
847 * Sets the status of all contexts associated with the request.
849 * @param int $requestid the requestid to which the contexts belong.
850 * @param int $status the status to set to.
851 * @throws \dml_exception if the requestid is invalid.
852 * @throws \moodle_exception if the status is invalid.
854 public static function update_request_contexts_with_status(int $requestid, int $status) {
855 // Validate contextlist_context status using the persistent's attribute validation.
856 $contextlistcontext = new contextlist_context();
857 $contextlistcontext->set('status', $status);
858 if (array_key_exists('status', $contextlistcontext->get_errors())) {
859 throw new moodle_exception("Invalid contextlist_context status: $status");
862 // Validate requestid using the persistent's record validation.
863 // A dml_exception is thrown if the record is missing.
864 $datarequest = new data_request($requestid);
866 // Bulk update the status of the request contexts.
867 global $DB;
869 $select = "SELECT ctx.id as id
870 FROM {" . request_contextlist::TABLE . "} rcl
871 JOIN {" . contextlist::TABLE . "} cl ON rcl.contextlistid = cl.id
872 JOIN {" . contextlist_context::TABLE . "} ctx ON cl.id = ctx.contextlistid
873 WHERE rcl.requestid = ?";
875 // Fetch records IDs to be updated and update by chunks, if applicable (limit of 1000 records per update).
876 $limit = 1000;
877 $idstoupdate = $DB->get_fieldset_sql($select, [$requestid]);
878 $count = count($idstoupdate);
879 $idchunks = $idstoupdate;
880 if ($count > $limit) {
881 $idchunks = array_chunk($idstoupdate, $limit);
883 $transaction = $DB->start_delegated_transaction();
884 $initialparams = [$status];
885 foreach ($idchunks as $chunk) {
886 list($insql, $inparams) = $DB->get_in_or_equal($chunk);
887 $update = "UPDATE {" . contextlist_context::TABLE . "}
888 SET status = ?
889 WHERE id $insql";
890 $params = array_merge($initialparams, $inparams);
891 $DB->execute($update, $params);
893 $transaction->allow_commit();
897 * Finds all request contextlists having at least on approved context, and returns them as in a contextlist_collection.
899 * @param data_request $request the data request with which the contextlists are associated.
900 * @return contextlist_collection the collection of approved_contextlist objects.
902 public static function get_approved_contextlist_collection_for_request(data_request $request) : contextlist_collection {
903 $foruser = core_user::get_user($request->get('userid'));
905 // Fetch all approved contextlists and create the core_privacy\local\request\contextlist objects here.
906 global $DB;
907 $sql = "SELECT cl.component, ctx.contextid
908 FROM {" . request_contextlist::TABLE . "} rcl
909 JOIN {" . contextlist::TABLE . "} cl ON rcl.contextlistid = cl.id
910 JOIN {" . contextlist_context::TABLE . "} ctx ON cl.id = ctx.contextlistid
911 WHERE rcl.requestid = ?
912 AND ctx.status = ?
913 ORDER BY cl.component, ctx.contextid";
915 // Create the approved contextlist collection object.
916 $lastcomponent = null;
917 $approvedcollection = new contextlist_collection($foruser->id);
919 $rs = $DB->get_recordset_sql($sql, [$request->get('id'), contextlist_context::STATUS_APPROVED]);
920 foreach ($rs as $record) {
921 // If we encounter a new component, and we've built up contexts for the last, then add the approved_contextlist for the
922 // last (the one we've just finished with) and reset the context array for the next one.
923 if ($lastcomponent != $record->component) {
924 if (!empty($contexts)) {
925 $approvedcollection->add_contextlist(new approved_contextlist($foruser, $lastcomponent, $contexts));
927 $contexts = [];
930 $contexts[] = $record->contextid;
931 $lastcomponent = $record->component;
933 $rs->close();
935 // The data for the last component contextlist won't have been written yet, so write it now.
936 if (!empty($contexts)) {
937 $approvedcollection->add_contextlist(new approved_contextlist($foruser, $lastcomponent, $contexts));
940 return $approvedcollection;