MDL-65788 tool_policy: Prevent two modals rendering for guests.
[moodle.git] / blocks / settings / block_settings.php
CommitLineData
7d2a0492 1<?php
7d2a0492 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 *
5bcfd504 21 * @since Moodle 2.0
f25a6839 22 * @package block_settings
7d2a0492 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 *
f25a6839 32 * @package block_settings
7d2a0492 33 * @copyright 2009 Sam Hemelryk
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
3406acde 36class block_settings extends block_base {
7d2a0492 37
38 /** @var string */
39 public static $navcount;
40 public $blockname = null;
d7319652 41 /** @var bool */
42 protected $contentgenerated = false;
43 /** @var bool|null */
44 protected $docked = null;
7d2a0492 45
46 /**
47 * Set the initial properties for the block
48 */
49 function init() {
50 $this->blockname = get_class($this);
9b0c81b6 51 $this->title = get_string('pluginname', $this->blockname);
7d2a0492 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
d8ef60bd
SH
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
7d2a0492 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
3406acde
SH
88 function instance_can_be_docked() {
89 return (parent::instance_can_be_docked() && (empty($this->config->enabledock) || $this->config->enabledock=='yes'));
90 }
91
9d897331 92 function get_required_javascript() {
a057ba59 93 global $PAGE;
a057ba59 94 $adminnode = $PAGE->settingsnav->find('siteadministration', navigation_node::TYPE_SITE_ADMIN);
84192d78
SH
95 parent::get_required_javascript();
96 $arguments = array(
fd5466af
CB
97 'instanceid' => $this->instance->id,
98 'adminnodeid' => $adminnode ? $adminnode->id : null
84192d78 99 );
fd5466af 100 $this->page->requires->js_call_amd('block_settings/settingsblock', 'init', $arguments);
9d897331
SH
101 }
102
7d2a0492 103 /**
104 * Gets the content for this block by grabbing it from $this->page
105 */
106 function get_content() {
107 global $CFG, $OUTPUT;
108 // First check if we have already generated, don't waste cycles
109 if ($this->contentgenerated === true) {
110 return true;
111 }
2574fae6 112 // JS for navigation moved to the standard theme, the code will probably have to depend on the actual page structure
9dec75db 113 // $this->page->requires->js('/lib/javascript-navigation.js');
3406acde 114 block_settings::$navcount++;
7d2a0492 115
d7319652 116 // Check if this block has been docked
117 if ($this->docked === null) {
3406acde 118 $this->docked = get_user_preferences('nav_in_tab_panel_settingsnav'.block_settings::$navcount, 0);
d7319652 119 }
120
121 // Check if there is a param to change the docked state
122 if ($this->docked && optional_param('undock', null, PARAM_INT)==$this->instance->id) {
3406acde 123 unset_user_preference('nav_in_tab_panel_settingsnav'.block_settings::$navcount, 0);
d7319652 124 $url = $this->page->url;
125 $url->remove_params(array('undock'));
126 redirect($url);
127 } else if (!$this->docked && optional_param('dock', null, PARAM_INT)==$this->instance->id) {
3406acde 128 set_user_preferences(array('nav_in_tab_panel_settingsnav'.block_settings::$navcount=>1));
d7319652 129 $url = $this->page->url;
130 $url->remove_params(array('dock'));
131 redirect($url);
132 }
133
3406acde 134 $renderer = $this->page->get_renderer('block_settings');
b85b25eb 135 $this->content = new stdClass();
3406acde 136 $this->content->text = $renderer->settings_tree($this->page->settingsnav);
9af2b86b 137
7d2a0492 138 // only do search if you have moodle/site:config
3406acde 139 if (!empty($this->content->text)) {
fe2fdd11 140 if (has_capability('moodle/site:config',context_system::instance()) ) {
3406acde 141 $this->content->footer = $renderer->search_form(new moodle_url("$CFG->wwwroot/$CFG->admin/search.php"), optional_param('query', '', PARAM_RAW));
7d2a0492 142 } else {
143 $this->content->footer = '';
144 }
145
3406acde
SH
146 if (!empty($this->config->enabledock) && $this->config->enabledock == 'yes') {
147 user_preference_allow_ajax_update('nav_in_tab_panel_settingsnav'.block_settings::$navcount, PARAM_INT);
7d2a0492 148 }
149 }
150
151 $this->contentgenerated = true;
152 return true;
153 }
697f7bd7
SH
154
155 /**
156 * Returns the role that best describes the settings block.
157 *
158 * @return string 'navigation'
159 */
160 public function get_aria_role() {
161 return 'navigation';
162 }
c1c0cecf 163}