See Release Notes
Long Term Support Release
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 /** 18 * Privacy Subsystem implementation for mnetservice_enrol. 19 * 20 * @package mnetservice_enrol 21 * @copyright 2018 Carlos Escobedo <carlos@moodle.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace mnetservice_enrol\privacy; 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 use core_privacy\local\metadata\collection; 30 use core_privacy\local\request\approved_contextlist; 31 use core_privacy\local\request\context; 32 use core_privacy\local\request\contextlist; 33 use core_privacy\local\request\transform; 34 use core_privacy\local\request\writer; 35 use core_privacy\local\request\userlist; 36 use core_privacy\local\request\approved_userlist; 37 38 /** 39 * Privacy Subsystem for mnetservice_enrol implementing metadata and plugin providers. 40 * 41 * @copyright 2018 Carlos Escobedo <carlos@moodle.com> 42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 43 */ 44 class provider implements 45 \core_privacy\local\metadata\provider, 46 \core_privacy\local\request\core_userlist_provider, 47 \core_privacy\local\request\plugin\provider { 48 49 /** 50 * Returns meta data about this system. 51 * 52 * @param collection $collection The initialised collection to add items to. 53 * @return collection A listing of user data stored through this system. 54 */ 55 public static function get_metadata(collection $collection) : collection { 56 $collection->add_database_table( 57 'mnetservice_enrol_enrolments', 58 [ 59 'hostid' => 'privacy:metadata:mnetservice_enrol_enrolments:hostid', 60 'userid' => 'privacy:metadata:mnetservice_enrol_enrolments:userid', 61 'remotecourseid' => 'privacy:metadata:mnetservice_enrol_enrolments:remotecourseid', 62 'rolename' => 'privacy:metadata:mnetservice_enrol_enrolments:rolename', 63 'enroltime' => 'privacy:metadata:mnetservice_enrol_enrolments:enroltime', 64 'enroltype' => 'privacy:metadata:mnetservice_enrol_enrolments:enroltype' 65 ], 66 'privacy:metadata:mnetservice_enrol_enrolments:tableexplanation' 67 ); 68 69 return $collection; 70 } 71 72 /** 73 * Get the list of contexts that contain user information for the specified user. 74 * 75 * @param int $userid The user to search. 76 * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin. 77 */ 78 public static function get_contexts_for_userid(int $userid) : contextlist { 79 $sql = "SELECT c.id 80 FROM {context} c 81 JOIN {mnetservice_enrol_enrolments} me 82 ON me.userid = c.instanceid 83 AND c.contextlevel = :contextlevel 84 WHERE me.userid = :userid"; 85 $params = [ 86 'contextlevel' => CONTEXT_USER, 87 'userid' => $userid 88 ]; 89 $contextlist = new contextlist(); 90 $contextlist->add_from_sql($sql, $params); 91 return $contextlist; 92 } 93 94 /** 95 * Get the list of users within a specific context. 96 * 97 * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination. 98 */ 99 public static function get_users_in_context(userlist $userlist) { 100 $context = $userlist->get_context(); 101 102 if (!$context instanceof \context_user) { 103 return; 104 } 105 106 $params = ['userid' => $context->instanceid]; 107 108 $sql = "SELECT userid 109 FROM {mnetservice_enrol_enrolments} 110 WHERE userid = :userid"; 111 112 $userlist->add_from_sql('userid', $sql, $params); 113 } 114 115 /** 116 * Export all user data for the specified user, in the specified contexts. 117 * 118 * @param approved_contextlist $contextlist The approved contexts to export information for. 119 */ 120 public static function export_user_data(approved_contextlist $contextlist) { 121 global $DB; 122 if (empty($contextlist->count())) { 123 return; 124 } 125 $userid = $contextlist->get_user()->id; 126 $contextuser = \context_user::instance($userid); 127 list($insql, $inparams) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED); 128 $params = [ 129 'userid' => $userid, 130 'contextlevel' => CONTEXT_USER 131 ]; 132 $params += $inparams; 133 $sql = "SELECT me.id, 134 me.rolename, 135 me.enroltime, 136 me.enroltype, 137 mh.name as hostname, 138 mc.fullname 139 FROM {mnetservice_enrol_enrolments} me 140 JOIN {context} ctx 141 ON ctx.instanceid = me.userid 142 AND ctx.contextlevel = :contextlevel 143 JOIN {mnet_host} mh 144 ON mh.id = me.hostid 145 JOIN {mnetservice_enrol_courses} mc 146 ON mc.remoteid = me.remotecourseid 147 WHERE me.userid = :userid 148 AND ctx.id {$insql}"; 149 $mnetenrolments = $DB->get_records_sql($sql, $params); 150 foreach ($mnetenrolments as $mnetenrolment) { 151 // The core_enrol data export is organised in: 152 // {User Context}/User enrolments/data.json. 153 $data[] = (object) [ 154 'host' => $mnetenrolment->hostname, 155 'remotecourseid' => $mnetenrolment->fullname, 156 'rolename' => $mnetenrolment->rolename, 157 'enroltime' => transform::datetime($mnetenrolment->enroltime), 158 'enroltype' => $mnetenrolment->enroltype 159 ]; 160 } 161 writer::with_context($contextuser)->export_data( 162 [get_string('privacy:metadata:mnetservice_enrol_enrolments', 'mnetservice_enrol')], 163 (object)$data 164 ); 165 } 166 167 /** 168 * Delete all data for all users in the specified context. 169 * 170 * @param context $context The specific context to delete data for. 171 */ 172 public static function delete_data_for_all_users_in_context(\context $context) { 173 // Sanity check that context is at the User context level. 174 if ($context->contextlevel == CONTEXT_USER) { 175 static::delete_user_data($context->instanceid); 176 } 177 } 178 179 /** 180 * Delete multiple users within a single context. 181 * 182 * @param approved_userlist $userlist The approved context and user information to delete information for. 183 */ 184 public static function delete_data_for_users(approved_userlist $userlist) { 185 $context = $userlist->get_context(); 186 187 if ($context instanceof \context_user) { 188 static::delete_user_data($context->instanceid); 189 } 190 } 191 192 /** 193 * Delete all user data for the specified user, in the specified contexts. 194 * 195 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for. 196 */ 197 public static function delete_data_for_user(approved_contextlist $contextlist) { 198 if (empty($contextlist->count())) { 199 return; 200 } 201 $user = $contextlist->get_user(); 202 foreach ($contextlist->get_contexts() as $context) { 203 // Verify the context is a user context and that the instanceid matches the userid of the contextlist. 204 if ($context->contextlevel == CONTEXT_USER && $context->instanceid == $user->id) { 205 // Get the data and write it. 206 static::delete_user_data($user->id); 207 } 208 } 209 } 210 211 /** 212 * This does the deletion of user data for the mnetservice_enrolments. 213 * 214 * @param int $userid The user ID 215 */ 216 protected static function delete_user_data(int $userid) { 217 global $DB; 218 // Because we only use user contexts the instance ID is the user ID. 219 $DB->delete_records('mnetservice_enrol_enrolments', ['userid' => $userid]); 220 } 221 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body