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