-
Notifications
You must be signed in to change notification settings - Fork 15
/
adminlib.php
392 lines (338 loc) · 12.5 KB
/
adminlib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the classes for the admin settings of the surveypro module.
*
* @package mod_surveypro
* @copyright 2013 onwards kordan <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/adminlib.php');
/**
* Admin external page that displays a list of the installed submission plugins.
*
* @package mod_surveypro
* @copyright 2013 onwards kordan <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_surveypro_admin_page_manage_surveypro_plugins extends admin_externalpage {
/**
* @var string Name of plugin subtype.
*/
private $subtype = '';
/**
* The constructor - calls parent constructor.
*
* @param string $subtype
*/
public function __construct($subtype) {
$this->subtype = $subtype;
$url = new \moodle_url('/mod/surveypro/adminmanageplugins.php', ['subtype' => $subtype]);
parent::__construct('manage'.$subtype.'plugins',
get_string('manage'.$subtype.'plugins', 'mod_surveypro'),
$url);
}
/**
* Search plugins for the specified string.
*
* @param string $query String to search for
* @return array
*/
public function search($query) {
if ($result = parent::search($query)) {
return $result;
}
$found = false;
foreach (core_component::get_plugin_list($this->subtype) as $name => $unused) {
if (strpos(strtolower(get_string('pluginname', $this->subtype.'_'.$name)), $query) !== false) {
$found = true;
break;
}
}
if ($found) {
$result = new \stdClass();
$result->page = $this;
$result->settings = [];
return [$this->name => $result];
} else {
return [];
}
}
}
/**
* Class that handles the display and configuration of the list of submission plugins.
*
* @package mod_surveypro
* @copyright 2013 onwards kordan <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_surveypro_plugin_manager {
/**
* @var object Url of the manage submission plugin page
*/
private $pageurl;
/**
* @var string Any error from the current action
*/
private $error = '';
/**
* @var string Either submission or feedback
*/
private $subtype = '';
/**
* Constructor for this surveypro plugin manager.
*
* @param string $subtype Either surveyprofield, surveyproformat, surveyprotemplate or surveyproreport
*/
public function __construct($subtype) {
$this->pageurl = new \moodle_url('/mod/surveypro/adminmanageplugins.php', ['subtype' => $subtype]);
$this->subtype = $subtype;
}
/**
* Return a list of plugins sorted by the order defined in the admin interface.
*
* @return array The list of plugins
*/
public function get_sorted_plugins_list() {
$names = core_component::get_plugin_list($this->subtype);
$result = [];
foreach ($names as $name => $unused) {
$idx = get_config($this->subtype.'_'.$name, 'sortorder');
if (!$idx) {
$idx = 0;
}
while (array_key_exists($idx, $result)) {
$idx += 1;
}
$result[$idx] = $name;
}
ksort($result);
return $result;
}
/**
* Util function for writing an action icon link
*
* @param string $action URL parameter to include in the link
* @param string $plugin URL parameter to include in the link
* @param string $icon The key to the icon to use (e.g. 't/up')
* @param string $alt The string description of the link used as the title and alt text
* @return string The icon/link
*/
private function format_icon_link($action, $plugin, $icon, $alt) {
global $OUTPUT;
$url = $this->pageurl;
if ($action === 'delete') {
$url = core_plugin_manager::instance()->get_uninstall_url($this->subtype.'_'.$plugin, 'manage');
if (!$url) {
return ' ';
}
return html_writer::link($url, get_string('uninstallplugin', 'core_admin'));
}
return $OUTPUT->action_icon(new moodle_url($url,
['action' => $action, 'plugin' => $plugin, 'sesskey' => sesskey()]),
new pix_icon($icon, $alt, 'moodle', ['title' => $alt]),
null, ['title' => $alt]) . ' ';
}
/**
* Write the HTML for the submission plugins table.
*
* @return void
*/
private function view_plugins_table() {
global $OUTPUT, $CFG, $DB;
require_once($CFG->libdir.'/tablelib.php');
require_once($CFG->dirroot.'/mod/surveypro/lib.php');
// Set up the table.
$this->view_header();
$table = new \flexible_table($this->subtype.'pluginsadminttable');
$table->define_baseurl($this->pageurl);
$tablecolumns = [];
$tablecolumns[] = 'pluginname';
$tablecolumns[] = 'version';
$tablecolumns[] = 'numinstances';
$tablecolumns[] = 'hideshow';
$tablecolumns[] = 'delete';
$tablecolumns[] = 'settings';
$table->define_columns($tablecolumns);
$tableheaders = [];
$tableheaders[] = get_string($this->subtype.'pluginname', 'mod_surveypro');
$tableheaders[] = get_string('version');
$tableheaders[] = get_string('numinstances', 'mod_surveypro');
$tableheaders[] = get_string('hideshow', 'mod_surveypro');
$tableheaders[] = get_string('delete');
$tableheaders[] = get_string('settings');
$table->define_headers($tableheaders);
$table->set_attribute('id', $this->subtype.'plugins');
$table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
$table->setup();
$plugins = $this->get_sorted_plugins_list();
$shortsubtype = core_text::substr($this->subtype, core_text::strlen('surveypro'));
if (($this->subtype == 'surveyprofield') || ($this->subtype == 'surveyproformat')) {
if ($this->subtype == 'surveyprofield') {
$type = SURVEYPRO_TYPEFIELD;
}
if ($this->subtype == 'surveyproformat') {
$type = SURVEYPRO_TYPEFORMAT;
}
$countsql = 'SELECT plugin, COUNT(1) as numinstances
FROM {surveypro_item}
WHERE type = :type
GROUP BY plugin';
$whereparams = ['type' => $type];
$counts = $DB->get_records_sql($countsql, $whereparams);
}
if ($this->subtype == 'surveyprotemplate') {
$countsql = 'SELECT template, COUNT(1) as numinstances
FROM {surveypro}
WHERE template IS NOT NULL
GROUP BY template';
$counts = $DB->get_records_sql($countsql);
}
foreach ($plugins as $plugin) {
$row = [];
$class = '';
// Pluginname.
$icon = $OUTPUT->pix_icon('icon', $plugin, $this->subtype.'_'.$plugin, ['title' => $plugin, 'class' => 'icon']);
$row[] = $icon.get_string('pluginname', $this->subtype.'_'.$plugin);
// Version.
$row[] = get_config($this->subtype.'_'.$plugin, 'version');
// Number of instances.
if (isset($counts[$plugin])) {
$row[] = $counts[$plugin]->numinstances;
} else {
$row[] = 0;
}
// Enable/disable.
$visible = !get_config($this->subtype.'_'.$plugin, 'disabled');
if ($visible) {
$row[] = $this->format_icon_link('hide', $plugin, 't/hide', get_string('disable'));
} else {
$row[] = $this->format_icon_link('show', $plugin, 't/show', get_string('enable'));
$class = 'dimmed_text';
}
// Delete.
if (isset($counts[$plugin])) {
$row[] = ' ';
} else {
$row[] = $this->format_icon_link('delete', $plugin, 't/delete', get_string('delete'));
}
$exists = file_exists($CFG->dirroot.'/mod/surveypro/'.$shortsubtype.'/'.$plugin.'/settings.php');
if ($row[1] != '' && $exists) {
$row[] = \html_writer::link(new \moodle_url('/admin/settings.php',
['section' => $this->subtype.'_'.$plugin]), get_string('settings'));
} else {
$row[] = ' ';
}
$table->add_data($row, $class);
}
$table->finish_output();
$this->view_footer();
}
/**
* Write the page header.
*
* @return void
*/
private function view_header() {
global $OUTPUT;
admin_externalpage_setup('manage'.$this->subtype.'plugins');
// Print the page heading.
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('manage'.$this->subtype.'plugins', 'mod_surveypro'));
}
/**
* Write the page footer.
*
* @return void
*/
private function view_footer() {
global $OUTPUT;
echo $OUTPUT->footer();
}
/**
* Check this user has permission to edit the list of installed plugins.
*
* @return void
*/
private function check_permissions() {
// Check permissions.
require_login();
$systemcontext = \context_system::instance();
require_capability('moodle/site:config', $systemcontext);
}
/**
* Show the page that gives the details of the plugin that was just deleted.
*
* @param string $plugin Plugin that was just deleted
* @return void
*/
private function view_plugin_deleted($plugin) {
global $OUTPUT;
$this->view_header();
$pluginname = get_string('pluginname', $this->subtype.'_'.$plugin);
echo $OUTPUT->heading(get_string('deletingplugin', 'mod_surveypro', $pluginname));
echo $this->error;
$shortsubtype = core_text::substr($this->subtype, core_text::strlen('surveypro'));
$messageparams = ['name' => $pluginname, 'directory' => ('/mod/surveypro/'.$shortsubtype.'/'.$plugin)];
echo $OUTPUT->notification(get_string('plugindeletefiles', 'moodle', $messageparams), 'notifymessage');
echo $OUTPUT->continue_button($this->pageurl);
$this->view_footer();
}
/**
* Hide this plugin.
*
* @param string $plugin Plugin to hide
* @return string The next page to display
*/
public function hide_plugin($plugin) {
set_config('disabled', 1, $this->subtype.'_'.$plugin);
return 'view';
}
/**
* Show this plugin.
*
* @param string $plugin Plugin to show
* @return string The next page to display
*/
public function show_plugin($plugin) {
set_config('disabled', 0, $this->subtype.'_'.$plugin);
return 'view';
}
/**
* This is the entry point for this controller class.
*
* @param string $action Action to perform
* @param string $plugin Optional name of a plugin type to perform the action on
* @return void
*/
public function execute($action, $plugin) {
if ($action == null) {
$action = 'view';
}
$this->check_permissions();
// Process.
if ($action == 'hide' && $plugin != null) {
$action = $this->hide_plugin($plugin);
} else if ($action == 'show' && $plugin != null) {
$action = $this->show_plugin($plugin);
}
// View.
if ($action == 'view') {
$this->view_plugins_table();
}
}
}