MDL-61899 tool_dataprivacy: Implement data export for the tool
[moodle.git] / admin / tool / dataprivacy / classes / privacy / provider.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/**
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
25namespace tool_dataprivacy\privacy;
a8a69050 26defined('MOODLE_INTERNAL') || die();
5efc1f9e 27
2076d34b
JP
28use coding_exception;
29use context;
30use context_user;
5efc1f9e
DM
31use core_privacy\local\metadata\collection;
32use core_privacy\local\request\approved_contextlist;
5efc1f9e 33use core_privacy\local\request\contextlist;
2076d34b
JP
34use core_privacy\local\request\helper;
35use core_privacy\local\request\transform;
36use core_privacy\local\request\writer;
37use dml_exception;
38use stdClass;
39use tool_dataprivacy\api;
40use tool_dataprivacy\local\helper as tool_helper;
5efc1f9e 41
a8a69050
DM
42/**
43 * Privacy class for requesting user data.
44 *
45 * @package tool_dataprivacy
46 * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
47 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48 */
5efc1f9e
DM
49class provider implements
50 // This tool stores user data.
51 \core_privacy\local\metadata\provider,
52
53 // This tool may provide access to and deletion of user data.
54 \core_privacy\local\request\plugin\provider {
55 /**
56 * Returns meta data about this system.
57 *
58 * @param collection $collection The initialised collection to add items to.
59 * @return collection A listing of user data stored through this system.
60 */
a8a69050 61 public static function get_metadata(collection $collection) : collection {
5efc1f9e
DM
62 $collection->add_database_table(
63 'tool_dataprivacy_request',
64 [
65 'comments' => 'privacy:metadata:request:comments',
66 'userid' => 'privacy:metadata:request:userid',
67 'requestedby' => 'privacy:metadata:request:requestedby',
68 'dpocomment' => 'privacy:metadata:request:dpocomment',
69 'timecreated' => 'privacy:metadata:request:timecreated'
70 ],
71 'privacy:metadata:request'
72 );
73 return $collection;
74 }
75
76 /**
77 * Get the list of contexts that contain user information for the specified user.
78 *
79 * @param int $userid The user to search.
80 * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
81 */
a8a69050 82 public static function get_contexts_for_userid(int $userid) : contextlist {
2076d34b
JP
83 $sql = "SELECT id
84 FROM {context}
85 WHERE instanceid = :userid
86 AND contextlevel = :contextlevel";
87
88 $contextlist = new contextlist();
89 $contextlist->set_component('tool_dataprivacy');
90 $contextlist->add_from_sql($sql, ['userid' => $userid, 'contextlevel' => CONTEXT_USER]);
91 return $contextlist;
5efc1f9e
DM
92 }
93
94 /**
95 * Export all user data for the specified user, in the specified contexts.
96 *
2076d34b
JP
97 * @param approved_contextlist $contextlist The approved contexts to export information for.
98 * @throws coding_exception
99 * @throws dml_exception
100 * @throws \moodle_exception
5efc1f9e
DM
101 */
102 public static function export_user_data(approved_contextlist $contextlist) {
2076d34b
JP
103 if (empty($contextlist->count())) {
104 return;
105 }
106
107 $user = $contextlist->get_user();
108 $datarequests = api::get_data_requests($user->id);
109 $context = context_user::instance($user->id);
110 $contextdatatowrite = [];
111 foreach ($datarequests as $request) {
112 $record = $request->to_record();
113 $data = new stdClass();
114
115 // The user ID that made the request/the request is made for.
116 if ($record->requestedby != $record->userid) {
117 if ($user->id != $record->requestedby) {
118 // This request is done by this user for another user.
119 $data->userid = fullname($user);
120 } else if ($user->id != $record->userid) {
121 // This request was done by another user on behalf of this user.
122 $data->requestedby = fullname($user);
123 }
124 }
125
126 // Request type.
127 $data->type = tool_helper::get_shortened_request_type_string($record->type);
128 // Status.
129 $data->status = tool_helper::get_request_status_string($record->status);
130 // Comments.
131 $data->comments = $record->comments;
132 // The DPO's comment about this request.
133 $data->dpocomment = $record->dpocomment;
134 // The date and time this request was lodged.
135 $data->timecreated = transform::datetime($record->timecreated);
136 $contextdatatowrite[] = $data;
137 }
138
139 // {User context} / Privacy and policies / Data requests.
140 $subcontext = [
141 get_string('privacyandpolicies', 'admin'),
142 get_string('datarequests', 'tool_dataprivacy'),
143 ];
144 writer::with_context($context)->export_data($subcontext, (object)$contextdatatowrite);
145
146 // Write generic module intro files.
147 helper::export_context_files($context, $user);
5efc1f9e
DM
148 }
149
150 /**
151 * Delete all data for all users in the specified context.
152 *
2076d34b 153 * @param context $context The specific context to delete data for.
5efc1f9e 154 */
2076d34b 155 public static function delete_data_for_all_users_in_context(context $context) {
5efc1f9e
DM
156 }
157
158 /**
159 * Delete all user data for the specified user, in the specified contexts.
160 *
161 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
162 */
163 public static function delete_data_for_user(approved_contextlist $contextlist) {
164 }
165}