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 * Privacy class for requesting user data. 18 * 19 * @package tool_monitor 20 * @copyright 2018 Adrian Greeve <adrian@moodle.com> 21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 22 */ 23 namespace tool_monitor\privacy; 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 use \core_privacy\local\metadata\collection; 28 use \core_privacy\local\request\contextlist; 29 use \core_privacy\local\request\approved_contextlist; 30 use \core_privacy\local\request\approved_userlist; 31 use \core_privacy\local\request\transform; 32 use \core_privacy\local\request\userlist; 33 use \core_privacy\local\request\writer; 34 use \tool_monitor\subscription_manager; 35 use \tool_monitor\rule_manager; 36 37 /** 38 * Privacy provider for tool_monitor 39 * 40 * @package tool_monitor 41 * @copyright 2018 Adrian Greeve <adrian@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 * Get information about the user data stored by this plugin. 51 * 52 * @param collection $collection An object for storing metadata. 53 * @return collection The metadata. 54 */ 55 public static function get_metadata(collection $collection) : collection { 56 $toolmonitorrules = [ 57 'description' => 'privacy:metadata:description', 58 'name' => 'privacy:metadata:name', 59 'userid' => 'privacy:metadata:userid', 60 'plugin' => 'privacy:metadata:plugin', 61 'eventname' => 'privacy:metadata:eventname', 62 'template' => 'privacy:metadata:template', 63 'frequency' => 'privacy:metadata:frequency', 64 'timewindow' => 'privacy:metadata:timewindow', 65 'timemodified' => 'privacy:metadata:timemodifiedrule', 66 'timecreated' => 'privacy:metadata:timecreatedrule' 67 ]; 68 $toolmonitorsubscriptions = [ 69 'userid' => 'privacy:metadata:useridsub', 70 'timecreated' => 'privacy:metadata:timecreatedsub', 71 'lastnotificationsent' => 'privacy:metadata:lastnotificationsent', 72 'inactivedate' => 'privacy:metadata:inactivedate' 73 ]; 74 // Tool monitor history doesn't look like it is used at all. 75 $toolmonitorhistory = [ 76 'userid' => 'privacy:metadata:useridhistory', 77 'timesent' => 'privacy:metadata:timesent' 78 ]; 79 $collection->add_database_table('tool_monitor_rules', $toolmonitorrules, 'privacy:metadata:rulessummary'); 80 $collection->add_database_table('tool_monitor_subscriptions', $toolmonitorsubscriptions, 81 'privacy:metadata:subscriptionssummary'); 82 $collection->add_database_table('tool_monitor_history', $toolmonitorhistory, 'privacy:metadata:historysummary'); 83 $collection->link_subsystem('core_message', 'privacy:metadata:messagesummary'); 84 return $collection; 85 } 86 87 /** 88 * Return all contexts for this userid. In this situation the user context. 89 * 90 * @param int $userid The user ID. 91 * @return contextlist The list of context IDs. 92 */ 93 public static function get_contexts_for_userid(int $userid) : contextlist { 94 $params = ['useridrules' => $userid, 'useridsubscriptions' => $userid, 'contextuserrule' => CONTEXT_USER, 95 'contextusersub' => CONTEXT_USER]; 96 $sql = "SELECT DISTINCT ctx.id 97 FROM {context} ctx 98 LEFT JOIN {tool_monitor_rules} mr ON ctx.instanceid = mr.userid AND ctx.contextlevel = :contextuserrule 99 AND mr.userid = :useridsubscriptions 100 LEFT JOIN {tool_monitor_subscriptions} ms ON ctx.instanceid = ms.userid AND ctx.contextlevel = :contextusersub 101 AND ms.userid = :useridrules 102 WHERE ms.id IS NOT NULL OR mr.id IS NOT NULL"; 103 104 $contextlist = new contextlist(); 105 $contextlist->add_from_sql($sql, $params); 106 return $contextlist; 107 } 108 109 /** 110 * Get the list of users who have data within a context. 111 * 112 * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination. 113 */ 114 public static function get_users_in_context(userlist $userlist) { 115 $context = $userlist->get_context(); 116 117 if (!$context instanceof \context_user) { 118 return; 119 } 120 121 $params = ['userid' => $context->instanceid]; 122 123 $sql = "SELECT userid FROM {tool_monitor_rules} WHERE userid = :userid"; 124 $userlist->add_from_sql('userid', $sql, $params); 125 126 $sql = "SELECT userid FROM {tool_monitor_subscriptions} WHERE userid = :userid"; 127 $userlist->add_from_sql('userid', $sql, $params); 128 } 129 130 /** 131 * Export all event monitor information for the list of contexts and this user. 132 * 133 * @param approved_contextlist $contextlist The list of approved contexts for a user. 134 */ 135 public static function export_user_data(approved_contextlist $contextlist) { 136 global $DB; 137 // Export rules. 138 $context = \context_user::instance($contextlist->get_user()->id); 139 $rules = $DB->get_records('tool_monitor_rules', ['userid' => $contextlist->get_user()->id]); 140 if ($rules) { 141 static::export_monitor_rules($rules, $context); 142 } 143 // Export subscriptions. 144 $subscriptions = subscription_manager::get_user_subscriptions(0, 0, $contextlist->get_user()->id); 145 if ($subscriptions) { 146 static::export_monitor_subscriptions($subscriptions, $context); 147 } 148 } 149 150 /** 151 * Delete all user data for this context. 152 * 153 * @param \context $context The context to delete data for. 154 */ 155 public static function delete_data_for_all_users_in_context(\context $context) { 156 // Only delete data for user contexts. 157 if ($context->contextlevel == CONTEXT_USER) { 158 static::delete_user_data($context->instanceid); 159 } 160 } 161 162 /** 163 * Delete all user data for this user only. 164 * 165 * @param approved_contextlist $contextlist The list of approved contexts for a user. 166 */ 167 public static function delete_data_for_user(approved_contextlist $contextlist) { 168 static::delete_user_data($contextlist->get_user()->id); 169 } 170 171 /** 172 * Delete multiple users within a single context. 173 * 174 * @param approved_userlist $userlist The approved context and user information to delete information for. 175 */ 176 public static function delete_data_for_users(approved_userlist $userlist) { 177 $context = $userlist->get_context(); 178 $userids = $userlist->get_userids(); 179 $userid = reset($userids); 180 181 // Only delete data for user context, which should be a single user. 182 if ($context->contextlevel == CONTEXT_USER && count($userids) == 1 && $userid == $context->instanceid) { 183 static::delete_user_data($userid); 184 } 185 } 186 187 /** 188 * This does the deletion of user data for the event monitor. 189 * 190 * @param int $userid The user ID 191 */ 192 protected static function delete_user_data(int $userid) { 193 global $DB; 194 // Delete this user's subscriptions first. 195 subscription_manager::delete_user_subscriptions($userid); 196 // Because we only use user contexts the instance ID is the user ID. 197 // Get the rules and check if this user has the capability to delete them. 198 $rules = $DB->get_records('tool_monitor_rules', ['userid' => $userid]); 199 foreach ($rules as $ruledata) { 200 $rule = rule_manager::get_rule($ruledata); 201 // If no-one is suscribed to the rule then it is safe to delete. 202 if ($rule->can_manage_rule($userid) && subscription_manager::count_rule_subscriptions($rule->id) == 0) { 203 $rule->delete_rule(); 204 } 205 } 206 } 207 208 /** 209 * This formats and then exports the monitor rules. 210 * 211 * @param array $rules The monitor rules. 212 * @param context_user $context The user context 213 */ 214 protected static function export_monitor_rules(array $rules, \context_user $context) { 215 foreach ($rules as $rule) { 216 $rule = rule_manager::get_rule($rule); 217 $ruledata = new \stdClass(); 218 $ruledata->name = $rule->name; 219 $ruledata->eventname = $rule->get_event_name(); 220 $ruledata->description = $rule->get_description($context); 221 $ruledata->plugin = $rule->get_plugin_name(); 222 $ruledata->template = $rule->template; 223 $ruledata->frequency = $rule->get_filters_description(); 224 $ruledata->course = $rule->get_course_name($context); 225 $ruledata->timecreated = transform::datetime($rule->timecreated); 226 $ruledata->timemodified = transform::datetime($rule->timemodified); 227 writer::with_context($context)->export_data([get_string('privacy:createdrules', 'tool_monitor'), 228 $rule->name . '_' . $rule->id], $ruledata); 229 } 230 } 231 232 /** 233 * This formats and then exports the event monitor subscriptions. 234 * 235 * @param array $subscriptions Subscriptions 236 * @param \context_user $context The user context 237 */ 238 protected static function export_monitor_subscriptions(array $subscriptions, \context_user $context) { 239 foreach ($subscriptions as $subscription) { 240 $subscriptiondata = new \stdClass(); 241 $subscriptiondata->instancename = $subscription->get_instance_name(); 242 $subscriptiondata->eventname = $subscription->get_event_name(); 243 $subscriptiondata->frequency = $subscription->get_filters_description(); 244 $subscriptiondata->name = $subscription->get_name($context); 245 $subscriptiondata->description = $subscription->get_description($context); 246 $subscriptiondata->pluginname = $subscription->get_plugin_name(); 247 $subscriptiondata->course = $subscription->get_course_name($context); 248 $subscriptiondata->timecreated = transform::datetime($subscription->timecreated); 249 $subscriptiondata->lastnotificationsent = transform::datetime($subscription->lastnotificationsent); 250 writer::with_context($context)->export_data([get_string('privacy:subscriptions', 'tool_monitor'), 251 $subscriptiondata->name . '_' . $subscription->id, $subscriptiondata->course, $subscriptiondata->instancename], 252 $subscriptiondata); 253 } 254 } 255 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body