2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Renderable class for manage rules page.
20 * @package tool_monitor
21 * @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace tool_monitor\output\managerules;
27 defined('MOODLE_INTERNAL') || die;
29 require_once($CFG->libdir . '/tablelib.php');
32 * Renderable class for manage rules page.
35 * @package tool_monitor
36 * @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class renderable extends \table_sql implements \renderable {
47 * @var \context_course|\context_system context of the page to be rendered.
52 * @var bool Does the user have capability to manage rules at site context.
54 protected $hassystemcap;
57 * Sets up the table_log parameters.
59 * @param string $uniqueid unique id of form.
60 * @param \moodle_url $url url where this table is displayed.
61 * @param int $courseid course id.
62 * @param int $perpage Number of rules to display per page.
64 public function __construct($uniqueid, \moodle_url $url, $courseid = 0, $perpage = 100) {
65 parent::__construct($uniqueid);
67 $this->set_attribute('class', 'toolmonitor managerules generaltable generalbox');
68 $this->define_columns(array('name', 'description', 'plugin', 'eventname', 'filters', 'manage'));
69 $this->define_headers(array(
71 get_string('description'),
73 get_string('eventname'),
74 get_string('frequency', 'tool_monitor'),
75 get_string('manage', 'tool_monitor'),
78 $this->courseid = $courseid;
79 $this->pagesize = $perpage;
80 $systemcontext = \context_system::instance();
81 $this->context = empty($courseid) ? $systemcontext : \context_course::instance($courseid);
82 $this->hassystemcap = has_capability('tool/monitor:managerules', $systemcontext);
83 $this->collapsible(false);
84 $this->sortable(false);
85 $this->pageable(true);
86 $this->is_downloadable(false);
87 $this->define_baseurl($url);
91 * Generate content for name column.
93 * @param \tool_monitor\rule $rule rule object
95 * @return string html used to display the column field.
97 public function col_name(\tool_monitor\rule $rule) {
98 return $rule->get_name($this->context);
102 * Generate content for description column.
104 * @param \tool_monitor\rule $rule rule object
106 * @return string html used to display the column field.
108 public function col_description(\tool_monitor\rule $rule) {
109 return $rule->get_description($this->context);
113 * Generate content for plugin column.
115 * @param \tool_monitor\rule $rule rule object
117 * @return string html used to display the column field.
119 public function col_plugin(\tool_monitor\rule $rule) {
120 return $rule->get_plugin_name();
124 * Generate content for eventname column.
126 * @param \tool_monitor\rule $rule rule object
128 * @return string html used to display the column field.
130 public function col_eventname(\tool_monitor\rule $rule) {
131 return $rule->get_event_name();
135 * Generate content for filters column.
137 * @param \tool_monitor\rule $rule rule object
139 * @return string html used to display the filters column field.
141 public function col_filters(\tool_monitor\rule $rule) {
142 return $rule->get_filters_description();
146 * Generate content for manage column.
148 * @param \tool_monitor\rule $rule rule object
150 * @return string html used to display the manage column field.
152 public function col_manage(\tool_monitor\rule $rule) {
153 global $OUTPUT, $CFG;
155 // We don't need to check for capability at course level since, user is never shown this page,
156 // if he doesn't have the capability.
157 if ($this->hassystemcap || ($rule->courseid !== 0)) {
158 // There might be site rules which the user can not manage.
159 $editurl = new \moodle_url($CFG->wwwroot. '/admin/tool/monitor/edit.php', array('ruleid' => $rule->id,
160 'courseid' => $rule->courseid));
161 $copyurl = new \moodle_url($CFG->wwwroot. '/admin/tool/monitor/managerules.php',
162 array('ruleid' => $rule->id, 'action' => 'copy', 'courseid' => $this->courseid));
163 $deleteurl = new \moodle_url($CFG->wwwroot. '/admin/tool/monitor/managerules.php', array('ruleid' => $rule->id,
164 'action' => 'delete', 'courseid' => $rule->courseid));
166 $icon = $OUTPUT->render(new \pix_icon('t/edit', get_string('editrule', 'tool_monitor')));
167 $manage .= \html_writer::link($editurl, $icon, array('class' => 'action-icon'));
169 $icon = $OUTPUT->render(new \pix_icon('t/copy', get_string('duplicaterule', 'tool_monitor')));
170 $manage .= \html_writer::link($copyurl, $icon, array('class' => 'action-icon'));
172 $a = $rule->get_name($this->context);
173 $action = new \component_action('click', 'M.util.show_confirm_dialog', array('message' => get_string('ruleareyousure',
174 'tool_monitor', $a)));
175 $icon = $OUTPUT->action_link($deleteurl, new \pix_icon('t/delete', get_string('deleterule', 'tool_monitor')), $action);
184 * Query the reader. Store results in the object for use by build_table.
186 * @param int $pagesize size of page for paginated displayed table.
187 * @param bool $useinitialsbar do you want to use the initials bar.
189 public function query_db($pagesize, $useinitialsbar = true) {
191 $total = \tool_monitor\rule_manager::count_rules_by_courseid($this->courseid);
192 $this->pagesize($pagesize, $total);
193 $rules = \tool_monitor\rule_manager::get_rules_by_courseid($this->courseid, $this->get_page_start(),
194 $this->get_page_size());
195 foreach ($rules as $rule) {
196 $this->rawdata[] = \tool_monitor\rule_manager::get_rule($rule);
199 if ($useinitialsbar) {
200 $this->initialbars($total > $pagesize);