Skip to content

Commit

Permalink
MDL-45758 tool_monitor: Update subscriptions management UI
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitagarwal committed Oct 15, 2014
1 parent 067e959 commit ffe9ed5
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 31 deletions.
17 changes: 16 additions & 1 deletion admin/tool/monitor/classes/output/managesubs/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,25 @@ protected function render_subs(subs $renderable) {
* @return string to display on the mangesubs page.
*/
protected function render_rules(rules $renderable) {
$o = $this->render_table($renderable);
$o = $this->render_course_select($renderable);
if (!empty($renderable->totalcount)) {
$o .= $this->render_table($renderable);
}
return $o;
}

/**
* Get html to display on the page for select dropdown..
*
* @param rules $renderable renderable widget
*
* @return string to display on the mangesubs page.
*/
protected function render_course_select(rules $renderable) {
$select = $renderable->get_user_courses_select();
return $this->render($select);;
}

/**
* Get html to display on the page.
*
Expand Down
25 changes: 25 additions & 0 deletions admin/tool/monitor/classes/output/managesubs/rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class rules extends \table_sql implements \renderable {
*/
public $courseid;

/**
* @var int total rules present.
*/
public $totalcount = 0;

/**
* @var \context_course|\context_system context of the page to be rendered.
*/
Expand Down Expand Up @@ -84,6 +89,8 @@ public function __construct($uniqueid, \moodle_url $url, $courseid = 0, $perpage
$this->is_downloadable(false);
$this->define_baseurl($url);
$this->helpiconrenderer = $PAGE->get_renderer('tool_monitor', 'helpicon');
$total = \tool_monitor\rule_manager::count_rules_by_courseid($this->courseid);
$this->totalcount = $total;
}

/**
Expand Down Expand Up @@ -143,4 +150,22 @@ public function query_db($pagesize, $useinitialsbar = true) {
$this->initialbars($total > $pagesize);
}
}

/**
* Gets a list of courses where the current user can subscribe to rules as a dropdown.
*
* @return \single_select list of courses.
*/
public function get_user_courses_select() {
$courses = get_user_capability_course('tool/monitor:subscribe', null, true, 'fullname');
$options = array(0 => get_string('site'));
$systemcontext = \context_system::instance();
foreach ($courses as $course) {
$options[$course->id] = format_text($course->fullname, array('context' => $systemcontext));
}
$url = new \moodle_url('/admin/tool/monitor/index.php');
$select = new \single_select($url, 'courseid', $options, $this->courseid);
$select->set_label(get_string('selectacourse', 'tool_monitor'));
return $select;
}
}
39 changes: 34 additions & 5 deletions admin/tool/monitor/classes/output/managesubs/subs.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ public function __construct($uniqueid, \moodle_url $url, $courseid = 0, $perpage
parent::__construct($uniqueid);

$this->set_attribute('class', 'toolmonitor subscriptions generaltable generalbox');
$this->define_columns(array('name', 'instance', 'unsubscribe'));
$this->define_columns(array('name', 'course', 'instance', 'unsubscribe', 'editrule'));
$this->define_headers(array(
get_string('name'),
get_string('course'),
get_string('moduleinstance', 'tool_monitor'),
get_string('unsubscribe', 'tool_monitor')
get_string('unsubscribe', 'tool_monitor'),
get_string('editrule', 'tool_monitor')
)
);
$this->courseid = $courseid;
Expand Down Expand Up @@ -101,6 +103,17 @@ public function col_name(\tool_monitor\subscription $sub) {
return $name . $helpicon;
}

/**
* Generate content for course column.
*
* @param \tool_monitor\subscription $sub subscription object
*
* @return string html used to display the column field.
*/
public function col_course(\tool_monitor\subscription $sub) {
return $sub->get_course_name($this->context);
}

/**
* Generate content for description column.
*
Expand Down Expand Up @@ -132,6 +145,23 @@ public function col_unsubscribe(\tool_monitor\subscription $sub) {
return $icon;
}

/**
* Generate content for edit rule column.
*
* @param \tool_monitor\subscription $sub subscription object
*
* @return string html used to display the column field.
*/
public function col_editrule(\tool_monitor\subscription $sub) {
if ($sub->can_manage_rule()) {
// User can manage rule.
$editurl = new \moodle_url('/admin/tool/monitor/edit.php', array('ruleid' => $sub->ruleid,
'courseid' => $sub->rulecourseid));
return \html_writer::link($editurl, get_string('editrule', 'tool_monitor'));
}
return '-';
}

/**
* Query the reader. Store results in the object for use by build_table.
*
Expand All @@ -140,10 +170,9 @@ public function col_unsubscribe(\tool_monitor\subscription $sub) {
*/
public function query_db($pagesize, $useinitialsbar = true) {

$total = \tool_monitor\subscription_manager::count_user_subscriptions_for_course($this->courseid);
$total = \tool_monitor\subscription_manager::count_user_subscriptions();
$this->pagesize($pagesize, $total);
$subs = \tool_monitor\subscription_manager::get_user_subscriptions_for_course($this->courseid, $this->get_page_start(),
$this->get_page_size());
$subs = \tool_monitor\subscription_manager::get_user_subscriptions($this->get_page_start(), $this->get_page_size());
$this->rawdata = $subs;
// Set initial bars.
if ($useinitialsbar) {
Expand Down
4 changes: 2 additions & 2 deletions admin/tool/monitor/classes/rule_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function definition () {
$mform->addHelpButton('description', 'description', 'tool_monitor');

// Filters.
$mform->addElement('header', 'customizefilters', get_string('customizefilters', 'tool_monitor'));
$mform->addElement('header', 'customisefilters', get_string('customisefilters', 'tool_monitor'));
$freq = array(1 => 1, 5 => 5, 10 => 10, 20 => 20, 30 => 30, 40 => 40, 50 => 50, 60 => 60, 70 => 70, 80 => 80, 90 => 90,
100 => 100, 1000 => 1000);
$mform->addElement('select', 'frequency', get_string('selectfrequency', 'tool_monitor'), $freq);
Expand All @@ -118,7 +118,7 @@ public function definition () {
$mform->addRule('minutes', get_string('required'), 'required');

// Message template.
$mform->addElement('header', 'customizemessage', get_string('customizemessage', 'tool_monitor'));
$mform->addElement('header', 'customisemessage', get_string('customisemessage', 'tool_monitor'));
$mform->addElement('editor', 'template', get_string('messagetemplate', 'tool_monitor'), $editoroptions);
$mform->setDefault('template', get_string('defaultmessagetpl', 'tool_monitor'));
$mform->addRule('template', get_string('required'), 'required');
Expand Down
32 changes: 32 additions & 0 deletions admin/tool/monitor/classes/subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,36 @@ public function get_plugin_name() {
}
return $string;
}

/**
* Get properly formatted name of the course associated.
*
* @param \context $context context where this name would be displayed.
*
* @return string Formatted name of the rule.
*/
public function get_course_name(\context $context) {
global $SITE;
$courseid = $this->courseid;
if (empty($courseid)) {
$coursename = format_string($SITE->fullname, true, array('context' => $context));
} else {
$course = get_course($this->courseid);
$link = new \moodle_url('/course/view.php', array('id' => $course->id));
$coursename = format_string($course->fullname, true, array('context' => $context));
$coursename = \html_writer::link($link, $coursename);
}
return $coursename;
}

/**
* Can the current user manage the rule associate with this subscription?
*
* @return bool true if the current user can manage this rule, else false.
*/
public function can_manage_rule() {
$courseid = $this->rulecourseid;
$context = empty($courseid) ? \context_system::instance() : \context_course::instance($courseid);
return has_capability('tool/monitor:managerules', $context);
}
}
42 changes: 41 additions & 1 deletion admin/tool/monitor/classes/subscription_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public static function get_user_subscriptions_for_course($courseid, $limitfrom =
* @param int $courseid course id.
* @param int $userid Id of the user for which the subscription needs to be fetched. Defaults to $USER;
*
* @return array list of subscriptions
* @return int number of subscriptions
*/
public static function count_user_subscriptions_for_course($courseid, $userid = 0) {
global $DB, $USER;
Expand All @@ -183,6 +183,46 @@ public static function count_user_subscriptions_for_course($courseid, $userid =
return $DB->count_records_sql($sql, array('courseid' => $courseid, 'userid' => $userid));
}

/**
* Get an array of subscriptions for a given user.
*
* @param int $limitfrom Limit from which to fetch rules.
* @param int $limitto Limit to which rules need to be fetched.
* @param int $userid Id of the user for which the subscription needs to be fetched. Defaults to $USER;
* @param string $order Order to sort the subscriptions.
*
* @return array list of subscriptions
*/
public static function get_user_subscriptions($limitfrom = 0, $limitto = 0, $userid = 0,
$order = 's.timecreated DESC' ) {
global $DB, $USER;
if ($userid == 0) {
$userid = $USER->id;
}
$sql = self::get_subscription_join_rule_sql();
$sql .= "WHERE s.userid = :userid ORDER BY $order";

return self::get_instances($DB->get_records_sql($sql, array('userid' => $userid), $limitfrom, $limitto));
}

/**
* Get count of subscriptions for a given user.
*
* @param int $userid Id of the user for which the subscription needs to be fetched. Defaults to $USER;
*
* @return int number of subscriptions
*/
public static function count_user_subscriptions($userid = 0) {
global $DB, $USER;;
if ($userid == 0) {
$userid = $USER->id;
}
$sql = self::get_subscription_join_rule_sql(true);
$sql .= "WHERE s.userid = :userid";

return $DB->count_records_sql($sql, array('userid' => $userid));
}

/**
* Return a list of subscriptions for a given event.
*
Expand Down
34 changes: 16 additions & 18 deletions admin/tool/monitor/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,24 @@
// 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 {
// They might want to see rules for this course.
$course = get_course($courseid);
require_login($course);
$context = context_course::instance($course->id);
$coursename = format_string($course->fullname, true, array('context' => $context));
$coursecontext = context_course::instance($course->id);
// Check for caps.
require_capability('tool/monitor:subscribe', $coursecontext);
$coursename = format_string($course->fullname, true, array('context' => $coursecontext));
}

// Check for caps.
require_capability('tool/monitor:subscribe', $context);
// Always build the page in site context.
$context = context_system::instance();
$sitename = format_string($SITE->fullname, true, array('context' => $context));
$PAGE->set_context($context);

// Set up the page.
$a = new stdClass();
$a->coursename = $coursename;
$a->coursename = $sitename;
$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));
Expand All @@ -59,11 +61,6 @@
$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.
Expand All @@ -84,7 +81,7 @@
}

// Render the current subscriptions list.
$totalsubs = \tool_monitor\subscription_manager::count_user_subscriptions_for_course($courseid);
$totalsubs = \tool_monitor\subscription_manager::count_user_subscriptions();
$renderer = $PAGE->get_renderer('tool_monitor', 'managesubs');
if (!empty($totalsubs)) {
// Show the subscriptions section only if there are subscriptions.
Expand All @@ -96,11 +93,11 @@
// 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 {
$rules = new \tool_monitor\output\managesubs\rules('toolmonitorrules', $indexurl, $courseid);
echo $renderer->render($rules);
if (empty($totalrules)) {
// No rules present. Show a link to manage rules page if permissions permit.
echo html_writer::start_div();
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));
Expand All @@ -109,5 +106,6 @@
$link .= html_writer::tag('span', get_string('manageruleslink', 'tool_monitor', $a));
echo $link;
}
echo html_writer::end_div();
}
echo $OUTPUT->footer();
9 changes: 5 additions & 4 deletions admin/tool/monitor/lang/en/tool_monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
$string['allevents'] = 'All events';
$string['allmodules'] = 'All modules';
$string['core'] = 'Core';
$string['customizefilters'] = 'Select the frequency of the events';
$string['customizemessage'] = 'Cutomize the notification message';
$string['customisefilters'] = 'Select the frequency of the events';
$string['customisemessage'] = 'Customise the notification message';
$string['currentsubscriptions'] = 'Your current subscriptions';
$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}';
Expand Down Expand Up @@ -70,14 +70,15 @@
$string['rulehelp_help'] = 'This rule listens for when the event \'{$a->eventname}\' in \'{$a->eventcomponent}\' has been triggered {$a->frequency} time(s) in {$a->minutes} minute(s).';
$string['rulenopermissions'] = 'You do not have permissions to "{$a} a rule"';
$string['rulescansubscribe'] = 'Rules you can subscribe to';
$string['selectacourse'] = 'Select a course';
$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.";
$string['selectevent_help'] = "Select an event to monitor. Please note that certain event can happen only at certain given context. For example, a rule based on 'course created' event inside a course can never be triggered.";
$string['selectfrequency'] = 'Frequency of events:';
$string['selectfrequency_help'] = "Frequency defines the denisty of the event occurrence. Select criterias to define how frequently the event should happen to trigger the notification.";
$string['selectminutes'] = 'in minutes:';
$string['selectplugin'] = 'Select the plugin type:';
$string['selectplugin_help'] = "Select a plugin that you are interested in monitoring.";
$string['selectplugin_help'] = "Select a plugin that you are interested in monitoring. The event list below would be updated to display events from the selected plugin.";
$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";
Expand Down

0 comments on commit ffe9ed5

Please sign in to comment.