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/>.
17 * Privacy Subsystem implementation for enrol_flatfile.
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
23 namespace enrol_flatfile\privacy;
24 use core_privacy\local\metadata\collection;
25 use core_privacy\local\request\approved_contextlist;
26 use core_privacy\local\request\context;
27 use core_privacy\local\request\contextlist;
28 use core_privacy\local\request\writer;
29 use core_privacy\local\request\transform;
31 defined('MOODLE_INTERNAL') || die();
33 * Privacy Subsystem for enrol_flatfile implementing null_provider.
35 * @copyright 2018 Carlos Escobedo <carlos@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class provider implements
39 \core_privacy\local\metadata\provider,
40 \core_privacy\local\request\plugin\provider {
43 * Returns meta data about this system.
45 * @param collection $collection The initialised collection to add items to.
46 * @return collection A listing of user data stored through this system.
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');
61 * Get the list of contexts that contain user information for the specified user.
63 * @param int $userid The user to search.
64 * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
66 public static function get_contexts_for_userid(int $userid) : contextlist {
68 FROM {enrol_flatfile} ef
69 JOIN {context} c ON c.contextlevel = ? AND c.instanceid = ef.courseid
71 $params = [CONTEXT_COURSE, $userid];
73 $contextlist = new contextlist();
74 $contextlist->set_component('enrol_flatfile');
75 return $contextlist->add_from_sql($sql, $params);
79 * Export all user data for the specified user, in the specified contexts.
81 * @param approved_contextlist $contextlist The approved contexts to export information for.
83 public static function export_user_data(approved_contextlist $contextlist) {
86 // Ensure all contexts are CONTEXT_COURSE.
87 $contexts = static::validate_contextlist_contexts($contextlist);
88 if (empty($contexts)) {
92 // Get the context instance ids from the contexts. These are the course ids..
93 $contextinstanceids = array_map(function($context) {
94 return $context->instanceid;
96 $userid = $contextlist->get_user()->id;
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);
109 $futureenrolments = $DB->get_recordset_sql($sql, $params);
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;
118 $futureenrolments->close();
120 // And finally, write out the data to the relevant course contexts.
121 $subcontext = \core_enrol\privacy\provider::get_subcontext([get_string('pluginname', 'enrol_flatfile')]);
122 foreach ($enrolmentdata as $courseid => $enrolments) {
124 'pendingenrolments' => $enrolments,
126 writer::with_context(\context_course::instance($courseid))->export_data($subcontext, $data);
131 * Delete all data for all users in the specified context.
133 * @param \context $context The specific context to delete data for.
135 public static function delete_data_for_all_users_in_context(\context $context) {
136 if ($context->contextlevel != CONTEXT_COURSE) {
140 $DB->delete_records('enrol_flatfile', ['courseid' => $context->instanceid]);
144 * Delete all user data for the specified user, in the specified contexts.
146 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
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)) {
155 // Get the course ids based on the provided contexts.
156 $contextinstanceids = array_map(function($context) {
157 return $context->instanceid;
158 }, $contextlist->get_contexts());
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);
169 * Simple sanity check on the contextlist contexts, making sure they're of CONTEXT_COURSE contextlevel.
171 * @param approved_contextlist $contextlist
172 * @return array the array of contexts filtered to only include those of CONTEXT_COURSE contextlevel.
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) {