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_configurable_reports.
19
 *
20
 * @package    block_configurable_reports
21
 * @category   privacy
22
 * @copyright  2019 Sara Arjona (sara@moodle.com)
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace block_configurable_reports\privacy;
27
 
28
use core_privacy\local\metadata\collection;
29
use core_privacy\local\request\approved_contextlist;
30
use core_privacy\local\request\approved_userlist;
31
use core_privacy\local\request\contextlist;
32
use core_privacy\local\request\userlist;
33
use core_privacy\local\request\transform;
34
use core_privacy\local\request\writer;
35
 
36
defined('MOODLE_INTERNAL') || die();
37
 
38
/**
39
 * Implementation of the privacy plugin provider for the configurable report block.
40
 *
41
 * @copyright  2019 Sara Arjona (sara@moodle.com)
42
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43
 */
44
class provider implements
45
    // This plugin has data.
46
    \core_privacy\local\metadata\provider,
47
 
48
    // This plugin is capable of determining which users have data within it.
49
    \core_privacy\local\request\core_userlist_provider,
50
 
51
    // This plugin currently implements the original plugin\provider interface.
52
    \core_privacy\local\request\plugin\provider {
53
 
54
    // This trait must be included to provide the relevant polyfill for the metadata provider.
55
    use \core_privacy\local\legacy_polyfill;
56
 
57
    /**
58
     * Returns metadata.
59
     *
60
     * @param collection $collection The initialised collection to add items to.
61
     * @return collection A listing of user data stored through this system.
62
     */
63
    public static function _get_metadata(collection $collection) {
64
 
65
        $collection->add_database_table('block_configurable_reports', [
66
            'courseid' => 'privacy:metadata:block_configurable_reports:courseid',
67
            'ownerid' => 'privacy:metadata:block_configurable_reports:ownerid',
68
            'visible' => 'privacy:metadata:block_configurable_reports:visible',
69
            'global' => 'privacy:metadata:block_configurable_reports:global',
70
            'name' => 'privacy:metadata:block_configurable_reports:name',
71
            'summary' => 'privacy:metadata:block_configurable_reports:summary',
72
            'type' => 'privacy:metadata:block_configurable_reports:type',
73
            'components' => 'privacy:metadata:block_configurable_reports:components',
74
            'lastexecutiontime' => 'privacy:metadata:block_configurable_reports:lastexecutiontime',
75
        ], 'privacy:metadata:block_configurable_reports');
76
 
77
        return $collection;
78
    }
79
 
80
    /**
81
     * Get the list of contexts that contain user information for the specified user.
82
     *
83
     * @param   int $userid The user to search.
84
     * @return  contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
85
     */
86
    public static function _get_contexts_for_userid($userid) {
87
        $contextlist = new contextlist();
88
 
89
        // Find the reports created by the userid.
90
        $sql = "SELECT ctx.id
91
                FROM {block_configurable_reports} bcr
92
                JOIN {context} ctx
93
                  ON ctx.instanceid = bcr.ownerid AND ctx.contextlevel = :contextlevel
94
                WHERE bcr.ownerid = :ownerid";
95
 
96
        $params = ['ownerid' => $userid, 'contextlevel' => CONTEXT_USER];
97
 
98
        $contextlist->add_from_sql($sql, $params);
99
        return $contextlist;
100
    }
101
 
102
    /**
103
     * Get the list of users who have data within a context.
104
     *
105
     * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
106
     */
107
    public static function get_users_in_context(userlist $userlist) {
108
        $context = $userlist->get_context();
109
 
110
        if (!$context instanceof \context_user) {
111
            return;
112
        }
113
 
114
        $params = [
115
            'contextid' => $context->id,
116
            'contextuser' => CONTEXT_USER,
117
        ];
118
 
119
        $sql = "SELECT bcr.ownerid as ownerid
120
                  FROM {block_configurable_reports} bcr
121
                  JOIN {context} ctx
122
                       ON ctx.instanceid = bcr.ownerid
123
                       AND ctx.contextlevel = :contextuser
124
                 WHERE ctx.id = :contextid";
125
 
126
        $userlist->add_from_sql('ownerid', $sql, $params);
127
    }
128
 
129
    /**
130
     * Export all user data for the specified user, in the specified contexts.
131
     *
132
     * @param approved_contextlist $contextlist The approved contexts to export information for.
133
     */
134
    public static function _export_user_data(approved_contextlist $contextlist) {
135
        global $DB;
136
 
137
        $reportsdata = [];
138
        $sql = "SELECT bcr.* , c.fullname as coursename
139
                  FROM {block_configurable_reports} bcr
140
                  JOIN {course} c ON c.id = bcr.courseid
141
                 WHERE bcr.ownerid = :ownerid";
142
        $params = ['ownerid' => $contextlist->get_user()->id];
143
        $results = $DB->get_records_sql($sql, $params);
144
        foreach ($results as $result) {
145
            $reportsdata[] = (object) [
146
                'coursename' => format_string($result->coursename, true),
147
                'visible' => transform::yesno($result->visible),
148
                'global' => transform::yesno($result->global),
149
                'name' => $result->name,
150
                'summary' => $result->summary,
151
                'type' => $result->type,
152
                'components' => $result->components,
153
                'lastexecutiontime' => transform::datetime($result->lastexecutiontime)
154
            ];
155
        }
156
        if (!empty($reportsdata)) {
157
            $data = (object) [
158
                'reports' => $reportsdata,
159
            ];
160
            writer::with_context($contextlist->current())->export_data([
161
                    get_string('pluginname', 'block_configurable_reports')], $data);
162
        }
163
    }
164
 
165
    /**
166
     * Delete all data for all users in the specified context.
167
     *
168
     * @param \context $context The specific context to delete data for.
169
     */
170
    public static function _delete_data_for_all_users_in_context(\context $context) {
171
        if ($context instanceof \context_user) {
172
            static::delete_data($context->instanceid);
173
        }
174
    }
175
 
176
    /**
177
     * Delete multiple users within a single context.
178
     *
179
     * @param approved_userlist $userlist The approved context and user information to delete information for.
180
     */
181
    public static function delete_data_for_users(approved_userlist $userlist) {
182
        $context = $userlist->get_context();
183
 
184
        if ($context instanceof \context_user) {
185
            static::delete_data($context->instanceid);
186
        }
187
    }
188
 
189
    /**
190
     * Delete all user data for the specified user, in the specified contexts.
191
     *
192
     * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
193
     */
194
    public static function _delete_data_for_user(approved_contextlist $contextlist) {
195
        static::delete_data($contextlist->get_user()->id);
196
    }
197
 
198
    /**
199
     * Delete data related to a userid.
200
     *
201
     * @param  int $userid The user ID
202
     */
203
    protected static function delete_data($userid) {
204
        global $DB;
205
 
206
        // Reports are considered to be 'owned' by the institution, even if they were originally written by a specific
207
        // user. They are still exported in the list of a users data, but they are not removed.
208
        // The ownerid is instead anonymised.
209
        $params['ownerid'] = $userid;
210
        $DB->set_field_select('block_configurable_reports', 'ownerid', 0, "ownerid = :ownerid", $params);
211
    }
212
}