1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * Renderable class for manage rules page.
19 *
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
23 */
24
25namespace tool_monitor\output\managerules;
26
27defined('MOODLE_INTERNAL') || die;
28
29require_once($CFG->libdir . '/tablelib.php');
30
31/**
32 * Renderable class for manage rules page.
33 *
34 * @since      Moodle 2.8
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
38 */
39class renderable extends \table_sql implements \renderable {
40
41    /**
42     * @var int course id.
43     */
44    public $courseid;
45
46    /**
47     * @var \context_course|\context_system context of the page to be rendered.
48     */
49    protected $context;
50
51    /**
52     * @var bool Does the user have capability to manage rules at site context.
53     */
54    protected $hassystemcap;
55
56    /**
57     * Sets up the table_log parameters.
58     *
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.
63     */
64    public function __construct($uniqueid, \moodle_url $url, $courseid = 0, $perpage = 100) {
65        parent::__construct($uniqueid);
66
67        $this->set_attribute('id', 'toolmonitorrules_table');
68        $this->set_attribute('class', 'toolmonitor managerules generaltable generalbox');
69        $this->define_columns(array('name', 'description', 'course', 'plugin', 'eventname', 'filters', 'manage'));
70        $this->define_headers(array(
71                get_string('rulename', 'tool_monitor'),
72                get_string('description'),
73                get_string('course'),
74                get_string('area', 'tool_monitor'),
75                get_string('event', 'tool_monitor'),
76                get_string('frequency', 'tool_monitor'),
77                get_string('manage', 'tool_monitor'),
78            )
79        );
80        $this->courseid = $courseid;
81        $this->pagesize = $perpage;
82        $systemcontext = \context_system::instance();
83        $this->context = empty($courseid) ? $systemcontext : \context_course::instance($courseid);
84        $this->hassystemcap = has_capability('tool/monitor:managerules', $systemcontext);
85        $this->collapsible(false);
86        $this->sortable(false);
87        $this->pageable(true);
88        $this->is_downloadable(false);
89        $this->define_baseurl($url);
90    }
91
92    /**
93     * Generate content for name column.
94     *
95     * @param \tool_monitor\rule $rule rule object
96     * @return string html used to display the column field.
97     */
98    public function col_name(\tool_monitor\rule $rule) {
99        return $rule->get_name($this->context);
100    }
101
102    /**
103     * Generate content for description column.
104     *
105     * @param \tool_monitor\rule $rule rule object
106     * @return string html used to display the column field.
107     */
108    public function col_description(\tool_monitor\rule $rule) {
109        return $rule->get_description($this->context);
110    }
111
112    /**
113     * Generate content for course column.
114     *
115     * @param \tool_monitor\rule $rule rule object
116     * @return string html used to display the context column field.
117     */
118    public function col_course(\tool_monitor\rule $rule) {
119        $coursename = $rule->get_course_name($this->context);
120
121        $courseid = $rule->courseid;
122        if (empty($courseid)) {
123            return $coursename;
124        } else {
125            return \html_writer::link(new \moodle_url('/course/view.php', array('id' => $courseid)), $coursename);
126        }
127    }
128
129    /**
130     * Generate content for plugin column.
131     *
132     * @param \tool_monitor\rule $rule rule object
133     * @return string html used to display the column field.
134     */
135    public function col_plugin(\tool_monitor\rule $rule) {
136        return $rule->get_plugin_name();
137    }
138
139    /**
140     * Generate content for eventname column.
141     *
142     * @param \tool_monitor\rule $rule rule object
143     * @return string html used to display the column field.
144     */
145    public function col_eventname(\tool_monitor\rule $rule) {
146        return $rule->get_event_name();
147    }
148
149    /**
150     * Generate content for filters column.
151     *
152     * @param \tool_monitor\rule $rule rule object
153     * @return string html used to display the filters column field.
154     */
155    public function col_filters(\tool_monitor\rule $rule) {
156        return $rule->get_filters_description();
157    }
158
159    /**
160     * Generate content for manage column.
161     *
162     * @param \tool_monitor\rule $rule rule object
163     * @return string html used to display the manage column field.
164     */
165    public function col_manage(\tool_monitor\rule $rule) {
166        global $OUTPUT, $CFG;
167
168        $manage = '';
169
170        // Do not allow the user to edit the rule unless they have the system capability, or we are viewing the rules
171        // for a course, and not the site. Note - we don't need to check for the capability at a course level since
172        // the user is never shown this page otherwise.
173        if ($this->hassystemcap || ($rule->courseid != 0)) {
174            $editurl = new \moodle_url($CFG->wwwroot. '/admin/tool/monitor/edit.php', array('ruleid' => $rule->id,
175                    'courseid' => $rule->courseid, 'sesskey' => sesskey()));
176            $icon = $OUTPUT->render(new \pix_icon('t/edit', get_string('editrule', 'tool_monitor')));
177            $manage .= \html_writer::link($editurl, $icon, array('class' => 'action-icon'));
178        }
179
180        // The user should always be able to copy the rule if they are able to view the page.
181        $copyurl = new \moodle_url($CFG->wwwroot. '/admin/tool/monitor/managerules.php',
182                array('ruleid' => $rule->id, 'action' => 'copy', 'courseid' => $this->courseid, 'sesskey' => sesskey()));
183        $icon = $OUTPUT->render(new \pix_icon('t/copy', get_string('duplicaterule', 'tool_monitor')));
184        $manage .= \html_writer::link($copyurl, $icon, array('class' => 'action-icon'));
185
186        if ($this->hassystemcap || ($rule->courseid != 0)) {
187            $deleteurl = new \moodle_url($CFG->wwwroot. '/admin/tool/monitor/managerules.php', array('ruleid' => $rule->id,
188                    'action' => 'delete', 'courseid' => $rule->courseid, 'sesskey' => sesskey()));
189            $icon = $OUTPUT->render(new \pix_icon('t/delete', get_string('deleterule', 'tool_monitor')));
190            $manage .= \html_writer::link($deleteurl, $icon, array('class' => 'action-icon'));
191        }
192
193        return $manage;
194    }
195
196    /**
197     * Query the reader. Store results in the object for use by build_table.
198     *
199     * @param int $pagesize size of page for paginated displayed table.
200     * @param bool $useinitialsbar do you want to use the initials bar.
201     */
202    public function query_db($pagesize, $useinitialsbar = true) {
203
204        $total = \tool_monitor\rule_manager::count_rules_by_courseid($this->courseid);
205        $this->pagesize($pagesize, $total);
206        $rules = \tool_monitor\rule_manager::get_rules_by_courseid($this->courseid, $this->get_page_start(),
207                $this->get_page_size());
208        $this->rawdata = $rules;
209        // Set initial bars.
210        if ($useinitialsbar) {
211            $this->initialbars($total > $pagesize);
212        }
213    }
214}
215