on-demand release 3.5dev+
[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,
43 \core_privacy\local\request\plugin\provider {
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 {
52 $collection->add_database_table('user_private_key', [
53 'value' => 'privacy:metadata:user_private_key:value',
54 'userid' => 'privacy:metadata:user_private_key:userid',
55 'validuntil' => 'privacy:metadata:user_private_key:validuntil',
56 'timecreated' => 'privacy:metadata:user_private_key:timecreated'
57 ], 'privacy:metadata:user_private_key');
58 return $collection;
59 }
60
61 /**
62 * Get the list of contexts that contain user information for the specified user.
63 *
64 * @param int $userid The user to search.
65 * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
66 */
67 public static function get_contexts_for_userid(int $userid) : contextlist {
68 $sql = "SELECT ctx.id
69 FROM {user_private_key} k
70 JOIN {user} u ON k.userid = u.id
71 JOIN {context} ctx ON ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel
72 WHERE k.userid = :userid AND k.script = 'rss'";
73
74 $params = ['userid' => $userid, 'contextlevel' => CONTEXT_USER];
75
76 $contextlist = new contextlist();
77 $contextlist->add_from_sql($sql, $params);
78 return $contextlist;
79 }
80
81 /**
82 * Export all user data for the specified user, in the specified contexts.
83 *
84 * @param approved_contextlist $contextlist The approved contexts to export information for.
85 */
86 public static function export_user_data(approved_contextlist $contextlist) {
87 $results = static::get_records($contextlist->get_user()->id);
88 $context = $contextlist->current();
89 if ($context->contextlevel == CONTEXT_USER) {
90 foreach ($results as $result) {
91 $context = \context_user::instance($result->userid);
92 $subcontext = [
93 get_string('rss', 'rss'),
94 transform::user($result->userid)
95 ];
96 $name = 'user_private_key-' . $result->id;
97 $data = (object)[
98 'value' => $result->value,
99 'iprestriction' => $result->iprestriction,
100 'validuntil' => $result->validuntil,
101 'timecreated' => transform::datetime($result->timecreated),
102 ];
103 writer::with_context($context)->export_related_data($subcontext, $name, $data);
104 }
105 }
106 }
107
108 /**
109 * Delete all use data which matches the specified deletion_criteria.
110 *
111 * @param context $context A user context.
112 */
113 public static function delete_data_for_all_users_in_context(\context $context) {
114 // The information in user_private_key table is removed automaticaly when a user is deteled.
115 }
116
117 /**
118 * Delete all user data for the specified user, in the specified contexts.
119 *
120 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
121 */
122 public static function delete_data_for_user(approved_contextlist $contextlist) {
123 // The information in user_private_key table is removed automaticaly when a user is deteled.
124 }
125
126 /**
127 * Get records related to this plugin and user.
128 *
129 * @param int $userid The user ID
130 * @return array An array of records.
131 */
132 protected static function get_records(int $userid) : array {
133 global $DB;
134
135 return $DB->get_records('user_private_key', ['userid' => $userid, 'script' => 'rss']);
136 }
137}