Permalink
Browse files

Merge branch 'wip-MDL-61808-34' of git://github.com/abgreeve/moodle i…

…nto MOODLE_34_STABLE
  • Loading branch information...
dmonllao committed Apr 16, 2018
2 parents 6b2ac96 + 496c5f5 commit e0c45f30a82e03049daed0d8a48d73502840be28
@@ -0,0 +1,211 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Privacy class for requesting user data.
*
* @package tool_monitor
* @copyright 2018 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_monitor\privacy;
defined('MOODLE_INTERNAL') || die();
use \core_privacy\local\metadata\collection;
use \core_privacy\local\request\contextlist;
use \core_privacy\local\request\approved_contextlist;
use \core_privacy\local\request\transform;
use \core_privacy\local\request\writer;
use \tool_monitor\subscription_manager;
use \tool_monitor\rule_manager;
/**
* Privacy provider for tool_monitor
*
* @package tool_monitor
* @copyright 2018 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\provider, \core_privacy\local\request\plugin\provider {
/**
* Get information about the user data stored by this plugin.
*
* @param collection $collection An object for storing metadata.
* @return collection The metadata.
*/
public static function get_metadata(collection $collection) : collection {
$toolmonitorrules = [
'description' => 'privacy:metadata:description',
'name' => 'privacy:metadata:name',
'userid' => 'privacy:metadata:userid',
'plugin' => 'privacy:metadata:plugin',
'eventname' => 'privacy:metadata:eventname',
'template' => 'privacy:metadata:template',
'frequency' => 'privacy:metadata:frequency',
'timewindow' => 'privacy:metadata:timewindow',
'timemodified' => 'privacy:metadata:timemodifiedrule',
'timecreated' => 'privacy:metadata:timecreatedrule'
];
$toolmonitorsubscriptions = [
'userid' => 'privacy:metadata:useridsub',
'timecreated' => 'privacy:metadata:timecreatedsub',
'lastnotificationsent' => 'privacy:metadata:lastnotificationsent',
'inactivedate' => 'privacy:metadata:inactivedate'
];
// Tool monitor history doesn't look like it is used at all.
$toolmonitorhistory = [
'userid' => 'privacy:metadata:useridhistory',
'timesent' => 'privacy:metadata:timesent'
];
$collection->add_database_table('tool_monitor_rules', $toolmonitorrules, 'privacy:metadata:rulessummary');
$collection->add_database_table('tool_monitor_subscriptions', $toolmonitorsubscriptions,
'privacy:metadata:subscriptionssummary');
$collection->add_database_table('tool_monitor_history', $toolmonitorhistory, 'privacy:metadata:historysummary');
$collection->link_subsystem('core_message', 'privacy:metadata:messagesummary');
return $collection;
}
/**
* Return all contexts for this userid. In this situation the user context.
*
* @param int $userid The user ID.
* @return contextlist The list of context IDs.
*/
public static function get_contexts_for_userid(int $userid) : contextlist {
$params = ['useridrules' => $userid, 'useridsubscriptions' => $userid, 'contextuserrule' => CONTEXT_USER,
'contextusersub' => CONTEXT_USER];
$sql = "SELECT DISTINCT ctx.id
FROM {context} ctx
LEFT JOIN {tool_monitor_rules} mr ON ctx.instanceid = mr.userid AND ctx.contextlevel = :contextuserrule
LEFT JOIN {tool_monitor_subscriptions} ms ON ctx.instanceid = ms.userid AND ctx.contextlevel = :contextusersub
WHERE (ms.userid = :useridrules OR mr.userid = :useridsubscriptions)";
$contextlist = new contextlist();
$contextlist->add_from_sql($sql, $params);
return $contextlist;
}
/**
* Export all event monitor information for the list of contexts and this user.
*
* @param approved_contextlist $contextlist The list of approved contexts for a user.
*/
public static function export_user_data(approved_contextlist $contextlist) {
global $DB;
// Export rules.
$context = \context_user::instance($contextlist->get_user()->id);
$rules = $DB->get_records('tool_monitor_rules', ['userid' => $contextlist->get_user()->id]);
if ($rules) {
static::export_monitor_rules($rules, $context);
}
// Export subscriptions.
$subscriptions = subscription_manager::get_user_subscriptions(0, 0, $contextlist->get_user()->id);
if ($subscriptions) {
static::export_monitor_subscriptions($subscriptions, $context);
}
}
/**
* Delete all user data for this context.
*
* @param \context $context The context to delete data for.
*/
public static function delete_data_for_all_users_in_context(\context $context) {
// Only delete data for user contexts.
if ($context->contextlevel == CONTEXT_USER) {
static::delete_user_data($context->instanceid);
}
}
/**
* Delete all user data for this user only.
*
* @param approved_contextlist $contextlist The list of approved contexts for a user.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
static::delete_user_data($contextlist->get_user()->id);
}
/**
* This does the deletion of user data for the event monitor.
*
* @param int $userid The user ID
*/
protected static function delete_user_data(int $userid) {
global $DB;
// Delete this user's subscriptions first.
subscription_manager::delete_user_subscriptions($userid);
// Because we only use user contexts the instance ID is the user ID.
// Get the rules and check if this user has the capability to delete them.
$rules = $DB->get_records('tool_monitor_rules', ['userid' => $userid]);
foreach ($rules as $ruledata) {
$rule = rule_manager::get_rule($ruledata);
// If no-one is suscribed to the rule then it is safe to delete.
if ($rule->can_manage_rule($userid) && subscription_manager::count_rule_subscriptions($rule->id) == 0) {
$rule->delete_rule();
}
}
}
/**
* This formats and then exports the monitor rules.
*
* @param array $rules The monitor rules.
* @param context_user $context The user context
*/
protected static function export_monitor_rules(array $rules, \context_user $context) {
foreach ($rules as $rule) {
$rule = rule_manager::get_rule($rule);
$ruledata = new \stdClass();
$ruledata->name = $rule->name;
$ruledata->eventname = $rule->get_event_name();
$ruledata->description = $rule->get_description($context);
$ruledata->plugin = $rule->get_plugin_name();
$ruledata->template = $rule->template;
$ruledata->frequency = $rule->get_filters_description();
$ruledata->course = $rule->get_course_name($context);
$ruledata->timecreated = transform::datetime($rule->timecreated);
$ruledata->timemodified = transform::datetime($rule->timemodified);
writer::with_context($context)->export_data([get_string('privacy:createdrules', 'tool_monitor'),
$rule->name . '_' . $rule->id], $ruledata);
}
}
/**
* This formats and then exports the event monitor subscriptions.
*
* @param array $subscriptions Subscriptions
* @param \context_user $context The user context
*/
protected static function export_monitor_subscriptions(array $subscriptions, \context_user $context) {
foreach ($subscriptions as $subscription) {
$subscriptiondata = new \stdClass();
$subscriptiondata->instancename = $subscription->get_instance_name();
$subscriptiondata->eventname = $subscription->get_event_name();
$subscriptiondata->frequency = $subscription->get_filters_description();
$subscriptiondata->name = $subscription->get_name($context);
$subscriptiondata->description = $subscription->get_description($context);
$subscriptiondata->pluginname = $subscription->get_plugin_name();
$subscriptiondata->course = $subscription->get_course_name($context);
$subscriptiondata->timecreated = transform::datetime($subscription->timecreated);
$subscriptiondata->lastnotificationsent = transform::datetime($subscription->lastnotificationsent);
writer::with_context($context)->export_data([get_string('privacy:subscriptions', 'tool_monitor'),
$subscriptiondata->name . '_' . $subscription->id, $subscriptiondata->course, $subscriptiondata->instancename],
$subscriptiondata);
}
}
}
@@ -51,14 +51,15 @@ public function __construct($rule) {
}
/**
* Can the current user manage this rule?
* Can the user manage this rule? Defaults to $USER.
*
* @param int $userid Check against this userid.
* @return bool true if the current user can manage this rule, else false.
*/
public function can_manage_rule() {
public function can_manage_rule($userid = null) {
$courseid = $this->courseid;
$context = empty($courseid) ? \context_system::instance() : \context_course::instance($this->courseid);
return has_capability('tool/monitor:managerules', $context);
return has_capability('tool/monitor:managerules', $context, $userid);
}
/**
@@ -79,6 +79,28 @@
$string['monitor:subscribe'] = 'Subscribe to event monitor rules';
$string['norules'] = 'There are no event monitoring rules.';
$string['pluginname'] = 'Event monitor';
$string['privacy:createdrules'] = 'Event monitor rules I created';
$string['privacy:metadata:description'] = 'Description of the rule';
$string['privacy:metadata:eventname'] = 'Fully qualified name of the event';
$string['privacy:metadata:frequency'] = 'Frequency of notifications';
$string['privacy:metadata:historysummary'] = 'Stores the history of the message notifications sent';
$string['privacy:metadata:inactivedate'] = 'Period of time, in days, after which an inactive subscription will be removed completely';
$string['privacy:metadata:lastnotificationsent'] = 'When a notification was last sent for this subscription.';
$string['privacy:metadata:messagesummary'] = 'Notifications are sent to the message system.';
$string['privacy:metadata:name'] = 'Name of the rule';
$string['privacy:metadata:plugin'] = 'Frankenstlye name of the plugin';
$string['privacy:metadata:rulessummary'] = 'This stores monitor rules.';
$string['privacy:metadata:subscriptionssummary'] = 'Stores user subscriptions to various rules';
$string['privacy:metadata:template'] = 'Message template';
$string['privacy:metadata:timecreatedrule'] = 'When this rule was created';
$string['privacy:metadata:timecreatedsub'] = 'When this subscription was created';
$string['privacy:metadata:timemodifiedrule'] = 'When this rule was last modified';
$string['privacy:metadata:timesent'] = 'When the message was sent';
$string['privacy:metadata:timewindow'] = 'Time window in seconds';
$string['privacy:metadata:userid'] = 'Id of user who created the rule';
$string['privacy:metadata:useridhistory'] = 'User to whom this notification was sent';
$string['privacy:metadata:useridsub'] = 'User id of the subscriber';
$string['privacy:subscriptions'] = 'My event monitor subscriptions';
$string['processevents'] = 'Process events';
$string['rulename'] = 'Rule name';
$string['ruleareyousure'] = 'Are you sure you want to delete the rule "{$a}"?';
Oops, something went wrong.

0 comments on commit e0c45f3

Please sign in to comment.