2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This file contains the classes for the admin settings of the assign module.
21 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /** Include adminlib.php */
28 require_once($CFG->libdir . '/adminlib.php');
31 * Admin external page that displays a list of the installed submission plugins.
34 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class assign_admin_page_manage_assign_plugins extends admin_externalpage {
39 /** @var string the name of plugin subtype */
40 private $subtype = '';
43 * The constructor - calls parent constructor
45 * @param string $subtype
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)));
54 * Search plugins for the specified string
56 * @param string $query The string to search for
59 public function search($query) {
60 if ($result = parent::search($query)) {
65 $textlib = new textlib();
67 foreach (get_plugin_list($this->subtype) as $name => $notused) {
68 if (strpos($textlib::strtolower(get_string('pluginname', $this->subtype . '_' . $name)),
75 $result = new stdClass();
76 $result->page = $this;
77 $result->settings = array();
78 return array($this->name => $result);
87 * Class that handles the display and configuration of the list of submission plugins.
90 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
91 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
93 class assign_plugin_manager {
95 /** @var object the url of the manage submission plugin page */
97 /** @var string any error from the current action */
99 /** @var string either submission or feedback */
100 private $subtype = '';
103 * Constructor for this assignment plugin manager
104 * @param string $subtype - either assignsubmission or assignfeedback
106 public function __construct($subtype) {
107 $this->pageurl = new moodle_url('/mod/assign/adminmanageplugins.php', array('subtype'=>$subtype));
108 $this->subtype = $subtype;
113 * Return a list of plugins sorted by the order defined in the admin interface
115 * @return array The list of plugins
117 public function get_sorted_plugins_list() {
118 $names = get_plugin_list($this->subtype);
122 foreach ($names as $name => $path) {
123 $idx = get_config($this->subtype . '_' . $name, 'sortorder');
127 while (array_key_exists($idx, $result)) $idx +=1;
128 $result[$idx] = $name;
137 * Util function for writing an action icon link
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
145 private function format_icon_link($action, $plugintype, $icon, $alt) {
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)) . ' ';
155 * Write the HTML for the submission plugins table.
159 private function view_plugins_table() {
160 global $OUTPUT, $CFG;
161 /** Include tablelib.php */
162 require_once($CFG->libdir . '/tablelib.php');
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');
178 $plugins = $this->get_sorted_plugins_list();
179 $shortsubtype = substr($this->subtype, strlen('assign'));
181 foreach ($plugins as $idx => $plugin) {
184 $row[] = get_string('pluginname', $this->subtype . '_' . $plugin);
185 $row[] = get_config($this->subtype . '_' . $plugin, 'version');
187 $visible = !get_config($this->subtype . '_' . $plugin, 'disabled');
190 $row[] = $this->format_icon_link('hide', $plugin, 'i/hide', get_string('disable'));
192 $row[] = $this->format_icon_link('show', $plugin, 'i/show', get_string('enable'));
197 $movelinks .= $this->format_icon_link('moveup', $plugin, 't/up', get_string('up'));
199 $movelinks .= $OUTPUT->spacer(array('width'=>15));
201 if ($idx != count($plugins) - 1) {
202 $movelinks .= $this->format_icon_link('movedown', $plugin, 't/down', get_string('down'));
207 $row[] = $this->format_icon_link('delete', $plugin, 't/delete', get_string('delete'));
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'));
217 $table->add_data($row);
221 $table->finish_output();
222 $this->view_footer();
226 * Write the page header
230 private function view_header() {
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'));
239 * Write the page footer
243 private function view_footer() {
245 echo $OUTPUT->footer();
249 * Check this user has permission to edit the list of installed plugins
253 private function check_permissions() {
254 // Check permissions.
256 $systemcontext = context_system::instance();
257 require_capability('moodle/site:config', $systemcontext);
261 * Delete the database and files associated with this plugin.
263 * @param string $plugin - The type of the plugin to delete
264 * @return string the name of the next page to display
266 public function delete_plugin($plugin) {
267 global $CFG, $DB, $OUTPUT;
268 $confirm = optional_param('confirm', null, PARAM_BOOL);
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));
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);
281 // delete the plugin specific config settings
282 $DB->delete_records('assign_plugin_config', array('plugin'=>$plugin, 'subtype'=>$this->subtype));
284 // Then the tables themselves
285 drop_plugin_tables($this->subtype . '_' . $plugin, $CFG->dirroot . '/mod/assign/' . $this->subtype . '/' .$plugin. '/db/install.xml', false);
287 // Remove event handlers and dequeue pending events
288 events_uninstall($this->subtype . '_' . $plugin);
290 // the page to display
291 return 'plugindeleted';
293 // the page to display
294 return 'confirmdelete';
300 * Show the page that gives the details of the plugin that was just deleted
302 * @param string $plugin - The plugin that was just deleted
305 private function view_plugin_deleted($plugin) {
307 $this->view_header();
308 echo $OUTPUT->heading(get_string('deletingplugin', 'assign', get_string('pluginname', $this->subtype . '_' . $plugin)));
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();
316 * Show the page that asks the user to confirm they want to delete a plugin
318 * @param string $plugin - The plugin that will be deleted
321 private function view_confirm_delete($plugin) {
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)),
328 $this->view_footer();
336 * @param string $plugin - The plugin to hide
337 * @return string The next page to display
339 public function hide_plugin($plugin) {
340 set_config('disabled', 1, $this->subtype . '_' . $plugin);
345 * Change the order of this plugin
347 * @param string $plugintomove - The plugin to move
348 * @param string $dir - up or down
349 * @return string The next page to display
351 public function move_plugin($plugintomove, $dir) {
352 // get a list of the current plugins
353 $plugins = $this->get_sorted_plugins_list();
357 // throw away the keys
359 $plugins = array_values($plugins);
361 // find this plugin in the list
362 foreach ($plugins as $key => $plugin) {
363 if ($plugin == $plugintomove) {
364 $currentindex = $key;
371 if ($currentindex > 0) {
372 $tempplugin = $plugins[$currentindex - 1];
373 $plugins[$currentindex - 1] = $plugins[$currentindex];
374 $plugins[$currentindex] = $tempplugin;
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;
384 // save the new normal order
385 foreach ($plugins as $key => $plugin) {
386 set_config('sortorder', $key, $this->subtype . '_' . $plugin);
395 * @param string $plugin - The plugin to show
396 * @return string The next page to display
398 public function show_plugin($plugin) {
399 set_config('disabled', 0, $this->subtype . '_' . $plugin);
405 * This is the entry point for this controller class
407 * @param string $action - The action to perform
408 * @param string $plugin - Optional name of a plugin type to perform the action on
411 public function execute($action, $plugin) {
412 if ($action == null) {
416 $this->check_permissions();
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');
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();
443 * This function adds plugin pages to the navigation menu
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
452 static function add_admin_assign_plugin_settings($subtype, part_of_admin_tree $admin, admin_settingpage $settings, stdClass $module) {
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;
461 ksort($pluginsbyname);
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");
471 $admin->add($subtype . 'plugins', $settings);