MDL-62357 enrol_cohort: Add privacy implementation for enrol_cohort
[moodle.git] / enrol / cohort / classes / privacy / provider.php
CommitLineData
980e85cd 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 * Privacy Subsystem implementation for enrol_cohort.
18 *
19 * @package enrol_cohort
20 * @copyright 2018 Carlos Escobedo <carlos@moodle.com>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23namespace enrol_cohort\privacy;
24defined('MOODLE_INTERNAL') || die();
30f3ea7f 25use \core_privacy\local\metadata\collection;
26use \core_privacy\local\request\contextlist;
27use \core_privacy\local\request\approved_contextlist;
28
980e85cd 29/**
30f3ea7f 30 * Privacy provider for enrol_cohort.
980e85cd 31 *
32 * @copyright 2018 Carlos Escobedo <carlos@moodle.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
30f3ea7f 35class provider implements
36 \core_privacy\local\metadata\provider,
37 \core_privacy\local\request\plugin\provider {
980e85cd 38 /**
30f3ea7f 39 * Returns meta data about this system.
980e85cd 40 *
30f3ea7f 41 * @param collection $collection The initialised item collection to add items to.
42 * @return collection A listing of user data stored through this system.
980e85cd 43 */
30f3ea7f 44 public static function get_metadata(collection $collection) : collection {
45
46 $collection->add_subsystem_link('core_group', [], 'privacy:metadata:core_group');
47 return $collection;
48 }
49 /**
50 * Get the list of contexts that contain user information for the specified user.
51 *
52 * @param int $userid The user to search.
53 * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
54 */
55 public static function get_contexts_for_userid(int $userid) : contextlist {
56 $contextlist = new contextlist();
57
58 $sql = "SELECT ctx.id
59 FROM {groups_members} gm
60 JOIN {groups} g ON gm.groupid = g.id
61 JOIN {context} ctx ON g.courseid = ctx.instanceid AND ctx.contextlevel = :contextlevel
62 WHERE gm.userid = :userid
63 AND gm.component = 'enrol_cohort'";
64
65 $params = [
66 'contextlevel' => CONTEXT_COURSE,
67 'userid' => $userid
68 ];
69
70 $contextlist->add_from_sql($sql, $params);
71
72 return $contextlist;
73 }
74 /**
75 * Export all user data for the specified user, in the specified contexts.
76 *
77 * @param approved_contextlist $contextlist The approved contexts to export information for.
78 */
79 public static function export_user_data(approved_contextlist $contextlist) {
80 if (empty($contextlist)) {
81 return;
82 }
83 foreach ($contextlist as $context) {
84 if ($context->contextlevel == CONTEXT_COURSE) {
85 \core_group\privacy\provider::export_groups(
86 $context,
87 'enrol_cohort',
88 [get_string('pluginname', 'enrol_cohort')]
89 );
90 }
91 }
92 }
93
94 /**
95 * Delete all use data which matches the specified deletion_criteria.
96 *
97 * @param context $context A user context.
98 */
99 public static function delete_data_for_all_users_in_context(\context $context) {
100 if (empty($context)) {
101 return;
102 }
103 if ($context->contextlevel == CONTEXT_COURSE) {
104 // Delete all the associated groups.
105 \core_group\privacy\provider::delete_groups_for_all_users($context, 'enrol_cohort');
106 }
107 }
108 /**
109 * Delete all user data for the specified user, in the specified contexts.
110 *
111 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
112 */
113 public static function delete_data_for_user(approved_contextlist $contextlist) {
114 if (empty($contextlist->count())) {
115 return;
116 }
117 \core_group\privacy\provider::delete_groups_for_user($contextlist, 'enrol_cohort');
980e85cd 118 }
119}