Proyectos de Subversion Moodle

Rev

Rev 4 | Ir a la última revisión | | 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
 
5 ariadna 29
use \core_privacy\local\metadata\collection;
4 ariadna 30
use core_privacy\local\request\approved_userlist;
5 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
     */
5 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',
5 ariadna 66
                'vote' => 'privacy:metadata:activity_votes_database:vote'
67
 
4 ariadna 68
            ],
69
            'privacy:metadata:block_point_view'
70
        );
71
 
72
        return $collection;
73
    }
74
 
75
    /**
76
     * Get the list of contexts that contain user information for the specified user.
77
     *
78
     * @param   int $userid The user to search.
79
     * @return  contextlist   $contextlist  The contextlist containing the list of contexts used in this plugin.
80
     */
5 ariadna 81
    public static function get_contexts_for_userid(int $userid) : contextlist {
4 ariadna 82
        $sql = 'SELECT DISTINCT ctx.id
83
                FROM {block_point_view} bpv
84
                JOIN {context} ctx
85
                    ON ctx.instanceid = bpv.userid
86
                        AND ctx.contextlevel = :contextlevel
87
                WHERE bpv.userid = :userid';
88
 
89
        $params = ['userid' => $userid, 'contextlevel' => CONTEXT_USER];
90
 
91
        $contextlist = new contextlist();
92
        $contextlist->add_from_sql($sql, $params);
93
        return $contextlist;
94
    }
95
 
96
    /**
97
     * Export all user data for the specified user, in the specified contexts.
98
     *
99
     * @param   approved_contextlist $contextlist The approved contexts to export information for.
100
     * @throws \coding_exception
101
     * @throws \dml_exception
102
     */
103
    public static function export_user_data(approved_contextlist $contextlist) {
104
        $pointviewdata = [];
105
        $results = static::get_records($contextlist->get_user()->id);
106
        foreach ($results as $result) {
107
            $pointviewdata[] = (object) [
108
                'courseid' => $result->courseid,
109
                'cmid' => $result->cmid,
5 ariadna 110
                'vote' => $result->vote
4 ariadna 111
            ];
112
        }
113
        if (!empty($pointviewdata)) {
114
            $data = (object) [
115
                'votes' => $pointviewdata,
116
            ];
117
            \core_privacy\local\request\writer::with_context($contextlist->current())->export_data([
118
                get_string('pluginname', 'block_point_view')], $data);
119
        }
120
    }
121
 
122
    /**
123
     * Delete all data for all users in the specified context.
124
     *
125
     * @param \context $context The specific context to delete data for.
126
     * @throws \dml_exception
127
     */
128
    public static function delete_data_for_all_users_in_context(\context $context) {
129
        if ($context instanceof \context_user) {
130
            static::delete_data($context->instanceid);
131
        }
132
    }
133
 
134
    /**
135
     * Delete all user data for the specified user, in the specified contexts.
136
     *
137
     * @param   approved_contextlist $contextlist The approved contexts and user information to delete information for.
138
     * @throws \dml_exception
139
     */
140
    public static function delete_data_for_user(approved_contextlist $contextlist) {
141
        static::delete_data($contextlist->get_user()->id);
142
    }
143
 
144
    /**
145
     * Delete data related to a userid.
146
     *
147
     * @param  int $userid The user ID
148
     * @throws \dml_exception
149
     */
150
    protected static function delete_data($userid) {
151
        global $DB;
152
 
153
        $DB->delete_records('block_point_view', ['userid' => $userid]);
154
    }
155
 
156
    /**
157
     * Get records related to this plugin and user.
158
     *
159
     * @param  int $userid The user ID
160
     * @return array An array of records.
161
     * @throws \dml_exception
162
     */
163
    protected static function get_records($userid) {
164
        global $DB;
165
 
166
        return $DB->get_records('block_point_view', ['userid' => $userid]);
167
    }
168
 
169
    /**
170
     * Get the list of users who have data within a context.
171
     *
172
     * @param   userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
173
     */
174
    public static function get_users_in_context(userlist $userlist) {
175
        $context = $userlist->get_context();
176
 
177
        if (!$context instanceof \context_user) {
178
            return;
179
        }
180
 
181
        $params = [
182
            'contextid' => $context->id,
183
            'contextuser' => CONTEXT_USER,
184
        ];
185
 
186
        $sql = "SELECT bpv.userid as userid
187
                  FROM {block_point_view} bpv
188
                  JOIN {context} ctx
189
                       ON ctx.instanceid = bpv.userid
190
                       AND ctx.contextlevel = :contextuser
191
                 WHERE ctx.id = :contextid";
192
 
193
        $userlist->add_from_sql('userid', $sql, $params);
194
    }
195
 
196
    /**
197
     * Delete multiple users within a single context.
198
     *
199
     * @param   approved_userlist $userlist The approved context and user information to delete information for.
200
     * @throws \dml_exception
201
     */
202
    public static function delete_data_for_users(approved_userlist $userlist) {
203
        $context = $userlist->get_context();
204
 
205
        if ($context instanceof \context_user) {
206
            static::delete_data($context->instanceid);
207
        }
208
    }
209
}