mirror of
https://github.com/moodle/moodle.git
synced 2025-08-08 02:16:41 +02:00
MDL-45758 tool_monitor: Update subscriptions management UI
This commit is contained in:
parent
067e9599ac
commit
ffe9ed541c
8 changed files with 171 additions and 31 deletions
|
@ -56,10 +56,25 @@ class renderer extends \plugin_renderer_base {
|
|||
* @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.
|
||||
*
|
||||
|
|
|
@ -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.
|
||||
*/
|
||||
|
@ -84,6 +89,8 @@ class rules extends \table_sql implements \renderable {
|
|||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -143,4 +150,22 @@ class rules extends \table_sql implements \renderable {
|
|||
$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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -67,11 +67,13 @@ class subs extends \table_sql implements \renderable {
|
|||
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;
|
||||
|
@ -101,6 +103,17 @@ class subs extends \table_sql implements \renderable {
|
|||
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.
|
||||
*
|
||||
|
@ -132,6 +145,23 @@ class subs extends \table_sql implements \renderable {
|
|||
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.
|
||||
*
|
||||
|
@ -140,10 +170,9 @@ class subs extends \table_sql implements \renderable {
|
|||
*/
|
||||
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) {
|
||||
|
|
|
@ -105,7 +105,7 @@ class rule_form extends \moodleform {
|
|||
$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);
|
||||
|
@ -118,7 +118,7 @@ class rule_form extends \moodleform {
|
|||
$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');
|
||||
|
|
|
@ -157,4 +157,36 @@ class subscription {
|
|||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -170,7 +170,7 @@ class subscription_manager {
|
|||
* @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;
|
||||
|
@ -183,6 +183,46 @@ class subscription_manager {
|
|||
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.
|
||||
*
|
||||
|
|
|
@ -34,22 +34,24 @@ $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 {
|
||||
// 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));
|
||||
|
@ -59,11 +61,6 @@ $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.
|
||||
|
@ -84,7 +81,7 @@ if (!empty($action)) {
|
|||
}
|
||||
|
||||
// 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.
|
||||
|
@ -96,11 +93,11 @@ if (!empty($totalsubs)) {
|
|||
// 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));
|
||||
|
@ -109,5 +106,6 @@ if (!empty($totalrules)) {
|
|||
$link .= html_writer::tag('span', get_string('manageruleslink', 'tool_monitor', $a));
|
||||
echo $link;
|
||||
}
|
||||
echo html_writer::end_div();
|
||||
}
|
||||
echo $OUTPUT->footer();
|
||||
|
|
|
@ -28,8 +28,8 @@ $string['addrule'] = 'Add a new rule';
|
|||
$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}';
|
||||
|
@ -70,14 +70,15 @@ $string['rulehelp'] = 'Rule details';
|
|||
$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";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue