Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402]

   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  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once($CFG->libdir . '/adminlib.php');
  28  
  29  /**
  30   * Admin external page that displays a list of the installed submission plugins.
  31   *
  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
  35   */
  36  class assign_admin_page_manage_assign_plugins extends admin_externalpage {
  37  
  38      /** @var string the name of plugin subtype */
  39      private $subtype = '';
  40  
  41      /**
  42       * The constructor - calls parent constructor
  43       *
  44       * @param string $subtype
  45       */
  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);
  52      }
  53  
  54      /**
  55       * Search plugins for the specified string
  56       *
  57       * @param string $query The string to search for
  58       * @return array
  59       */
  60      public function search($query) {
  61          if ($result = parent::search($query)) {
  62              return $result;
  63          }
  64  
  65          $found = false;
  66  
  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;
  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   */
  93  class 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 = core_component::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)) {
 128                  $idx +=1;
 129              }
 130              $result[$idx] = $name;
 131          }
 132          ksort($result);
 133  
 134          return $result;
 135      }
 136  
 137  
 138      /**
 139       * Util function for writing an action icon link
 140       *
 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
 146       */
 147      private function format_icon_link($action, $plugin, $icon, $alt) {
 148          global $OUTPUT;
 149  
 150          $url = $this->pageurl;
 151  
 152          if ($action === 'delete') {
 153              $url = core_plugin_manager::instance()->get_uninstall_url($this->subtype.'_'.$plugin, 'manage');
 154              if (!$url) {
 155                  return '&nbsp;';
 156              }
 157              return html_writer::link($url, get_string('uninstallplugin', 'core_admin'));
 158          }
 159  
 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)) . ' ';
 164      }
 165  
 166      /**
 167       * Write the HTML for the submission plugins table.
 168       *
 169       * @return None
 170       */
 171      private function view_plugins_table() {
 172          global $OUTPUT, $CFG;
 173          require_once($CFG->libdir . '/tablelib.php');
 174  
 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();
 187  
 188          $plugins = $this->get_sorted_plugins_list();
 189          $shortsubtype = substr($this->subtype, strlen('assign'));
 190  
 191          foreach ($plugins as $idx => $plugin) {
 192              $row = array();
 193              $class = '';
 194  
 195              $row[] = get_string('pluginname', $this->subtype . '_' . $plugin);
 196              $row[] = get_config($this->subtype . '_' . $plugin, 'version');
 197  
 198              $visible = !get_config($this->subtype . '_' . $plugin, 'disabled');
 199  
 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';
 205              }
 206  
 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));
 212              }
 213              if ($idx != count($plugins) - 1) {
 214                  $movelinks .= $this->format_icon_link('movedown', $plugin, 't/down', get_string('down'));
 215              }
 216              $row[] = $movelinks;
 217  
 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;';
 224              }
 225  
 226              $row[] = $this->format_icon_link('delete', $plugin, 't/delete', get_string('uninstallplugin', 'core_admin'));
 227  
 228              $table->add_data($row, $class);
 229          }
 230  
 231          $table->finish_output();
 232          $this->view_footer();
 233      }
 234  
 235      /**
 236       * Write the page header
 237       *
 238       * @return None
 239       */
 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'));
 246      }
 247  
 248      /**
 249       * Write the page footer
 250       *
 251       * @return None
 252       */
 253      private function view_footer() {
 254          global $OUTPUT;
 255          echo $OUTPUT->footer();
 256      }
 257  
 258      /**
 259       * Check this user has permission to edit the list of installed plugins
 260       *
 261       * @return None
 262       */
 263      private function check_permissions() {
 264          // Check permissions.
 265          require_login();
 266          $systemcontext = context_system::instance();
 267          require_capability('moodle/site:config', $systemcontext);
 268      }
 269  
 270      /**
 271       * Hide this plugin.
 272       *
 273       * @param string $plugin - The plugin to hide
 274       * @return string The next page to display
 275       */
 276      public function hide_plugin($plugin) {
 277          $class = \core_plugin_manager::resolve_plugininfo_class($this->subtype);
 278          $class::enable_plugin($plugin, false);
 279          return 'view';
 280      }
 281  
 282      /**
 283       * Change the order of this plugin.
 284       *
 285       * @param string $plugintomove - The plugin to move
 286       * @param string $dir - up or down
 287       * @return string The next page to display
 288       */
 289      public function move_plugin($plugintomove, $dir) {
 290          // Get a list of the current plugins.
 291          $plugins = $this->get_sorted_plugins_list();
 292  
 293          $currentindex = 0;
 294  
 295          // Throw away the keys.
 296          $plugins = array_values($plugins);
 297  
 298          // Find this plugin in the list.
 299          foreach ($plugins as $key => $plugin) {
 300              if ($plugin == $plugintomove) {
 301                  $currentindex = $key;
 302                  break;
 303              }
 304          }
 305  
 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;
 312              }
 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;
 318              }
 319          }
 320  
 321          // Save the new normal order.
 322          foreach ($plugins as $key => $plugin) {
 323              set_config('sortorder', $key, $this->subtype . '_' . $plugin);
 324          }
 325          return 'view';
 326      }
 327  
 328  
 329      /**
 330       * Show this plugin.
 331       *
 332       * @param string $plugin - The plugin to show
 333       * @return string The next page to display
 334       */
 335      public function show_plugin($plugin) {
 336          $class = \core_plugin_manager::resolve_plugininfo_class($this->subtype);
 337          $class::enable_plugin($plugin, true);
 338          return 'view';
 339      }
 340  
 341  
 342      /**
 343       * This is the entry point for this controller class.
 344       *
 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
 348       */
 349      public function execute($action, $plugin) {
 350          if ($action == null) {
 351              $action = 'view';
 352          }
 353  
 354          $this->check_permissions();
 355  
 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');
 365          }
 366  
 367          // View.
 368          if ($action == 'view') {
 369              $this->view_plugins_table();
 370          }
 371      }
 372  }