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_course
21 * @copyright 2018 Adrian Greeve <adrian@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_course\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\writer;
33 use \core_privacy\local\request\transform;
36 * Privacy class for requesting user data.
38 * @package core_course
39 * @copyright 2018 Adrian Greeve <adrian@moodle.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class provider implements
43 \core_privacy\local\metadata\provider,
44 \core_privacy\local\request\context_aware_provider,
45 \core_privacy\local\request\plugin\provider,
46 \core_privacy\local\request\user_preference_provider {
49 * Returns meta data about this system.
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_subsystem_link('core_completion', [], 'privacy:metadata:completionsummary');
56 $collection->add_user_preference('coursecat_management_perpage', 'privacy:perpage');
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 {
67 list($join, $where, $params) = \core_completion\privacy\provider::get_course_completion_join_sql($userid, 'cc', 'c.id');
70 JOIN {course} c ON ctx.instanceid = c.id AND ctx.contextlevel = :contextcourse
73 $params['contextcourse'] = CONTEXT_COURSE;
74 $contextlist = new contextlist();
75 $contextlist->add_from_sql($sql, $params);
80 * Export all user data for the specified user, in the specified contexts.
82 * @param approved_contextlist $contextlist The approved contexts to export information for.
84 public static function export_user_data(approved_contextlist $contextlist) {
88 list($select, $params) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED);
89 $params['contextcourse'] = CONTEXT_COURSE;
93 JOIN {context} ctx ON c.id = ctx.instanceid AND ctx.contextlevel = :contextcourse
94 WHERE ctx.id $select";
96 $courses = $DB->get_recordset_sql($sql, $params);
97 foreach ($courses as $course) {
98 $coursecompletion = \core_completion\privacy\provider::get_course_completion_info($contextlist->get_user(), $course);
99 writer::with_context(\context_course::instance($course->id))->export_data(
100 [get_string('privacy:completionpath', 'course')], (object) $coursecompletion);
106 * Exports course information based on the whole approved context list collection.
108 * @param \core_privacy\local\request\contextlist_collection $contextcollection The collection of approved context lists.
110 public static function export_complete_context_data(\core_privacy\local\request\contextlist_collection $completelist) {
113 $coursecontextids = $DB->get_records('context', ['contextlevel' => CONTEXT_COURSE], '', 'id, instanceid');
116 foreach ($completelist as $component) {
117 foreach ($component->get_contexts() as $context) {
118 if ($context->contextlevel == CONTEXT_USER
119 || $context->contextlevel == CONTEXT_SYSTEM
120 || $context->contextlevel == CONTEXT_COURSECAT) {
121 // Move onto the next context as these will not contain course contexts.
124 foreach ($coursecontextids as $contextid => $record) {
125 if (stripos($context->path, '/' . $contextid . '/') !== false) {
126 $courseids[$contextid] = $record->instanceid;
131 if (empty($courseids)) {
135 // Export general data for these contexts.
136 list($sql, $params) = $DB->get_in_or_equal($courseids);
138 $coursedata = $DB->get_records_select('course', $sql, $params);
140 foreach ($coursedata as $course) {
141 $context = \context_course::instance($course->id);
143 'fullname' => $course->fullname,
144 'shortname' => $course->shortname,
145 'idnumber' => $course->idnumber,
146 'summary' => writer::with_context($context)->rewrite_pluginfile_urls([], 'course', 'summary', 0, $course->summary),
147 'format' => get_string('pluginname', 'format_' . $course->format),
148 'startdate' => transform::datetime($course->startdate),
149 'enddate' => transform::datetime($course->enddate)
151 writer::with_context($context)
152 ->export_area_files([], 'course', 'summary', 0)
153 ->export_area_files([], 'course', 'overviewfiles', 0)
154 ->export_data([], $data);
159 * Export all user preferences for the plugin.
161 * @param int $userid The userid of the user whose data is to be exported.
163 public static function export_user_preferences(int $userid) {
164 $perpage = get_user_preferences('coursecat_management_perpage', null, $userid);
165 if (isset($perpage)) {
166 writer::export_user_preference('core_course',
167 'coursecat_management_perpage',
169 get_string('privacy:perpage', 'course')
175 * Delete all data for all users in the specified context.
177 * @param context $context The specific context to delete data for.
179 public static function delete_data_for_all_users_in_context(\context $context) {
180 // Check what context we've been delivered.
181 if ($context->contextlevel == CONTEXT_COURSE) {
182 // Delete course completion data.
183 \core_completion\privacy\provider::delete_completion(null, $context->instanceid);
188 * Delete all user data for the specified user, in the specified contexts.
190 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
192 public static function delete_data_for_user(approved_contextlist $contextlist) {
193 foreach ($contextlist as $context) {
194 if ($context->contextlevel == CONTEXT_COURSE) {
195 // Delete course completion data.
196 \core_completion\privacy\provider::delete_completion($contextlist->get_user(), $context->instanceid);