Moodle release 3.5rc1
[moodle.git] / admin / tool / policy / classes / privacy / provider.php
CommitLineData
24966a26
SA
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 Subsystem implementation for tool_policy.
19 *
20 * @package tool_policy
21 * @copyright 2018 Sara Arjona <sara@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace tool_policy\privacy;
26
27use core_privacy\local\metadata\collection;
28use core_privacy\local\request\approved_contextlist;
29use core_privacy\local\request\contextlist;
432a90f1
MG
30use core_privacy\local\request\moodle_content_writer;
31use core_privacy\local\request\transform;
32use core_privacy\local\request\writer;
24966a26
SA
33
34defined('MOODLE_INTERNAL') || die();
35
36/**
37 * Implementation of the privacy subsystem plugin provider for the policy tool.
38 *
39 * @copyright 2018 Sara Arjona <sara@moodle.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class provider implements
43 // This tool stores user data.
44 \core_privacy\local\metadata\provider,
45
46 // This tool may provide access to and deletion of user data.
47 \core_privacy\local\request\plugin\provider {
48
49 /**
50 * Return the fields which contain personal data.
51 *
52 * @param collection $items A reference to the collection to use to store the metadata.
53 * @return collection The updated collection of metadata items.
54 */
55 public static function get_metadata(collection $items) : collection {
56 $items->add_database_table(
57 'tool_policy_acceptances',
58 [
59 'policyversionid' => 'privacy:metadata:acceptances:policyversionid',
60 'userid' => 'privacy:metadata:acceptances:userid',
61 'status' => 'privacy:metadata:acceptances:status',
62 'lang' => 'privacy:metadata:acceptances:lang',
63 'usermodified' => 'privacy:metadata:acceptances:usermodified',
64 'timecreated' => 'privacy:metadata:acceptances:timecreated',
65 'timemodified' => 'privacy:metadata:acceptances:timemodified',
66 'note' => 'privacy:metadata:acceptances:note',
67 ],
68 'privacy:metadata:acceptances'
69 );
70
71 return $items;
72 }
73
74 /**
75 * Get the list of contexts that contain user information for the specified user.
76 *
77 * @param int $userid The userid.
78 * @return contextlist The list of contexts containing user info for the user.
79 */
80 public static function get_contexts_for_userid(int $userid) : contextlist {
432a90f1
MG
81 $contextlist = new contextlist();
82 $contextlist->add_from_sql('SELECT DISTINCT c.id
83 FROM {tool_policy_acceptances} a
84 JOIN {context} c ON a.userid = c.instanceid AND c.contextlevel = ?
85 WHERE a.userid = ? OR a.usermodified = ?',
86 [CONTEXT_USER, $userid, $userid]);
87 return $contextlist;
24966a26
SA
88 }
89
90 /**
91 * Export personal data for the given approved_contextlist. User and context information is contained within the contextlist.
92 *
93 * @param approved_contextlist $contextlist A list of contexts approved for export.
94 */
95 public static function export_user_data(approved_contextlist $contextlist) {
432a90f1
MG
96 global $DB;
97 foreach ($contextlist->get_contexts() as $context) {
98 if ($context->contextlevel != CONTEXT_USER) {
99 continue;
100 }
101 $user = $contextlist->get_user();
102 $agreements = $DB->get_records_sql('SELECT a.id, a.userid, v.name, v.revision, a.usermodified, a.timecreated,
103 a.timemodified, a.note, v.archived, p.currentversionid, a.status, a.policyversionid
104 FROM {tool_policy_acceptances} a
105 JOIN {tool_policy_versions} v ON v.id=a.policyversionid
106 JOIN {tool_policy} p ON v.policyid = p.id
107 WHERE a.userid = ? AND (a.userid = ? OR a.usermodified = ?)
108 ORDER BY a.userid, v.archived, v.timecreated DESC',
109 [$context->instanceid, $user->id, $user->id]);
110 foreach ($agreements as $agreement) {
111 $context = \context_user::instance($agreement->userid);
112 $subcontext = [
113 get_string('userpoliciesagreements', 'tool_policy'),
114 transform::user($agreement->userid)
115 ];
116 $name = 'policyagreement-' . $agreement->policyversionid;
117 $agreementcontent = (object) [
118 'userid' => transform::user($agreement->userid),
119 'status' => $agreement->status,
120 'versionid' => $agreement->policyversionid,
121 'name' => $agreement->name,
122 'revision' => $agreement->revision,
123 'isactive' => transform::yesno($agreement->policyversionid == $agreement->currentversionid),
124 'usermodified' => transform::user($agreement->usermodified),
125 'timecreated' => transform::datetime($agreement->timecreated),
126 'timemodified' => transform::datetime($agreement->timemodified),
127 'note' => $agreement->note,
128 ];
129 writer::with_context($context)->export_related_data($subcontext, $name, $agreementcontent);
130 }
131 }
24966a26
SA
132 }
133
134 /**
135 * Delete all data for all users in the specified context.
136 *
432a90f1
MG
137 * We never delete user agreements to the policies because they are part of privacy data.
138 *
24966a26
SA
139 * @param \context $context The context to delete in.
140 */
141 public static function delete_data_for_all_users_in_context(\context $context) {
142 }
143
144 /**
145 * Delete all user data for the specified user, in the specified contexts.
146 *
432a90f1
MG
147 * We never delete user agreements to the policies because they are part of privacy data.
148 *
24966a26
SA
149 * @param approved_contextlist $contextlist A list of contexts approved for deletion.
150 */
151 public static function delete_data_for_user(approved_contextlist $contextlist) {
152 }
153}