mirrored from git://git.moodle.org/moodle.git
/
provider.php
166 lines (149 loc) · 5.78 KB
/
provider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Privacy class for requesting user data.
*
* @package profilefield_checkbox
* @copyright 2018 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace profilefield_checkbox\privacy;
defined('MOODLE_INTERNAL') || die();
use \core_privacy\local\metadata\collection;
use \core_privacy\local\request\contextlist;
use \core_privacy\local\request\approved_contextlist;
/**
* Privacy class for requesting user data.
*
* @package profilefield_checkbox
* @copyright 2018 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\plugin\provider {
/**
* Returns meta data about this system.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection) {
$collection->add_database_table('user_info_data', [
'userid' => 'privacy:metadata:profilefield_checkbox:userid',
'fieldid' => 'privacy:metadata:profilefield_checkbox:fieldid',
'data' => 'privacy:metadata:profilefield_checkbox:data',
'dataformat' => 'privacy:metadata:profilefield_checkbox:dataformat'
], 'privacy:metadata:profilefield_checkbox:tableexplanation');
return $collection;
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid The user to search.
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid($userid) {
$sql = "SELECT ctx.id
FROM {user_info_data} uid
JOIN {user_info_field} uif
ON uid.fieldid = uif.id
JOIN {context} ctx
ON ctx.instanceid = uid.userid
AND ctx.contextlevel = :contextlevel
WHERE uid.userid = :userid
AND uif.datatype = :datatype";
$params = [
'userid' => $userid,
'contextlevel' => CONTEXT_USER,
'datatype' => 'checkbox'
];
$contextlist = new contextlist();
$contextlist->add_from_sql($sql, $params);
return $contextlist;
}
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
$results = static::get_records($contextlist->get_user()->id);
foreach ($results as $result) {
$data = (object) [
'name' => $result->name,
'description' => $result->description,
'data' => $result->data
];
\core_privacy\local\request\writer::with_context($contextlist->current())->export_data([
get_string('pluginname', 'profilefield_checkbox')], $data);
}
}
/**
* Delete all use data which matches the specified deletion_criteria.
*
* @param context $context A user context.
*/
public static function delete_data_for_all_users_in_context(\context $context) {
// Delete data only for user context.
if ($context->contextlevel == CONTEXT_USER) {
static::delete_data($context->instanceid);
}
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
static::delete_data($contextlist->get_user()->id);
}
/**
* Delete data related to a userid.
*
* @param int $userid The user ID
*/
protected static function delete_data($userid) {
global $DB;
$params = [
'userid' => $userid,
'datatype' => 'checkbox'
];
$DB->delete_records_select('user_info_data', "fieldid IN (
SELECT id FROM {user_info_field} WHERE datatype = :datatype)
AND userid = :userid", $params);
}
/**
* Get records related to this plugin and user.
*
* @param int $userid The user ID
* @return array An array of records.
*/
protected static function get_records($userid) {
global $DB;
$sql = "SELECT *
FROM {user_info_data} uid
JOIN {user_info_field} uif
ON uid.fieldid = uif.id
WHERE uid.userid = :userid
AND uif.datatype = :datatype";
$params = [
'userid' => $userid,
'datatype' => 'checkbox'
];
return $DB->get_records_sql($sql, $params);
}
}