Merge branch 'MDL-59927-33-enfix' of git://github.com/mudrd8mz/moodle into MOODLE_33_...
[moodle.git] / mod / assign / adminlib.php
blob9461138e21e50c763e2b53c70030048ca9c6ae2f
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.
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/>.
17 /**
18 * This file contains the classes for the admin settings of the assign module.
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
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->libdir . '/adminlib.php');
29 /**
30 * Admin external page that displays a list of the installed submission plugins.
32 * @package mod_assign
33 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class assign_admin_page_manage_assign_plugins extends admin_externalpage {
38 /** @var string the name of plugin subtype */
39 private $subtype = '';
41 /**
42 * The constructor - calls parent constructor
44 * @param string $subtype
46 public function __construct($subtype) {
47 $this->subtype = $subtype;
48 $url = new moodle_url('/mod/assign/adminmanageplugins.php', array('subtype'=>$subtype));
49 parent::__construct('manage' . $subtype . 'plugins',
50 get_string('manage' . $subtype . 'plugins', 'assign'),
51 $url);
54 /**
55 * Search plugins for the specified string
57 * @param string $query The string to search for
58 * @return array
60 public function search($query) {
61 if ($result = parent::search($query)) {
62 return $result;
65 $found = false;
67 foreach (core_component::get_plugin_list($this->subtype) as $name => $notused) {
68 if (strpos(core_text::strtolower(get_string('pluginname', $this->subtype . '_' . $name)),
69 $query) !== false) {
70 $found = true;
71 break;
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();
86 /**
87 * Class that handles the display and configuration of the list of submission plugins.
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
93 class assign_plugin_manager {
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 = '';
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 = core_component::get_plugin_list($this->subtype);
120 $result = array();
122 foreach ($names as $name => $path) {
123 $idx = get_config($this->subtype . '_' . $name, 'sortorder');
124 if (!$idx) {
125 $idx = 0;
127 while (array_key_exists($idx, $result)) {
128 $idx +=1;
130 $result[$idx] = $name;
132 ksort($result);
134 return $result;
139 * Util function for writing an action icon link
141 * @param string $action URL parameter to include in the link
142 * @param string $plugin URL parameter to include in the link
143 * @param string $icon The key to the icon to use (e.g. 't/up')
144 * @param string $alt The string description of the link used as the title and alt text
145 * @return string The icon/link
147 private function format_icon_link($action, $plugin, $icon, $alt) {
148 global $OUTPUT;
150 $url = $this->pageurl;
152 if ($action === 'delete') {
153 $url = core_plugin_manager::instance()->get_uninstall_url($this->subtype.'_'.$plugin, 'manage');
154 if (!$url) {
155 return '&nbsp;';
157 return html_writer::link($url, get_string('uninstallplugin', 'core_admin'));
160 return $OUTPUT->action_icon(new moodle_url($url,
161 array('action' => $action, 'plugin'=> $plugin, 'sesskey' => sesskey())),
162 new pix_icon($icon, $alt, 'moodle', array('title' => $alt)),
163 null, array('title' => $alt)) . ' ';
167 * Write the HTML for the submission plugins table.
169 * @return None
171 private function view_plugins_table() {
172 global $OUTPUT, $CFG;
173 require_once($CFG->libdir . '/tablelib.php');
175 // Set up the table.
176 $this->view_header();
177 $table = new flexible_table($this->subtype . 'pluginsadminttable');
178 $table->define_baseurl($this->pageurl);
179 $table->define_columns(array('pluginname', 'version', 'hideshow', 'order',
180 'settings', 'uninstall'));
181 $table->define_headers(array(get_string($this->subtype . 'pluginname', 'assign'),
182 get_string('version'), get_string('hideshow', 'assign'),
183 get_string('order'), get_string('settings'), get_string('uninstallplugin', 'core_admin')));
184 $table->set_attribute('id', $this->subtype . 'plugins');
185 $table->set_attribute('class', 'admintable generaltable');
186 $table->setup();
188 $plugins = $this->get_sorted_plugins_list();
189 $shortsubtype = substr($this->subtype, strlen('assign'));
191 foreach ($plugins as $idx => $plugin) {
192 $row = array();
193 $class = '';
195 $row[] = get_string('pluginname', $this->subtype . '_' . $plugin);
196 $row[] = get_config($this->subtype . '_' . $plugin, 'version');
198 $visible = !get_config($this->subtype . '_' . $plugin, 'disabled');
200 if ($visible) {
201 $row[] = $this->format_icon_link('hide', $plugin, 't/hide', get_string('disable'));
202 } else {
203 $row[] = $this->format_icon_link('show', $plugin, 't/show', get_string('enable'));
204 $class = 'dimmed_text';
207 $movelinks = '';
208 if (!$idx == 0) {
209 $movelinks .= $this->format_icon_link('moveup', $plugin, 't/up', get_string('up'));
210 } else {
211 $movelinks .= $OUTPUT->spacer(array('width'=>16));
213 if ($idx != count($plugins) - 1) {
214 $movelinks .= $this->format_icon_link('movedown', $plugin, 't/down', get_string('down'));
216 $row[] = $movelinks;
218 $exists = file_exists($CFG->dirroot . '/mod/assign/' . $shortsubtype . '/' . $plugin . '/settings.php');
219 if ($row[1] != '' && $exists) {
220 $row[] = html_writer::link(new moodle_url('/admin/settings.php',
221 array('section' => $this->subtype . '_' . $plugin)), get_string('settings'));
222 } else {
223 $row[] = '&nbsp;';
226 $row[] = $this->format_icon_link('delete', $plugin, 't/delete', get_string('uninstallplugin', 'core_admin'));
228 $table->add_data($row, $class);
231 $table->finish_output();
232 $this->view_footer();
236 * Write the page header
238 * @return None
240 private function view_header() {
241 global $OUTPUT;
242 admin_externalpage_setup('manage' . $this->subtype . 'plugins');
243 // Print the page heading.
244 echo $OUTPUT->header();
245 echo $OUTPUT->heading(get_string('manage' . $this->subtype . 'plugins', 'assign'));
249 * Write the page footer
251 * @return None
253 private function view_footer() {
254 global $OUTPUT;
255 echo $OUTPUT->footer();
259 * Check this user has permission to edit the list of installed plugins
261 * @return None
263 private function check_permissions() {
264 // Check permissions.
265 require_login();
266 $systemcontext = context_system::instance();
267 require_capability('moodle/site:config', $systemcontext);
271 * Hide this plugin.
273 * @param string $plugin - The plugin to hide
274 * @return string The next page to display
276 public function hide_plugin($plugin) {
277 set_config('disabled', 1, $this->subtype . '_' . $plugin);
278 core_plugin_manager::reset_caches();
279 return 'view';
283 * Change the order of this plugin.
285 * @param string $plugintomove - The plugin to move
286 * @param string $dir - up or down
287 * @return string The next page to display
289 public function move_plugin($plugintomove, $dir) {
290 // Get a list of the current plugins.
291 $plugins = $this->get_sorted_plugins_list();
293 $currentindex = 0;
295 // Throw away the keys.
296 $plugins = array_values($plugins);
298 // Find this plugin in the list.
299 foreach ($plugins as $key => $plugin) {
300 if ($plugin == $plugintomove) {
301 $currentindex = $key;
302 break;
306 // Make the switch.
307 if ($dir == 'up') {
308 if ($currentindex > 0) {
309 $tempplugin = $plugins[$currentindex - 1];
310 $plugins[$currentindex - 1] = $plugins[$currentindex];
311 $plugins[$currentindex] = $tempplugin;
313 } else if ($dir == 'down') {
314 if ($currentindex < (count($plugins) - 1)) {
315 $tempplugin = $plugins[$currentindex + 1];
316 $plugins[$currentindex + 1] = $plugins[$currentindex];
317 $plugins[$currentindex] = $tempplugin;
321 // Save the new normal order.
322 foreach ($plugins as $key => $plugin) {
323 set_config('sortorder', $key, $this->subtype . '_' . $plugin);
325 return 'view';
330 * Show this plugin.
332 * @param string $plugin - The plugin to show
333 * @return string The next page to display
335 public function show_plugin($plugin) {
336 set_config('disabled', 0, $this->subtype . '_' . $plugin);
337 core_plugin_manager::reset_caches();
338 return 'view';
343 * This is the entry point for this controller class.
345 * @param string $action - The action to perform
346 * @param string $plugin - Optional name of a plugin type to perform the action on
347 * @return None
349 public function execute($action, $plugin) {
350 if ($action == null) {
351 $action = 'view';
354 $this->check_permissions();
356 // Process.
357 if ($action == 'hide' && $plugin != null) {
358 $action = $this->hide_plugin($plugin);
359 } else if ($action == 'show' && $plugin != null) {
360 $action = $this->show_plugin($plugin);
361 } else if ($action == 'moveup' && $plugin != null) {
362 $action = $this->move_plugin($plugin, 'up');
363 } else if ($action == 'movedown' && $plugin != null) {
364 $action = $this->move_plugin($plugin, 'down');
367 // View.
368 if ($action == 'view') {
369 $this->view_plugins_table();