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] [Versions 400 and 402] [Versions 401 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   * Defines classes used for plugin info.
  19   *
  20   * @package    core
  21   * @copyright  2011 David Mudrak <david@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core\plugininfo;
  25  
  26  use admin_settingpage;
  27  use moodle_url;
  28  use part_of_admin_tree;
  29  
  30  /**
  31   * Class for page side blocks
  32   */
  33  class block extends base {
  34  
  35      public static function plugintype_supports_disabling(): bool {
  36          return true;
  37      }
  38  
  39      /**
  40       * Finds all enabled plugins, the result may include missing plugins.
  41       * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
  42       */
  43      public static function get_enabled_plugins() {
  44          global $DB;
  45  
  46          return $DB->get_records_menu('block', array('visible'=>1), 'name ASC', 'name, name AS val');
  47      }
  48  
  49      public static function enable_plugin(string $pluginname, int $enabled): bool {
  50          global $DB;
  51  
  52          if (!$block = $DB->get_record('block', ['name' => $pluginname])) {
  53              throw new \moodle_exception('blockdoesnotexist', 'error');
  54          }
  55  
  56          $haschanged = false;
  57  
  58          // Only set visibility if it's different from the current value.
  59          if ($block->visible != $enabled) {
  60              // Set block visibility.
  61              $DB->set_field('block', 'visible', $enabled, ['id' => $block->id]);
  62              $haschanged = true;
  63  
  64              // Include this information into config changes table.
  65              add_to_config_log('block_visibility', $block->visible, $enabled, $pluginname);
  66              \core_plugin_manager::reset_caches();
  67          }
  68  
  69          return $haschanged;
  70      }
  71  
  72      /**
  73       * Magic method getter, redirects to read only values.
  74       *
  75       * For block plugins pretends the object has 'visible' property for compatibility
  76       * with plugins developed for Moodle version below 2.4
  77       *
  78       * @param string $name
  79       * @return mixed
  80       */
  81      public function __get($name) {
  82          if ($name === 'visible') {
  83              debugging('This is now an instance of plugininfo_block, please use $block->is_enabled() instead of $block->visible', DEBUG_DEVELOPER);
  84              return ($this->is_enabled() !== false);
  85          }
  86          return parent::__get($name);
  87      }
  88  
  89      public function init_display_name() {
  90  
  91          if (get_string_manager()->string_exists('pluginname', 'block_' . $this->name)) {
  92              $this->displayname = get_string('pluginname', 'block_' . $this->name);
  93  
  94          } else if (($block = block_instance($this->name)) !== false) {
  95              $this->displayname = $block->get_title();
  96  
  97          } else {
  98              parent::init_display_name();
  99          }
 100      }
 101  
 102      public function get_settings_section_name() {
 103          return 'blocksetting' . $this->name;
 104      }
 105  
 106      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
 107          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
 108          /** @var \admin_root $ADMIN */
 109          $ADMIN = $adminroot; // May be used in settings.php.
 110          $plugininfo = $this; // Also can be used inside settings.php.
 111          $block = $this;      // Also can be used inside settings.php.
 112  
 113          if (!$this->is_installed_and_upgraded()) {
 114              return;
 115          }
 116  
 117          $section = $this->get_settings_section_name();
 118  
 119          if (!$hassiteconfig || (($blockinstance = block_instance($this->name)) === false)) {
 120              return;
 121          }
 122  
 123          $settings = null;
 124          if ($blockinstance->has_config()) {
 125              if (file_exists($this->full_path('settings.php'))) {
 126                  $settings = new admin_settingpage($section, $this->displayname,
 127                      'moodle/site:config', $this->is_enabled() === false);
 128                  include($this->full_path('settings.php')); // This may also set $settings to null.
 129              }
 130          }
 131          if ($settings) {
 132              $ADMIN->add($parentnodename, $settings);
 133          }
 134      }
 135  
 136      public function is_uninstall_allowed() {
 137          if ($this->name === 'settings' or $this->name === 'navigation') {
 138              return false;
 139          }
 140          return true;
 141      }
 142  
 143      /**
 144       * Return URL used for management of plugins of this type.
 145       * @return moodle_url
 146       */
 147      public static function get_manage_url() {
 148          return new moodle_url('/admin/blocks.php');
 149      }
 150  
 151      /**
 152       * Warning with number of block instances.
 153       *
 154       * @return string
 155       */
 156      public function get_uninstall_extra_warning() {
 157          global $DB;
 158  
 159          if (!$count = $DB->count_records('block_instances', array('blockname'=>$this->name))) {
 160              return '';
 161          }
 162  
 163          return '<p>'.get_string('uninstallextraconfirmblock', 'core_plugin', array('instances'=>$count)).'</p>';
 164      }
 165  
 166      /**
 167       * Pre-uninstall hook.
 168       *
 169       * This is intended for disabling of plugin, some DB table purging, etc.
 170       *
 171       * NOTE: to be called from uninstall_plugin() only.
 172       * @private
 173       */
 174      public function uninstall_cleanup() {
 175          global $DB, $CFG;
 176  
 177          if ($block = $DB->get_record('block', array('name'=>$this->name))) {
 178              // Inform block it's about to be deleted.
 179              if (file_exists("$CFG->dirroot/blocks/$block->name/block_$block->name.php")) {
 180                  $blockobject = block_instance($block->name);
 181                  if ($blockobject) {
 182                      $blockobject->before_delete();  // Only if we can create instance, block might have been already removed.
 183                  }
 184              }
 185  
 186              // First delete instances and related contexts.
 187              $instances = $DB->get_records('block_instances', array('blockname' => $block->name));
 188              foreach ($instances as $instance) {
 189                  blocks_delete_instance($instance);
 190              }
 191  
 192              // Delete block.
 193              $DB->delete_records('block', array('id'=>$block->id));
 194          }
 195  
 196          parent::uninstall_cleanup();
 197      }
 198  }