MDL-55471 javascript: Fix conflict between bootstrapbase and bootstrap4
[moodle.git] / theme / noname / classes / output / core_renderer.php
CommitLineData
536f0460
DW
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
17namespace theme_noname\output;
18
da4d9eb7 19use coding_exception;
536f0460
DW
20use html_writer;
21use tabobject;
22use tabtree;
23use custom_menu_item;
24use custom_menu;
d7fbf722
DW
25use block_contents;
26use stdClass;
f130c411 27use moodle_url;
3f0544b8
DW
28use preferences_groups;
29use action_menu;
536f0460
DW
30
31defined('MOODLE_INTERNAL') || die;
32
33/**
34 * Renderers to align Moodle's HTML with that expected by Bootstrap
35 *
36 * @package theme_noname
37 * @copyright 2012 Bas Brands, www.basbrands.nl
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40
41class core_renderer extends \core_renderer {
42
43 /** @var custom_menu_item language The language menu if created */
44 protected $language = null;
45
46 /**
47 * The standard tags that should be included in the <head> tag
48 * including a meta description for the front page
49 *
50 * @return string HTML fragment.
51 */
52 public function standard_head_html() {
53 global $SITE, $PAGE;
54
55 $output = parent::standard_head_html();
56 if ($PAGE->pagelayout == 'frontpage') {
57 $summary = s(strip_tags(format_text($SITE->summary, FORMAT_HTML)));
58 if (!empty($summary)) {
59 $output .= "<meta name=\"description\" content=\"$summary\" />\n";
60 }
61 }
62
63 return $output;
64 }
65
66 /*
67 * This renders the navbar.
68 * Uses bootstrap compatible html.
69 */
70 public function navbar() {
13d07a01 71 return $this->render_from_template('core/navbar', $this->page->navbar);
536f0460
DW
72 }
73
74 /*
75 * Overriding the custom_menu function ensures the custom menu is
76 * always shown, even if no menu items are configured in the global
77 * theme settings page.
78 */
79 public function custom_menu($custommenuitems = '') {
80 global $CFG;
81
82 if (empty($custommenuitems) && !empty($CFG->custommenuitems)) {
83 $custommenuitems = $CFG->custommenuitems;
84 }
85 $custommenu = new custom_menu($custommenuitems, current_language());
86 return $this->render_custom_menu($custommenu);
87 }
88
89 /*
90 * This renders the bootstrap top menu.
91 *
92 * This renderer is needed to enable the Bootstrap style navigation.
93 */
94 protected function render_custom_menu(custom_menu $menu) {
95 global $CFG;
96
97 $langs = get_string_manager()->get_list_of_translations();
98 $haslangmenu = $this->lang_menu() != '';
99
100 if (!$menu->has_children() && !$haslangmenu) {
101 return '';
102 }
103
104 if ($haslangmenu) {
f130c411 105 $strlang = get_string('language');
536f0460
DW
106 $currentlang = current_language();
107 if (isset($langs[$currentlang])) {
108 $currentlang = $langs[$currentlang];
109 } else {
110 $currentlang = $strlang;
111 }
112 $this->language = $menu->add($currentlang, new moodle_url('#'), $strlang, 10000);
113 foreach ($langs as $langtype => $langname) {
114 $this->language->add($langname, new moodle_url($this->page->url, array('lang' => $langtype)), $langname);
115 }
116 }
117
f130c411 118 $content = '';
536f0460 119 foreach ($menu->get_children() as $item) {
f130c411
DW
120 $context = $item->export_for_template($this);
121 $content .= $this->render_from_template('core/custom_menu_item', $context);
536f0460
DW
122 }
123
536f0460
DW
124 return $content;
125 }
126
127 /**
128 * This code renders the navbar button to control the display of the custom menu
129 * on smaller screens.
130 *
131 * Do not display the button if the menu is empty.
132 *
133 * @return string HTML fragment
134 */
135 public function navbar_button() {
136 global $CFG;
137
138 if (empty($CFG->custommenuitems) && $this->lang_menu() == '') {
139 return '';
140 }
141
142 $iconbar = html_writer::tag('span', '', array('class' => 'icon-bar'));
143 $button = html_writer::tag('a', $iconbar . "\n" . $iconbar. "\n" . $iconbar, array(
144 'class' => 'btn btn-navbar',
145 'data-toggle' => 'collapse',
146 'data-target' => '.nav-collapse'
147 ));
148 return $button;
149 }
150
151 /**
152 * Renders tabtree
153 *
154 * @param tabtree $tabtree
155 * @return string
156 */
157 protected function render_tabtree(tabtree $tabtree) {
158 if (empty($tabtree->subtree)) {
159 return '';
160 }
da4d9eb7
FM
161 $data = $tabtree->export_for_template($this);
162 return $this->render_from_template('core/tabtree', $data);
536f0460
DW
163 }
164
165 /**
166 * Renders tabobject (part of tabtree)
167 *
168 * This function is called from {@link core_renderer::render_tabtree()}
169 * and also it calls itself when printing the $tabobject subtree recursively.
170 *
171 * @param tabobject $tabobject
172 * @return string HTML fragment
173 */
174 protected function render_tabobject(tabobject $tab) {
da4d9eb7 175 throw new coding_exception('Tab objects should not be directly rendered.');
536f0460 176 }
d7fbf722
DW
177
178 /**
179 * Prints a nice side block with an optional header.
180 *
181 * @param block_contents $bc HTML for the content
182 * @param string $region the region the block is appearing in.
183 * @return string the HTML to be output.
184 */
185 public function block(block_contents $bc, $region) {
186 $bc = clone($bc); // Avoid messing up the object passed in.
187 if (empty($bc->blockinstanceid) || !strip_tags($bc->title)) {
188 $bc->collapsible = block_contents::NOT_HIDEABLE;
189 }
190 if (!empty($bc->blockinstanceid)) {
191 $bc->attributes['data-instanceid'] = $bc->blockinstanceid;
192 }
193 $skiptitle = strip_tags($bc->title);
194 if ($bc->blockinstanceid && !empty($skiptitle)) {
195 $bc->attributes['aria-labelledby'] = 'instance-'.$bc->blockinstanceid.'-header';
196 } else if (!empty($bc->arialabel)) {
197 $bc->attributes['aria-label'] = $bc->arialabel;
198 }
199 if ($bc->dockable) {
200 $bc->attributes['data-dockable'] = 1;
201 }
202 if ($bc->collapsible == block_contents::HIDDEN) {
203 $bc->add_class('hidden');
204 }
205 if (!empty($bc->controls)) {
206 $bc->add_class('block_with_controls');
207 }
208
209 $id = !empty($bc->attributes['id']) ? $bc->attributes['id'] : uniqid('block-');
210 $context = new stdClass();
211 $context->skipid = $bc->skipid;
212 $context->blockinstanceid = $bc->blockinstanceid;
213 $context->dockable = $bc->dockable;
214 $context->id = $id;
215 $context->hidden = $bc->collapsible == block_contents::HIDDEN;
216 $context->skiptitle = strip_tags($bc->title);
217 $context->showskiplink = !empty($context->skiptitle);
218 $context->arialabel = $bc->arialabel;
219 $context->ariarole = !empty($bc->attributes['role']) ? $bc->attributes['role'] : 'complementary';
220 $context->type = $bc->attributes['data-block'];
221 $context->title = $bc->title;
222 $context->content = $bc->content;
223 $context->annotation = $bc->annotation;
224 $context->footer = $bc->footer;
225 $context->hascontrols = !empty($bc->controls);
226 if ($context->hascontrols) {
227 $context->controls = $this->block_controls($bc->controls, $id);
228 }
229
230 return $this->render_from_template('core/block', $context);
231 }
232
233 /**
234 * Returns the CSS classes to apply to the body tag.
235 *
236 * @since Moodle 2.5.1 2.6
237 * @param array $additionalclasses Any additional classes to apply.
238 * @return string
239 */
240 public function body_css_classes(array $additionalclasses = array()) {
241 return $this->page->bodyclasses;
242 }
3f0544b8
DW
243
244 /**
245 * Renders preferences groups.
246 *
247 * @param preferences_groups $renderable The renderable
248 * @return string The output.
249 */
250 public function render_preferences_groups(preferences_groups $renderable) {
251 return $this->render_from_template('core/preferences_groups', $renderable);
252 }
253
3f0544b8
DW
254 /**
255 * Renders an action menu component.
256 *
257 * @param action_menu $menu
258 * @return string HTML
259 */
260 public function render_action_menu(action_menu $menu) {
261 return $this->render_from_template('core/action_menu', $menu);
262 }
ac4a389e 263
536f0460 264}