block_settings.php 5.5 KB

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