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