MDL-63919 privacy: Disable notifications for auto created data requests
[moodle.git] / admin / tool / dataprivacy / classes / task / initiate_data_request_task.php
CommitLineData
5efc1f9e
DM
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/**
cb775057 18 * Adhoc task that processes a data request and prepares the user's relevant contexts for review.
5efc1f9e
DM
19 *
20 * @package tool_dataprivacy
21 * @copyright 2018 Jun Pataleta
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace tool_dataprivacy\task;
26
27use coding_exception;
28use core\task\adhoc_task;
29use moodle_exception;
30use tool_dataprivacy\api;
cb775057 31use tool_dataprivacy\contextlist_context;
5efc1f9e
DM
32use tool_dataprivacy\data_request;
33
34defined('MOODLE_INTERNAL') || die();
35
36/**
cb775057 37 * Class that processes a data request and prepares the user's relevant contexts for review.
5efc1f9e
DM
38 *
39 * Custom data accepted:
40 * - requestid -> The ID of the data request to be processed.
41 *
42 * @package tool_dataprivacy
43 * @copyright 2018 Jun Pataleta
44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 */
46class initiate_data_request_task extends adhoc_task {
47
48 /**
49 * Run the task to initiate the data request process.
50 *
51 * @throws coding_exception
52 * @throws moodle_exception
53 */
54 public function execute() {
55 global $CFG;
56
c6363e8c 57 require_once($CFG->dirroot . "/{$CFG->admin}/tool/dataprivacy/lib.php");
5efc1f9e
DM
58
59 if (!isset($this->get_custom_data()->requestid)) {
60 throw new coding_exception('The custom data \'requestid\' is required.');
61 }
62 $requestid = $this->get_custom_data()->requestid;
63
64 $datarequest = new data_request($requestid);
65
66 // Check if this request still needs to be processed. e.g. The user might have cancelled it before this task has run.
67 $status = $datarequest->get('status');
68 if (!api::is_active($status)) {
69 mtrace('Request ' . $requestid . ' with status ' . $status . ' doesn\'t need to be processed. Skipping...');
70 return;
71 }
72
73 // Update the status of this request as pre-processing.
cb775057 74 mtrace('Generating the contexts containing personal data for the user...');
5efc1f9e
DM
75 api::update_request_status($requestid, api::DATAREQUEST_STATUS_PREPROCESSING);
76
cb775057 77 // Add the list of relevant contexts to the request, and mark all as pending approval.
8760b733
AN
78 $privacymanager = new \core_privacy\manager();
79 $privacymanager->set_observer(new \tool_dataprivacy\manager_observer());
80
cb775057
JP
81 $contextlistcollection = $privacymanager->get_contexts_for_userid($datarequest->get('userid'));
82 api::add_request_contexts_with_status($contextlistcollection, $requestid, contextlist_context::STATUS_PENDING);
5efc1f9e 83
cb775057 84 // When the preparation of the contexts finishes, update the request status to awaiting approval.
5efc1f9e 85 api::update_request_status($requestid, api::DATAREQUEST_STATUS_AWAITING_APPROVAL);
cb775057 86 mtrace('Context generation complete...');
5efc1f9e
DM
87
88 // Get the list of the site Data Protection Officers.
89 $dpos = api::get_site_dpos();
90
77c21518
MG
91 // We should prevent DPO(s)/Admin(s) being flooded with notifications for each request when bulk delete
92 // data requests are being created.
93 // NOTE: This should be improved, we should not totally disable the notifications for automatically
94 // created requests. Possibly, we should create one notification for these such cases.
95 if ($datarequest->get('creationmethod') != data_request::DATAREQUEST_CREATION_AUTO) {
96 // Email the data request to the Data Protection Officer(s)/Admin(s).
97 foreach ($dpos as $dpo) {
98 $dponame = fullname($dpo);
99 if (api::notify_dpo($dpo, $datarequest)) {
100 mtrace('Message sent to DPO: ' . $dponame);
101 } else {
102 mtrace('A problem was encountered while sending the message to the DPO: ' . $dponame);
103 }
5efc1f9e
DM
104 }
105 }
106 }
107}