Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 block_multiblock.
19
 *
20
 * @package   block_multiblock
21
 * @copyright 2020 Peter Spicer <peter.spicer@catalyst-eu.net>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace block_multiblock\privacy;
26
 
27
use context;
28
use context_block;
29
use core_privacy\local\metadata\collection;
30
use core_privacy\local\metadata\provider as metadata_provider;
31
use core_privacy\local\request\approved_contextlist;
32
use core_privacy\local\request\approved_userlist;
33
use core_privacy\local\request\contextlist;
34
use core_privacy\local\request\core_userlist_provider as userlist_provider;
35
use core_privacy\local\request\plugin\provider as plugin_provider;
36
use core_privacy\local\request\userlist;
37
 
38
/**
39
 * Privacy Subsystem implementation for block_multiblock.
40
 *
41
 * @package   block_multiblock
42
 * @copyright 2020 Peter Spicer <peter.spicer@catalyst-eu.net>
43
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44
 */
45
class provider implements metadata_provider, userlist_provider, plugin_provider {
46
 
47
    /**
48
     * Returns information about how block_multiblock stores its data.
49
     *
50
     * This plugin implements several interfaces:
51
     * - The \core_privacy\local\metadata\provider interface - Multiblock manages blocks that might store user data.
52
     * - The \core_privacy\local\request\core_userlist_provider interface - Multiblock queries dependent blocks for this data.
53
     * - The \core_privacy\local\request\plugin\provider interface - Multiblock interacts directly with core.
54
     *
55
     * @param collection $collection The initialised collection to add items to.
56
     * @return collection A listing of user data stored through this system.
57
     */
58
    public static function get_metadata(collection $collection) : collection {
59
        $collection->link_subsystem('block', 'privacy:metadata:block');
60
 
61
        return $collection;
62
    }
63
 
64
    /**
65
     * Get the list of contexts that contain user information for the requested user.
66
     *
67
     * @param int $userid The user to lookup.
68
     * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
69
     */
70
    public static function get_contexts_for_userid(int $userid) : contextlist {
71
        // This won't be the full list of contexts, this is the list of contexts of the multiblock parents.
72
        // We will resolve the full list out when fetching or pruning actual data.
73
        // Note that we can only connect blocks to user data when they're in a user context.
74
        $contextlist = new contextlist;
75
 
76
        $sql = "SELECT c.id
77
                  FROM {block_instances} b
78
            INNER JOIN {context} c ON c.instanceid = b.id AND c.contextlevel = :contextblock
79
            INNER JOIN {context} bpc ON bpc.id = b.parentcontextid
80
                 WHERE b.blockname = :blockname
81
                   AND bpc.contextlevel = :contextuser
82
                   AND bpc.instanceid = :userid";
83
 
84
        $params = [
85
            'blockname' => 'multiblock',
86
            'contextblock' => CONTEXT_BLOCK,
87
            'contextuser' => CONTEXT_USER,
88
            'userid' => $userid,
89
        ];
90
 
91
        $contextlist->add_from_sql($sql, $params);
92
 
93
        return $contextlist;
94
    }
95
 
96
 
97
    /**
98
     * Get the list of users who have data within a context.
99
     *
100
     * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
101
     */
102
    public static function get_users_in_context(userlist $userlist) {
103
        // The users of a given multiblock context are the ones who own it; subblocks just inherit the parent context.
104
        // By extension this means they inherit the parent owner too.
105
        $context = $userlist->get_context();
106
 
107
        if (!is_a($context, context_block::class)) {
108
            return;
109
        }
110
 
111
        $params = [
112
            'blockname' => 'multiblock',
113
            'contextid' => $context->id,
114
            'contextuser' => CONTEXT_USER,
115
        ];
116
 
117
        $sql = "SELECT bpc.instanceid AS userid
118
                  FROM {context} c
119
                  JOIN {block_instances} bi ON bi.id = c.instanceid AND bi.blockname = :blockname
120
                  JOIN {context} bpc ON bpc.id = bi.parentcontextid AND bpc.contextlevel = :contextuser
121
                 WHERE c.id = :contextid";
122
 
123
        $userlist->add_from_sql('userid', $sql, $params);
124
    }
125
 
126
    /**
127
     * Export all user data for the specified user, in the specified contexts.
128
     *
129
     * Unlike other parts of the privacy provider, this time we actually fetch the sub-block data.
130
     *
131
     * @param approved_contextlist $contextlist The approved contexts to export information for.
132
     */
133
    public static function export_user_data(approved_contextlist $contextlist) {
134
        global $DB;
135
 
136
        $user = $contextlist->get_user();
137
 
138
        list($contextsql, $contextparams) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED);
139
 
140
        $sql = "SELECT c.id AS contextid, bi.*
141
                  FROM {context} c
142
                  JOIN {block_instances} bi ON bi.id = c.instanceid AND c.contextlevel = :contextlevel
143
                 WHERE bi.blockname = :blockname
144
                   AND (c.id {$contextsql})";
145
 
146
        $params = [
147
            'blockname' => 'multiblock',
148
            'contextlevel' => CONTEXT_BLOCK,
149
        ];
150
        $params += $contextparams;
151
 
152
        $multiblocks = $DB->get_recordset_sql($sql, $params);
153
        $subblockcontexts = [];
154
 
155
        // We need to build contextlists for each of the subblocks, with their aggregate context lists.
156
        foreach ($multiblocks as $multiblock) {
157
            $subblock = "SELECT c.id AS contextid, bi.blockname
158
                           FROM {context} c
159
                           JOIN {block_instances} bi ON bi.id = c.instanceid AND c.contextlevel = :contextlevel
160
                          WHERE bi.parentcontextid = :parentcontext";
161
            $subblockparams = [
162
                'contextlevel' => CONTEXT_BLOCK,
163
                'parentcontext' => $multiblock->contextid,
164
            ];
165
 
166
            // Now step through all the subblocks of this particular block and query it.
167
            $subblocks = $DB->get_records_sql($subblock, $subblockparams);
168
            foreach ($subblocks as $subblock) {
169
                $subblockcontexts['block_' . $subblock->blockname][] = $subblock->contextid;
170
            }
171
        }
172
 
173
        foreach ($subblockcontexts as $component => $contexts) {
174
            $componentcontextlist = new approved_contextlist($user, $component, $contexts);
175
            $classname = $component . '\\privacy\\provider';
176
            if (class_exists($classname) && method_exists($classname, 'export_user_data')) {
177
                $classname::export_user_data($componentcontextlist);
178
            }
179
        }
180
    }
