MDL-62426 core_enrol: control enrolment subcontexts at the provider
[moodle.git] / enrol / flatfile / 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_flatfile.
18 *
19 * @package enrol_flatfile
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_flatfile\privacy;
37e10692
JD
24use core_privacy\local\metadata\collection;
25use core_privacy\local\request\approved_contextlist;
26use core_privacy\local\request\context;
27use core_privacy\local\request\contextlist;
28use core_privacy\local\request\writer;
29use core_privacy\local\request\transform;
30
980e85cd 31defined('MOODLE_INTERNAL') || die();
32/**
33 * Privacy Subsystem for enrol_flatfile implementing null_provider.
34 *
35 * @copyright 2018 Carlos Escobedo <carlos@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
37e10692
JD
38class provider implements
39 \core_privacy\local\metadata\provider,
40 \core_privacy\local\request\plugin\provider {
41
980e85cd 42 /**
37e10692 43 * Returns meta data about this system.
980e85cd 44 *
37e10692
JD
45 * @param collection $collection The initialised collection to add items to.
46 * @return collection A listing of user data stored through this system.
980e85cd 47 */
37e10692
JD
48 public static function get_metadata(collection $collection) : collection {
49 return $collection->add_database_table('enrol_flatfile', [
50 'action' => 'privacy:metadata:enrol_flatfile:action',
51 'roleid' => 'privacy:metadata:enrol_flatfile:roleid',
52 'userid' => 'privacy:metadata:enrol_flatfile:userid',
53 'courseid' => 'privacy:metadata:enrol_flatfile:courseid',
54 'timestart' => 'privacy:metadata:enrol_flatfile:timestart',
55 'timeend' => 'privacy:metadata:enrol_flatfile:timeend',
56 'timemodified' => 'privacy:metadata:enrol_flatfile:timemodified'
57 ], 'privacy:metadata:enrol_flatfile');
980e85cd 58 }
37e10692
JD
59
60 /**
61 * Get the list of contexts that contain user information for the specified user.
62 *
63 * @param int $userid The user to search.
64 * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
65 */
66 public static function get_contexts_for_userid(int $userid) : contextlist {
67 $sql = "SELECT c.id
68 FROM {enrol_flatfile} ef
69 JOIN {context} c ON c.contextlevel = ? AND c.instanceid = ef.courseid
70 WHERE ef.userid = ?";
71 $params = [CONTEXT_COURSE, $userid];
72
73 $contextlist = new contextlist();
74 $contextlist->set_component('enrol_flatfile');
75 return $contextlist->add_from_sql($sql, $params);
76 }
77
78 /**
79 * Export all user data for the specified user, in the specified contexts.
80 *
81 * @param approved_contextlist $contextlist The approved contexts to export information for.
82 */
83 public static function export_user_data(approved_contextlist $contextlist) {
84 global $DB;
85
86 // Ensure all contexts are CONTEXT_COURSE.
87 $contexts = static::validate_contextlist_contexts($contextlist);
88 if (empty($contexts)) {
89 return;
90 }
91
92 // Get the context instance ids from the contexts. These are the course ids..
93 $contextinstanceids = array_map(function($context) {
94 return $context->instanceid;
95 }, $contexts);
96 $userid = $contextlist->get_user()->id;
97
98 // Now, we just need to fetch and format all entries corresponding to the contextids provided.
99 $sql = "SELECT ef.action, r.shortname, ef.courseid, ef.timestart, ef.timeend, ef.timemodified
100 FROM {enrol_flatfile} ef
101 JOIN {context} c ON c.contextlevel = :contextlevel AND c.instanceid = ef.courseid
102 JOIN {role} r ON r.id = ef.roleid
103 WHERE ef.userid = :userid";
104 $params = ['contextlevel' => CONTEXT_COURSE, 'userid' => $userid];
105 list($insql, $inparams) = $DB->get_in_or_equal($contextinstanceids, SQL_PARAMS_NAMED);
106 $sql .= " AND ef.courseid $insql";
107 $params = array_merge($params, $inparams);
108
109 $futureenrolments = $DB->get_recordset_sql($sql, $params);
110 $enrolmentdata = [];
111 foreach ($futureenrolments as $futureenrolment) {
112 // It's possible to have more than one future enrolment per course.
113 $futureenrolment->timestart = transform::datetime($futureenrolment->timestart);
114 $futureenrolment->timeend = transform::datetime($futureenrolment->timeend);
115 $futureenrolment->timemodified = transform::datetime($futureenrolment->timemodified);
116 $enrolmentdata[$futureenrolment->courseid][] = $futureenrolment;
117 }
118 $futureenrolments->close();
119
120 // And finally, write out the data to the relevant course contexts.
72580c08 121 $subcontext = \core_enrol\privacy\provider::get_subcontext([get_string('pluginname', 'enrol_flatfile')]);
37e10692
JD
122 foreach ($enrolmentdata as $courseid => $enrolments) {
123 $data = (object) [
124 'pendingenrolments' => $enrolments,
125 ];
126 writer::with_context(\context_course::instance($courseid))->export_data($subcontext, $data);
127 }
128 }
129
130 /**
131 * Delete all data for all users in the specified context.
132 *
133 * @param \context $context The specific context to delete data for.
134 */
135 public static function delete_data_for_all_users_in_context(\context $context) {
136 if ($context->contextlevel != CONTEXT_COURSE) {
137 return;
138 }
139 global $DB;
140 $DB->delete_records('enrol_flatfile', ['courseid' => $context->instanceid]);
141 }
142
143 /**
144 * Delete all user data for the specified user, in the specified contexts.
145 *
146 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
147 */
148 public static function delete_data_for_user(approved_contextlist $contextlist) {
149 // Only delete data from contexts which are at the COURSE_MODULE contextlevel.
150 $contexts = self::validate_contextlist_contexts($contextlist);
151 if (empty($contexts)) {
152 return;
153 }
154
155 // Get the course ids based on the provided contexts.
156 $contextinstanceids = array_map(function($context) {
157 return $context->instanceid;
158 }, $contextlist->get_contexts());
159
160 global $DB;
161 $user = $contextlist->get_user();
162 list($insql, $inparams) = $DB->get_in_or_equal($contextinstanceids, SQL_PARAMS_NAMED);
163 $params = array_merge(['userid' => $user->id], $inparams);
164 $sql = "userid = :userid AND courseid $insql";
165 $DB->delete_records_select('enrol_flatfile', $sql, $params);
166 }
167
168 /**
169 * Simple sanity check on the contextlist contexts, making sure they're of CONTEXT_COURSE contextlevel.
170 *
171 * @param approved_contextlist $contextlist
172 * @return array the array of contexts filtered to only include those of CONTEXT_COURSE contextlevel.
173 */
174 protected static function validate_contextlist_contexts(approved_contextlist $contextlist) {
175 return array_reduce($contextlist->get_contexts(), function($carry, $context) {
176 if ($context->contextlevel == CONTEXT_COURSE) {
177 $carry[] = $context;
178 }
179 return $carry;
180 }, []);
181 }
182}