Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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   * This file contains classes used to manage the navigation structures in Moodle
  19   * and was introduced as part of the changes occuring in Moodle 2.0
  20   *
  21   * @since Moodle 2.0
  22   * @package block_settings
  23   * @copyright 2009 Sam Hemelryk
  24   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  /**
  28   * The settings navigation tree block class
  29   *
  30   * Used to produce the settings navigation block new to Moodle 2.0
  31   *
  32   * @package block_settings
  33   * @copyright 2009 Sam Hemelryk
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class block_settings extends block_base {
  37  
  38      /** @var string */
  39      public static $navcount;
  40      public $blockname = null;
  41      /** @var bool */
  42      protected $contentgenerated = false;
  43      /** @var bool|null */
  44      protected $docked = null;
  45  
  46      /**
  47       * Set the initial properties for the block
  48       */
  49      function init() {
  50          $this->blockname = get_class($this);
  51          $this->title = get_string('pluginname', $this->blockname);
  52      }
  53  
  54      /**
  55       * All multiple instances of this block
  56       * @return bool Returns true
  57       */
  58      function instance_allow_multiple() {
  59          return false;
  60      }
  61  
  62      /**
  63       * The settings block cannot be hidden by default as it is integral to
  64       * the navigation of Moodle.
  65       *
  66       * @return false
  67       */
  68      function  instance_can_be_hidden() {
  69          return false;
  70      }
  71  
  72      /**
  73       * Set the applicable formats for this block to all
  74       * @return array
  75       */
  76      function applicable_formats() {
  77          return array('all' => true);
  78      }
  79  
  80      /**
  81       * Allow the user to configure a block instance
  82       * @return bool Returns true
  83       */
  84      function instance_allow_config() {
  85          return true;
  86      }
  87  
  88      function instance_can_be_docked() {
  89          return (parent::instance_can_be_docked() && (empty($this->config->enabledock) || $this->config->enabledock=='yes'));
  90      }
  91  
  92      function get_required_javascript() {
  93          $adminnode = $this->page->settingsnav->find('siteadministration', navigation_node::TYPE_SITE_ADMIN);
  94          parent::get_required_javascript();
  95          $arguments = array(
  96              'instanceid' => $this->instance->id,
  97              'adminnodeid' => $adminnode ? $adminnode->id : null
  98          );
  99          $this->page->requires->js_call_amd('block_settings/settingsblock', 'init', $arguments);
 100      }
 101  
 102      /**
 103       * Gets the content for this block by grabbing it from $this->page
 104       */
 105      function get_content() {
 106          global $CFG, $OUTPUT;
 107          // First check if we have already generated, don't waste cycles
 108          if ($this->contentgenerated === true) {
 109              return true;
 110          }
 111          // JS for navigation moved to the standard theme, the code will probably have to depend on the actual page structure
 112          // $this->page->requires->js('/lib/javascript-navigation.js');
 113          block_settings::$navcount++;
 114  
 115          // Check if this block has been docked
 116          if ($this->docked === null) {
 117              $this->docked = get_user_preferences('nav_in_tab_panel_settingsnav'.block_settings::$navcount, 0);
 118          }
 119  
 120          // Check if there is a param to change the docked state
 121          if ($this->docked && optional_param('undock', null, PARAM_INT)==$this->instance->id) {
 122              unset_user_preference('nav_in_tab_panel_settingsnav'.block_settings::$navcount, 0);
 123              $url = $this->page->url;
 124              $url->remove_params(array('undock'));
 125              redirect($url);
 126          } else if (!$this->docked && optional_param('dock', null, PARAM_INT)==$this->instance->id) {
 127              set_user_preferences(array('nav_in_tab_panel_settingsnav'.block_settings::$navcount=>1));
 128              $url = $this->page->url;
 129              $url->remove_params(array('dock'));
 130              redirect($url);
 131          }
 132  
 133          $renderer = $this->page->get_renderer('block_settings');
 134          $this->content = new stdClass();
 135          $this->content->text = $renderer->settings_tree($this->page->settingsnav);
 136  
 137          // only do search if you have moodle/site:config
 138          if (!empty($this->content->text)) {
 139              if (has_capability('moodle/site:config',context_system::instance()) ) {
 140                  $this->content->footer = $renderer->search_form(new moodle_url("$CFG->wwwroot/$CFG->admin/search.php"), optional_param('query', '', PARAM_RAW));
 141              } else {
 142                  $this->content->footer = '';
 143              }
 144          }
 145  
 146          $this->contentgenerated = true;
 147          return true;
 148      }
 149  
 150      /**
 151       * Returns the role that best describes the settings block.
 152       *
 153       * @return string 'navigation'
 154       */
 155      public function get_aria_role() {
 156          return 'navigation';
 157      }
 158  
 159      /**
 160       * Return the plugin config settings for external functions.
 161       *
 162       * @return stdClass the configs for both the block instance and plugin
 163       * @since Moodle 3.8
 164       */
 165      public function get_config_for_external() {
 166          // Return all settings for all users since it is safe (no private keys, etc..).
 167          $configs = !empty($this->config) ? $this->config : new stdClass();
 168  
 169          return (object) [
 170              'instance' => $configs,
 171              'plugin' => new stdClass(),
 172          ];
 173      }
 174  }