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 class for requesting user data.
19
 *
20
 * @package    profilefield_text
21
 * @copyright  2018 Mihail Geshoski <mihail@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace profilefield_text\privacy;
26
 
27
use \core_privacy\local\metadata\collection;
28
use \core_privacy\local\request\contextlist;
29
use \core_privacy\local\request\approved_contextlist;
30
use core_privacy\local\request\userlist;
31
use core_privacy\local\request\approved_userlist;
32
 
33
/**
34
 * Privacy class for requesting user data.
35
 *
36
 * @copyright  2018 Mihail Geshoski <mihail@moodle.com>
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class provider implements
40
    \core_privacy\local\metadata\provider,
41
    \core_privacy\local\request\core_userlist_provider,
42
    \core_privacy\local\request\plugin\provider {
43
 
44
    /**
45
     * Returns meta data about this system.
46
     *
47
     * @param   collection $collection The initialised collection to add items to.
48
     * @return  collection A listing of user data stored through this system.
49
     */
50
    public static function get_metadata(collection $collection): collection {
51
        return $collection->add_database_table('user_info_data', [
52
            'userid' => 'privacy:metadata:profilefield_text:userid',
53
            'fieldid' => 'privacy:metadata:profilefield_text:fieldid',
54
            'data' => 'privacy:metadata:profilefield_text:data',
55
            'dataformat' => 'privacy:metadata:profilefield_text:dataformat'
56
        ], 'privacy:metadata:profilefield_text:tableexplanation');
57
    }
58
 
59
    /**
60
     * Get the list of contexts that contain user information for the specified user.
61
     *
62
     * @param   int         $userid     The user to search.
63
     * @return  contextlist $contextlist  The contextlist containing the list of contexts used in this plugin.
64
     */
65
    public static function get_contexts_for_userid(int $userid): contextlist {
66
        $sql = "SELECT ctx.id
67
                  FROM {user_info_data} uda
68
                  JOIN {user_info_field} uif ON uda.fieldid = uif.id
69
                  JOIN {context} ctx ON ctx.instanceid = uda.userid
70
                       AND ctx.contextlevel = :contextlevel
71
                 WHERE uda.userid = :userid
72
                       AND uif.datatype = :datatype";
73
        $params = [
74
            'userid' => $userid,
75
            'contextlevel' => CONTEXT_USER,
76
            'datatype' => 'text'
77
        ];
78
        $contextlist = new contextlist();
79
        $contextlist->add_from_sql($sql, $params);
80
 
81
        return $contextlist;
82
    }
83
 
84
    /**
85
     * Get the list of users within a specific context.
86
     *
87
     * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
88
     */
89
    public static function get_users_in_context(userlist $userlist) {
90
        $context = $userlist->get_context();
91
 
92
        if (!$context instanceof \context_user) {
93
            return;
94
        }
95
 
96
        $sql = "SELECT uda.userid
97
                  FROM {user_info_data} uda
98
                  JOIN {user_info_field} uif
99
                       ON uda.fieldid = uif.id
100
                 WHERE uda.userid = :userid
101
                       AND uif.datatype = :datatype";
102
 
103
        $params = [
104
            'userid' => $context->instanceid,
105
            'datatype' => 'text'
106
        ];
107
 
108
        $userlist->add_from_sql('userid', $sql, $params);
109
    }
110
 
111
    /**
112
     * Export all user data for the specified user, in the specified contexts.
113
     *
114
     * @param approved_contextlist $contextlist The approved contexts to export information for.
115
     */
116
    public static function export_user_data(approved_contextlist $contextlist) {
117
        $user = $contextlist->get_user();
118
        foreach ($contextlist->get_contexts() as $context) {
119
            // Check if the context is a user context.
120
            if ($context->contextlevel == CONTEXT_USER && $context->instanceid == $user->id) {
121
                $results = static::get_records($user->id);
122
                foreach ($results as $result) {
123
                    $data = (object) [
124
                        'name' => $result->name,
125
                        'description' => $result->description,
126
                        'data' => $result->data
127
                    ];
128
                    \core_privacy\local\request\writer::with_context($context)->export_data([
129
                        get_string('pluginname', 'profilefield_text')], $data);
130
                }
131
            }
132
        }
133
    }
134
 
135
    /**
136
     * Delete all user data which matches the specified context.
137
     *
138
     * @param   context $context A user context.
139
     */
140
    public static function delete_data_for_all_users_in_context(\context $context) {
141
        // Delete data only for user context.
142
        if ($context->contextlevel == CONTEXT_USER) {
143
            static::delete_data($context->instanceid);
144
        }
145
    }
146
 
147
    /**
148
     * Delete multiple users within a single context.
149
     *
150
     * @param approved_userlist $userlist The approved context and user information to delete information for.
151
     */
152
    public static function delete_data_for_users(approved_userlist $userlist) {
153
        $context = $userlist->get_context();
154
 
155
        if ($context instanceof \context_user) {
156
            static::delete_data($context->instanceid);
157
        }
158
    }
159
 
160
    /**
161
     * Delete all user data for the specified user, in the specified contexts.
162
     *
163
     * @param   approved_contextlist    $contextlist    The approved contexts and user information to delete information for.
164
     */
165
    public static function delete_data_for_user(approved_contextlist $contextlist) {
166
        $user = $contextlist->get_user();
167
        foreach ($contextlist->get_contexts() as $context) {
168
            // Check if the context is a user context.
169
            if ($context->contextlevel == CONTEXT_USER && $context->instanceid == $user->id) {
170
                static::delete_data($context->instanceid);
171
            }
172
        }
173
    }
174
 
175
    /**
176
     * Delete data related to a userid.
177
     *
178
     * @param  int $userid The user ID
179
     */
180
    protected static function delete_data($userid) {
181
        global $DB;
182
 
183
        $params = [
184
            'userid' => $userid,
185
            'datatype' => 'text'
186
        ];
187
 
188
        $DB->delete_records_select('user_info_data', "fieldid IN (
189
                SELECT id FROM {user_info_field} WHERE datatype = :datatype)
190
                AND userid = :userid", $params);
191
    }
192
 
193
    /**
194
     * Get records related to this plugin and user.
195
     *
196
     * @param  int $userid The user ID
197
     * @return array An array of records.
198
     */
199
    protected static function get_records($userid) {
200
        global $DB;
201
 
202
        $sql = "SELECT *
203
                  FROM {user_info_data} uda
204
                  JOIN {user_info_field} uif ON uda.fieldid = uif.id
205
                 WHERE uda.userid = :userid
206
                       AND uif.datatype = :datatype";
207
        $params = [
208
            'userid' => $userid,
209
            'datatype' => 'text'
210
        ];
211
 
212
        return $DB->get_records_sql($sql, $params);
213
    }
214
}