PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/assign/adminlib.php

http://github.com/moodle/moodle
PHP | 372 lines | 196 code | 52 blank | 124 comment | 52 complexity | f4ffcf1ed2abaccc2b0743cb1042b1e0 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * This file contains the classes for the admin settings of the assign module.
  18. *
  19. * @package mod_assign
  20. * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. require_once($CFG->libdir . '/adminlib.php');
  25. /**
  26. * Admin external page that displays a list of the installed submission plugins.
  27. *
  28. * @package mod_assign
  29. * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class assign_admin_page_manage_assign_plugins extends admin_externalpage {
  33. /** @var string the name of plugin subtype */
  34. private $subtype = '';
  35. /**
  36. * The constructor - calls parent constructor
  37. *
  38. * @param string $subtype
  39. */
  40. public function __construct($subtype) {
  41. $this->subtype = $subtype;
  42. $url = new moodle_url('/mod/assign/adminmanageplugins.php', array('subtype'=>$subtype));
  43. parent::__construct('manage' . $subtype . 'plugins',
  44. get_string('manage' . $subtype . 'plugins', 'assign'),
  45. $url);
  46. }
  47. /**
  48. * Search plugins for the specified string
  49. *
  50. * @param string $query The string to search for
  51. * @return array
  52. */
  53. public function search($query) {
  54. if ($result = parent::search($query)) {
  55. return $result;
  56. }
  57. $found = false;
  58. foreach (core_component::get_plugin_list($this->subtype) as $name => $notused) {
  59. if (strpos(core_text::strtolower(get_string('pluginname', $this->subtype . '_' . $name)),
  60. $query) !== false) {
  61. $found = true;
  62. break;
  63. }
  64. }
  65. if ($found) {
  66. $result = new stdClass();
  67. $result->page = $this;
  68. $result->settings = array();
  69. return array($this->name => $result);
  70. } else {
  71. return array();
  72. }
  73. }
  74. }
  75. /**
  76. * Class that handles the display and configuration of the list of submission plugins.
  77. *
  78. * @package mod_assign
  79. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  80. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  81. */
  82. class assign_plugin_manager {
  83. /** @var object the url of the manage submission plugin page */
  84. private $pageurl;
  85. /** @var string any error from the current action */
  86. private $error = '';
  87. /** @var string either submission or feedback */
  88. private $subtype = '';
  89. /**
  90. * Constructor for this assignment plugin manager
  91. * @param string $subtype - either assignsubmission or assignfeedback
  92. */
  93. public function __construct($subtype) {
  94. $this->pageurl = new moodle_url('/mod/assign/adminmanageplugins.php', array('subtype'=>$subtype));
  95. $this->subtype = $subtype;
  96. }
  97. /**
  98. * Return a list of plugins sorted by the order defined in the admin interface
  99. *
  100. * @return array The list of plugins
  101. */
  102. public function get_sorted_plugins_list() {
  103. $names = core_component::get_plugin_list($this->subtype);
  104. $result = array();
  105. foreach ($names as $name => $path) {
  106. $idx = get_config($this->subtype . '_' . $name, 'sortorder');
  107. if (!$idx) {
  108. $idx = 0;
  109. }
  110. while (array_key_exists($idx, $result)) {
  111. $idx +=1;
  112. }
  113. $result[$idx] = $name;
  114. }
  115. ksort($result);
  116. return $result;
  117. }
  118. /**
  119. * Util function for writing an action icon link
  120. *
  121. * @param string $action URL parameter to include in the link
  122. * @param string $plugin URL parameter to include in the link
  123. * @param string $icon The key to the icon to use (e.g. 't/up')
  124. * @param string $alt The string description of the link used as the title and alt text
  125. * @return string The icon/link
  126. */
  127. private function format_icon_link($action, $plugin, $icon, $alt) {
  128. global $OUTPUT;
  129. $url = $this->pageurl;
  130. if ($action === 'delete') {
  131. $url = core_plugin_manager::instance()->get_uninstall_url($this->subtype.'_'.$plugin, 'manage');
  132. if (!$url) {
  133. return '&nbsp;';
  134. }
  135. return html_writer::link($url, get_string('uninstallplugin', 'core_admin'));
  136. }
  137. return $OUTPUT->action_icon(new moodle_url($url,
  138. array('action' => $action, 'plugin'=> $plugin, 'sesskey' => sesskey())),
  139. new pix_icon($icon, $alt, 'moodle', array('title' => $alt)),
  140. null, array('title' => $alt)) . ' ';
  141. }
  142. /**
  143. * Write the HTML for the submission plugins table.
  144. *
  145. * @return None
  146. */
  147. private function view_plugins_table() {
  148. global $OUTPUT, $CFG;
  149. require_once($CFG->libdir . '/tablelib.php');
  150. // Set up the table.
  151. $this->view_header();
  152. $table = new flexible_table($this->subtype . 'pluginsadminttable');
  153. $table->define_baseurl($this->pageurl);
  154. $table->define_columns(array('pluginname', 'version', 'hideshow', 'order',
  155. 'settings', 'uninstall'));
  156. $table->define_headers(array(get_string($this->subtype . 'pluginname', 'assign'),
  157. get_string('version'), get_string('hideshow', 'assign'),
  158. get_string('order'), get_string('settings'), get_string('uninstallplugin', 'core_admin')));
  159. $table->set_attribute('id', $this->subtype . 'plugins');
  160. $table->set_attribute('class', 'admintable generaltable');
  161. $table->setup();
  162. $plugins = $this->get_sorted_plugins_list();
  163. $shortsubtype = substr($this->subtype, strlen('assign'));
  164. foreach ($plugins as $idx => $plugin) {
  165. $row = array();
  166. $class = '';
  167. $row[] = get_string('pluginname', $this->subtype . '_' . $plugin);
  168. $row[] = get_config($this->subtype . '_' . $plugin, 'version');
  169. $visible = !get_config($this->subtype . '_' . $plugin, 'disabled');
  170. if ($visible) {
  171. $row[] = $this->format_icon_link('hide', $plugin, 't/hide', get_string('disable'));
  172. } else {
  173. $row[] = $this->format_icon_link('show', $plugin, 't/show', get_string('enable'));
  174. $class = 'dimmed_text';
  175. }
  176. $movelinks = '';
  177. if (!$idx == 0) {
  178. $movelinks .= $this->format_icon_link('moveup', $plugin, 't/up', get_string('up'));
  179. } else {
  180. $movelinks .= $OUTPUT->spacer(array('width'=>16));
  181. }
  182. if ($idx != count($plugins) - 1) {
  183. $movelinks .= $this->format_icon_link('movedown', $plugin, 't/down', get_string('down'));
  184. }
  185. $row[] = $movelinks;
  186. $exists = file_exists($CFG->dirroot . '/mod/assign/' . $shortsubtype . '/' . $plugin . '/settings.php');
  187. if ($row[1] != '' && $exists) {
  188. $row[] = html_writer::link(new moodle_url('/admin/settings.php',
  189. array('section' => $this->subtype . '_' . $plugin)), get_string('settings'));
  190. } else {
  191. $row[] = '&nbsp;';
  192. }
  193. $row[] = $this->format_icon_link('delete', $plugin, 't/delete', get_string('uninstallplugin', 'core_admin'));
  194. $table->add_data($row, $class);
  195. }
  196. $table->finish_output();
  197. $this->view_footer();
  198. }
  199. /**
  200. * Write the page header
  201. *
  202. * @return None
  203. */
  204. private function view_header() {
  205. global $OUTPUT;
  206. admin_externalpage_setup('manage' . $this->subtype . 'plugins');
  207. // Print the page heading.
  208. echo $OUTPUT->header();
  209. echo $OUTPUT->heading(get_string('manage' . $this->subtype . 'plugins', 'assign'));
  210. }
  211. /**
  212. * Write the page footer
  213. *
  214. * @return None
  215. */
  216. private function view_footer() {
  217. global $OUTPUT;
  218. echo $OUTPUT->footer();
  219. }
  220. /**
  221. * Check this user has permission to edit the list of installed plugins
  222. *
  223. * @return None
  224. */
  225. private function check_permissions() {
  226. // Check permissions.
  227. require_login();
  228. $systemcontext = context_system::instance();
  229. require_capability('moodle/site:config', $systemcontext);
  230. }
  231. /**
  232. * Hide this plugin.
  233. *
  234. * @param string $plugin - The plugin to hide
  235. * @return string The next page to display
  236. */
  237. public function hide_plugin($plugin) {
  238. set_config('disabled', 1, $this->subtype . '_' . $plugin);
  239. core_plugin_manager::reset_caches();
  240. return 'view';
  241. }
  242. /**
  243. * Change the order of this plugin.
  244. *
  245. * @param string $plugintomove - The plugin to move
  246. * @param string $dir - up or down
  247. * @return string The next page to display
  248. */
  249. public function move_plugin($plugintomove, $dir) {
  250. // Get a list of the current plugins.
  251. $plugins = $this->get_sorted_plugins_list();
  252. $currentindex = 0;
  253. // Throw away the keys.
  254. $plugins = array_values($plugins);
  255. // Find this plugin in the list.
  256. foreach ($plugins as $key => $plugin) {
  257. if ($plugin == $plugintomove) {
  258. $currentindex = $key;
  259. break;
  260. }
  261. }
  262. // Make the switch.
  263. if ($dir == 'up') {
  264. if ($currentindex > 0) {
  265. $tempplugin = $plugins[$currentindex - 1];
  266. $plugins[$currentindex - 1] = $plugins[$currentindex];
  267. $plugins[$currentindex] = $tempplugin;
  268. }
  269. } else if ($dir == 'down') {
  270. if ($currentindex < (count($plugins) - 1)) {
  271. $tempplugin = $plugins[$currentindex + 1];
  272. $plugins[$currentindex + 1] = $plugins[$currentindex];
  273. $plugins[$currentindex] = $tempplugin;
  274. }
  275. }
  276. // Save the new normal order.
  277. foreach ($plugins as $key => $plugin) {
  278. set_config('sortorder', $key, $this->subtype . '_' . $plugin);
  279. }
  280. return 'view';
  281. }
  282. /**
  283. * Show this plugin.
  284. *
  285. * @param string $plugin - The plugin to show
  286. * @return string The next page to display
  287. */
  288. public function show_plugin($plugin) {
  289. set_config('disabled', 0, $this->subtype . '_' . $plugin);
  290. core_plugin_manager::reset_caches();
  291. return 'view';
  292. }
  293. /**
  294. * This is the entry point for this controller class.
  295. *
  296. * @param string $action - The action to perform
  297. * @param string $plugin - Optional name of a plugin type to perform the action on
  298. * @return None
  299. */
  300. public function execute($action, $plugin) {
  301. if ($action == null) {
  302. $action = 'view';
  303. }
  304. $this->check_permissions();
  305. // Process.
  306. if ($action == 'hide' && $plugin != null) {
  307. $action = $this->hide_plugin($plugin);
  308. } else if ($action == 'show' && $plugin != null) {
  309. $action = $this->show_plugin($plugin);
  310. } else if ($action == 'moveup' && $plugin != null) {
  311. $action = $this->move_plugin($plugin, 'up');
  312. } else if ($action == 'movedown' && $plugin != null) {
  313. $action = $this->move_plugin($plugin, 'down');
  314. }
  315. // View.
  316. if ($action == 'view') {
  317. $this->view_plugins_table();
  318. }
  319. }
  320. }