MDL-55793 theme_noname: Use fixed width container
[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;
b7e95263 30use help_icon;
d7c65752 31use single_button;
bf7f35e9 32use single_select;
b0da86e0 33use paging_bar;
f1b34660 34use url_select;
b71c82ad 35use context_course;
132093f0 36use pix_icon;
536f0460
DW
37
38defined('MOODLE_INTERNAL') || die;
39
40/**
41 * Renderers to align Moodle's HTML with that expected by Bootstrap
42 *
43 * @package theme_noname
44 * @copyright 2012 Bas Brands, www.basbrands.nl
45 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46 */
47
48class core_renderer extends \core_renderer {
49
50 /** @var custom_menu_item language The language menu if created */
51 protected $language = null;
52
7fcfc073
FM
53 /**
54 * Outputs the opening section of a box.
55 *
56 * @param string $classes A space-separated list of CSS classes
57 * @param string $id An optional ID
58 * @param array $attributes An array of other attributes to give the box.
59 * @return string the HTML to output.
60 */
61 public function box_start($classes = 'generalbox', $id = null, $attributes = array()) {
62 return parent::box_start($classes . ' p-a-1', $id, $attributes);
63 }
64
1ecc3daf
FM
65 /**
66 * Wrapper for header elements.
67 *
68 * @return string HTML to display the main header.
69 */
70 public function full_header() {
71 $html = html_writer::start_tag('header', array('id' => 'page-header', 'class' => 'row'));
72 $html .= html_writer::start_div('col-xs-12 p-t-1 p-b-1');
73 $html .= $this->context_header();
74 $html .= html_writer::start_div('clearfix', array('id' => 'page-navbar'));
75 $html .= html_writer::tag('div', $this->navbar(), array('class' => 'breadcrumb-nav'));
76 $html .= html_writer::div($this->page_heading_button(), 'breadcrumb-button');
77 $html .= html_writer::end_div();
78 $html .= html_writer::tag('div', $this->course_header(), array('id' => 'course-header'));
79 $html .= html_writer::end_div();
80 $html .= html_writer::end_tag('header');
81 return $html;
82 }
83
536f0460
DW
84 /**
85 * The standard tags that should be included in the <head> tag
86 * including a meta description for the front page
87 *
88 * @return string HTML fragment.
89 */
90 public function standard_head_html() {
91 global $SITE, $PAGE;
92
93 $output = parent::standard_head_html();
94 if ($PAGE->pagelayout == 'frontpage') {
95 $summary = s(strip_tags(format_text($SITE->summary, FORMAT_HTML)));
96 if (!empty($summary)) {
97 $output .= "<meta name=\"description\" content=\"$summary\" />\n";
98 }
99 }
100
101 return $output;
102 }
103
104 /*
105 * This renders the navbar.
106 * Uses bootstrap compatible html.
107 */
108 public function navbar() {
13d07a01 109 return $this->render_from_template('core/navbar', $this->page->navbar);
536f0460
DW
110 }
111
b71c82ad
FM
112 /**
113 * Override to inject the logo.
114 *
115 * @param array $headerinfo The header info.
116 * @param int $headinglevel What level the 'h' tag will be.
117 * @return string HTML for the header bar.
118 */
119 public function context_header($headerinfo = null, $headinglevel = 1) {
120 global $SITE;
121
122 if ($this->should_display_main_logo($headinglevel)) {
123 $sitename = format_string($SITE->fullname, true, array('context' => context_course::instance(SITEID)));
124 return html_writer::div(html_writer::empty_tag('img', [
125 'src' => $this->get_logo_url(null, 75), 'alt' => $sitename]), 'logo');
126 }
127
128 return parent::context_header($headerinfo, $headinglevel);
129 }
130
131 /**
132 * Get the compact logo URL.
133 *
134 * @return string
135 */
136 public function get_compact_logo_url($maxwidth = 100, $maxheight = 100) {
137 return parent::get_compact_logo_url(null, 43);
138 }
139
140 /**
141 * Whether we should display the main logo.
142 *
143 * @return bool
144 */
145 public function should_display_main_logo($headinglevel = 1) {
146 global $PAGE;
147
148 // Only render the logo if we're on the front page or login page and the we have a logo.
149 $logo = $this->get_logo_url();
150 if ($headinglevel == 1 && !empty($logo)) {
151 if ($PAGE->pagelayout == 'frontpage' || $PAGE->pagelayout == 'login') {
152 return true;
153 }
154 }
155
156 return false;
157 }
158 /**
159 * Whether we should display the logo in the navbar.
160 *
161 * We will when there are no main logos, and we have compact logo.
162 *
163 * @return bool
164 */
165 public function should_display_navbar_logo() {
166 $logo = $this->get_compact_logo_url();
167 return !empty($logo) && !$this->should_display_main_logo();
168 }
169
536f0460
DW
170 /*
171 * Overriding the custom_menu function ensures the custom menu is
172 * always shown, even if no menu items are configured in the global
173 * theme settings page.
174 */
175 public function custom_menu($custommenuitems = '') {
176 global $CFG;
177
178 if (empty($custommenuitems) && !empty($CFG->custommenuitems)) {
179 $custommenuitems = $CFG->custommenuitems;
180 }
181 $custommenu = new custom_menu($custommenuitems, current_language());
182 return $this->render_custom_menu($custommenu);
183 }
184
185 /*
186 * This renders the bootstrap top menu.
187 *
188 * This renderer is needed to enable the Bootstrap style navigation.
189 */
190 protected function render_custom_menu(custom_menu $menu) {
191 global $CFG;
192
193 $langs = get_string_manager()->get_list_of_translations();
194 $haslangmenu = $this->lang_menu() != '';
195
196 if (!$menu->has_children() && !$haslangmenu) {
197 return '';
198 }
199
200 if ($haslangmenu) {
f130c411 201 $strlang = get_string('language');
536f0460
DW
202 $currentlang = current_language();
203 if (isset($langs[$currentlang])) {
204 $currentlang = $langs[$currentlang];
205 } else {
206 $currentlang = $strlang;
207 }
208 $this->language = $menu->add($currentlang, new moodle_url('#'), $strlang, 10000);
209 foreach ($langs as $langtype => $langname) {
210 $this->language->add($langname, new moodle_url($this->page->url, array('lang' => $langtype)), $langname);
211 }
212 }
213
f130c411 214 $content = '';
536f0460 215 foreach ($menu->get_children() as $item) {
f130c411
DW
216 $context = $item->export_for_template($this);
217 $content .= $this->render_from_template('core/custom_menu_item', $context);
536f0460
DW
218 }
219
536f0460
DW
220 return $content;
221 }
222
223 /**
224 * This code renders the navbar button to control the display of the custom menu
225 * on smaller screens.
226 *
227 * Do not display the button if the menu is empty.
228 *
229 * @return string HTML fragment
230 */
231 public function navbar_button() {
232 global $CFG;
233
234 if (empty($CFG->custommenuitems) && $this->lang_menu() == '') {
235 return '';
236 }
237
238 $iconbar = html_writer::tag('span', '', array('class' => 'icon-bar'));
239 $button = html_writer::tag('a', $iconbar . "\n" . $iconbar. "\n" . $iconbar, array(
240 'class' => 'btn btn-navbar',
241 'data-toggle' => 'collapse',
242 'data-target' => '.nav-collapse'
243 ));
244 return $button;
245 }
246
247 /**
248 * Renders tabtree
249 *
250 * @param tabtree $tabtree
251 * @return string
252 */
253 protected function render_tabtree(tabtree $tabtree) {
254 if (empty($tabtree->subtree)) {
255 return '';
256 }
da4d9eb7
FM
257 $data = $tabtree->export_for_template($this);
258 return $this->render_from_template('core/tabtree', $data);
536f0460
DW
259 }
260
261 /**
262 * Renders tabobject (part of tabtree)
263 *
264 * This function is called from {@link core_renderer::render_tabtree()}
265 * and also it calls itself when printing the $tabobject subtree recursively.
266 *
267 * @param tabobject $tabobject
268 * @return string HTML fragment
269 */
270 protected function render_tabobject(tabobject $tab) {
da4d9eb7 271 throw new coding_exception('Tab objects should not be directly rendered.');
536f0460 272 }
d7fbf722
DW
273
274 /**
275 * Prints a nice side block with an optional header.
276 *
277 * @param block_contents $bc HTML for the content
278 * @param string $region the region the block is appearing in.
279 * @return string the HTML to be output.
280 */
281 public function block(block_contents $bc, $region) {
282 $bc = clone($bc); // Avoid messing up the object passed in.
283 if (empty($bc->blockinstanceid) || !strip_tags($bc->title)) {
284 $bc->collapsible = block_contents::NOT_HIDEABLE;
285 }
286 if (!empty($bc->blockinstanceid)) {
287 $bc->attributes['data-instanceid'] = $bc->blockinstanceid;
288 }
289 $skiptitle = strip_tags($bc->title);
290 if ($bc->blockinstanceid && !empty($skiptitle)) {
291 $bc->attributes['aria-labelledby'] = 'instance-'.$bc->blockinstanceid.'-header';
292 } else if (!empty($bc->arialabel)) {
293 $bc->attributes['aria-label'] = $bc->arialabel;
294 }
295 if ($bc->dockable) {
296 $bc->attributes['data-dockable'] = 1;
297 }
298 if ($bc->collapsible == block_contents::HIDDEN) {
299 $bc->add_class('hidden');
300 }
301 if (!empty($bc->controls)) {
302 $bc->add_class('block_with_controls');
303 }
304
305 $id = !empty($bc->attributes['id']) ? $bc->attributes['id'] : uniqid('block-');
306 $context = new stdClass();
307 $context->skipid = $bc->skipid;
308 $context->blockinstanceid = $bc->blockinstanceid;
309 $context->dockable = $bc->dockable;
310 $context->id = $id;
311 $context->hidden = $bc->collapsible == block_contents::HIDDEN;
312 $context->skiptitle = strip_tags($bc->title);
313 $context->showskiplink = !empty($context->skiptitle);
314 $context->arialabel = $bc->arialabel;
315 $context->ariarole = !empty($bc->attributes['role']) ? $bc->attributes['role'] : 'complementary';
316 $context->type = $bc->attributes['data-block'];
317 $context->title = $bc->title;
318 $context->content = $bc->content;
319 $context->annotation = $bc->annotation;
320 $context->footer = $bc->footer;
321 $context->hascontrols = !empty($bc->controls);
322 if ($context->hascontrols) {
323 $context->controls = $this->block_controls($bc->controls, $id);
324 }
325
326 return $this->render_from_template('core/block', $context);
327 }
328
329 /**
330 * Returns the CSS classes to apply to the body tag.
331 *
332 * @since Moodle 2.5.1 2.6
333 * @param array $additionalclasses Any additional classes to apply.
334 * @return string
335 */
336 public function body_css_classes(array $additionalclasses = array()) {
337 return $this->page->bodyclasses;
338 }
3f0544b8
DW
339
340 /**
341 * Renders preferences groups.
342 *
343 * @param preferences_groups $renderable The renderable
344 * @return string The output.
345 */
346 public function render_preferences_groups(preferences_groups $renderable) {
347 return $this->render_from_template('core/preferences_groups', $renderable);
348 }
349
3f0544b8
DW
350 /**
351 * Renders an action menu component.
352 *
353 * @param action_menu $menu
354 * @return string HTML
355 */
356 public function render_action_menu(action_menu $menu) {
132093f0
FM
357
358 // We don't want the class icon there!
359 foreach ($menu->get_secondary_actions() as $action) {
360 if ($action instanceof \action_menu_link && $action->has_class('icon')) {
361 $action->attributes['class'] = preg_replace('/(^|\s+)icon(\s+|$)/i', '', $action->attributes['class']);
362 }
363 }
364
365 $context = $menu->export_for_template($this);
366
367 // We do not want the icon with the caret, the caret is added by Bootstrap.
368 if (empty($context->primary->menutrigger)) {
369 $newurl = $this->pix_url('t/edit', 'moodle');
370 $context->primary->icon['attributes'] = array_reduce($context->primary->icon['attributes'],
371 function($carry, $item) use ($newurl) {
372 if ($item['name'] === 'src') {
373 $item['value'] = $newurl->out(false);
374 }
375 $carry[] = $item;
376 return $carry;
377 }, []
378 );
379 }
380
381 return $this->render_from_template('core/action_menu', $context);
3f0544b8 382 }
ac4a389e 383
b7e95263
DW
384 /**
385 * Implementation of user image rendering.
386 *
387 * @param help_icon $helpicon A help icon instance
388 * @return string HTML fragment
389 */
390 protected function render_help_icon(help_icon $helpicon) {
391 $context = $helpicon->export_for_template($this);
392 return $this->render_from_template('core/help_icon', $context);
393 }
bf7f35e9 394
d7c65752
FM
395 /**
396 * Renders a single button widget.
397 *
398 * This will return HTML to display a form containing a single button.
399 *
400 * @param single_button $button
401 * @return string HTML fragment
402 */
403 protected function render_single_button(single_button $button) {
404 return $this->render_from_template('core/single_button', $button->export_for_template($this));
405 }
406
bf7f35e9
FM
407 /**
408 * Renders a single select.
409 *
410 * @param single_select $select The object.
411 * @return string HTML
412 */
413 protected function render_single_select(single_select $select) {
414 return $this->render_from_template('core/single_select', $select->export_for_template($this));
415 }
416
b0da86e0
FM
417 /**
418 * Renders a paging bar.
419 *
420 * @param paging_bar $pagingbar The object.
421 * @return string HTML
422 */
423 protected function render_paging_bar(paging_bar $pagingbar) {
8a47abcd
DW
424 // Any more than 10 is not usable and causes wierd wrapping of the pagination in this theme.
425 $pagingbar->maxdisplay = 10;
b0da86e0
FM
426 return $this->render_from_template('core/paging_bar', $pagingbar->export_for_template($this));
427 }
428
f1b34660
FM
429 /**
430 * Renders a url select.
431 *
432 * @param url_select $select The object.
433 * @return string HTML
434 */
435 protected function render_url_select(url_select $select) {
436 return $this->render_from_template('core/url_select', $select->export_for_template($this));
437 }
536f0460 438}