Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

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