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