on-demand release 3.6dev+
[moodle.git] / rss / classes / privacy / provider.php
CommitLineData
8d3238d8
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 core_rss
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 core_rss\privacy;
26
27defined('MOODLE_INTERNAL') || die();
28
29use \core_privacy\local\metadata\collection;
30use \core_privacy\local\request\contextlist;
31use \core_privacy\local\request\approved_contextlist;
32use \core_privacy\local\request\transform;
33use \core_privacy\local\request\writer;
34
35/**
36 * Privacy class for requesting user data.
37 *
38 * @copyright 2018 Sara Arjona <sara@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class provider implements
42 \core_privacy\local\metadata\provider,
97fcf562 43 \core_privacy\local\request\subsystem\provider {
8d3238d8
DM
44
45 /**
46 * Return the fields which contain personal data.
47 *
48 * @param collection $collection The initialised collection to add items to.
49 * @return collection A listing of user data stored through this system.
50 */
51 public static function get_metadata(collection $collection) : collection {
b720d9da 52 $collection->add_subsystem_link('core_userkey', [], 'privacy:metadata:core_userkey');
53
8d3238d8
DM
54 return $collection;
55 }
56
57 /**
58 * Get the list of contexts that contain user information for the specified user.
59 *
60 * @param int $userid The user to search.
61 * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
62 */
63 public static function get_contexts_for_userid(int $userid) : contextlist {
64 $sql = "SELECT ctx.id
65 FROM {user_private_key} k
66 JOIN {user} u ON k.userid = u.id
67 JOIN {context} ctx ON ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel
68 WHERE k.userid = :userid AND k.script = 'rss'";
69
70 $params = ['userid' => $userid, 'contextlevel' => CONTEXT_USER];
71
72 $contextlist = new contextlist();
73 $contextlist->add_from_sql($sql, $params);
74 return $contextlist;
75 }
76
77 /**
78 * Export all user data for the specified user, in the specified contexts.
79 *
80 * @param approved_contextlist $contextlist The approved contexts to export information for.
81 */
82 public static function export_user_data(approved_contextlist $contextlist) {
b720d9da 83 // If the user has data, then only the CONTEXT_USER should be present so get the first context.
84 $contexts = $contextlist->get_contexts();
85 if (count($contexts) == 0) {
86 return;
87 }
88 $context = reset($contexts);
89
90 // Sanity check that context is at the user context level, then get the userid.
91 if ($context->contextlevel !== CONTEXT_USER) {
92 return;
8d3238d8 93 }
b720d9da 94
95 // Export associated userkeys.
96 \core_userkey\privacy\provider::export_userkeys($context, [], 'rss');
8d3238d8
DM
97 }
98
99 /**
100 * Delete all use data which matches the specified deletion_criteria.
101 *
102 * @param context $context A user context.
103 */
104 public static function delete_data_for_all_users_in_context(\context $context) {
b720d9da 105 // Sanity check that context is at the user context level, then get the userid.
106 if ($context->contextlevel !== CONTEXT_USER) {
107 return;
108 }
109 $userid = $context->instanceid;
110
111 // Delete all the userkeys.
112 \core_userkey\privacy\provider::delete_userkeys('rss', $userid);
113
8d3238d8
DM
114 }
115
116 /**
117 * Delete all user data for the specified user, in the specified contexts.
118 *
119 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
120 */
121 public static function delete_data_for_user(approved_contextlist $contextlist) {
b720d9da 122 // If the user has data, then only the user context should be present so get the first context.
123 $contexts = $contextlist->get_contexts();
124 if (count($contexts) == 0) {
125 return;
126 }
127 $context = reset($contexts);
8d3238d8 128
b720d9da 129 // Sanity check that context is at the user context level, then get the userid.
130 if ($context->contextlevel !== CONTEXT_USER) {
131 return;
132 }
133 $userid = $context->instanceid;
134 // Delete all the userkeys.
135 \core_userkey\privacy\provider::delete_userkeys('rss', $userid);
8d3238d8
DM
136 }
137}