From 044503e33df2100ecf81010da7999f60fd43dd4d Mon Sep 17 00:00:00 2001 From: Ankit Agarwal Date: Tue, 9 Sep 2014 09:52:51 +0530 Subject: [PATCH] MDL-45758 tool_monitor: Add a UI page to manage subscriptions Original issue - MDL-46111 --- admin/tool/monitor/index.php | 110 ++++++++++++++++++++ admin/tool/monitor/lang/en/tool_monitor.php | 12 ++- 2 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 admin/tool/monitor/index.php diff --git a/admin/tool/monitor/index.php b/admin/tool/monitor/index.php new file mode 100644 index 00000000000..d2a90a57d8b --- /dev/null +++ b/admin/tool/monitor/index.php @@ -0,0 +1,110 @@ +. + +/** + * This page lets users to manage rules for a given course. + * + * @package tool_monitor + * @copyright 2014 onwards Ankit Agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(__DIR__ . '/../../../config.php'); +require_once($CFG->libdir.'/adminlib.php'); + +$courseid = optional_param('courseid', 0, PARAM_INT); +$action = optional_param('action', '', PARAM_ALPHA); +$cmid = optional_param('cmid', 0, PARAM_INT); +$ruleid = optional_param('ruleid', 0, PARAM_INT); +$subscriptionid = optional_param('subscriptionid', 0, PARAM_INT); + +// Validate course id. +if (empty($courseid)) { + require_login(); + $context = context_system::instance(); + $coursename = format_string($SITE->fullname, true, array('context' => $context)); + $PAGE->set_context($context); +} else { + $course = get_course($courseid); + require_login($course); + $context = context_course::instance($course->id); + $coursename = format_string($course->fullname, true, array('context' => $context)); +} + +// Check for caps. +require_capability('tool/monitor:subscribe', $context); + +// Set up the page. +$a = new stdClass(); +$a->coursename = $coursename; +$a->reportname = get_string('pluginname', 'tool_monitor'); +$title = get_string('title', 'tool_monitor', $a); +$indexurl = new moodle_url("/admin/tool/monitor/index.php", array('courseid' => $courseid)); + +$PAGE->set_url($indexurl); +$PAGE->set_pagelayout('report'); +$PAGE->set_title($title); +$PAGE->set_heading($title); + +// Site level report. +if (empty($courseid)) { + admin_externalpage_setup('toolmonitorsubscriptions', '', null, '', array('pagelayout' => 'report')); +} + +echo $OUTPUT->header(); + +// Create/delete subscription if needed. +if (!empty($action)) { + switch ($action) { + case 'subscribe' : + $rule = \tool_monitor\rule_manager::get_rule($ruleid); + $rule->subscribe_user($courseid, $cmid); + echo $OUTPUT->notification(get_string('subcreatesuccess', 'tool_monitor'), 'notifysuccess'); + break; + case 'unsubscribe' : + \tool_monitor\subscription_manager::delete_subscription($subscriptionid); + echo $OUTPUT->notification(get_string('subdeletesuccess', 'tool_monitor'), 'notifysuccess'); + break; + default: + } +} + +// Render the current subscriptions list. +$totalsubs = \tool_monitor\subscription_manager::count_user_subscriptions_for_course($courseid); +$renderer = $PAGE->get_renderer('tool_monitor', 'managesubs'); +if (!empty($totalsubs)) { + // Show the subscriptions section only if there are subscriptions. + $subs = new \tool_monitor\output\managesubs\subs('toolmonitorsubs', $indexurl, $courseid); + echo $OUTPUT->heading(get_string('currentsubscriptions', 'tool_monitor')); + echo $renderer->render($subs); +} + +// Render the potential rules list. +$totalrules = \tool_monitor\rule_manager::count_rules_by_courseid($courseid); +echo $OUTPUT->heading(get_string('rulescansubscribe', 'tool_monitor')); +if (!empty($totalrules)) { + $rules = new \tool_monitor\output\managesubs\rules('toolmonitorrules', $indexurl, $courseid); + echo $renderer->render($rules); +} else { + // No rules present. Show a link to manage rules page if permissions permit. + echo html_writer::tag('span', get_string('norules', 'tool_monitor')); + if (has_capability('tool/monitor:managerules', $context)) { + $manageurl = new moodle_url("/admin/tool/monitor/managerules.php", array('courseid' => $courseid)); + $a = html_writer::link($manageurl, get_string('managerules', 'tool_monitor')); + echo html_writer::tag('span', get_string('manageruleslink', 'tool_monitor', $a)); + } +} +echo $OUTPUT->footer(); diff --git a/admin/tool/monitor/lang/en/tool_monitor.php b/admin/tool/monitor/lang/en/tool_monitor.php index f9ff73e4712..e7e510a731a 100644 --- a/admin/tool/monitor/lang/en/tool_monitor.php +++ b/admin/tool/monitor/lang/en/tool_monitor.php @@ -30,13 +30,18 @@ $string['allmodules'] = 'All modules'; $string['core'] = 'Core'; $string['customizefilters'] = 'Select the frequency of the events'; $string['customizemessage'] = 'Cutomize the notification message'; +$string['currentsubscriptions'] = 'Your current subscriptions'; $string['description'] = 'Description:'; $string['description_help'] = "Description is displayed to users when they want to subscribe to this rule. This helps them understand what the rule is about."; $string['defaultmessagetpl'] = 'Rule "{rulename}" has happened. You can find further details at {link}'; $string['eventnotfound'] = 'Event not found'; $string['errorincorrectevent'] = 'Please select an event related to the selected plugin'; +$string['freqdesc'] = '{$a->freq} times in {$a->mins} minutes'; $string['frequency'] = 'Frequency'; $string['invalidmodule'] = 'Invalid module'; +$string['norules'] = 'There are no rules you can subscribe to.'; +$string['manageruleslink'] = 'You can manage rules from {$a} page.'; +$string['moduleinstance'] = 'Module instance'; $string['manage'] = 'Manage'; $string['managesubscriptions'] = 'Manage subscriptions'; $string['managerules'] = 'Manage rules'; @@ -51,14 +56,13 @@ $string['messagetemplate_help'] = 'This is the content of the message that will $string['minutes'] = 'in minutes:'; $string['name'] = 'Name of the rule: '; $string['name_help'] = "Choose a name for the rule."; -$string['norules'] = 'There are no rules you can subscribe to.'; -$string['manageruleslink'] = 'You can manage rules from {$a} page.'; $string['pluginname'] = 'Event monitor'; $string['processevents'] = 'Process events'; $string['ruleareyousure'] = 'Are you sure you want to delete rule "{$a}"?'; $string['rulecopysuccess'] = 'Rule successfully copied'; $string['ruledeletesuccess'] = 'Rule successfully deleted'; $string['rulenopermissions'] = 'You do not have permissions to "{$a} a rule"'; +$string['rulescansubscribe'] = 'Rules you can subscribe to'; $string['selectcourse'] = 'Visit this report at course level to get a list of possible modules'; $string['selectevent'] = 'Select an event:'; $string['selectevent_help'] = "Select an event to monitor."; @@ -67,7 +71,11 @@ $string['selectfrequency_help'] = "Frequency defines the denisty of the event oc $string['selectminutes'] = 'in minutes:'; $string['selectplugin'] = 'Select the plugin type:'; $string['selectplugin_help'] = "Select a plugin that you are interested in monitoring."; +$string['subareyousure'] = 'Are you sure you want to delete this subscription for the rule "{$a}"?'; +$string['subcreatesuccess'] = "Subscription successfully created"; +$string['subdeletesuccess'] = "Subscription successfully removed"; $string['title'] = '{$a->coursename} : {$a->reportname}'; $string['tool/monitor:managerules'] = 'Manage event monitor rules'; $string['tool/monitor:subscribe'] = 'Subscribe to event monitor rules'; +$string['unsubscribe'] = 'Unsubscribe';