2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Privacy class for requesting user data.
20 * @package core_cohort
21 * @copyright 2018 Sara Arjona <sara@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_cohort\privacy
;
27 defined('MOODLE_INTERNAL') ||
die();
29 use core_privacy\local\metadata\collection
;
30 use core_privacy\local\request\contextlist
;
31 use core_privacy\local\request\approved_contextlist
;
32 use core_privacy\local\request\transform
;
33 use core_privacy\local\request\writer
;
34 use core_privacy\local\request\userlist
;
35 use core_privacy\local\request\approved_userlist
;
38 * Privacy class for requesting user data.
40 * @copyright 2018 Sara Arjona <sara@moodle.com>
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class provider
implements
44 \core_privacy\local\metadata\provider
,
45 \core_privacy\local\request\core_userlist_provider
,
46 \core_privacy\local\request\plugin\provider
{
49 * Return the fields which contain personal data.
51 * @param collection $collection The initialised collection to add items to.
52 * @return collection A listing of user data stored through this system.
54 public static function get_metadata(collection
$collection) : collection
{
55 $collection->add_database_table('cohort_members', [
56 'cohortid' => 'privacy:metadata:cohort_members:cohortid',
57 'userid' => 'privacy:metadata:cohort_members:userid',
58 'timeadded' => 'privacy:metadata:cohort_members:timeadded'
59 ], 'privacy:metadata:cohort_members');
64 * Get the list of contexts that contain user information for the specified user.
66 * @param int $userid The user to search.
67 * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
69 public static function get_contexts_for_userid(int $userid) : contextlist
{
72 INNER JOIN {cohort} c ON c.contextid = ctx.id
73 INNER JOIN {cohort_members} cm ON cm.cohortid = c.id
74 WHERE cm.userid = :userid AND (ctx.contextlevel = :contextlevel1 OR ctx.contextlevel = :contextlevel2)";
77 'contextlevel1' => CONTEXT_SYSTEM
,
78 'contextlevel2' => CONTEXT_COURSECAT
,
80 $contextlist = new contextlist();
81 $contextlist->add_from_sql($sql, $params);
87 * Get the list of users within a specific context.
89 * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
91 public static function get_users_in_context(userlist
$userlist) {
92 $context = $userlist->get_context();
94 if (!$context instanceof \context_system
&& !$context instanceof \context_coursecat
) {
99 'contextid' => $context->id
,
100 'contextsystem' => CONTEXT_SYSTEM
,
101 'contextcoursecat' => CONTEXT_COURSECAT
,
104 $sql = "SELECT cm.userid as userid
105 FROM {cohort_members} cm
107 ON cm.cohortid = c.id
109 ON c.contextid = ctx.id
110 AND (ctx.contextlevel = :contextsystem
111 OR ctx.contextlevel = :contextcoursecat)
112 WHERE ctx.id = :contextid";
114 $userlist->add_from_sql('userid', $sql, $params);
118 * Export all user data for the specified user, in the specified contexts.
120 * @param approved_contextlist $contextlist The approved contexts to export information for.
122 public static function export_user_data(approved_contextlist
$contextlist) {
125 // Remove contexts different from SYSTEM or COURSECAT.
126 $contexts = array_reduce($contextlist->get_contexts(), function($carry, $context) {
127 if ($context->contextlevel
== CONTEXT_SYSTEM ||
$context->contextlevel
== CONTEXT_COURSECAT
) {
128 $carry[] = $context->id
;
133 if (empty($contexts)) {
138 $userid = $contextlist->get_user()->id
;
139 list($contextsql, $contextparams) = $DB->get_in_or_equal($contexts, SQL_PARAMS_NAMED
);
140 $sql = "SELECT c.name,
147 INNER JOIN {cohort} c ON c.contextid = ctx.id
148 INNER JOIN {cohort_members} cm ON cm.cohortid = c.id
149 WHERE ctx.id {$contextsql}
150 AND cm.userid = :userid";
155 $cohorts = $DB->get_recordset_sql($sql, $params);
156 foreach ($cohorts as $cohort) {
157 $alldata[$cohort->contextid
][] = (object)[
158 'name' => $cohort->name
,
159 'idnumber' => $cohort->idnumber
,
160 'visible' => transform
::yesno($cohort->visible
),
161 'timeadded' => transform
::datetime($cohort->timeadded
),
166 // Export cohort data.
167 array_walk($alldata, function($data, $contextid) {
168 $context = \context
::instance_by_id($contextid);
169 writer
::with_context($context)->export_related_data([], 'cohort', $data);
175 * Delete all use data which matches the specified context.
177 * @param context $context A user context.
179 public static function delete_data_for_all_users_in_context(\context
$context) {
180 if (!$context instanceof \context_system
&& !$context instanceof \context_coursecat
) {
184 static::delete_data($context);
188 * Delete multiple users within a single context.
190 * @param approved_userlist $userlist The approved context and user information to delete information for.
192 public static function delete_data_for_users(approved_userlist
$userlist) {
193 $context = $userlist->get_context();
195 if ($context instanceof \context_system ||
$context instanceof \context_coursecat
) {
196 foreach ($userlist->get_userids() as $userid) {
197 static::delete_data($context, $userid);
203 * Delete all user data for the specified user, in the specified contexts.
205 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
207 public static function delete_data_for_user(approved_contextlist
$contextlist) {
208 if (empty($contextlist->count())) {
212 $userid = $contextlist->get_user()->id
;
213 foreach ($contextlist->get_contexts() as $context) {
214 if (!$context instanceof \context_system
&& !$context instanceof \context_coursecat
) {
217 static::delete_data($context, $userid);
222 * Delete data related to a context and user (if defined).
224 * @param context $context A context.
225 * @param int $userid The user ID.
227 protected static function delete_data(\context
$context, int $userid = null) {
230 $cohortids = $DB->get_fieldset_select('cohort', 'id', 'contextid = :contextid', ['contextid' => $context->id
]);
231 foreach ($cohortids as $cohortid) {
232 $params = ['cohortid' => $cohortid];
233 if (!empty($userid)) {
234 $params['userid'] = $userid;
236 $DB->delete_records('cohort_members', $params);