Automatically generated installer lang files
[moodle.git] / files / classes / privacy / provider.php
blobefae2a97fd76eea9bc73db4e9340ee0ffab9879c
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.
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/>.
17 /**
18 * Data provider.
20 * @package core_files
21 * @copyright 2018 Frédéric Massart
22 * @author Frédéric Massart <fred@branchup.tech>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 namespace core_files\privacy;
27 defined('MOODLE_INTERNAL') || die();
29 use core_privacy\local\metadata\collection;
30 use core_privacy\local\request\contextlist;
31 use core_privacy\local\request\approved_contextlist;
32 use core_privacy\local\request\userlist;
33 use core_privacy\local\request\approved_userlist;
35 /**
36 * Data provider class.
38 * This only describes the files table, all components must handle the file exporting
39 * and deletion themselves.
41 * @package core_files
42 * @copyright 2018 Frédéric Massart
43 * @author Frédéric Massart <fred@branchup.tech>
44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46 class provider implements
47 \core_privacy\local\metadata\provider,
48 \core_privacy\local\request\subsystem\plugin_provider,
49 \core_privacy\local\request\core_userlist_provider,
50 // We store a userkey for token-based file access.
51 \core_privacy\local\request\subsystem\provider,
52 \core_privacy\local\request\shared_userlist_provider {
54 /**
55 * Returns metadata.
57 * @param collection $collection The initialised collection to add items to.
58 * @return collection A listing of user data stored through this system.
60 public static function get_metadata(collection $collection) : collection {
62 $collection->add_database_table('files', [
63 'contenthash' => 'privacy:metadata:files:contenthash',
64 'filepath' => 'privacy:metadata:files:filepath',
65 'filename' => 'privacy:metadata:files:filename',
66 'userid' => 'privacy:metadata:files:userid',
67 'filesize' => 'privacy:metadata:files:filesize',
68 'mimetype' => 'privacy:metadata:files:mimetype',
69 'source' => 'privacy:metadata:files:source',
70 'author' => 'privacy:metadata:files:author',
71 'license' => 'privacy:metadata:files:license',
72 'timecreated' => 'privacy:metadata:files:timecreated',
73 'timemodified' => 'privacy:metadata:files:timemodified',
74 ], 'privacy:metadata:files');
76 $collection->add_subsystem_link('core_userkey', [], 'privacy:metadata:core_userkey');
78 return $collection;
81 /**
82 * Get the list of contexts that contain user information for the specified user.
84 * This is currently just the user context.
86 * @param int $userid The user to search.
87 * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
89 public static function get_contexts_for_userid(int $userid) : contextlist {
90 $sql = "SELECT ctx.id
91 FROM {user_private_key} k
92 JOIN {user} u ON k.userid = u.id
93 JOIN {context} ctx ON ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel
94 WHERE k.userid = :userid AND k.script = :script";
95 $params = [
96 'userid' => $userid,
97 'contextlevel' => CONTEXT_USER,
98 'script' => 'core_files',
100 $contextlist = new contextlist();
101 $contextlist->add_from_sql($sql, $params);
103 return $contextlist;
107 * Get the list of users within a specific context.
109 * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
111 public static function get_users_in_context(userlist $userlist) {
112 $context = $userlist->get_context();
114 if (!$context instanceof \context_user) {
115 return;
118 \core_userkey\privacy\provider::get_user_contexts_with_script($userlist, $context, 'core_files');
122 * Export all user data for the specified user, in the specified contexts.
124 * @param approved_contextlist $contextlist The approved contexts to export information for.
126 public static function export_user_data(approved_contextlist $contextlist) {
127 // If the user has data, then only the CONTEXT_USER should be present so get the first context.
128 $contexts = $contextlist->get_contexts();
129 if (count($contexts) == 0) {
130 return;
133 // Sanity check that context is at the user context level, then get the userid.
134 $context = reset($contexts);
135 if ($context->contextlevel !== CONTEXT_USER) {
136 return;
139 // Export associated userkeys.
140 $subcontext = [
141 get_string('files'),
143 \core_userkey\privacy\provider::export_userkeys($context, $subcontext, 'core_files');
147 * Delete all use data which matches the specified deletion_criteria.
149 * @param context $context A user context.
151 public static function delete_data_for_all_users_in_context(\context $context) {
152 // Sanity check that context is at the user context level, then get the userid.
153 if ($context->contextlevel !== CONTEXT_USER) {
154 return;
157 // Delete all the userkeys.
158 \core_userkey\privacy\provider::delete_userkeys('core_files', $context->instanceid);
162 * Delete multiple users within a single context.
164 * @param approved_userlist $userlist The approved context and user information to delete information for.
166 public static function delete_data_for_users(approved_userlist $userlist) {
167 $context = $userlist->get_context();
169 if ($context instanceof \context_user) {
170 \core_userkey\privacy\provider::delete_userkeys('core_files', $context->instanceid);
175 * Delete all user data for the specified user, in the specified contexts.
177 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
179 public static function delete_data_for_user(approved_contextlist $contextlist) {
180 // If the user has data, then only the user context should be present so get the first context.
181 $contexts = $contextlist->get_contexts();
182 if (count($contexts) == 0) {
183 return;
186 // Sanity check that context is at the user context level, then get the userid.
187 $context = reset($contexts);
188 if ($context->contextlevel !== CONTEXT_USER) {
189 return;
192 // Delete all the userkeys for core_files..
193 \core_userkey\privacy\provider::delete_userkeys('core_files', $context->instanceid);