181
 
182
    /**
183
     * Delete all data for all users in the specified context.
184
     *
185
     * @param context $context The specific context to delete data for.
186
     */
187
    public static function delete_data_for_all_users_in_context(context $context) {
188
        if (!$context instanceof context_block) {
189
            return;
190
        }
191
 
192
        // The only way to delete data for the html block is to delete the block instance itself.
193
        if ($blockinstance = static::get_instance_from_context($context)) {
194
            blocks_delete_instance($blockinstance);
195
        }
196
    }
197
 
198
    /**
199
     * Delete multiple users within a single context.
200
     *
201
     * This will delete the main multiblocks, which will also delete all child blocks.
202
     *
203
     * @param approved_userlist $userlist The approved context and user information to delete information for.
204
     */
205
    public static function delete_data_for_users(approved_userlist $userlist) {
206
        $context = $userlist->get_context();
207
 
208
        if (!$context instanceof context_block) {
209
            return;
210
        }
211
 
212
        if ($blockinstance = static::get_instance_from_context($context)) {
213
            blocks_delete_instance($blockinstance);
214
        }
215
    }
216
 
217
    /**
218
     * Delete all user data for the specified user, in the specified contexts.
219
     *
220
     * This will delete the main multiblocks, which will also delete all child blocks.
221
     *
222
     * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
223
     */
224
    public static function delete_data_for_user(approved_contextlist $contextlist) {
225
        foreach ($contextlist as $context) {
226
            if (!$context instanceof context_block) {
227
                continue;
228
            }
229
 
230
            if ($blockinstance = static::get_instance_from_context($context)) {
231
                blocks_delete_instance($blockinstance);
232
            }
233
        }
234
    }
235
 
236
    /**
237
     * Get the block instance record for the specified context.
238
     *
239
     * @param   context_block $context The context to fetch
240
     * @return  stdClass
241
     */
242
    protected static function get_instance_from_context(context_block $context) {
243
        global $DB;
244
 
245
        return $DB->get_record('block_instances', ['id' => $context->instanceid, 'blockname' => 'multiblock']);
246
    }
247
}