MDL-32903: Remove unused variable $tempsettings from mod_assign
[moodle.git] / mod / assign / adminlib.php
CommitLineData
bbd0e548
DW
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 * This file contains the classes for the admin settings of the assign module.
19 *
20 * @package mod_assign
21 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/** Include adminlib.php */
28require_once($CFG->libdir . '/adminlib.php');
29
30/**
31 * Admin external page that displays a list of the installed submission plugins.
32 *
33 * @package mod_assign
34 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class assign_admin_page_manage_assign_plugins extends admin_externalpage {
38
39 /** @var string the name of plugin subtype */
40 private $subtype = '';
41
42 /**
43 * The constructor - calls parent constructor
44 *
45 * @param string $subtype
46 */
47 public function __construct($subtype) {
48 $this->subtype = $subtype;
49 parent::__construct('manage' . $subtype . 'plugins', get_string('manage' . $subtype . 'plugins', 'assign'),
50 new moodle_url('/mod/assign/adminmanageplugins.php', array('subtype'=>$subtype)));
51 }
52
53 /**
54 * Search plugins for the specified string
55 *
56 * @param string $query The string to search for
57 * @return array
58 */
59 public function search($query) {
60 if ($result = parent::search($query)) {
61 return $result;
62 }
63
64 $found = false;
65 $textlib = new textlib();
66
67 foreach (get_plugin_list($this->subtype) as $name => $notused) {
68 if (strpos($textlib::strtolower(get_string('pluginname', $this->subtype . '_' . $name)),
69 $query) !== false) {
70 $found = true;
71 break;
72 }
73 }
74 if ($found) {
75 $result = new stdClass();
76 $result->page = $this;
77 $result->settings = array();
78 return array($this->name => $result);
79 } else {
80 return array();
81 }
82 }
83}
84
85
86/**
87 * Class that handles the display and configuration of the list of submission plugins.
88 *
89 * @package mod_assign
90 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
91 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
92 */
93class assign_plugin_manager {
94
95 /** @var object the url of the manage submission plugin page */
96 private $pageurl;
97 /** @var string any error from the current action */
98 private $error = '';
99 /** @var string either submission or feedback */
100 private $subtype = '';
101
102 /**
103 * Constructor for this assignment plugin manager
104 * @param string $subtype - either assignsubmission or assignfeedback
105 */
106 public function __construct($subtype) {
107 $this->pageurl = new moodle_url('/mod/assign/adminmanageplugins.php', array('subtype'=>$subtype));
108 $this->subtype = $subtype;
109 }
110
111
112 /**
113 * Return a list of plugins sorted by the order defined in the admin interface
114 *
115 * @return array The list of plugins
116 */
117 public function get_sorted_plugins_list() {
118 $names = get_plugin_list($this->subtype);
119
120 $result = array();
121
122 foreach ($names as $name => $path) {
123 $idx = get_config($this->subtype . '_' . $name, 'sortorder');
124 if (!$idx) {
125 $idx = 0;
126 }
127 while (array_key_exists($idx, $result)) $idx +=1;
128 $result[$idx] = $name;
129 }
130 ksort($result);
131
132 return $result;
133 }
134
135
136 /**
137 * Util function for writing an action icon link
138 *
139 * @param string $action URL parameter to include in the link
140 * @param string $plugintype URL parameter to include in the link
141 * @param string $icon The key to the icon to use (e.g. 't/up')
142 * @param string $alt The string description of the link used as the title and alt text
143 * @return string The icon/link
144 */
145 private function format_icon_link($action, $plugintype, $icon, $alt) {
146 global $OUTPUT;
147
148 return $OUTPUT->action_icon(new moodle_url($this->pageurl,
149 array('action' => $action, 'plugin'=> $plugintype, 'sesskey' => sesskey())),
150 new pix_icon($icon, $alt, 'moodle', array('title' => $alt)),
151 null, array('title' => $alt)) . ' ';
152 }
153
154 /**
155 * Write the HTML for the submission plugins table.
156 *
157 * @return None
158 */
159 private function view_plugins_table() {
160 global $OUTPUT, $CFG;
161 /** Include tablelib.php */
162 require_once($CFG->libdir . '/tablelib.php');
163
164 // Set up the table.
165 $this->view_header();
166 $table = new flexible_table($this->subtype . 'pluginsadminttable');
167 $table->define_baseurl($this->pageurl);
168 $table->define_columns(array('pluginname', 'version', 'hideshow', 'order',
169 'delete', 'settings'));
170 $table->define_headers(array(get_string($this->subtype . 'pluginname', 'assign'),
171 get_string('version'), get_string('hideshow', 'assign'),
172 get_string('order'), get_string('delete'), get_string('settings')));
173 $table->set_attribute('id', $this->subtype . 'plugins');
174 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
175 $table->setup();
176
177
178 $plugins = $this->get_sorted_plugins_list();
179 $shortsubtype = substr($this->subtype, strlen('assign'));
180
181 foreach ($plugins as $idx => $plugin) {
182 $row = array();
183
184 $row[] = get_string('pluginname', $this->subtype . '_' . $plugin);
185 $row[] = get_config($this->subtype . '_' . $plugin, 'version');
186
187 $visible = !get_config($this->subtype . '_' . $plugin, 'disabled');
188
189 if ($visible) {
190 $row[] = $this->format_icon_link('hide', $plugin, 'i/hide', get_string('disable'));
191 } else {
192 $row[] = $this->format_icon_link('show', $plugin, 'i/show', get_string('enable'));
193 }
194
195 $movelinks = '';
196 if (!$idx == 0) {
197 $movelinks .= $this->format_icon_link('moveup', $plugin, 't/up', get_string('up'));
198 } else {
199 $movelinks .= $OUTPUT->spacer(array('width'=>15));
200 }
201 if ($idx != count($plugins) - 1) {
202 $movelinks .= $this->format_icon_link('movedown', $plugin, 't/down', get_string('down'));
203 }
204 $row[] = $movelinks;
205
206 if ($row[1] != '') {
207 $row[] = $this->format_icon_link('delete', $plugin, 't/delete', get_string('delete'));
208 } else {
209 $row[] = '&nbsp;';
210 }
211 if ($row[1] != '' && file_exists($CFG->dirroot . '/mod/assign/' . $shortsubtype . '/' . $plugin . '/settings.php')) {
212 $row[] = html_writer::link(new moodle_url('/admin/settings.php',
213 array('section' => $this->subtype . '_' . $plugin)), get_string('settings'));
214 } else {
215 $row[] = '&nbsp;';
216 }
217 $table->add_data($row);
218 }
219
220
221 $table->finish_output();
222 $this->view_footer();
223 }
224
225 /**
226 * Write the page header
227 *
228 * @return None
229 */
230 private function view_header() {
231 global $OUTPUT;
232 admin_externalpage_setup('manage' . $this->subtype . 'plugins');
233 // Print the page heading.
234 echo $OUTPUT->header();
235 echo $OUTPUT->heading(get_string('manage' . $this->subtype . 'plugins', 'assign'));
236 }
237
238 /**
239 * Write the page footer
240 *
241 * @return None
242 */
243 private function view_footer() {
244 global $OUTPUT;
245 echo $OUTPUT->footer();
246 }
247
248 /**
249 * Check this user has permission to edit the list of installed plugins
250 *
251 * @return None
252 */
253 private function check_permissions() {
254 // Check permissions.
255 require_login();
256 $systemcontext = context_system::instance();
257 require_capability('moodle/site:config', $systemcontext);
258 }
259
260 /**
261 * Delete the database and files associated with this plugin.
262 *
263 * @param string $plugin - The type of the plugin to delete
264 * @return string the name of the next page to display
265 */
266 public function delete_plugin($plugin) {
267 global $CFG, $DB, $OUTPUT;
268 $confirm = optional_param('confirm', null, PARAM_BOOL);
269
270 if ($confirm) {
271 // Delete any configuration records.
272 if (!unset_all_config_for_plugin('assignsubmission_' . $plugin)) {
273 $this->error = $OUTPUT->notification(get_string('errordeletingconfig', 'admin', $this->subtype . '_' . $plugin));
274 }
275
276
277 // Should be covered by the previous function - but just in case
278 unset_config('disabled', $this->subtype . '_' . $plugin);
279 unset_config('sortorder', $this->subtype . '_' . $plugin);
280
281 // delete the plugin specific config settings
282 $DB->delete_records('assign_plugin_config', array('plugin'=>$plugin, 'subtype'=>$this->subtype));
283
284 // Then the tables themselves
285 drop_plugin_tables($this->subtype . '_' . $plugin, $CFG->dirroot . '/mod/assign/' . $this->subtype . '/' .$plugin. '/db/install.xml', false);
286
287 // Remove event handlers and dequeue pending events
288 events_uninstall($this->subtype . '_' . $plugin);
289
290 // the page to display
291 return 'plugindeleted';
292 } else {
293 // the page to display
294 return 'confirmdelete';
295 }
296
297 }
298
299 /**
300 * Show the page that gives the details of the plugin that was just deleted
301 *
302 * @param string $plugin - The plugin that was just deleted
303 * @return None
304 */
305 private function view_plugin_deleted($plugin) {
306 global $OUTPUT;
307 $this->view_header();
308 echo $OUTPUT->heading(get_string('deletingplugin', 'assign', get_string('pluginname', $this->subtype . '_' . $plugin)));
309 echo $this->error;
310 echo $OUTPUT->notification(get_string('plugindeletefiles', 'moodle', array('name'=>get_string('pluginname', $this->subtype . '_' . $plugin), 'directory'=>('/mod/assign/' . $this->subtype . '/'.$plugin))));
311 echo $OUTPUT->continue_button($this->pageurl);
312 $this->view_footer();
313 }
314
315 /**
316 * Show the page that asks the user to confirm they want to delete a plugin
317 *
318 * @param string $plugin - The plugin that will be deleted
319 * @return None
320 */
321 private function view_confirm_delete($plugin) {
322 global $OUTPUT;
323 $this->view_header();
324 echo $OUTPUT->heading(get_string('deletepluginareyousure', 'assign', get_string('pluginname', $this->subtype . '_' . $plugin)));
325 echo $OUTPUT->confirm(get_string('deletepluginareyousuremessage', 'assign', get_string('pluginname', $this->subtype . '_' . $plugin)),
326 new moodle_url($this->pageurl, array('action' => 'delete', 'plugin'=>$plugin, 'confirm' => 1)),
327 $this->pageurl);
328 $this->view_footer();
329 }
330
331
332
333 /**
334 * Hide this plugin
335 *
336 * @param string $plugin - The plugin to hide
337 * @return string The next page to display
338 */
339 public function hide_plugin($plugin) {
340 set_config('disabled', 1, $this->subtype . '_' . $plugin);
341 return 'view';
342 }
343
344 /**
345 * Change the order of this plugin
346 *
347 * @param string $plugintomove - The plugin to move
348 * @param string $dir - up or down
349 * @return string The next page to display
350 */
351 public function move_plugin($plugintomove, $dir) {
352 // get a list of the current plugins
353 $plugins = $this->get_sorted_plugins_list();
354
355 $currentindex = 0;
356
357 // throw away the keys
358
359 $plugins = array_values($plugins);
360
361 // find this plugin in the list
362 foreach ($plugins as $key => $plugin) {
363 if ($plugin == $plugintomove) {
364 $currentindex = $key;
365 break;
366 }
367 }
368
369 // make the switch
370 if ($dir == 'up') {
371 if ($currentindex > 0) {
372 $tempplugin = $plugins[$currentindex - 1];
373 $plugins[$currentindex - 1] = $plugins[$currentindex];
374 $plugins[$currentindex] = $tempplugin;
375 }
376 } else if ($dir == 'down') {
377 if ($currentindex < (count($plugins) - 1)) {
378 $tempplugin = $plugins[$currentindex + 1];
379 $plugins[$currentindex + 1] = $plugins[$currentindex];
380 $plugins[$currentindex] = $tempplugin;
381 }
382 }
383
384 // save the new normal order
385 foreach ($plugins as $key => $plugin) {
386 set_config('sortorder', $key, $this->subtype . '_' . $plugin);
387 }
388 return 'view';
389 }
390
391
392 /**
393 * Show this plugin
394 *
395 * @param string $plugin - The plugin to show
396 * @return string The next page to display
397 */
398 public function show_plugin($plugin) {
399 set_config('disabled', 0, $this->subtype . '_' . $plugin);
400 return 'view';
401 }
402
403
404 /**
405 * This is the entry point for this controller class
406 *
407 * @param string $action - The action to perform
408 * @param string $plugin - Optional name of a plugin type to perform the action on
409 * @return None
410 */
411 public function execute($action, $plugin) {
412 if ($action == null) {
413 $action = 'view';
414 }
415
416 $this->check_permissions();
417
418 // process
419 if ($action == 'delete' && $plugin != null) {
420 $action = $this->delete_plugin($plugin);
421 } else if ($action == 'hide' && $plugin != null) {
422 $action = $this->hide_plugin($plugin);
423 } else if ($action == 'show' && $plugin != null) {
424 $action = $this->show_plugin($plugin);
425 } else if ($action == 'moveup' && $plugin != null) {
426 $action = $this->move_plugin($plugin, 'up');
427 } else if ($action == 'movedown' && $plugin != null) {
428 $action = $this->move_plugin($plugin, 'down');
429 }
430
431
432 // view
433 if ($action == 'confirmdelete' && $plugin != null) {
434 $this->view_confirm_delete($plugin);
435 } else if ($action == 'plugindeleted' && $plugin != null) {
436 $this->view_plugin_deleted($plugin);
437 } else if ($action == 'view') {
438 $this->view_plugins_table();
439 }
440 }
441
442 /**
443 * This function adds plugin pages to the navigation menu
444 *
445 * @static
446 * @param string $subtype - The type of plugin (submission or feedback)
447 * @param part_of_admin_tree $admin - The handle to the admin menu
448 * @param admin_settingpage $settings - The handle to current node in the navigation tree
449 * @param stdClass $module - The handle to the current module
450 * @return None
451 */
452 static function add_admin_assign_plugin_settings($subtype, part_of_admin_tree $admin, admin_settingpage $settings, stdClass $module) {
453 global $CFG;
454
455 $plugins = get_plugin_list_with_file($subtype, 'settings.php', false);
456 $pluginsbyname = array();
457 foreach ($plugins as $plugin => $plugindir) {
458 $pluginname = get_string('pluginname', $subtype . '_'.$plugin);
459 $pluginsbyname[$pluginname] = $plugin;
460 }
461 ksort($pluginsbyname);
462
bbd0e548
DW
463 foreach ($pluginsbyname as $pluginname => $plugin) {
464 $settings = new admin_settingpage($subtype . '_'.$plugin,
465 $pluginname, 'moodle/site:config', !$module->visible);
466 if ($admin->fulltree) {
467 $shortsubtype = substr($subtype, strlen('assign'));
468 include($CFG->dirroot . "/mod/assign/$shortsubtype/$plugin/settings.php");
469 }
470
471 $admin->add($subtype . 'plugins', $settings);
472 }
473
bbd0e548
DW
474 }
475}