| 1441 |
ariadna |
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 |
namespace core_sms\table;
|
|
|
18 |
|
|
|
19 |
use context_system;
|
|
|
20 |
use core_table\dynamic as dynamic_table;
|
|
|
21 |
use flexible_table;
|
|
|
22 |
use moodle_url;
|
|
|
23 |
use stdClass;
|
|
|
24 |
|
|
|
25 |
defined('MOODLE_INTERNAL') || die();
|
|
|
26 |
require_once("{$CFG->libdir}/tablelib.php");
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* List sms gateway instances in a table.
|
|
|
30 |
*
|
|
|
31 |
* @package core_sms
|
|
|
32 |
* @copyright 2024 Safat Shahin <safat.shahin@moodle.com>
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
|
|
35 |
class sms_gateway_table extends flexible_table implements dynamic_table {
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* @var array $smsgateways List of gateway instances from the db.
|
|
|
39 |
*/
|
|
|
40 |
protected array $smsgateways = [];
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Constructor for the sms gateway table.
|
|
|
44 |
*/
|
|
|
45 |
public function __construct() {
|
|
|
46 |
parent::__construct($this->get_table_id());
|
|
|
47 |
|
|
|
48 |
$this->smsgateways = $this->get_sms_gateways();
|
|
|
49 |
$this->setup_column_configuration();
|
|
|
50 |
$this->set_filterset(new sms_gateway_table_filterset());
|
|
|
51 |
$this->setup();
|
|
|
52 |
$tableclasses = $this->attributes['class'] . ' ' . $this->get_table_id();
|
|
|
53 |
$this->set_attribute('class', $tableclasses);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
#[\Override]
|
|
|
57 |
public function get_context(): context_system {
|
|
|
58 |
return context_system::instance();
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Get the js module needed for the table.
|
|
|
63 |
*
|
|
|
64 |
* This module can include table specific ajax calls etc.
|
|
|
65 |
*
|
|
|
66 |
* @return string
|
|
|
67 |
*/
|
|
|
68 |
protected function get_table_js_module(): string {
|
|
|
69 |
return 'core_admin/plugin_management_table';
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
#[\Override]
|
|
|
73 |
protected function get_dynamic_table_html_end(): string {
|
|
|
74 |
global $PAGE;
|
|
|
75 |
|
|
|
76 |
$PAGE->requires->js_call_amd($this->get_table_js_module(), 'init');
|
|
|
77 |
return parent::get_dynamic_table_html_end();
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Setup the column configs for the table.
|
|
|
82 |
*/
|
|
|
83 |
protected function setup_column_configuration(): void {
|
|
|
84 |
$columnlist = $this->get_column_list();
|
|
|
85 |
$this->define_columns(array_keys($columnlist));
|
|
|
86 |
$this->define_headers(array_values($columnlist));
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Get the columns for the table.
|
|
|
91 |
*
|
|
|
92 |
* @return array
|
|
|
93 |
*/
|
|
|
94 |
protected function get_column_list(): array {
|
|
|
95 |
return [
|
|
|
96 |
'name' => get_string('name'),
|
|
|
97 |
'gateway' => get_string('gateway', 'sms'),
|
|
|
98 |
'enabled' => get_string('status'),
|
|
|
99 |
'actions' => get_string('actions', 'sms'),
|
|
|
100 |
];
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Get the table id for the table.
|
|
|
105 |
*
|
|
|
106 |
* @return string
|
|
|
107 |
*/
|
|
|
108 |
protected function get_table_id(): string {
|
|
|
109 |
return 'sms_gateways_table';
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
#[\Override]
|
|
|
113 |
public function guess_base_url(): void {
|
|
|
114 |
$this->define_baseurl(new moodle_url('/sms/sms_gateways.php'));
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Get the content of the table.
|
|
|
119 |
*
|
|
|
120 |
* @return string
|
|
|
121 |
*/
|
|
|
122 |
public function get_content(): string {
|
|
|
123 |
ob_start();
|
|
|
124 |
$this->out();
|
|
|
125 |
$content = ob_get_contents();
|
|
|
126 |
ob_end_clean();
|
|
|
127 |
return $content;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Get the sms gateways from the manager.
|
|
|
132 |
*
|
|
|
133 |
* @return array
|
|
|
134 |
*/
|
|
|
135 |
protected function get_sms_gateways(): array {
|
|
|
136 |
$gateways = \core\di::get(\core_sms\manager::class)->get_gateway_records();
|
|
|
137 |
if (!empty($gateways)) {
|
|
|
138 |
\core_collator::asort_objects_by_property($gateways, 'id');
|
|
|
139 |
}
|
|
|
140 |
return $gateways;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* Add the row data for the table.
|
|
|
145 |
*/
|
|
|
146 |
public function out(): void {
|
|
|
147 |
foreach ($this->smsgateways as $gateway) {
|
|
|
148 |
$rowdata = (object) [
|
|
|
149 |
'id' => $gateway->id,
|
|
|
150 |
'name' => $gateway->name,
|
|
|
151 |
'gateway' => $gateway->gateway,
|
|
|
152 |
'enabled' => (int)$gateway->enabled,
|
|
|
153 |
];
|
|
|
154 |
$this->add_data_keyed(
|
|
|
155 |
$this->format_row($rowdata),
|
|
|
156 |
$this->get_row_class($rowdata)
|
|
|
157 |
);
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
$this->finish_output(false);
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
/**
|
|
|
164 |
* Get the class for row whether is dimmed or not according to enabled or disabled.
|
|
|
165 |
*
|
|
|
166 |
* @param stdClass $row The row object
|
|
|
167 |
* @return string
|
|
|
168 |
*/
|
|
|
169 |
protected function get_row_class(stdClass $row): string {
|
|
|
170 |
if ($row->enabled) {
|
|
|
171 |
return '';
|
|
|
172 |
}
|
|
|
173 |
return 'dimmed_text';
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* The column for the gateway instance Name.
|
|
|
178 |
*
|
|
|
179 |
* @param stdClass $row The row object
|
|
|
180 |
* @return string
|
|
|
181 |
*/
|
|
|
182 |
public function col_name(stdClass $row): string {
|
|
|
183 |
return $row->name;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
/**
|
|
|
187 |
* The column for the Gateway plugin name.
|
|
|
188 |
*
|
|
|
189 |
* @param stdClass $row The row object
|
|
|
190 |
* @return string
|
|
|
191 |
*/
|
|
|
192 |
public function col_gateway(stdClass $row): string {
|
|
|
193 |
return $this->get_gateway_name($row->gateway);
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
/**
|
|
|
197 |
* Get the gateway name according to the gateway class from db.
|
|
|
198 |
*
|
|
|
199 |
* @param string $gateway The name of the gateway
|
|
|
200 |
* @return string
|
|
|
201 |
*/
|
|
|
202 |
public function get_gateway_name(string $gateway): string {
|
|
|
203 |
$values = explode('\\', $gateway);
|
|
|
204 |
$gateway = $values[0];
|
|
|
205 |
return get_string('pluginname', $gateway);
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
/**
|
|
|
209 |
* Webservice for toggle.
|
|
|
210 |
*
|
|
|
211 |
* @return string
|
|
|
212 |
*/
|
|
|
213 |
protected function get_toggle_service(): string {
|
|
|
214 |
return 'core_sms_set_gateway_status';
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
/**
|
|
|
218 |
* The column for enabled or disabled status of the gateway instance.
|
|
|
219 |
*
|
|
|
220 |
* @param stdClass $row The row object
|
|
|
221 |
* @return string
|
|
|
222 |
*/
|
|
|
223 |
public function col_enabled(stdClass $row): string {
|
|
|
224 |
global $OUTPUT;
|
|
|
225 |
|
|
|
226 |
$enabled = $row->enabled;
|
|
|
227 |
if ($enabled) {
|
|
|
228 |
$labelstr = get_string('disableplugin', 'core_admin', $row->name);
|
|
|
229 |
} else {
|
|
|
230 |
$labelstr = get_string('enableplugin', 'core_admin', $row->name);
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
$params = [
|
|
|
234 |
'id' => 'sms-gateway-toggle-' . $row->id,
|
|
|
235 |
'checked' => $enabled,
|
|
|
236 |
'dataattributes' => [
|
|
|
237 |
'name' => 'id',
|
|
|
238 |
'value' => $this->get_gateway_name($row->gateway),
|
|
|
239 |
'toggle-method' => $this->get_toggle_service(),
|
|
|
240 |
'action' => 'togglestate',
|
|
|
241 |
'plugin' => $row->id, // Set plugin attribute to gateway ID.
|
|
|
242 |
'state' => $enabled ? 1 : 0,
|
|
|
243 |
],
|
|
|
244 |
'title' => $labelstr,
|
|
|
245 |
'label' => $labelstr,
|
|
|
246 |
'labelclasses' => 'visually-hidden',
|
|
|
247 |
];
|
|
|
248 |
|
|
|
249 |
return $OUTPUT->render_from_template('core_admin/setting_configtoggle', $params);
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
/**
|
|
|
253 |
* The column to show the actions of the gateway instance.
|
|
|
254 |
*
|
|
|
255 |
* @param stdClass $row The row object.
|
|
|
256 |
* @return string
|
|
|
257 |
*/
|
|
|
258 |
public function col_actions(stdClass $row): string {
|
|
|
259 |
global $OUTPUT;
|
|
|
260 |
|
|
|
261 |
$editurl = new moodle_url('/sms/configure.php', ['id' => $row->id]);
|
|
|
262 |
$deleteurl = new moodle_url('/sms/sms_gateways.php', ['id' => $row->id, 'action' => 'delete']);
|
|
|
263 |
|
|
|
264 |
$templatecontext = [
|
|
|
265 |
'editurl' => $editurl->out(false),
|
|
|
266 |
'deleteurl' => $deleteurl->out(false),
|
|
|
267 |
];
|
|
|
268 |
|
|
|
269 |
return $OUTPUT->render_from_template('core_sms/sms_action_icons', $templatecontext);
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
#[\Override]
|
|
|
273 |
public function has_capability(): bool {
|
|
|
274 |
return has_capability('moodle/site:config', $this->get_context());
|
|
|
275 |
}
|
|
|
276 |
}
|