Proyectos de Subversion Moodle

Rev

Rev 5 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
4 ariadna 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
 * Point of View privacy
19
 *
20
 *
21
 * @package    block_point_view
22
 * @copyright  2020 Quentin Fombaron
23
 * @author     Quentin Fombaron <q.fombaron@outlook.fr>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
 
27
namespace block_point_view\privacy;
28
 
6 ariadna 29
use core_privacy\local\metadata\collection;
4 ariadna 30
use core_privacy\local\request\approved_userlist;
6 ariadna 31
use core_privacy\local\request\contextlist;
32
use core_privacy\local\request\approved_contextlist;
4 ariadna 33
use core_privacy\local\request\userlist;
34
 
35
/**
36
 * Class provider
37
 *
38
 * @package block_point_view
39
 * @copyright  2020 Quentin Fombaron
40
 * @author     Quentin Fombaron <q.fombaron@outlook.fr>
41
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42
 */
43
class provider implements
44
    // This tool stores user data.
45
    \core_privacy\local\metadata\provider,
46
 
47
    // This plugin is capable of determining which users have data within it.
48
    \core_privacy\local\request\core_userlist_provider,
49
 
50
    // This tool may provide access to and deletion of user data.
51
    \core_privacy\local\request\plugin\provider {
52
 
53
    /**
54
     * Point of View Metadata
55
     *
56
     * @param collection $collection
57
     * @return collection
58
     */
6 ariadna 59
    public static function get_metadata(collection $collection): collection {
4 ariadna 60
        $collection->add_database_table(
61
            'block_point_view',
62
            [
63
                'courseid' => 'privacy:metadata:activity_votes_database:courseid',
64
                'cmid' => 'privacy:metadata:activity_votes_database:cmid',
65
                'userid' => 'privacy:metadata:activity_votes_database:userid',
6 ariadna 66
                'vote' => 'privacy:metadata:activity_votes_database:vote',
4 ariadna 67
            ],
68
            'privacy:metadata:block_point_view'
69
        );
70
 
71
        return $collection;
72
    }
73
 
74
    /**
75
     * Get the list of contexts that contain user information for the specified user.
76
     *
77
     * @param   int $userid The user to search.
78
     * @return  contextlist   $contextlist  The contextlist containing the list of contexts used in this plugin.
79
     */
6 ariadna 80
    public static function get_contexts_for_userid(int $userid): contextlist {
4 ariadna 81
        $sql = 'SELECT DISTINCT ctx.id
82
                FROM {block_point_view} bpv
83
                JOIN {context} ctx
84
                    ON ctx.instanceid = bpv.userid
85
                        AND ctx.contextlevel = :contextlevel
86
                WHERE bpv.userid = :userid';
87
 
88
        $params = ['userid' => $userid, 'contextlevel' => CONTEXT_USER];
89
 
90
        $contextlist = new contextlist();
91
        $contextlist->add_from_sql($sql, $params);
92
        return $contextlist;
93
    }
94
 
95
    /**
96
     * Export all user data for the specified user, in the specified contexts.
97
     *
98
     * @param   approved_contextlist $contextlist The approved contexts to export information for.
99
     * @throws \coding_exception
100
     * @throws \dml_exception
101
     */
102
    public static function export_user_data(approved_contextlist $contextlist) {
103
        $pointviewdata = [];
104
        $results = static::get_records($contextlist->get_user()->id);
105
        foreach ($results as $result) {
106
            $pointviewdata[] = (object) [
107
                'courseid' => $result->courseid,
108
                'cmid' => $result->cmid,
6 ariadna 109
                'vote' => $result->vote,
4 ariadna 110
            ];
111
        }
112
        if (!empty($pointviewdata)) {
113
            $data = (object) [
114
                'votes' => $pointviewdata,
115
            ];
116
            \core_privacy\local\request\writer::with_context($contextlist->current())->export_data([
117
                get_string('pluginname', 'block_point_view')], $data);
118
        }
119
    }
120
 
121
    /**
122
     * Delete all data for all users in the specified context.
123
     *
124
     * @param \context $context The specific context to delete data for.
125
     * @throws \dml_exception
126
     */
127
    public static function delete_data_for_all_users_in_context(\context $context) {
128
        if ($context instanceof \context_user) {
129
            static::delete_data($context->instanceid);
130
        }
131
    }
132
 
133
    /**
134
     * Delete all user data for the specified user, in the specified contexts.
135
     *
136
     * @param   approved_contextlist $contextlist The approved contexts and user information to delete information for.
137
     * @throws \dml_exception
138
     */
139
    public static function delete_data_for_user(approved_contextlist $contextlist) {
140
        static::delete_data($contextlist->get_user()->id);
141
    }
142
 
143
    /**
144
     * Delete data related to a userid.
145
     *
146
     * @param  int $userid The user ID
147
     * @throws \dml_exception
148
     */
149
    protected static function delete_data($userid) {
150
        global $DB;
151
 
152
        $DB->delete_records('block_point_view', ['userid' => $userid]);
153
    }
154
 
155
    /**
156
     * Get records related to this plugin and user.
157
     *
158
     * @param  int $userid The user ID
159
     * @return array An array of records.
160
     * @throws \dml_exception
161
     */
162
    protected static function get_records($userid) {
163
        global $DB;
164
 
165
        return $DB->get_records('block_point_view', ['userid' => $userid]);
166
    }
167
 
168
    /**
169
     * Get the list of users who have data within a context.
170
     *
171
     * @param   userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
172
     */
173
    public static function get_users_in_context(userlist $userlist) {
174
        $context = $userlist->get_context();
175
 
176
        if (!$context instanceof \context_user) {
177
            return;
178
        }
179
 
180
        $params = [
181
            'contextid' => $context->id,
182
            'contextuser' => CONTEXT_USER,
183
        ];
184
 
185
        $sql = "SELECT bpv.userid as userid
186
                  FROM {block_point_view} bpv
187
                  JOIN {context} ctx
188
                       ON ctx.instanceid = bpv.userid
189
                       AND ctx.contextlevel = :contextuser
190
                 WHERE ctx.id = :contextid";
191
 
192
        $userlist->add_from_sql('userid', $sql, $params);
193
    }
194
 
195
    /**
196
     * Delete multiple users within a single context.
197
     *
198
     * @param   approved_userlist $userlist The approved context and user information to delete information for.
199
     * @throws \dml_exception
200
     */
201
    public static function delete_data_for_users(approved_userlist $userlist) {
202
        $context = $userlist->get_context();
203
 
204
        if ($context instanceof \context_user) {
205
            static::delete_data($context->instanceid);
206
        }
207
    }
208
}