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 to display a set of subscriptions in the manage subscriptions 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\managesubs;
26
27defined('MOODLE_INTERNAL') || die;
28
29require_once($CFG->libdir . '/tablelib.php');
30
31/**
32 * Renderable class to display a set of subscriptions in the manage subscriptions 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 subs 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     * Sets up the table_log parameters.
53     *
54     * @param string $uniqueid unique id of form.
55     * @param \moodle_url $url url where this table is displayed.
56     * @param int $courseid course id.
57     * @param int $perpage Number of rules to display per page.
58     */
59    public function __construct($uniqueid, \moodle_url $url, $courseid = 0, $perpage = 100) {
60        parent::__construct($uniqueid);
61
62        $this->set_attribute('class', 'toolmonitor subscriptions generaltable generalbox');
63        $this->define_columns(array('name', 'description', 'course', 'plugin', 'instance', 'eventname',
64            'filters', 'unsubscribe'));
65        $this->define_headers(array(
66                get_string('rulename', 'tool_monitor'),
67                get_string('description'),
68                get_string('course'),
69                get_string('area', 'tool_monitor'),
70                get_string('moduleinstance', 'tool_monitor'),
71                get_string('event', 'tool_monitor'),
72                get_string('frequency', 'tool_monitor'),
73                get_string('unsubscribe', 'tool_monitor')
74            )
75        );
76        $this->courseid = $courseid;
77        $this->pagesize = $perpage;
78        $systemcontext = \context_system::instance();
79        $this->context = empty($courseid) ? $systemcontext : \context_course::instance($courseid);
80        $this->collapsible(false);
81        $this->sortable(false);
82        $this->pageable(true);
83        $this->is_downloadable(false);
84        $this->define_baseurl($url);
85    }
86
87    /**
88     * Generate content for name column.
89     *
90     * @param \tool_monitor\subscription $sub subscription object
91     * @return string html used to display the rule name.
92     */
93    public function col_name(\tool_monitor\subscription $sub) {
94        return $sub->get_name($this->context);
95    }
96
97    /**
98     * Generate content for description column.
99     *
100     * @param \tool_monitor\subscription $sub subscription object
101     * @return string html used to display the description.
102     */
103    public function col_description(\tool_monitor\subscription $sub) {
104        return $sub->get_description($this->context);
105    }
106
107    /**
108     * Generate content for course column.
109     *
110     * @param \tool_monitor\subscription $sub subscription object
111     * @return string html used to display the course name.
112     */
113    public function col_course(\tool_monitor\subscription $sub) {
114        $coursename = $sub->get_course_name($this->context);
115
116        $courseid = $sub->courseid;
117        if (empty($courseid)) {
118            return $coursename;
119        } else {
120            return \html_writer::link(new \moodle_url('/course/view.php', array('id' => $courseid)), $coursename);
121        }
122    }
123
124    /**
125     * Generate content for plugin column.
126     *
127     * @param \tool_monitor\subscription $sub subscription object
128     * @return string html used to display the plugin name.
129     */
130    public function col_plugin(\tool_monitor\subscription $sub) {
131        return $sub->get_plugin_name();
132    }
133
134    /**
135     * Generate content for instance column.
136     *
137     * @param \tool_monitor\subscription $sub subscription object
138     * @return string html used to display the instance name.
139     */
140    public function col_instance(\tool_monitor\subscription $sub) {
141        return $sub->get_instance_name();
142    }
143
144    /**
145     * Generate content for eventname column.
146     *
147     * @param \tool_monitor\subscription $sub subscription object
148     * @return string html used to display the event name.
149     */
150    public function col_eventname(\tool_monitor\subscription $sub) {
151        return $sub->get_event_name();
152    }
153
154    /**
155     * Generate content for filters column.
156     *
157     * @param \tool_monitor\subscription $sub subscription object
158     * @return string html used to display the filters.
159     */
160    public function col_filters(\tool_monitor\subscription $sub) {
161        return $sub->get_filters_description();
162    }
163
164    /**
165     * Generate content for unsubscribe column.
166     *
167     * @param \tool_monitor\subscription $sub subscription object
168     * @return string html used to display the unsubscribe field.
169     */
170    public function col_unsubscribe(\tool_monitor\subscription $sub) {
171        global $OUTPUT, $CFG;
172
173        $deleteurl = new \moodle_url($CFG->wwwroot. '/admin/tool/monitor/index.php', array('subscriptionid' => $sub->id,
174                'action' => 'unsubscribe', 'courseid' => $this->courseid, 'sesskey' => sesskey()));
175        $icon = $OUTPUT->render(new \pix_icon('t/delete', get_string('deletesubscription', 'tool_monitor')));
176
177        return \html_writer::link($deleteurl, $icon, array('class' => 'action-icon'));
178    }
179
180    /**
181     * Query the reader. Store results in the object for use by build_table.
182     *
183     * @param int $pagesize size of page for paginated displayed table.
184     * @param bool $useinitialsbar do you want to use the initials bar.
185     */
186    public function query_db($pagesize, $useinitialsbar = true) {
187
188        $total = \tool_monitor\subscription_manager::count_user_subscriptions();
189        $this->pagesize($pagesize, $total);
190        $subs = \tool_monitor\subscription_manager::get_user_subscriptions($this->get_page_start(), $this->get_page_size());
191        $this->rawdata = $subs;
192        // Set initial bars.
193        if ($useinitialsbar) {
194            $this->initialbars($total > $pagesize);
195        }
196    }
197}
198