MDL-46285 coding style fix
[moodle.git] / lib / outputrenderers.php
CommitLineData
d9c8f425 1<?php
d9c8f425 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
17/**
18 * Classes for rendering HTML output for Moodle.
19 *
7a3c215b 20 * Please see {@link http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML}
d9c8f425 21 * for an overview.
22 *
7a3c215b
SH
23 * Included in this file are the primary renderer classes:
24 * - renderer_base: The renderer outline class that all renderers
25 * should inherit from.
26 * - core_renderer: The standard HTML renderer.
27 * - core_renderer_cli: An adaption of the standard renderer for CLI scripts.
28 * - core_renderer_ajax: An adaption of the standard renderer for AJAX scripts.
29 * - plugin_renderer_base: A renderer class that should be extended by all
30 * plugin renderers.
31 *
f8129210 32 * @package core
76be40cc 33 * @category output
78bfb562
PS
34 * @copyright 2009 Tim Hunt
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d9c8f425 36 */
37
78bfb562
PS
38defined('MOODLE_INTERNAL') || die();
39
d9c8f425 40/**
41 * Simple base class for Moodle renderers.
42 *
43 * Tracks the xhtml_container_stack to use, which is passed in in the constructor.
44 *
45 * Also has methods to facilitate generating HTML output.
46 *
47 * @copyright 2009 Tim Hunt
7a3c215b
SH
48 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
49 * @since Moodle 2.0
f8129210 50 * @package core
76be40cc 51 * @category output
d9c8f425 52 */
78946b9b 53class renderer_base {
7a3c215b 54 /**
76be40cc 55 * @var xhtml_container_stack The xhtml_container_stack to use.
7a3c215b 56 */
d9c8f425 57 protected $opencontainers;
7a3c215b
SH
58
59 /**
76be40cc 60 * @var moodle_page The Moodle page the renderer has been created to assist with.
7a3c215b 61 */
d9c8f425 62 protected $page;
7a3c215b
SH
63
64 /**
76be40cc 65 * @var string The requested rendering target.
7a3c215b 66 */
c927e35c 67 protected $target;
d9c8f425 68
69 /**
70 * Constructor
7a3c215b 71 *
3d3fae72 72 * The constructor takes two arguments. The first is the page that the renderer
7a3c215b
SH
73 * has been created to assist with, and the second is the target.
74 * The target is an additional identifier that can be used to load different
75 * renderers for different options.
76 *
d9c8f425 77 * @param moodle_page $page the page we are doing output for.
c927e35c 78 * @param string $target one of rendering target constants
d9c8f425 79 */
c927e35c 80 public function __construct(moodle_page $page, $target) {
d9c8f425 81 $this->opencontainers = $page->opencontainers;
82 $this->page = $page;
c927e35c 83 $this->target = $target;
d9c8f425 84 }
85
86 /**
5d0c95a5 87 * Returns rendered widget.
7a3c215b
SH
88 *
89 * The provided widget needs to be an object that extends the renderable
90 * interface.
3d3fae72 91 * If will then be rendered by a method based upon the classname for the widget.
7a3c215b
SH
92 * For instance a widget of class `crazywidget` will be rendered by a protected
93 * render_crazywidget method of this renderer.
94 *
996b1e0c 95 * @param renderable $widget instance with renderable interface
5d0c95a5 96 * @return string
d9c8f425 97 */
5d0c95a5 98 public function render(renderable $widget) {
d2bba1ee
DW
99 $classname = get_class($widget);
100 // Strip namespaces.
101 $classname = preg_replace('/^.*\\\/', '', $classname);
102 // Remove _renderable suffixes
103 $classname = preg_replace('/_renderable$/', '', $classname);
104
105 $rendermethod = 'render_'.$classname;
5d0c95a5
PS
106 if (method_exists($this, $rendermethod)) {
107 return $this->$rendermethod($widget);
108 }
109 throw new coding_exception('Can not render widget, renderer method ('.$rendermethod.') not found.');
d9c8f425 110 }
111
112 /**
7a3c215b
SH
113 * Adds a JS action for the element with the provided id.
114 *
115 * This method adds a JS event for the provided component action to the page
116 * and then returns the id that the event has been attached to.
f8129210 117 * If no id has been provided then a new ID is generated by {@link html_writer::random_id()}
7a3c215b 118 *
f8129210 119 * @param component_action $action
c80877aa
PS
120 * @param string $id
121 * @return string id of element, either original submitted or random new if not supplied
d9c8f425 122 */
7a3c215b 123 public function add_action_handler(component_action $action, $id = null) {
c80877aa
PS
124 if (!$id) {
125 $id = html_writer::random_id($action->event);
126 }
d96d8f03 127 $this->page->requires->event_handler("#$id", $action->event, $action->jsfunction, $action->jsfunctionargs);
c80877aa 128 return $id;
d9c8f425 129 }
130
131 /**
7a3c215b
SH
132 * Returns true is output has already started, and false if not.
133 *
5d0c95a5 134 * @return boolean true if the header has been printed.
d9c8f425 135 */
5d0c95a5
PS
136 public function has_started() {
137 return $this->page->state >= moodle_page::STATE_IN_BODY;
d9c8f425 138 }
139
140 /**
141 * Given an array or space-separated list of classes, prepares and returns the HTML class attribute value
7a3c215b 142 *
d9c8f425 143 * @param mixed $classes Space-separated string or array of classes
144 * @return string HTML class attribute value
145 */
146 public static function prepare_classes($classes) {
147 if (is_array($classes)) {
148 return implode(' ', array_unique($classes));
149 }
150 return $classes;
151 }
152
d9c8f425 153 /**
897e902b 154 * Return the moodle_url for an image.
7a3c215b 155 *
897e902b
PS
156 * The exact image location and extension is determined
157 * automatically by searching for gif|png|jpg|jpeg, please
158 * note there can not be diferent images with the different
159 * extension. The imagename is for historical reasons
160 * a relative path name, it may be changed later for core
161 * images. It is recommended to not use subdirectories
162 * in plugin and theme pix directories.
d9c8f425 163 *
897e902b
PS
164 * There are three types of images:
165 * 1/ theme images - stored in theme/mytheme/pix/,
166 * use component 'theme'
167 * 2/ core images - stored in /pix/,
168 * overridden via theme/mytheme/pix_core/
169 * 3/ plugin images - stored in mod/mymodule/pix,
170 * overridden via theme/mytheme/pix_plugins/mod/mymodule/,
171 * example: pix_url('comment', 'mod_glossary')
172 *
173 * @param string $imagename the pathname of the image
174 * @param string $component full plugin name (aka component) or 'theme'
78946b9b 175 * @return moodle_url
d9c8f425 176 */
c927e35c 177 public function pix_url($imagename, $component = 'moodle') {
c39e5ba2 178 return $this->page->theme->pix_url($imagename, $component);
d9c8f425 179 }
d9c8f425 180}
181
c927e35c 182
75590935
PS
183/**
184 * Basis for all plugin renderers.
185 *
f8129210
SH
186 * @copyright Petr Skoda (skodak)
187 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
188 * @since Moodle 2.0
189 * @package core
76be40cc 190 * @category output
75590935
PS
191 */
192class plugin_renderer_base extends renderer_base {
7a3c215b 193
75590935 194 /**
3d3fae72
SH
195 * @var renderer_base|core_renderer A reference to the current renderer.
196 * The renderer provided here will be determined by the page but will in 90%
f8129210 197 * of cases by the {@link core_renderer}
75590935
PS
198 */
199 protected $output;
200
201 /**
996b1e0c 202 * Constructor method, calls the parent constructor
7a3c215b 203 *
75590935 204 * @param moodle_page $page
c927e35c 205 * @param string $target one of rendering target constants
75590935 206 */
c927e35c 207 public function __construct(moodle_page $page, $target) {
166ac0a3
SH
208 if (empty($target) && $page->pagelayout === 'maintenance') {
209 // If the page is using the maintenance layout then we're going to force the target to maintenance.
210 // This way we'll get a special maintenance renderer that is designed to block access to API's that are likely
211 // unavailable for this page layout.
212 $target = RENDERER_TARGET_MAINTENANCE;
213 }
c927e35c
PS
214 $this->output = $page->get_renderer('core', null, $target);
215 parent::__construct($page, $target);
75590935 216 }
ff5265c6 217
5d0c95a5 218 /**
7a3c215b
SH
219 * Renders the provided widget and returns the HTML to display it.
220 *
71c03ac1 221 * @param renderable $widget instance with renderable interface
5d0c95a5
PS
222 * @return string
223 */
224 public function render(renderable $widget) {
d2bba1ee
DW
225 $classname = get_class($widget);
226 // Strip namespaces.
227 $classname = preg_replace('/^.*\\\/', '', $classname);
ebdcb292
SH
228 // Keep a copy at this point, we may need to look for a deprecated method.
229 $deprecatedmethod = 'render_'.$classname;
d2bba1ee 230 // Remove _renderable suffixes
ebdcb292 231 $classname = preg_replace('/_renderable$/', '', $classname);
d2bba1ee
DW
232
233 $rendermethod = 'render_'.$classname;
5d0c95a5
PS
234 if (method_exists($this, $rendermethod)) {
235 return $this->$rendermethod($widget);
236 }
ebdcb292
SH
237 if ($rendermethod !== $deprecatedmethod && method_exists($this, $deprecatedmethod)) {
238 // This is exactly where we don't want to be.
239 // If you have arrived here you have a renderable component within your plugin that has the name
240 // blah_renderable, and you have a render method render_blah_renderable on your plugin.
241 // In 2.8 we revamped output, as part of this change we changed slightly how renderables got rendered
242 // and the _renderable suffix now gets removed when looking for a render method.
243 // You need to change your renderers render_blah_renderable to render_blah.
244 // Until you do this it will not be possible for a theme to override the renderer to override your method.
245 // Please do it ASAP.
246 static $debugged = array();
247 if (!isset($debugged[$deprecatedmethod])) {
248 debugging(sprintf('Deprecated call. Please rename your renderables render method from %s to %s.',
249 $deprecatedmethod, $rendermethod), DEBUG_DEVELOPER);
250 $debugged[$deprecatedmethod] = true;
251 }
252 return $this->$deprecatedmethod($widget);
253 }
5d0c95a5 254 // pass to core renderer if method not found here
469bf7a4 255 return $this->output->render($widget);
5d0c95a5
PS
256 }
257
ff5265c6
PS
258 /**
259 * Magic method used to pass calls otherwise meant for the standard renderer
996b1e0c 260 * to it to ensure we don't go causing unnecessary grief.
ff5265c6
PS
261 *
262 * @param string $method
263 * @param array $arguments
264 * @return mixed
265 */
266 public function __call($method, $arguments) {
37b5b18e 267 if (method_exists('renderer_base', $method)) {
fede0be5 268 throw new coding_exception('Protected method called against '.get_class($this).' :: '.$method);
37b5b18e 269 }
ff5265c6
PS
270 if (method_exists($this->output, $method)) {
271 return call_user_func_array(array($this->output, $method), $arguments);
272 } else {
fede0be5 273 throw new coding_exception('Unknown method called against '.get_class($this).' :: '.$method);
ff5265c6
PS
274 }
275 }
75590935 276}
d9c8f425 277
c927e35c 278
d9c8f425 279/**
78946b9b 280 * The standard implementation of the core_renderer interface.
d9c8f425 281 *
282 * @copyright 2009 Tim Hunt
7a3c215b
SH
283 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
284 * @since Moodle 2.0
f8129210 285 * @package core
76be40cc 286 * @category output
d9c8f425 287 */
78946b9b 288class core_renderer extends renderer_base {
72009b87
PS
289 /**
290 * Do NOT use, please use <?php echo $OUTPUT->main_content() ?>
291 * in layout files instead.
72009b87 292 * @deprecated
f8129210 293 * @var string used in {@link core_renderer::header()}.
72009b87 294 */
d9c8f425 295 const MAIN_CONTENT_TOKEN = '[MAIN CONTENT GOES HERE]';
7a3c215b
SH
296
297 /**
f8129210
SH
298 * @var string Used to pass information from {@link core_renderer::doctype()} to
299 * {@link core_renderer::standard_head_html()}.
7a3c215b 300 */
d9c8f425 301 protected $contenttype;
7a3c215b
SH
302
303 /**
f8129210
SH
304 * @var string Used by {@link core_renderer::redirect_message()} method to communicate
305 * with {@link core_renderer::header()}.
7a3c215b 306 */
d9c8f425 307 protected $metarefreshtag = '';
7a3c215b
SH
308
309 /**
76be40cc 310 * @var string Unique token for the closing HTML
7a3c215b 311 */
72009b87 312 protected $unique_end_html_token;
7a3c215b
SH
313
314 /**
76be40cc 315 * @var string Unique token for performance information
7a3c215b 316 */
72009b87 317 protected $unique_performance_info_token;
7a3c215b
SH
318
319 /**
76be40cc 320 * @var string Unique token for the main content.
7a3c215b 321 */
72009b87
PS
322 protected $unique_main_content_token;
323
324 /**
325 * Constructor
7a3c215b 326 *
72009b87
PS
327 * @param moodle_page $page the page we are doing output for.
328 * @param string $target one of rendering target constants
329 */
330 public function __construct(moodle_page $page, $target) {
331 $this->opencontainers = $page->opencontainers;
332 $this->page = $page;
333 $this->target = $target;
334
335 $this->unique_end_html_token = '%%ENDHTML-'.sesskey().'%%';
336 $this->unique_performance_info_token = '%%PERFORMANCEINFO-'.sesskey().'%%';
337 $this->unique_main_content_token = '[MAIN CONTENT GOES HERE - '.sesskey().']';
338 }
d9c8f425 339
340 /**
341 * Get the DOCTYPE declaration that should be used with this page. Designed to
342 * be called in theme layout.php files.
7a3c215b 343 *
13725b37 344 * @return string the DOCTYPE declaration that should be used.
d9c8f425 345 */
346 public function doctype() {
13725b37
PS
347 if ($this->page->theme->doctype === 'html5') {
348 $this->contenttype = 'text/html; charset=utf-8';
349 return "<!DOCTYPE html>\n";
d9c8f425 350
13725b37 351 } else if ($this->page->theme->doctype === 'xhtml5') {
d9c8f425 352 $this->contenttype = 'application/xhtml+xml; charset=utf-8';
13725b37 353 return "<!DOCTYPE html>\n";
d9c8f425 354
355 } else {
13725b37
PS
356 // legacy xhtml 1.0
357 $this->contenttype = 'text/html; charset=utf-8';
358 return ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' . "\n");
d9c8f425 359 }
d9c8f425 360 }
361
362 /**
363 * The attributes that should be added to the <html> tag. Designed to
364 * be called in theme layout.php files.
7a3c215b 365 *
d9c8f425 366 * @return string HTML fragment.
367 */
368 public function htmlattributes() {
13725b37
PS
369 $return = get_html_lang(true);
370 if ($this->page->theme->doctype !== 'html5') {
371 $return .= ' xmlns="http://www.w3.org/1999/xhtml"';
372 }
373 return $return;
d9c8f425 374 }
375
376 /**
377 * The standard tags (meta tags, links to stylesheets and JavaScript, etc.)
378 * that should be included in the <head> tag. Designed to be called in theme
379 * layout.php files.
7a3c215b 380 *
d9c8f425 381 * @return string HTML fragment.
382 */
383 public function standard_head_html() {
b5bbeaf0 384 global $CFG, $SESSION;
59849f79
AN
385
386 // Before we output any content, we need to ensure that certain
387 // page components are set up.
388
389 // Blocks must be set up early as they may require javascript which
390 // has to be included in the page header before output is created.
391 foreach ($this->page->blocks->get_regions() as $region) {
392 $this->page->blocks->ensure_content_created($region, $this);
393 }
394
d9c8f425 395 $output = '';
396 $output .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . "\n";
397 $output .= '<meta name="keywords" content="moodle, ' . $this->page->title . '" />' . "\n";
f8129210 398 // This is only set by the {@link redirect()} method
d9c8f425 399 $output .= $this->metarefreshtag;
400
401 // Check if a periodic refresh delay has been set and make sure we arn't
402 // already meta refreshing
403 if ($this->metarefreshtag=='' && $this->page->periodicrefreshdelay!==null) {
404 $output .= '<meta http-equiv="refresh" content="'.$this->page->periodicrefreshdelay.';url='.$this->page->url->out().'" />';
405 }
406
fcd2cbaf
PS
407 // flow player embedding support
408 $this->page->requires->js_function_call('M.util.load_flowplayer');
409
238b8bc9 410 // Set up help link popups for all links with the helptooltip class
afe3566c
ARN
411 $this->page->requires->js_init_call('M.util.help_popups.setup');
412
238b8bc9
ARN
413 // Setup help icon overlays.
414 $this->page->requires->yui_module('moodle-core-popuphelp', 'M.core.init_popuphelp');
415 $this->page->requires->strings_for_js(array(
416 'morehelp',
417 'loadinghelp',
418 ), 'moodle');
419
7d2a0492 420 $this->page->requires->js_function_call('setTimeout', array('fix_column_widths()', 20));
d9c8f425 421
422 $focus = $this->page->focuscontrol;
423 if (!empty($focus)) {
424 if (preg_match("#forms\['([a-zA-Z0-9]+)'\].elements\['([a-zA-Z0-9]+)'\]#", $focus, $matches)) {
425 // This is a horrifically bad way to handle focus but it is passed in
426 // through messy formslib::moodleform
7d2a0492 427 $this->page->requires->js_function_call('old_onload_focus', array($matches[1], $matches[2]));
d9c8f425 428 } else if (strpos($focus, '.')!==false) {
429 // Old style of focus, bad way to do it
430 debugging('This code is using the old style focus event, Please update this code to focus on an element id or the moodleform focus method.', DEBUG_DEVELOPER);
431 $this->page->requires->js_function_call('old_onload_focus', explode('.', $focus, 2));
432 } else {
433 // Focus element with given id
7d2a0492 434 $this->page->requires->js_function_call('focuscontrol', array($focus));
d9c8f425 435 }
436 }
437
78946b9b
PS
438 // Get the theme stylesheet - this has to be always first CSS, this loads also styles.css from all plugins;
439 // any other custom CSS can not be overridden via themes and is highly discouraged
efaa4c08 440 $urls = $this->page->theme->css_urls($this->page);
78946b9b 441 foreach ($urls as $url) {
c0467479 442 $this->page->requires->css_theme($url);
78946b9b
PS
443 }
444
04c01408 445 // Get the theme javascript head and footer
d7656956
ARN
446 if ($jsurl = $this->page->theme->javascript_url(true)) {
447 $this->page->requires->js($jsurl, true);
448 }
449 if ($jsurl = $this->page->theme->javascript_url(false)) {
450 $this->page->requires->js($jsurl);
451 }
5d0c95a5 452
d9c8f425 453 // Get any HTML from the page_requirements_manager.
945f19f7 454 $output .= $this->page->requires->get_head_code($this->page, $this);
d9c8f425 455
456 // List alternate versions.
457 foreach ($this->page->alternateversions as $type => $alt) {
5d0c95a5 458 $output .= html_writer::empty_tag('link', array('rel' => 'alternate',
d9c8f425 459 'type' => $type, 'title' => $alt->title, 'href' => $alt->url));
460 }
8a7703ce 461
90e920c7
SH
462 if (!empty($CFG->additionalhtmlhead)) {
463 $output .= "\n".$CFG->additionalhtmlhead;
464 }
d9c8f425 465
466 return $output;
467 }
468
469 /**
470 * The standard tags (typically skip links) that should be output just inside
471 * the start of the <body> tag. Designed to be called in theme layout.php files.
7a3c215b 472 *
d9c8f425 473 * @return string HTML fragment.
474 */
475 public function standard_top_of_body_html() {
90e920c7
SH
476 global $CFG;
477 $output = $this->page->requires->get_top_of_body_code();
478 if (!empty($CFG->additionalhtmltopofbody)) {
479 $output .= "\n".$CFG->additionalhtmltopofbody;
480 }
48e114a5
PS
481 $output .= $this->maintenance_warning();
482 return $output;
483 }
484
485 /**
486 * Scheduled maintenance warning message.
487 *
488 * Note: This is a nasty hack to display maintenance notice, this should be moved
489 * to some general notification area once we have it.
490 *
491 * @return string
492 */
493 public function maintenance_warning() {
494 global $CFG;
495
496 $output = '';
497 if (isset($CFG->maintenance_later) and $CFG->maintenance_later > time()) {
f487a8f8
RT
498 $timeleft = $CFG->maintenance_later - time();
499 // If timeleft less than 30 sec, set the class on block to error to highlight.
500 $errorclass = ($timeleft < 30) ? 'error' : 'warning';
501 $output .= $this->box_start($errorclass . ' moodle-has-zindex maintenancewarning');
502 $a = new stdClass();
503 $a->min = (int)($timeleft/60);
504 $a->sec = (int)($timeleft % 60);
505 $output .= get_string('maintenancemodeisscheduled', 'admin', $a) ;
48e114a5 506 $output .= $this->box_end();
f487a8f8
RT
507 $this->page->requires->yui_module('moodle-core-maintenancemodetimer', 'M.core.maintenancemodetimer',
508 array(array('timeleftinsec' => $timeleft)));
509 $this->page->requires->strings_for_js(
510 array('maintenancemodeisscheduled', 'sitemaintenance'),
511 'admin');
48e114a5 512 }
90e920c7 513 return $output;
d9c8f425 514 }
515
516 /**
517 * The standard tags (typically performance information and validation links,
518 * if we are in developer debug mode) that should be output in the footer area
519 * of the page. Designed to be called in theme layout.php files.
7a3c215b 520 *
d9c8f425 521 * @return string HTML fragment.
522 */
523 public function standard_footer_html() {
6af80cae 524 global $CFG, $SCRIPT;
d9c8f425 525
ec3ce3a9
PS
526 if (during_initial_install()) {
527 // Debugging info can not work before install is finished,
528 // in any case we do not want any links during installation!
529 return '';
530 }
531
f8129210 532 // This function is normally called from a layout.php file in {@link core_renderer::header()}
d9c8f425 533 // but some of the content won't be known until later, so we return a placeholder
f8129210 534 // for now. This will be replaced with the real content in {@link core_renderer::footer()}.
72009b87 535 $output = $this->unique_performance_info_token;
e5824bb9 536 if ($this->page->devicetypeinuse == 'legacy') {
ee8df661
SH
537 // The legacy theme is in use print the notification
538 $output .= html_writer::tag('div', get_string('legacythemeinuse'), array('class'=>'legacythemeinuse'));
539 }
37959dd4 540
e5824bb9 541 // Get links to switch device types (only shown for users not on a default device)
37959dd4
AF
542 $output .= $this->theme_switch_links();
543
d9c8f425 544 if (!empty($CFG->debugpageinfo)) {
d4c3f025 545 $output .= '<div class="performanceinfo pageinfo">This page is: ' . $this->page->debug_summary() . '</div>';
d9c8f425 546 }
b0c6dc1c 547 if (debugging(null, DEBUG_DEVELOPER) and has_capability('moodle/site:config', context_system::instance())) { // Only in developer mode
6af80cae
EL
548 // Add link to profiling report if necessary
549 if (function_exists('profiling_is_running') && profiling_is_running()) {
550 $txt = get_string('profiledscript', 'admin');
551 $title = get_string('profiledscriptview', 'admin');
4ac92d2a 552 $url = $CFG->wwwroot . '/admin/tool/profiling/index.php?script=' . urlencode($SCRIPT);
6af80cae
EL
553 $link= '<a title="' . $title . '" href="' . $url . '">' . $txt . '</a>';
554 $output .= '<div class="profilingfooter">' . $link . '</div>';
555 }
2d22f3d9
TH
556 $purgeurl = new moodle_url('/admin/purgecaches.php', array('confirm' => 1,
557 'sesskey' => sesskey(), 'returnurl' => $this->page->url->out_as_local_url(false)));
558 $output .= '<div class="purgecaches">' .
559 html_writer::link($purgeurl, get_string('purgecaches', 'admin')) . '</div>';
ba6c97ee 560 }
d9c8f425 561 if (!empty($CFG->debugvalidators)) {
f0202ae9 562 // NOTE: this is not a nice hack, $PAGE->url is not always accurate and $FULLME neither, it is not a bug if it fails. --skodak
d9c8f425 563 $output .= '<div class="validators"><ul>
564 <li><a href="http://validator.w3.org/check?verbose=1&amp;ss=1&amp;uri=' . urlencode(qualified_me()) . '">Validate HTML</a></li>
565 <li><a href="http://www.contentquality.com/mynewtester/cynthia.exe?rptmode=-1&amp;url1=' . urlencode(qualified_me()) . '">Section 508 Check</a></li>
566 <li><a href="http://www.contentquality.com/mynewtester/cynthia.exe?rptmode=0&amp;warnp2n3e=1&amp;url1=' . urlencode(qualified_me()) . '">WCAG 1 (2,3) Check</a></li>
567 </ul></div>';
568 }
569 return $output;
570 }
571
72009b87
PS
572 /**
573 * Returns standard main content placeholder.
574 * Designed to be called in theme layout.php files.
7a3c215b 575 *
72009b87
PS
576 * @return string HTML fragment.
577 */
578 public function main_content() {
537ba512
SH
579 // This is here because it is the only place we can inject the "main" role over the entire main content area
580 // without requiring all theme's to manually do it, and without creating yet another thing people need to
581 // remember in the theme.
582 // This is an unfortunate hack. DO NO EVER add anything more here.
583 // DO NOT add classes.
584 // DO NOT add an id.
585 return '<div role="main">'.$this->unique_main_content_token.'</div>';
72009b87
PS
586 }
587
d9c8f425 588 /**
589 * The standard tags (typically script tags that are not needed earlier) that
391edc51 590 * should be output after everything else. Designed to be called in theme layout.php files.
7a3c215b 591 *
d9c8f425 592 * @return string HTML fragment.
593 */
594 public function standard_end_of_body_html() {
391edc51
TH
595 global $CFG;
596
f8129210 597 // This function is normally called from a layout.php file in {@link core_renderer::header()}
d9c8f425 598 // but some of the content won't be known until later, so we return a placeholder
f8129210 599 // for now. This will be replaced with the real content in {@link core_renderer::footer()}.
391edc51
TH
600 $output = '';
601 if (!empty($CFG->additionalhtmlfooter)) {
602 $output .= "\n".$CFG->additionalhtmlfooter;
603 }
604 $output .= $this->unique_end_html_token;
605 return $output;
d9c8f425 606 }
607
608 /**
609 * Return the standard string that says whether you are logged in (and switched
610 * roles/logged in as another user).
2d0e682d
MV
611 * @param bool $withlinks if false, then don't include any links in the HTML produced.
612 * If not set, the default is the nologinlinks option from the theme config.php file,
613 * and if that is not set, then links are included.
d9c8f425 614 * @return string HTML fragment.
615 */
2d0e682d 616 public function login_info($withlinks = null) {
8f0fe0b8 617 global $USER, $CFG, $DB, $SESSION;
4bcc5118 618
244a32c6
PS
619 if (during_initial_install()) {
620 return '';
621 }
4bcc5118 622
2d0e682d
MV
623 if (is_null($withlinks)) {
624 $withlinks = empty($this->page->layout_options['nologinlinks']);
625 }
626
1dedecf2 627 $loginpage = ((string)$this->page->url === get_login_url());
244a32c6 628 $course = $this->page->course;
d79d5ac2
PS
629 if (\core\session\manager::is_loggedinas()) {
630 $realuser = \core\session\manager::get_realuser();
244a32c6 631 $fullname = fullname($realuser, true);
2d0e682d 632 if ($withlinks) {
fa84b901
RT
633 $loginastitle = get_string('loginas');
634 $realuserinfo = " [<a href=\"$CFG->wwwroot/course/loginas.php?id=$course->id&amp;sesskey=".sesskey()."\"";
635 $realuserinfo .= "title =\"".$loginastitle."\">$fullname</a>] ";
2d0e682d
MV
636 } else {
637 $realuserinfo = " [$fullname] ";
638 }
244a32c6
PS
639 } else {
640 $realuserinfo = '';
641 }
4bcc5118 642
244a32c6 643 $loginurl = get_login_url();
4bcc5118 644
244a32c6
PS
645 if (empty($course->id)) {
646 // $course->id is not defined during installation
647 return '';
4f0c2d00 648 } else if (isloggedin()) {
b0c6dc1c 649 $context = context_course::instance($course->id);
4bcc5118 650
244a32c6 651 $fullname = fullname($USER, true);
03d9401e 652 // Since Moodle 2.0 this link always goes to the public profile page (not the course profile page)
2d0e682d 653 if ($withlinks) {
c7fe9f81
RT
654 $linktitle = get_string('viewprofile');
655 $username = "<a href=\"$CFG->wwwroot/user/profile.php?id=$USER->id\" title=\"$linktitle\">$fullname</a>";
2d0e682d
MV
656 } else {
657 $username = $fullname;
658 }
244a32c6 659 if (is_mnet_remote_user($USER) and $idprovider = $DB->get_record('mnet_host', array('id'=>$USER->mnethostid))) {
2d0e682d
MV
660 if ($withlinks) {
661 $username .= " from <a href=\"{$idprovider->wwwroot}\">{$idprovider->name}</a>";
662 } else {
663 $username .= " from {$idprovider->name}";
664 }
244a32c6 665 }
b3df1764 666 if (isguestuser()) {
2778744b 667 $loggedinas = $realuserinfo.get_string('loggedinasguest');
2d0e682d 668 if (!$loginpage && $withlinks) {
2778744b
PS
669 $loggedinas .= " (<a href=\"$loginurl\">".get_string('login').'</a>)';
670 }
f5c1e621 671 } else if (is_role_switched($course->id)) { // Has switched roles
244a32c6
PS
672 $rolename = '';
673 if ($role = $DB->get_record('role', array('id'=>$USER->access['rsw'][$context->path]))) {
f2cf0f84 674 $rolename = ': '.role_get_name($role, $context);
244a32c6 675 }
2d0e682d
MV
676 $loggedinas = get_string('loggedinas', 'moodle', $username).$rolename;
677 if ($withlinks) {
aae028d9 678 $url = new moodle_url('/course/switchrole.php', array('id'=>$course->id,'sesskey'=>sesskey(), 'switchrole'=>0, 'returnurl'=>$this->page->url->out_as_local_url(false)));
8bf91fb8 679 $loggedinas .= ' ('.html_writer::tag('a', get_string('switchrolereturn'), array('href' => $url)).')';
2d0e682d 680 }
244a32c6 681 } else {
2d0e682d
MV
682 $loggedinas = $realuserinfo.get_string('loggedinas', 'moodle', $username);
683 if ($withlinks) {
684 $loggedinas .= " (<a href=\"$CFG->wwwroot/login/logout.php?sesskey=".sesskey()."\">".get_string('logout').'</a>)';
685 }
244a32c6
PS
686 }
687 } else {
2778744b 688 $loggedinas = get_string('loggedinnot', 'moodle');
2d0e682d 689 if (!$loginpage && $withlinks) {
2778744b
PS
690 $loggedinas .= " (<a href=\"$loginurl\">".get_string('login').'</a>)';
691 }
244a32c6 692 }
4bcc5118 693
244a32c6 694 $loggedinas = '<div class="logininfo">'.$loggedinas.'</div>';
4bcc5118 695
244a32c6
PS
696 if (isset($SESSION->justloggedin)) {
697 unset($SESSION->justloggedin);
698 if (!empty($CFG->displayloginfailures)) {
b3df1764 699 if (!isguestuser()) {
52dc1de7
AA
700 // Include this file only when required.
701 require_once($CFG->dirroot . '/user/lib.php');
702 if ($count = user_count_login_failures($USER)) {
2b0c88e2 703 $loggedinas .= '<div class="loginfailures">';
52dc1de7
AA
704 $a = new stdClass();
705 $a->attempts = $count;
706 $loggedinas .= get_string('failedloginattempts', '', $a);
b0c6dc1c 707 if (file_exists("$CFG->dirroot/report/log/index.php") and has_capability('report/log:view', context_system::instance())) {
46fa0628
DP
708 $loggedinas .= ' ('.html_writer::link(new moodle_url('/report/log/index.php', array('chooselog' => 1,
709 'id' => 0 , 'modid' => 'site_errors')), get_string('logs')).')';
244a32c6
PS
710 }
711 $loggedinas .= '</div>';
712 }
713 }
714 }
715 }
4bcc5118 716
244a32c6 717 return $loggedinas;
d9c8f425 718 }
719
720 /**
721 * Return the 'back' link that normally appears in the footer.
7a3c215b 722 *
d9c8f425 723 * @return string HTML fragment.
724 */
725 public function home_link() {
726 global $CFG, $SITE;
727
728 if ($this->page->pagetype == 'site-index') {
729 // Special case for site home page - please do not remove
730 return '<div class="sitelink">' .
34dff6aa 731 '<a title="Moodle" href="http://moodle.org/">' .
970c4419 732 '<img src="' . $this->pix_url('moodlelogo') . '" alt="moodlelogo" /></a></div>';
d9c8f425 733
734 } else if (!empty($CFG->target_release) && $CFG->target_release != $CFG->release) {
735 // Special case for during install/upgrade.
736 return '<div class="sitelink">'.
34dff6aa 737 '<a title="Moodle" href="http://docs.moodle.org/en/Administrator_documentation" onclick="this.target=\'_blank\'">' .
970c4419 738 '<img src="' . $this->pix_url('moodlelogo') . '" alt="moodlelogo" /></a></div>';
d9c8f425 739
740 } else if ($this->page->course->id == $SITE->id || strpos($this->page->pagetype, 'course-view') === 0) {
741 return '<div class="homelink"><a href="' . $CFG->wwwroot . '/">' .
742 get_string('home') . '</a></div>';
743
744 } else {
745 return '<div class="homelink"><a href="' . $CFG->wwwroot . '/course/view.php?id=' . $this->page->course->id . '">' .
8ebbb06a 746 format_string($this->page->course->shortname, true, array('context' => $this->page->context)) . '</a></div>';
d9c8f425 747 }
748 }
749
750 /**
751 * Redirects the user by any means possible given the current state
752 *
753 * This function should not be called directly, it should always be called using
754 * the redirect function in lib/weblib.php
755 *
756 * The redirect function should really only be called before page output has started
757 * however it will allow itself to be called during the state STATE_IN_BODY
758 *
759 * @param string $encodedurl The URL to send to encoded if required
760 * @param string $message The message to display to the user if any
761 * @param int $delay The delay before redirecting a user, if $message has been
762 * set this is a requirement and defaults to 3, set to 0 no delay
763 * @param boolean $debugdisableredirect this redirect has been disabled for
764 * debugging purposes. Display a message that explains, and don't
765 * trigger the redirect.
766 * @return string The HTML to display to the user before dying, may contain
767 * meta refresh, javascript refresh, and may have set header redirects
768 */
769 public function redirect_message($encodedurl, $message, $delay, $debugdisableredirect) {
770 global $CFG;
771 $url = str_replace('&amp;', '&', $encodedurl);
772
773 switch ($this->page->state) {
774 case moodle_page::STATE_BEFORE_HEADER :
775 // No output yet it is safe to delivery the full arsenal of redirect methods
776 if (!$debugdisableredirect) {
777 // Don't use exactly the same time here, it can cause problems when both redirects fire at the same time.
778 $this->metarefreshtag = '<meta http-equiv="refresh" content="'. $delay .'; url='. $encodedurl .'" />'."\n";
593f9b87 779 $this->page->requires->js_function_call('document.location.replace', array($url), false, ($delay + 3));
d9c8f425 780 }
781 $output = $this->header();
782 break;
783 case moodle_page::STATE_PRINTING_HEADER :
784 // We should hopefully never get here
785 throw new coding_exception('You cannot redirect while printing the page header');
786 break;
787 case moodle_page::STATE_IN_BODY :
788 // We really shouldn't be here but we can deal with this
789 debugging("You should really redirect before you start page output");
790 if (!$debugdisableredirect) {
593f9b87 791 $this->page->requires->js_function_call('document.location.replace', array($url), false, $delay);
d9c8f425 792 }
793 $output = $this->opencontainers->pop_all_but_last();
794 break;
795 case moodle_page::STATE_DONE :
796 // Too late to be calling redirect now
797 throw new coding_exception('You cannot redirect after the entire page has been generated');
798 break;
799 }
800 $output .= $this->notification($message, 'redirectmessage');
3ab2e357 801 $output .= '<div class="continuebutton">(<a href="'. $encodedurl .'">'. get_string('continue') .'</a>)</div>';
d9c8f425 802 if ($debugdisableredirect) {
803 $output .= '<p><strong>Error output, so disabling automatic redirect.</strong></p>';
804 }
805 $output .= $this->footer();
806 return $output;
807 }
808
809 /**
810 * Start output by sending the HTTP headers, and printing the HTML <head>
811 * and the start of the <body>.
812 *
813 * To control what is printed, you should set properties on $PAGE. If you
f8129210 814 * are familiar with the old {@link print_header()} function from Moodle 1.9
d9c8f425 815 * you will find that there are properties on $PAGE that correspond to most
816 * of the old parameters to could be passed to print_header.
817 *
818 * Not that, in due course, the remaining $navigation, $menu parameters here
819 * will be replaced by more properties of $PAGE, but that is still to do.
820 *
d9c8f425 821 * @return string HTML that you must output this, preferably immediately.
822 */
e120c61d 823 public function header() {
d9c8f425 824 global $USER, $CFG;
825
d79d5ac2 826 if (\core\session\manager::is_loggedinas()) {
e884f63a
PS
827 $this->page->add_body_class('userloggedinas');
828 }
829
63c88397
PS
830 // Give themes a chance to init/alter the page object.
831 $this->page->theme->init_page($this->page);
832
d9c8f425 833 $this->page->set_state(moodle_page::STATE_PRINTING_HEADER);
834
78946b9b
PS
835 // Find the appropriate page layout file, based on $this->page->pagelayout.
836 $layoutfile = $this->page->theme->layout_file($this->page->pagelayout);
837 // Render the layout using the layout file.
838 $rendered = $this->render_page_layout($layoutfile);
67e84a7f 839
78946b9b 840 // Slice the rendered output into header and footer.
72009b87
PS
841 $cutpos = strpos($rendered, $this->unique_main_content_token);
842 if ($cutpos === false) {
843 $cutpos = strpos($rendered, self::MAIN_CONTENT_TOKEN);
844 $token = self::MAIN_CONTENT_TOKEN;
845 } else {
846 $token = $this->unique_main_content_token;
847 }
848
d9c8f425 849 if ($cutpos === false) {
72009b87 850 throw new coding_exception('page layout file ' . $layoutfile . ' does not contain the main content placeholder, please include "<?php echo $OUTPUT->main_content() ?>" in theme layout file.');
d9c8f425 851 }
78946b9b 852 $header = substr($rendered, 0, $cutpos);
72009b87 853 $footer = substr($rendered, $cutpos + strlen($token));
d9c8f425 854
855 if (empty($this->contenttype)) {
78946b9b 856 debugging('The page layout file did not call $OUTPUT->doctype()');
67e84a7f 857 $header = $this->doctype() . $header;
d9c8f425 858 }
859
fdd4b9a5
MG
860 // If this theme version is below 2.4 release and this is a course view page
861 if ((!isset($this->page->theme->settings->version) || $this->page->theme->settings->version < 2012101500) &&
862 $this->page->pagelayout === 'course' && $this->page->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
863 // check if course content header/footer have not been output during render of theme layout
864 $coursecontentheader = $this->course_content_header(true);
865 $coursecontentfooter = $this->course_content_footer(true);
866 if (!empty($coursecontentheader)) {
867 // display debug message and add header and footer right above and below main content
868 // Please note that course header and footer (to be displayed above and below the whole page)
869 // are not displayed in this case at all.
870 // Besides the content header and footer are not displayed on any other course page
871 debugging('The current theme is not optimised for 2.4, the course-specific header and footer defined in course format will not be output', DEBUG_DEVELOPER);
872 $header .= $coursecontentheader;
873 $footer = $coursecontentfooter. $footer;
874 }
875 }
876
d9c8f425 877 send_headers($this->contenttype, $this->page->cacheable);
67e84a7f 878
d9c8f425 879 $this->opencontainers->push('header/footer', $footer);
880 $this->page->set_state(moodle_page::STATE_IN_BODY);
67e84a7f 881
29ba64e5 882 return $header . $this->skip_link_target('maincontent');
d9c8f425 883 }
884
885 /**
78946b9b 886 * Renders and outputs the page layout file.
7a3c215b
SH
887 *
888 * This is done by preparing the normal globals available to a script, and
889 * then including the layout file provided by the current theme for the
890 * requested layout.
891 *
78946b9b 892 * @param string $layoutfile The name of the layout file
d9c8f425 893 * @return string HTML code
894 */
78946b9b 895 protected function render_page_layout($layoutfile) {
92e01ab7 896 global $CFG, $SITE, $USER;
d9c8f425 897 // The next lines are a bit tricky. The point is, here we are in a method
898 // of a renderer class, and this object may, or may not, be the same as
78946b9b 899 // the global $OUTPUT object. When rendering the page layout file, we want to use
d9c8f425 900 // this object. However, people writing Moodle code expect the current
901 // renderer to be called $OUTPUT, not $this, so define a variable called
902 // $OUTPUT pointing at $this. The same comment applies to $PAGE and $COURSE.
903 $OUTPUT = $this;
904 $PAGE = $this->page;
905 $COURSE = $this->page->course;
906
d9c8f425 907 ob_start();
78946b9b
PS
908 include($layoutfile);
909 $rendered = ob_get_contents();
d9c8f425 910 ob_end_clean();
78946b9b 911 return $rendered;
d9c8f425 912 }
913
914 /**
915 * Outputs the page's footer
7a3c215b 916 *
d9c8f425 917 * @return string HTML fragment
918 */
919 public function footer() {
d5a8d9aa 920 global $CFG, $DB;
0f0801f4 921
f6794ace 922 $output = $this->container_end_all(true);
4d2ee4c2 923
d9c8f425 924 $footer = $this->opencontainers->pop('header/footer');
925
d5a8d9aa 926 if (debugging() and $DB and $DB->is_transaction_started()) {
03221650 927 // TODO: MDL-20625 print warning - transaction will be rolled back
d5a8d9aa
PS
928 }
929
d9c8f425 930 // Provide some performance info if required
931 $performanceinfo = '';
932 if (defined('MDL_PERF') || (!empty($CFG->perfdebug) and $CFG->perfdebug > 7)) {
933 $perf = get_performance_info();
d9c8f425 934 if (defined('MDL_PERFTOFOOT') || debugging() || $CFG->perfdebug > 7) {
935 $performanceinfo = $perf['html'];
936 }
937 }
58a3a34e
DM
938
939 // We always want performance data when running a performance test, even if the user is redirected to another page.
940 if (MDL_PERF_TEST && strpos($footer, $this->unique_performance_info_token) === false) {
941 $footer = $this->unique_performance_info_token . $footer;
942 }
72009b87 943 $footer = str_replace($this->unique_performance_info_token, $performanceinfo, $footer);
d9c8f425 944
72009b87 945 $footer = str_replace($this->unique_end_html_token, $this->page->requires->get_end_code(), $footer);
d9c8f425 946
947 $this->page->set_state(moodle_page::STATE_DONE);
d9c8f425 948
949 return $output . $footer;
950 }
951
f6794ace
PS
952 /**
953 * Close all but the last open container. This is useful in places like error
954 * handling, where you want to close all the open containers (apart from <body>)
955 * before outputting the error message.
7a3c215b 956 *
f6794ace
PS
957 * @param bool $shouldbenone assert that the stack should be empty now - causes a
958 * developer debug warning if it isn't.
959 * @return string the HTML required to close any open containers inside <body>.
960 */
961 public function container_end_all($shouldbenone = false) {
962 return $this->opencontainers->pop_all_but_last($shouldbenone);
963 }
964
fdd4b9a5
MG
965 /**
966 * Returns course-specific information to be output immediately above content on any course page
967 * (for the current course)
968 *
969 * @param bool $onlyifnotcalledbefore output content only if it has not been output before
970 * @return string
971 */
972 public function course_content_header($onlyifnotcalledbefore = false) {
973 global $CFG;
974 if ($this->page->course->id == SITEID) {
975 // return immediately and do not include /course/lib.php if not necessary
976 return '';
977 }
978 static $functioncalled = false;
979 if ($functioncalled && $onlyifnotcalledbefore) {
980 // we have already output the content header
981 return '';
982 }
983 require_once($CFG->dirroot.'/course/lib.php');
984 $functioncalled = true;
985 $courseformat = course_get_format($this->page->course);
986 if (($obj = $courseformat->course_content_header()) !== null) {
06a72e01 987 return html_writer::div($courseformat->get_renderer($this->page)->render($obj), 'course-content-header');
fdd4b9a5
MG
988 }
989 return '';
990 }
991
992 /**
993 * Returns course-specific information to be output immediately below content on any course page
994 * (for the current course)
995 *
996 * @param bool $onlyifnotcalledbefore output content only if it has not been output before
997 * @return string
998 */
999 public function course_content_footer($onlyifnotcalledbefore = false) {
1000 global $CFG;
1001 if ($this->page->course->id == SITEID) {
1002 // return immediately and do not include /course/lib.php if not necessary
1003 return '';
1004 }
1005 static $functioncalled = false;
1006 if ($functioncalled && $onlyifnotcalledbefore) {
1007 // we have already output the content footer
1008 return '';
1009 }
1010 $functioncalled = true;
1011 require_once($CFG->dirroot.'/course/lib.php');
1012 $courseformat = course_get_format($this->page->course);
1013 if (($obj = $courseformat->course_content_footer()) !== null) {
06a72e01 1014 return html_writer::div($courseformat->get_renderer($this->page)->render($obj), 'course-content-footer');
fdd4b9a5
MG
1015 }
1016 return '';
1017 }
1018
1019 /**
1020 * Returns course-specific information to be output on any course page in the header area
1021 * (for the current course)
1022 *
1023 * @return string
1024 */
1025 public function course_header() {
1026 global $CFG;
1027 if ($this->page->course->id == SITEID) {
1028 // return immediately and do not include /course/lib.php if not necessary
1029 return '';
1030 }
1031 require_once($CFG->dirroot.'/course/lib.php');
1032 $courseformat = course_get_format($this->page->course);
1033 if (($obj = $courseformat->course_header()) !== null) {
1034 return $courseformat->get_renderer($this->page)->render($obj);
1035 }
1036 return '';
1037 }
1038
1039 /**
1040 * Returns course-specific information to be output on any course page in the footer area
1041 * (for the current course)
1042 *
1043 * @return string
1044 */
1045 public function course_footer() {
1046 global $CFG;
1047 if ($this->page->course->id == SITEID) {
1048 // return immediately and do not include /course/lib.php if not necessary
1049 return '';
1050 }
1051 require_once($CFG->dirroot.'/course/lib.php');
1052 $courseformat = course_get_format($this->page->course);
1053 if (($obj = $courseformat->course_footer()) !== null) {
1054 return $courseformat->get_renderer($this->page)->render($obj);
1055 }
1056 return '';
1057 }
1058
244a32c6
PS
1059 /**
1060 * Returns lang menu or '', this method also checks forcing of languages in courses.
7a3c215b 1061 *
2fada290
MG
1062 * This function calls {@link core_renderer::render_single_select()} to actually display the language menu.
1063 *
7a3c215b 1064 * @return string The lang menu HTML or empty string
244a32c6
PS
1065 */
1066 public function lang_menu() {
1067 global $CFG;
1068
1069 if (empty($CFG->langmenu)) {
1070 return '';
1071 }
1072
1073 if ($this->page->course != SITEID and !empty($this->page->course->lang)) {
1074 // do not show lang menu if language forced
1075 return '';
1076 }
1077
1078 $currlang = current_language();
1f96e907 1079 $langs = get_string_manager()->get_list_of_translations();
4bcc5118 1080
244a32c6
PS
1081 if (count($langs) < 2) {
1082 return '';
1083 }
1084
a9967cf5
PS
1085 $s = new single_select($this->page->url, 'lang', $langs, $currlang, null);
1086 $s->label = get_accesshide(get_string('language'));
1087 $s->class = 'langmenu';
1088 return $this->render($s);
244a32c6
PS
1089 }
1090
d9c8f425 1091 /**
1092 * Output the row of editing icons for a block, as defined by the controls array.
7a3c215b 1093 *
f8129210 1094 * @param array $controls an array like {@link block_contents::$controls}.
b59f2e3b 1095 * @param string $blockid The ID given to the block.
7a3c215b 1096 * @return string HTML fragment.
d9c8f425 1097 */
b59f2e3b 1098 public function block_controls($actions, $blockid = null) {
10fc1569 1099 global $CFG;
b59f2e3b
SH
1100 if (empty($actions)) {
1101 return '';
1102 }
cf69a00a 1103 $menu = new action_menu($actions);
b59f2e3b
SH
1104 if ($blockid !== null) {
1105 $menu->set_owner_selector('#'.$blockid);
1106 }
ae3fd8eb 1107 $menu->set_constraint('.block-region');
b59f2e3b 1108 $menu->attributes['class'] .= ' block-control-actions commands';
10fc1569
SH
1109 if (isset($CFG->blockeditingmenu) && !$CFG->blockeditingmenu) {
1110 $menu->do_not_enhance();
1111 }
b59f2e3b
SH
1112 return $this->render($menu);
1113 }
1114
1115 /**
1116 * Renders an action menu component.
1117 *
3665af78
SH
1118 * ARIA references:
1119 * - http://www.w3.org/WAI/GL/wiki/Using_ARIA_menus
1120 * - http://stackoverflow.com/questions/12279113/recommended-wai-aria-implementation-for-navigation-bar-menu
1121 *
b59f2e3b
SH
1122 * @param action_menu $menu
1123 * @return string HTML
1124 */
1125 public function render_action_menu(action_menu $menu) {
1126 $menu->initialise_js($this->page);
1127
1128 $output = html_writer::start_tag('div', $menu->attributes);
3665af78 1129 $output .= html_writer::start_tag('ul', $menu->attributesprimary);
b59f2e3b
SH
1130 foreach ($menu->get_primary_actions($this) as $action) {
1131 if ($action instanceof renderable) {
3665af78 1132 $content = $this->render($action);
b59f2e3b 1133 } else {
3665af78 1134 $content = $action;
e282c679 1135 }
11dd4dad 1136 $output .= html_writer::tag('li', $content, array('role' => 'presentation'));
d9c8f425 1137 }
3665af78
SH
1138 $output .= html_writer::end_tag('ul');
1139 $output .= html_writer::start_tag('ul', $menu->attributessecondary);
b59f2e3b 1140 foreach ($menu->get_secondary_actions() as $action) {
3665af78
SH
1141 if ($action instanceof renderable) {
1142 $content = $this->render($action);
3665af78
SH
1143 } else {
1144 $content = $action;
3665af78 1145 }
11dd4dad 1146 $output .= html_writer::tag('li', $content, array('role' => 'presentation'));
b59f2e3b 1147 }
3665af78 1148 $output .= html_writer::end_tag('ul');
b59f2e3b 1149 $output .= html_writer::end_tag('div');
e282c679 1150 return $output;
d9c8f425 1151 }
1152
cf69a00a 1153 /**
3665af78 1154 * Renders an action_menu_link item.
cf69a00a 1155 *
3665af78 1156 * @param action_menu_link $action
cf69a00a
SH
1157 * @return string HTML fragment
1158 */
3665af78 1159 protected function render_action_menu_link(action_menu_link $action) {
d0647301
SH
1160 static $actioncount = 0;
1161 $actioncount++;
cf69a00a 1162
ea5a01fb 1163 $comparetoalt = '';
cf69a00a 1164 $text = '';
ea5a01fb 1165 if (!$action->icon || $action->primary === false) {
d0647301 1166 $text .= html_writer::start_tag('span', array('class'=>'menu-action-text', 'id' => 'actionmenuaction-'.$actioncount));
cf69a00a
SH
1167 if ($action->text instanceof renderable) {
1168 $text .= $this->render($action->text);
1169 } else {
1170 $text .= $action->text;
cfd1e08a 1171 $comparetoalt = (string)$action->text;
cf69a00a
SH
1172 }
1173 $text .= html_writer::end_tag('span');
1174 }
1175
ea5a01fb
SH
1176 $icon = '';
1177 if ($action->icon) {
1178 $icon = $action->icon;
f803ce26 1179 if ($action->primary || !$action->actionmenu->will_be_enhanced()) {
ea5a01fb
SH
1180 $action->attributes['title'] = $action->text;
1181 }
cfd1e08a
SH
1182 if (!$action->primary && $action->actionmenu->will_be_enhanced()) {
1183 if ((string)$icon->attributes['alt'] === $comparetoalt) {
d0647301 1184 $icon->attributes['alt'] = '';
cfd1e08a 1185 }
c7a2291f 1186 if (isset($icon->attributes['title']) && (string)$icon->attributes['title'] === $comparetoalt) {
cfd1e08a
SH
1187 unset($icon->attributes['title']);
1188 }
1189 }
ea5a01fb
SH
1190 $icon = $this->render($icon);
1191 }
1192
3665af78 1193 // A disabled link is rendered as formatted text.
cf69a00a 1194 if (!empty($action->attributes['disabled'])) {
3665af78 1195 // Do not use div here due to nesting restriction in xhtml strict.
ea5a01fb 1196 return html_writer::tag('span', $icon.$text, array('class'=>'currentlink', 'role' => 'menuitem'));
cf69a00a
SH
1197 }
1198
1199 $attributes = $action->attributes;
1200 unset($action->attributes['disabled']);
1201 $attributes['href'] = $action->url;
d0647301
SH
1202 if ($text !== '') {
1203 $attributes['aria-labelledby'] = 'actionmenuaction-'.$actioncount;
1204 }
cf69a00a 1205
ea5a01fb 1206 return html_writer::tag('a', $icon.$text, $attributes);
cf69a00a
SH
1207 }
1208
9ac099a1
AN
1209 /**
1210 * Renders a primary action_menu_filler item.
1211 *
1212 * @param action_menu_link_filler $action
1213 * @return string HTML fragment
1214 */
1215 protected function render_action_menu_filler(action_menu_filler $action) {
1216 return html_writer::span('&nbsp;', 'filler');
1217 }
1218
cf69a00a 1219 /**
3665af78 1220 * Renders a primary action_menu_link item.
cf69a00a 1221 *
3665af78 1222 * @param action_menu_link_primary $action
cf69a00a
SH
1223 * @return string HTML fragment
1224 */
3665af78
SH
1225 protected function render_action_menu_link_primary(action_menu_link_primary $action) {
1226 return $this->render_action_menu_link($action);
cf69a00a
SH
1227 }
1228
1229 /**
3665af78 1230 * Renders a secondary action_menu_link item.
cf69a00a 1231 *
3665af78 1232 * @param action_menu_link_secondary $action
cf69a00a
SH
1233 * @return string HTML fragment
1234 */
3665af78
SH
1235 protected function render_action_menu_link_secondary(action_menu_link_secondary $action) {
1236 return $this->render_action_menu_link($action);
cf69a00a
SH
1237 }
1238
d9c8f425 1239 /**
1240 * Prints a nice side block with an optional header.
1241 *
1242 * The content is described
f8129210 1243 * by a {@link core_renderer::block_contents} object.
d9c8f425 1244 *
cbb54cce
SH
1245 * <div id="inst{$instanceid}" class="block_{$blockname} block">
1246 * <div class="header"></div>
1247 * <div class="content">
1248 * ...CONTENT...
1249 * <div class="footer">
1250 * </div>
1251 * </div>
1252 * <div class="annotation">
1253 * </div>
1254 * </div>
1255 *
d9c8f425 1256 * @param block_contents $bc HTML for the content
1257 * @param string $region the region the block is appearing in.
1258 * @return string the HTML to be output.
1259 */
7a3c215b 1260 public function block(block_contents $bc, $region) {
d9c8f425 1261 $bc = clone($bc); // Avoid messing up the object passed in.
dd72b308
PS
1262 if (empty($bc->blockinstanceid) || !strip_tags($bc->title)) {
1263 $bc->collapsible = block_contents::NOT_HIDEABLE;
1264 }
84192d78
SH
1265 if (!empty($bc->blockinstanceid)) {
1266 $bc->attributes['data-instanceid'] = $bc->blockinstanceid;
1267 }
91d941c3
SH
1268 $skiptitle = strip_tags($bc->title);
1269 if ($bc->blockinstanceid && !empty($skiptitle)) {
1270 $bc->attributes['aria-labelledby'] = 'instance-'.$bc->blockinstanceid.'-header';
1271 } else if (!empty($bc->arialabel)) {
1272 $bc->attributes['aria-label'] = $bc->arialabel;
1273 }
84192d78
SH
1274 if ($bc->dockable) {
1275 $bc->attributes['data-dockable'] = 1;
1276 }
dd72b308
PS
1277 if ($bc->collapsible == block_contents::HIDDEN) {
1278 $bc->add_class('hidden');
1279 }
1280 if (!empty($bc->controls)) {
1281 $bc->add_class('block_with_controls');
1282 }
d9c8f425 1283
91d941c3 1284
d9c8f425 1285 if (empty($skiptitle)) {
1286 $output = '';
1287 $skipdest = '';
1288 } else {
26acc814
PS
1289 $output = html_writer::tag('a', get_string('skipa', 'access', $skiptitle), array('href' => '#sb-' . $bc->skipid, 'class' => 'skip-block'));
1290 $skipdest = html_writer::tag('span', '', array('id' => 'sb-' . $bc->skipid, 'class' => 'skip-block-to'));
d9c8f425 1291 }
4d2ee4c2 1292
5d0c95a5 1293 $output .= html_writer::start_tag('div', $bc->attributes);
d9c8f425 1294
9f5c39b5
SH
1295 $output .= $this->block_header($bc);
1296 $output .= $this->block_content($bc);
1297
1298 $output .= html_writer::end_tag('div');
1299
1300 $output .= $this->block_annotation($bc);
1301
1302 $output .= $skipdest;
1303
1304 $this->init_block_hider_js($bc);
1305 return $output;
1306 }
1307
1308 /**
1309 * Produces a header for a block
fa7f2a45 1310 *
9f5c39b5
SH
1311 * @param block_contents $bc
1312 * @return string
1313 */
1314 protected function block_header(block_contents $bc) {
d9c8f425 1315
1316 $title = '';
1317 if ($bc->title) {
91d941c3
SH
1318 $attributes = array();
1319 if ($bc->blockinstanceid) {
1320 $attributes['id'] = 'instance-'.$bc->blockinstanceid.'-header';
1321 }
1322 $title = html_writer::tag('h2', $bc->title, $attributes);
d9c8f425 1323 }
1324
b59f2e3b
SH
1325 $blockid = null;
1326 if (isset($bc->attributes['id'])) {
1327 $blockid = $bc->attributes['id'];
1328 }
1329 $controlshtml = $this->block_controls($bc->controls, $blockid);
9f5c39b5
SH
1330
1331 $output = '';
d9c8f425 1332 if ($title || $controlshtml) {
46de77b6 1333 $output .= html_writer::tag('div', html_writer::tag('div', html_writer::tag('div', '', array('class'=>'block_action')). $title . $controlshtml, array('class' => 'title')), array('class' => 'header'));
d9c8f425 1334 }
9f5c39b5
SH
1335 return $output;
1336 }
d9c8f425 1337
9f5c39b5
SH
1338 /**
1339 * Produces the content area for a block
1340 *
1341 * @param block_contents $bc
1342 * @return string
1343 */
1344 protected function block_content(block_contents $bc) {
1345 $output = html_writer::start_tag('div', array('class' => 'content'));
1346 if (!$bc->title && !$this->block_controls($bc->controls)) {
46de77b6
SH
1347 $output .= html_writer::tag('div', '', array('class'=>'block_action notitle'));
1348 }
d9c8f425 1349 $output .= $bc->content;
9f5c39b5
SH
1350 $output .= $this->block_footer($bc);
1351 $output .= html_writer::end_tag('div');
fa7f2a45 1352
9f5c39b5
SH
1353 return $output;
1354 }
d9c8f425 1355
9f5c39b5
SH
1356 /**
1357 * Produces the footer for a block
1358 *
1359 * @param block_contents $bc
1360 * @return string
1361 */
1362 protected function block_footer(block_contents $bc) {
1363 $output = '';
d9c8f425 1364 if ($bc->footer) {
26acc814 1365 $output .= html_writer::tag('div', $bc->footer, array('class' => 'footer'));
d9c8f425 1366 }
9f5c39b5
SH
1367 return $output;
1368 }
d9c8f425 1369
9f5c39b5
SH
1370 /**
1371 * Produces the annotation for a block
1372 *
1373 * @param block_contents $bc
1374 * @return string
1375 */
1376 protected function block_annotation(block_contents $bc) {
1377 $output = '';
d9c8f425 1378 if ($bc->annotation) {
26acc814 1379 $output .= html_writer::tag('div', $bc->annotation, array('class' => 'blockannotation'));
d9c8f425 1380 }
d9c8f425 1381 return $output;
1382 }
1383
1384 /**
1385 * Calls the JS require function to hide a block.
7a3c215b 1386 *
d9c8f425 1387 * @param block_contents $bc A block_contents object
d9c8f425 1388 */
dd72b308
PS
1389 protected function init_block_hider_js(block_contents $bc) {
1390 if (!empty($bc->attributes['id']) and $bc->collapsible != block_contents::NOT_HIDEABLE) {
cbb54cce
SH
1391 $config = new stdClass;
1392 $config->id = $bc->attributes['id'];
1393 $config->title = strip_tags($bc->title);
1394 $config->preference = 'block' . $bc->blockinstanceid . 'hidden';
1395 $config->tooltipVisible = get_string('hideblocka', 'access', $config->title);
1396 $config->tooltipHidden = get_string('showblocka', 'access', $config->title);
1397
1398 $this->page->requires->js_init_call('M.util.init_block_hider', array($config));
1399 user_preference_allow_ajax_update($config->preference, PARAM_BOOL);
d9c8f425 1400 }
1401 }
1402
1403 /**
1404 * Render the contents of a block_list.
7a3c215b 1405 *
d9c8f425 1406 * @param array $icons the icon for each item.
1407 * @param array $items the content of each item.
1408 * @return string HTML
1409 */
1410 public function list_block_contents($icons, $items) {
1411 $row = 0;
1412 $lis = array();
1413 foreach ($items as $key => $string) {
5d0c95a5 1414 $item = html_writer::start_tag('li', array('class' => 'r' . $row));
2c5ec833 1415 if (!empty($icons[$key])) { //test if the content has an assigned icon
26acc814 1416 $item .= html_writer::tag('div', $icons[$key], array('class' => 'icon column c0'));
d9c8f425 1417 }
26acc814 1418 $item .= html_writer::tag('div', $string, array('class' => 'column c1'));
5d0c95a5 1419 $item .= html_writer::end_tag('li');
d9c8f425 1420 $lis[] = $item;
1421 $row = 1 - $row; // Flip even/odd.
1422 }
f2a04fc1 1423 return html_writer::tag('ul', implode("\n", $lis), array('class' => 'unlist'));
d9c8f425 1424 }
1425
1426 /**
1427 * Output all the blocks in a particular region.
7a3c215b 1428 *
d9c8f425 1429 * @param string $region the name of a region on this page.
1430 * @return string the HTML to be output.
1431 */
1432 public function blocks_for_region($region) {
1433 $blockcontents = $this->page->blocks->get_content_for_region($region, $this);
6671fa73
JF
1434 $blocks = $this->page->blocks->get_blocks_for_region($region);
1435 $lastblock = null;
1436 $zones = array();
1437 foreach ($blocks as $block) {
1438 $zones[] = $block->title;
1439 }
d9c8f425 1440 $output = '';
6671fa73 1441
d9c8f425 1442 foreach ($blockcontents as $bc) {
1443 if ($bc instanceof block_contents) {
1444 $output .= $this->block($bc, $region);
6671fa73 1445 $lastblock = $bc->title;
d9c8f425 1446 } else if ($bc instanceof block_move_target) {
686e3b3a 1447 $output .= $this->block_move_target($bc, $zones, $lastblock, $region);
d9c8f425 1448 } else {
1449 throw new coding_exception('Unexpected type of thing (' . get_class($bc) . ') found in list of block contents.');
1450 }
1451 }
1452 return $output;
1453 }
1454
1455 /**
1456 * Output a place where the block that is currently being moved can be dropped.
7a3c215b 1457 *
d9c8f425 1458 * @param block_move_target $target with the necessary details.
6671fa73
JF
1459 * @param array $zones array of areas where the block can be moved to
1460 * @param string $previous the block located before the area currently being rendered.
686e3b3a 1461 * @param string $region the name of the region
d9c8f425 1462 * @return string the HTML to be output.
1463 */
686e3b3a 1464 public function block_move_target($target, $zones, $previous, $region) {
0e2ca62e 1465 if ($previous == null) {
686e3b3a
FM
1466 if (empty($zones)) {
1467 // There are no zones, probably because there are no blocks.
1468 $regions = $this->page->theme->get_all_block_regions();
1469 $position = get_string('moveblockinregion', 'block', $regions[$region]);
1470 } else {
1471 $position = get_string('moveblockbefore', 'block', $zones[0]);
1472 }
6671fa73
JF
1473 } else {
1474 $position = get_string('moveblockafter', 'block', $previous);
1475 }
1476 return html_writer::tag('a', html_writer::tag('span', $position, array('class' => 'accesshide')), array('href' => $target->url, 'class' => 'blockmovetarget'));
d9c8f425 1477 }
1478
574fbea4 1479 /**
996b1e0c 1480 * Renders a special html link with attached action
574fbea4 1481 *
2fada290
MG
1482 * Theme developers: DO NOT OVERRIDE! Please override function
1483 * {@link core_renderer::render_action_link()} instead.
1484 *
574fbea4
PS
1485 * @param string|moodle_url $url
1486 * @param string $text HTML fragment
1487 * @param component_action $action
11820bac 1488 * @param array $attributes associative array of html link attributes + disabled
e282c679 1489 * @param pix_icon optional pix icon to render with the link
7a3c215b 1490 * @return string HTML fragment
574fbea4 1491 */
e282c679 1492 public function action_link($url, $text, component_action $action = null, array $attributes = null, $icon = null) {
574fbea4
PS
1493 if (!($url instanceof moodle_url)) {
1494 $url = new moodle_url($url);
1495 }
e282c679 1496 $link = new action_link($url, $text, $action, $attributes, $icon);
574fbea4 1497
f14b641b 1498 return $this->render($link);
574fbea4
PS
1499 }
1500
1501 /**
7a3c215b
SH
1502 * Renders an action_link object.
1503 *
1504 * The provided link is renderer and the HTML returned. At the same time the
f8129210 1505 * associated actions are setup in JS by {@link core_renderer::add_action_handler()}
7a3c215b 1506 *
574fbea4
PS
1507 * @param action_link $link
1508 * @return string HTML fragment
1509 */
1510 protected function render_action_link(action_link $link) {
1511 global $CFG;
1512
e282c679
SH
1513 $text = '';
1514 if ($link->icon) {
1515 $text .= $this->render($link->icon);
1516 }
1517
7749e187 1518 if ($link->text instanceof renderable) {
e282c679 1519 $text .= $this->render($link->text);
7749e187 1520 } else {
e282c679 1521 $text .= $link->text;
7749e187
SH
1522 }
1523
574fbea4
PS
1524 // A disabled link is rendered as formatted text
1525 if (!empty($link->attributes['disabled'])) {
1526 // do not use div here due to nesting restriction in xhtml strict
7749e187 1527 return html_writer::tag('span', $text, array('class'=>'currentlink'));
574fbea4 1528 }
11820bac 1529
574fbea4
PS
1530 $attributes = $link->attributes;
1531 unset($link->attributes['disabled']);
1532 $attributes['href'] = $link->url;
1533
1534 if ($link->actions) {
f14b641b 1535 if (empty($attributes['id'])) {
574fbea4
PS
1536 $id = html_writer::random_id('action_link');
1537 $attributes['id'] = $id;
1538 } else {
1539 $id = $attributes['id'];
1540 }
1541 foreach ($link->actions as $action) {
c80877aa 1542 $this->add_action_handler($action, $id);
574fbea4
PS
1543 }
1544 }
1545
7749e187 1546 return html_writer::tag('a', $text, $attributes);
574fbea4
PS
1547 }
1548
c63923bd
PS
1549
1550 /**
7a3c215b
SH
1551 * Renders an action_icon.
1552 *
f8129210 1553 * This function uses the {@link core_renderer::action_link()} method for the
7a3c215b
SH
1554 * most part. What it does different is prepare the icon as HTML and use it
1555 * as the link text.
c63923bd 1556 *
2fada290
MG
1557 * Theme developers: If you want to change how action links and/or icons are rendered,
1558 * consider overriding function {@link core_renderer::render_action_link()} and
1559 * {@link core_renderer::render_pix_icon()}.
1560 *
c63923bd
PS
1561 * @param string|moodle_url $url A string URL or moodel_url
1562 * @param pix_icon $pixicon
1563 * @param component_action $action
1564 * @param array $attributes associative array of html link attributes + disabled
1565 * @param bool $linktext show title next to image in link
1566 * @return string HTML fragment
1567 */
1568 public function action_icon($url, pix_icon $pixicon, component_action $action = null, array $attributes = null, $linktext=false) {
1569 if (!($url instanceof moodle_url)) {
1570 $url = new moodle_url($url);
1571 }
1572 $attributes = (array)$attributes;
1573
524645e7 1574 if (empty($attributes['class'])) {
c63923bd
PS
1575 // let ppl override the class via $options
1576 $attributes['class'] = 'action-icon';
1577 }
1578
1579 $icon = $this->render($pixicon);
1580
1581 if ($linktext) {
1582 $text = $pixicon->attributes['alt'];
1583 } else {
1584 $text = '';
1585 }
1586
1587 return $this->action_link($url, $text.$icon, $action, $attributes);
1588 }
1589
d9c8f425 1590 /**
0b634d75 1591 * Print a message along with button choices for Continue/Cancel
1592 *
4ed85790 1593 * If a string or moodle_url is given instead of a single_button, method defaults to post.
0b634d75 1594 *
d9c8f425 1595 * @param string $message The question to ask the user
3ba60ee1
PS
1596 * @param single_button|moodle_url|string $continue The single_button component representing the Continue answer. Can also be a moodle_url or string URL
1597 * @param single_button|moodle_url|string $cancel The single_button component representing the Cancel answer. Can also be a moodle_url or string URL
d9c8f425 1598 * @return string HTML fragment
1599 */
1600 public function confirm($message, $continue, $cancel) {
4871a238 1601 if ($continue instanceof single_button) {
11820bac 1602 // ok
4871a238
PS
1603 } else if (is_string($continue)) {
1604 $continue = new single_button(new moodle_url($continue), get_string('continue'), 'post');
1605 } else if ($continue instanceof moodle_url) {
26eab8d4 1606 $continue = new single_button($continue, get_string('continue'), 'post');
d9c8f425 1607 } else {
4ed85790 1608 throw new coding_exception('The continue param to $OUTPUT->confirm() must be either a URL (string/moodle_url) or a single_button instance.');
d9c8f425 1609 }
1610
4871a238 1611 if ($cancel instanceof single_button) {
11820bac 1612 // ok
4871a238
PS
1613 } else if (is_string($cancel)) {
1614 $cancel = new single_button(new moodle_url($cancel), get_string('cancel'), 'get');
1615 } else if ($cancel instanceof moodle_url) {
26eab8d4 1616 $cancel = new single_button($cancel, get_string('cancel'), 'get');
d9c8f425 1617 } else {
4ed85790 1618 throw new coding_exception('The cancel param to $OUTPUT->confirm() must be either a URL (string/moodle_url) or a single_button instance.');
d9c8f425 1619 }
1620
d9c8f425 1621 $output = $this->box_start('generalbox', 'notice');
26acc814
PS
1622 $output .= html_writer::tag('p', $message);
1623 $output .= html_writer::tag('div', $this->render($continue) . $this->render($cancel), array('class' => 'buttons'));
d9c8f425 1624 $output .= $this->box_end();
1625 return $output;
1626 }
1627
3cd5305f 1628 /**
3ba60ee1 1629 * Returns a form with a single button.
3cd5305f 1630 *
2fada290
MG
1631 * Theme developers: DO NOT OVERRIDE! Please override function
1632 * {@link core_renderer::render_single_button()} instead.
1633 *
3ba60ee1 1634 * @param string|moodle_url $url
3cd5305f
PS
1635 * @param string $label button text
1636 * @param string $method get or post submit method
3ba60ee1 1637 * @param array $options associative array {disabled, title, etc.}
3cd5305f
PS
1638 * @return string HTML fragment
1639 */
3ba60ee1 1640 public function single_button($url, $label, $method='post', array $options=null) {
574fbea4
PS
1641 if (!($url instanceof moodle_url)) {
1642 $url = new moodle_url($url);
3ba60ee1 1643 }
574fbea4
PS
1644 $button = new single_button($url, $label, $method);
1645
3ba60ee1
PS
1646 foreach ((array)$options as $key=>$value) {
1647 if (array_key_exists($key, $button)) {
1648 $button->$key = $value;
1649 }
3cd5305f
PS
1650 }
1651
3ba60ee1 1652 return $this->render($button);
3cd5305f
PS
1653 }
1654
d9c8f425 1655 /**
7a3c215b
SH
1656 * Renders a single button widget.
1657 *
1658 * This will return HTML to display a form containing a single button.
1659 *
3ba60ee1 1660 * @param single_button $button
d9c8f425 1661 * @return string HTML fragment
1662 */
3ba60ee1
PS
1663 protected function render_single_button(single_button $button) {
1664 $attributes = array('type' => 'submit',
1665 'value' => $button->label,
db09524d 1666 'disabled' => $button->disabled ? 'disabled' : null,
3ba60ee1
PS
1667 'title' => $button->tooltip);
1668
1669 if ($button->actions) {
1670 $id = html_writer::random_id('single_button');
1671 $attributes['id'] = $id;
1672 foreach ($button->actions as $action) {
c80877aa 1673 $this->add_action_handler($action, $id);
3ba60ee1 1674 }
d9c8f425 1675 }
d9c8f425 1676
3ba60ee1
PS
1677 // first the input element
1678 $output = html_writer::empty_tag('input', $attributes);
d9c8f425 1679
3ba60ee1
PS
1680 // then hidden fields
1681 $params = $button->url->params();
1682 if ($button->method === 'post') {
1683 $params['sesskey'] = sesskey();
1684 }
1685 foreach ($params as $var => $val) {
1686 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $var, 'value' => $val));
1687 }
d9c8f425 1688
3ba60ee1 1689 // then div wrapper for xhtml strictness
26acc814 1690 $output = html_writer::tag('div', $output);
d9c8f425 1691
3ba60ee1 1692 // now the form itself around it
a12cd69c
DM
1693 if ($button->method === 'get') {
1694 $url = $button->url->out_omit_querystring(true); // url without params, the anchor part allowed
1695 } else {
1696 $url = $button->url->out_omit_querystring(); // url without params, the anchor part not allowed
1697 }
a6855934
PS
1698 if ($url === '') {
1699 $url = '#'; // there has to be always some action
1700 }
3ba60ee1 1701 $attributes = array('method' => $button->method,
a6855934 1702 'action' => $url,
3ba60ee1 1703 'id' => $button->formid);
26acc814 1704 $output = html_writer::tag('form', $output, $attributes);
d9c8f425 1705
3ba60ee1 1706 // and finally one more wrapper with class
26acc814 1707 return html_writer::tag('div', $output, array('class' => $button->class));
d9c8f425 1708 }
1709
a9967cf5 1710 /**
ab08be98 1711 * Returns a form with a single select widget.
7a3c215b 1712 *
2fada290
MG
1713 * Theme developers: DO NOT OVERRIDE! Please override function
1714 * {@link core_renderer::render_single_select()} instead.
1715 *
a9967cf5
PS
1716 * @param moodle_url $url form action target, includes hidden fields
1717 * @param string $name name of selection field - the changing parameter in url
1718 * @param array $options list of options
1719 * @param string $selected selected element
1720 * @param array $nothing
f8dab966 1721 * @param string $formid
a9967cf5
PS
1722 * @return string HTML fragment
1723 */
7a3c215b 1724 public function single_select($url, $name, array $options, $selected = '', $nothing = array('' => 'choosedots'), $formid = null) {
a9967cf5
PS
1725 if (!($url instanceof moodle_url)) {
1726 $url = new moodle_url($url);
1727 }
f8dab966 1728 $select = new single_select($url, $name, $options, $selected, $nothing, $formid);
a9967cf5
PS
1729
1730 return $this->render($select);
1731 }
1732
1733 /**
1734 * Internal implementation of single_select rendering
7a3c215b 1735 *
a9967cf5
PS
1736 * @param single_select $select
1737 * @return string HTML fragment
1738 */
1739 protected function render_single_select(single_select $select) {
1740 $select = clone($select);
1741 if (empty($select->formid)) {
1742 $select->formid = html_writer::random_id('single_select_f');
1743 }
1744
1745 $output = '';
1746 $params = $select->url->params();
1747 if ($select->method === 'post') {
1748 $params['sesskey'] = sesskey();
1749 }
1750 foreach ($params as $name=>$value) {
1751 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$name, 'value'=>$value));
1752 }
1753
1754 if (empty($select->attributes['id'])) {
1755 $select->attributes['id'] = html_writer::random_id('single_select');
1756 }
1757
0b2cb132
PS
1758 if ($select->disabled) {
1759 $select->attributes['disabled'] = 'disabled';
1760 }
4d10e579 1761
a9967cf5
PS
1762 if ($select->tooltip) {
1763 $select->attributes['title'] = $select->tooltip;
1764 }
1765
7266bd3e
ARN
1766 $select->attributes['class'] = 'autosubmit';
1767 if ($select->class) {
1768 $select->attributes['class'] .= ' ' . $select->class;
1769 }
1770
a9967cf5 1771 if ($select->label) {
ecc5cc31 1772 $output .= html_writer::label($select->label, $select->attributes['id'], false, $select->labelattributes);
a9967cf5
PS
1773 }
1774
1775 if ($select->helpicon instanceof help_icon) {
1776 $output .= $this->render($select->helpicon);
1777 }
a9967cf5
PS
1778 $output .= html_writer::select($select->options, $select->name, $select->selected, $select->nothing, $select->attributes);
1779
1780 $go = html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('go')));
ebc583e4 1781 $output .= html_writer::tag('noscript', html_writer::tag('div', $go), array('class' => 'inline'));
a9967cf5
PS
1782
1783 $nothing = empty($select->nothing) ? false : key($select->nothing);
7266bd3e
ARN
1784 $this->page->requires->yui_module('moodle-core-formautosubmit',
1785 'M.core.init_formautosubmit',
1786 array(array('selectid' => $select->attributes['id'], 'nothing' => $nothing))
1787 );
a9967cf5
PS
1788
1789 // then div wrapper for xhtml strictness
26acc814 1790 $output = html_writer::tag('div', $output);
a9967cf5
PS
1791
1792 // now the form itself around it
a12cd69c
DM
1793 if ($select->method === 'get') {
1794 $url = $select->url->out_omit_querystring(true); // url without params, the anchor part allowed
1795 } else {
1796 $url = $select->url->out_omit_querystring(); // url without params, the anchor part not allowed
1797 }
a9967cf5 1798 $formattributes = array('method' => $select->method,
a12cd69c 1799 'action' => $url,
a9967cf5 1800 'id' => $select->formid);
26acc814 1801 $output = html_writer::tag('form', $output, $formattributes);
4d10e579
PS
1802
1803 // and finally one more wrapper with class
26acc814 1804 return html_writer::tag('div', $output, array('class' => $select->class));
4d10e579
PS
1805 }
1806
1807 /**
ab08be98 1808 * Returns a form with a url select widget.
7a3c215b 1809 *
2fada290
MG
1810 * Theme developers: DO NOT OVERRIDE! Please override function
1811 * {@link core_renderer::render_url_select()} instead.
1812 *
4d10e579
PS
1813 * @param array $urls list of urls - array('/course/view.php?id=1'=>'Frontpage', ....)
1814 * @param string $selected selected element
1815 * @param array $nothing
1816 * @param string $formid
1817 * @return string HTML fragment
1818 */
7a3c215b 1819 public function url_select(array $urls, $selected, $nothing = array('' => 'choosedots'), $formid = null) {
4d10e579
PS
1820 $select = new url_select($urls, $selected, $nothing, $formid);
1821 return $this->render($select);
1822 }
1823
1824 /**
ab08be98 1825 * Internal implementation of url_select rendering
7a3c215b
SH
1826 *
1827 * @param url_select $select
4d10e579
PS
1828 * @return string HTML fragment
1829 */
1830 protected function render_url_select(url_select $select) {
c422efcf
PS
1831 global $CFG;
1832
4d10e579
PS
1833 $select = clone($select);
1834 if (empty($select->formid)) {
1835 $select->formid = html_writer::random_id('url_select_f');
1836 }
1837
1838 if (empty($select->attributes['id'])) {
1839 $select->attributes['id'] = html_writer::random_id('url_select');
1840 }
1841
1842 if ($select->disabled) {
1843 $select->attributes['disabled'] = 'disabled';
1844 }
1845
1846 if ($select->tooltip) {
1847 $select->attributes['title'] = $select->tooltip;
1848 }
1849
1850 $output = '';
1851
1852 if ($select->label) {
ecc5cc31 1853 $output .= html_writer::label($select->label, $select->attributes['id'], false, $select->labelattributes);
4d10e579
PS
1854 }
1855
50d6ad84
ARN
1856 $classes = array();
1857 if (!$select->showbutton) {
1858 $classes[] = 'autosubmit';
1859 }
7266bd3e 1860 if ($select->class) {
50d6ad84
ARN
1861 $classes[] = $select->class;
1862 }
1863 if (count($classes)) {
1864 $select->attributes['class'] = implode(' ', $classes);
7266bd3e
ARN
1865 }
1866
4d10e579
PS
1867 if ($select->helpicon instanceof help_icon) {
1868 $output .= $this->render($select->helpicon);
1869 }
1870
d4dcfc6b
DM
1871 // For security reasons, the script course/jumpto.php requires URL starting with '/'. To keep
1872 // backward compatibility, we are removing heading $CFG->wwwroot from URLs here.
c422efcf
PS
1873 $urls = array();
1874 foreach ($select->urls as $k=>$v) {
d4dcfc6b
DM
1875 if (is_array($v)) {
1876 // optgroup structure
1877 foreach ($v as $optgrouptitle => $optgroupoptions) {
1878 foreach ($optgroupoptions as $optionurl => $optiontitle) {
1879 if (empty($optionurl)) {
1880 $safeoptionurl = '';
1881 } else if (strpos($optionurl, $CFG->wwwroot.'/') === 0) {
1882 // debugging('URLs passed to url_select should be in local relative form - please fix the code.', DEBUG_DEVELOPER);
1883 $safeoptionurl = str_replace($CFG->wwwroot, '', $optionurl);
1884 } else if (strpos($optionurl, '/') !== 0) {
1885 debugging("Invalid url_select urls parameter inside optgroup: url '$optionurl' is not local relative url!");
1886 continue;
1887 } else {
1888 $safeoptionurl = $optionurl;
1889 }
1890 $urls[$k][$optgrouptitle][$safeoptionurl] = $optiontitle;
1891 }
1892 }
1893 } else {
1894 // plain list structure
1895 if (empty($k)) {
1896 // nothing selected option
1897 } else if (strpos($k, $CFG->wwwroot.'/') === 0) {
1898 $k = str_replace($CFG->wwwroot, '', $k);
1899 } else if (strpos($k, '/') !== 0) {
1900 debugging("Invalid url_select urls parameter: url '$k' is not local relative url!");
1901 continue;
1902 }
1903 $urls[$k] = $v;
1904 }
1905 }
1906 $selected = $select->selected;
1907 if (!empty($selected)) {
1908 if (strpos($select->selected, $CFG->wwwroot.'/') === 0) {
1909 $selected = str_replace($CFG->wwwroot, '', $selected);
1910 } else if (strpos($selected, '/') !== 0) {
1911 debugging("Invalid value of parameter 'selected': url '$selected' is not local relative url!");
c422efcf 1912 }
c422efcf
PS
1913 }
1914
4d10e579 1915 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
d4dcfc6b 1916 $output .= html_writer::select($urls, 'jump', $selected, $select->nothing, $select->attributes);
4d10e579 1917
15e48a1a
SM
1918 if (!$select->showbutton) {
1919 $go = html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('go')));
ebc583e4 1920 $output .= html_writer::tag('noscript', html_writer::tag('div', $go), array('class' => 'inline'));
15e48a1a 1921 $nothing = empty($select->nothing) ? false : key($select->nothing);
7266bd3e
ARN
1922 $this->page->requires->yui_module('moodle-core-formautosubmit',
1923 'M.core.init_formautosubmit',
1924 array(array('selectid' => $select->attributes['id'], 'nothing' => $nothing))
1925 );
15e48a1a
SM
1926 } else {
1927 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>$select->showbutton));
1928 }
4d10e579
PS
1929
1930 // then div wrapper for xhtml strictness
26acc814 1931 $output = html_writer::tag('div', $output);
4d10e579
PS
1932
1933 // now the form itself around it
1934 $formattributes = array('method' => 'post',
1935 'action' => new moodle_url('/course/jumpto.php'),
1936 'id' => $select->formid);
26acc814 1937 $output = html_writer::tag('form', $output, $formattributes);
a9967cf5
PS
1938
1939 // and finally one more wrapper with class
26acc814 1940 return html_writer::tag('div', $output, array('class' => $select->class));
a9967cf5
PS
1941 }
1942
d9c8f425 1943 /**
1944 * Returns a string containing a link to the user documentation.
1945 * Also contains an icon by default. Shown to teachers and admin only.
7a3c215b 1946 *
d9c8f425 1947 * @param string $path The page link after doc root and language, no leading slash.
1948 * @param string $text The text to be displayed for the link
afe3566c 1949 * @param boolean $forcepopup Whether to force a popup regardless of the value of $CFG->doctonewwindow
996b1e0c 1950 * @return string
d9c8f425 1951 */
afe3566c 1952 public function doc_link($path, $text = '', $forcepopup = false) {
8ae8bf8a
PS
1953 global $CFG;
1954
c22fbd38 1955 $icon = $this->pix_icon('docs', $text, 'moodle', array('class'=>'iconhelp icon-pre'));
8ae8bf8a 1956
000c278c 1957 $url = new moodle_url(get_docs_url($path));
8ae8bf8a 1958
c80877aa 1959 $attributes = array('href'=>$url);
afe3566c
ARN
1960 if (!empty($CFG->doctonewwindow) || $forcepopup) {
1961 $attributes['class'] = 'helplinkpopup';
d9c8f425 1962 }
1adaa404 1963
26acc814 1964 return html_writer::tag('a', $icon.$text, $attributes);
d9c8f425 1965 }
1966
000c278c 1967 /**
7a3c215b
SH
1968 * Return HTML for a pix_icon.
1969 *
2fada290
MG
1970 * Theme developers: DO NOT OVERRIDE! Please override function
1971 * {@link core_renderer::render_pix_icon()} instead.
1972 *
000c278c
PS
1973 * @param string $pix short pix name
1974 * @param string $alt mandatory alt attribute
eb557002 1975 * @param string $component standard compoennt name like 'moodle', 'mod_forum', etc.
000c278c
PS
1976 * @param array $attributes htm lattributes
1977 * @return string HTML fragment
1978 */
1979 public function pix_icon($pix, $alt, $component='moodle', array $attributes = null) {
1980 $icon = new pix_icon($pix, $alt, $component, $attributes);
1981 return $this->render($icon);
1982 }
1983
1984 /**
7a3c215b
SH
1985 * Renders a pix_icon widget and returns the HTML to display it.
1986 *
000c278c
PS
1987 * @param pix_icon $icon
1988 * @return string HTML fragment
1989 */
ce0110bf 1990 protected function render_pix_icon(pix_icon $icon) {
000c278c
PS
1991 $attributes = $icon->attributes;
1992 $attributes['src'] = $this->pix_url($icon->pix, $icon->component);
c80877aa 1993 return html_writer::empty_tag('img', $attributes);
000c278c
PS
1994 }
1995
d63c5073 1996 /**
7a3c215b
SH
1997 * Return HTML to display an emoticon icon.
1998 *
d63c5073
DM
1999 * @param pix_emoticon $emoticon
2000 * @return string HTML fragment
2001 */
2002 protected function render_pix_emoticon(pix_emoticon $emoticon) {
2003 $attributes = $emoticon->attributes;
2004 $attributes['src'] = $this->pix_url($emoticon->pix, $emoticon->component);
2005 return html_writer::empty_tag('img', $attributes);
2006 }
2007
a09aeee4 2008 /**
7a3c215b
SH
2009 * Produces the html that represents this rating in the UI
2010 *
2011 * @param rating $rating the page object on which this rating will appear
2012 * @return string
2013 */
a09aeee4 2014 function render_rating(rating $rating) {
7ac928a7 2015 global $CFG, $USER;
a09aeee4 2016
2b04c41c 2017 if ($rating->settings->aggregationmethod == RATING_AGGREGATE_NONE) {
63e87951
AD
2018 return null;//ratings are turned off
2019 }
2020
2b04c41c
SH
2021 $ratingmanager = new rating_manager();
2022 // Initialise the JavaScript so ratings can be done by AJAX.
2023 $ratingmanager->initialise_rating_javascript($this->page);
a09aeee4 2024
63e87951
AD
2025 $strrate = get_string("rate", "rating");
2026 $ratinghtml = ''; //the string we'll return
2027
2b04c41c
SH
2028 // permissions check - can they view the aggregate?
2029 if ($rating->user_can_view_aggregate()) {
a09aeee4 2030
2b04c41c
SH
2031 $aggregatelabel = $ratingmanager->get_aggregate_label($rating->settings->aggregationmethod);
2032 $aggregatestr = $rating->get_aggregate_string();
a09aeee4 2033
6278ce45 2034 $aggregatehtml = html_writer::tag('span', $aggregatestr, array('id' => 'ratingaggregate'.$rating->itemid, 'class' => 'ratingaggregate')).' ';
2b04c41c 2035 if ($rating->count > 0) {
6278ce45 2036 $countstr = "({$rating->count})";
d251b259 2037 } else {
6278ce45 2038 $countstr = '-';
d251b259 2039 }
6278ce45 2040 $aggregatehtml .= html_writer::tag('span', $countstr, array('id'=>"ratingcount{$rating->itemid}", 'class' => 'ratingcount')).' ';
63e87951 2041
c6de9cef 2042 $ratinghtml .= html_writer::tag('span', $aggregatelabel, array('class'=>'rating-aggregate-label'));
d251b259 2043 if ($rating->settings->permissions->viewall && $rating->settings->pluginpermissions->viewall) {
2b04c41c
SH
2044
2045 $nonpopuplink = $rating->get_view_ratings_url();
2046 $popuplink = $rating->get_view_ratings_url(true);
a09aeee4 2047
d251b259 2048 $action = new popup_action('click', $popuplink, 'ratings', array('height' => 400, 'width' => 600));
c6de9cef 2049 $ratinghtml .= $this->action_link($nonpopuplink, $aggregatehtml, $action);
d251b259 2050 } else {
c6de9cef 2051 $ratinghtml .= $aggregatehtml;
a09aeee4 2052 }
d251b259 2053 }
a09aeee4 2054
d251b259 2055 $formstart = null;
2b04c41c
SH
2056 // if the item doesn't belong to the current user, the user has permission to rate
2057 // and we're within the assessable period
2058 if ($rating->user_can_rate()) {
771b3fbe 2059
2b04c41c
SH
2060 $rateurl = $rating->get_rate_url();
2061 $inputs = $rateurl->params();
771b3fbe 2062
2b04c41c
SH
2063 //start the rating form
2064 $formattrs = array(
2065 'id' => "postrating{$rating->itemid}",
2066 'class' => 'postratingform',
2067 'method' => 'post',
2068 'action' => $rateurl->out_omit_querystring()
2069 );
2070 $formstart = html_writer::start_tag('form', $formattrs);
2071 $formstart .= html_writer::start_tag('div', array('class' => 'ratingform'));
2072
2073 // add the hidden inputs
2074 foreach ($inputs as $name => $value) {
2075 $attributes = array('type' => 'hidden', 'class' => 'ratinginput', 'name' => $name, 'value' => $value);
2076 $formstart .= html_writer::empty_tag('input', $attributes);
2077 }
3180bc2c 2078
d251b259
AD
2079 if (empty($ratinghtml)) {
2080 $ratinghtml .= $strrate.': ';
2081 }
d251b259 2082 $ratinghtml = $formstart.$ratinghtml;
63e87951 2083
2b04c41c
SH
2084 $scalearray = array(RATING_UNSET_RATING => $strrate.'...') + $rating->settings->scale->scaleitems;
2085 $scaleattrs = array('class'=>'postratingmenu ratinginput','id'=>'menurating'.$rating->itemid);
ecc5cc31 2086 $ratinghtml .= html_writer::label($rating->rating, 'menurating'.$rating->itemid, false, array('class' => 'accesshide'));
2b04c41c 2087 $ratinghtml .= html_writer::select($scalearray, 'rating', $rating->rating, false, $scaleattrs);
a09aeee4 2088
d251b259 2089 //output submit button
771b3fbe
AD
2090 $ratinghtml .= html_writer::start_tag('span', array('class'=>"ratingsubmit"));
2091
2b04c41c 2092 $attributes = array('type' => 'submit', 'class' => 'postratingmenusubmit', 'id' => 'postratingsubmit'.$rating->itemid, 'value' => s(get_string('rate', 'rating')));
771b3fbe 2093 $ratinghtml .= html_writer::empty_tag('input', $attributes);
a09aeee4 2094
2b04c41c 2095 if (!$rating->settings->scale->isnumeric) {
eaf52ff0
MN
2096 // If a global scale, try to find current course ID from the context
2097 if (empty($rating->settings->scale->courseid) and $coursecontext = $rating->context->get_course_context(false)) {
2098 $courseid = $coursecontext->instanceid;
2099 } else {
2100 $courseid = $rating->settings->scale->courseid;
2101 }
2102 $ratinghtml .= $this->help_icon_scale($courseid, $rating->settings->scale);
a09aeee4 2103 }
771b3fbe
AD
2104 $ratinghtml .= html_writer::end_tag('span');
2105 $ratinghtml .= html_writer::end_tag('div');
2106 $ratinghtml .= html_writer::end_tag('form');
a09aeee4
AD
2107 }
2108
63e87951 2109 return $ratinghtml;
a09aeee4
AD
2110 }
2111
7a3c215b 2112 /**
d9c8f425 2113 * Centered heading with attached help button (same title text)
7a3c215b
SH
2114 * and optional icon attached.
2115 *
4bcc5118 2116 * @param string $text A heading text
53a78cef 2117 * @param string $helpidentifier The keyword that defines a help page
4bcc5118
PS
2118 * @param string $component component name
2119 * @param string|moodle_url $icon
2120 * @param string $iconalt icon alt text
699e2fd0
RW
2121 * @param int $level The level of importance of the heading. Defaulting to 2
2122 * @param string $classnames A space-separated list of CSS classes. Defaulting to null
d9c8f425 2123 * @return string HTML fragment
2124 */
699e2fd0 2125 public function heading_with_help($text, $helpidentifier, $component = 'moodle', $icon = '', $iconalt = '', $level = 2, $classnames = null) {
4bcc5118
PS
2126 $image = '';
2127 if ($icon) {
8ef1aa40 2128 $image = $this->pix_icon($icon, $iconalt, $component, array('class'=>'icon iconlarge'));
d9c8f425 2129 }
4bcc5118 2130
259c165d
PS
2131 $help = '';
2132 if ($helpidentifier) {
2133 $help = $this->help_icon($helpidentifier, $component);
2134 }
4bcc5118 2135
699e2fd0 2136 return $this->heading($image.$text.$help, $level, $classnames);
d9c8f425 2137 }
2138
2139 /**
7a3c215b 2140 * Returns HTML to display a help icon.
d9c8f425 2141 *
cb616be8 2142 * @deprecated since Moodle 2.0
bf11293a 2143 */
596509e4 2144 public function old_help_icon($helpidentifier, $title, $component = 'moodle', $linktext = '') {
a6d81a73 2145 throw new coding_exception('old_help_icon() can not be used any more, please see help_icon().');
d9c8f425 2146 }
2147
259c165d 2148 /**
7a3c215b 2149 * Returns HTML to display a help icon.
259c165d 2150 *
2fada290
MG
2151 * Theme developers: DO NOT OVERRIDE! Please override function
2152 * {@link core_renderer::render_help_icon()} instead.
2153 *
259c165d
PS
2154 * @param string $identifier The keyword that defines a help page
2155 * @param string $component component name
2156 * @param string|bool $linktext true means use $title as link text, string means link text value
2157 * @return string HTML fragment
2158 */
2159 public function help_icon($identifier, $component = 'moodle', $linktext = '') {
2cf81209 2160 $icon = new help_icon($identifier, $component);
259c165d
PS
2161 $icon->diag_strings();
2162 if ($linktext === true) {
2163 $icon->linktext = get_string($icon->identifier, $icon->component);
2164 } else if (!empty($linktext)) {
2165 $icon->linktext = $linktext;
2166 }
2167 return $this->render($icon);
2168 }
2169
2170 /**
2171 * Implementation of user image rendering.
7a3c215b 2172 *
3d3fae72 2173 * @param help_icon $helpicon A help icon instance
259c165d
PS
2174 * @return string HTML fragment
2175 */
2176 protected function render_help_icon(help_icon $helpicon) {
2177 global $CFG;
2178
2179 // first get the help image icon
2180 $src = $this->pix_url('help');
2181
2182 $title = get_string($helpicon->identifier, $helpicon->component);
2183
2184 if (empty($helpicon->linktext)) {
cab2c7ea 2185 $alt = get_string('helpprefix2', '', trim($title, ". \t"));
259c165d
PS
2186 } else {
2187 $alt = get_string('helpwiththis');
2188 }
2189
2190 $attributes = array('src'=>$src, 'alt'=>$alt, 'class'=>'iconhelp');
2191 $output = html_writer::empty_tag('img', $attributes);
2192
2193 // add the link text if given
2194 if (!empty($helpicon->linktext)) {
2195 // the spacing has to be done through CSS
2196 $output .= $helpicon->linktext;
2197 }
2198
69542fb3
PS
2199 // now create the link around it - we need https on loginhttps pages
2200 $url = new moodle_url($CFG->httpswwwroot.'/help.php', array('component' => $helpicon->component, 'identifier' => $helpicon->identifier, 'lang'=>current_language()));
259c165d
PS
2201
2202 // note: this title is displayed only if JS is disabled, otherwise the link will have the new ajax tooltip
2203 $title = get_string('helpprefix2', '', trim($title, ". \t"));
2204
e88419a2 2205 $attributes = array('href' => $url, 'title' => $title, 'aria-haspopup' => 'true', 'target'=>'_blank');
259c165d
PS
2206 $output = html_writer::tag('a', $output, $attributes);
2207
2208 // and finally span
238b8bc9 2209 return html_writer::tag('span', $output, array('class' => 'helptooltip'));
259c165d
PS
2210 }
2211
d9c8f425 2212 /**
7a3c215b 2213 * Returns HTML to display a scale help icon.
d9c8f425 2214 *
4bcc5118 2215 * @param int $courseid
7a3c215b
SH
2216 * @param stdClass $scale instance
2217 * @return string HTML fragment
d9c8f425 2218 */
4bcc5118
PS
2219 public function help_icon_scale($courseid, stdClass $scale) {
2220 global $CFG;
02f64f97 2221
4bcc5118 2222 $title = get_string('helpprefix2', '', $scale->name) .' ('.get_string('newwindow').')';
02f64f97 2223
0029a917 2224 $icon = $this->pix_icon('help', get_string('scales'), 'moodle', array('class'=>'iconhelp'));
02f64f97 2225
68bf577b
AD
2226 $scaleid = abs($scale->id);
2227
2228 $link = new moodle_url('/course/scales.php', array('id' => $courseid, 'list' => true, 'scaleid' => $scaleid));
230ec401 2229 $action = new popup_action('click', $link, 'ratingscale');
02f64f97 2230
26acc814 2231 return html_writer::tag('span', $this->action_link($link, $icon, $action), array('class' => 'helplink'));
d9c8f425 2232 }
2233
2234 /**
2235 * Creates and returns a spacer image with optional line break.
2236 *
3d3fae72
SH
2237 * @param array $attributes Any HTML attributes to add to the spaced.
2238 * @param bool $br Include a BR after the spacer.... DON'T USE THIS. Don't be
2239 * laxy do it with CSS which is a much better solution.
d9c8f425 2240 * @return string HTML fragment
2241 */
0029a917
PS
2242 public function spacer(array $attributes = null, $br = false) {
2243 $attributes = (array)$attributes;
2244 if (empty($attributes['width'])) {
2245 $attributes['width'] = 1;
1ba862ec 2246 }
e1a5a9cc 2247 if (empty($attributes['height'])) {
0029a917 2248 $attributes['height'] = 1;
d9c8f425 2249 }
0029a917 2250 $attributes['class'] = 'spacer';
d9c8f425 2251
0029a917 2252 $output = $this->pix_icon('spacer', '', 'moodle', $attributes);
b65bfc3e 2253
0029a917 2254 if (!empty($br)) {
1ba862ec
PS
2255 $output .= '<br />';
2256 }
d9c8f425 2257
2258 return $output;
2259 }
2260
d9c8f425 2261 /**
7a3c215b 2262 * Returns HTML to display the specified user's avatar.
d9c8f425 2263 *
5d0c95a5 2264 * User avatar may be obtained in two ways:
d9c8f425 2265 * <pre>
812dbaf7
PS
2266 * // Option 1: (shortcut for simple cases, preferred way)
2267 * // $user has come from the DB and has fields id, picture, imagealt, firstname and lastname
2268 * $OUTPUT->user_picture($user, array('popup'=>true));
2269 *
5d0c95a5
PS
2270 * // Option 2:
2271 * $userpic = new user_picture($user);
d9c8f425 2272 * // Set properties of $userpic
812dbaf7 2273 * $userpic->popup = true;
5d0c95a5 2274 * $OUTPUT->render($userpic);
d9c8f425 2275 * </pre>
2276 *
2fada290
MG
2277 * Theme developers: DO NOT OVERRIDE! Please override function
2278 * {@link core_renderer::render_user_picture()} instead.
2279 *
7a3c215b 2280 * @param stdClass $user Object with at least fields id, picture, imagealt, firstname, lastname
812dbaf7 2281 * If any of these are missing, the database is queried. Avoid this
d9c8f425 2282 * if at all possible, particularly for reports. It is very bad for performance.
812dbaf7
PS
2283 * @param array $options associative array with user picture options, used only if not a user_picture object,
2284 * options are:
2285 * - courseid=$this->page->course->id (course id of user profile in link)
2286 * - size=35 (size of image)
2287 * - link=true (make image clickable - the link leads to user profile)
2288 * - popup=false (open in popup)
2289 * - alttext=true (add image alt attribute)
5d0c95a5 2290 * - class = image class attribute (default 'userpicture')
e4a1efcb 2291 * - visibletoscreenreaders=true (whether to be visible to screen readers)
d9c8f425 2292 * @return string HTML fragment
2293 */
5d0c95a5
PS
2294 public function user_picture(stdClass $user, array $options = null) {
2295 $userpicture = new user_picture($user);
2296 foreach ((array)$options as $key=>$value) {
2297 if (array_key_exists($key, $userpicture)) {
2298 $userpicture->$key = $value;
2299 }
2300 }
2301 return $this->render($userpicture);
2302 }
2303
2304 /**
2305 * Internal implementation of user image rendering.
7a3c215b 2306 *
5d0c95a5
PS
2307 * @param user_picture $userpicture
2308 * @return string
2309 */
2310 protected function render_user_picture(user_picture $userpicture) {
2311 global $CFG, $DB;
812dbaf7 2312
5d0c95a5
PS
2313 $user = $userpicture->user;
2314
2315 if ($userpicture->alttext) {
2316 if (!empty($user->imagealt)) {
2317 $alt = $user->imagealt;
2318 } else {
2319 $alt = get_string('pictureof', '', fullname($user));
2320 }
d9c8f425 2321 } else {
97c10099 2322 $alt = '';
5d0c95a5
PS
2323 }
2324
2325 if (empty($userpicture->size)) {
5d0c95a5
PS
2326 $size = 35;
2327 } else if ($userpicture->size === true or $userpicture->size == 1) {
5d0c95a5 2328 $size = 100;
5d0c95a5 2329 } else {
5d0c95a5 2330 $size = $userpicture->size;
d9c8f425 2331 }
2332
5d0c95a5 2333 $class = $userpicture->class;
d9c8f425 2334
4d254790 2335 if ($user->picture == 0) {
5d0c95a5 2336 $class .= ' defaultuserpic';
5d0c95a5 2337 }
d9c8f425 2338
871a3ec5
SH
2339 $src = $userpicture->get_url($this->page, $this);
2340
29cf6631 2341 $attributes = array('src'=>$src, 'alt'=>$alt, 'title'=>$alt, 'class'=>$class, 'width'=>$size, 'height'=>$size);
e4a1efcb
JC
2342 if (!$userpicture->visibletoscreenreaders) {
2343 $attributes['role'] = 'presentation';
2344 }
2345
5d0c95a5 2346 // get the image html output fisrt
0e35ba6f 2347 $output = html_writer::empty_tag('img', $attributes);
5d0c95a5
PS
2348
2349 // then wrap it in link if needed
2350 if (!$userpicture->link) {
2351 return $output;
d9c8f425 2352 }
2353
5d0c95a5
PS
2354 if (empty($userpicture->courseid)) {
2355 $courseid = $this->page->course->id;
2356 } else {
2357 $courseid = $userpicture->courseid;
2358 }
2359
03d9401e
MD
2360 if ($courseid == SITEID) {
2361 $url = new moodle_url('/user/profile.php', array('id' => $user->id));
2362 } else {
2363 $url = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $courseid));
2364 }
5d0c95a5
PS
2365
2366 $attributes = array('href'=>$url);
e4a1efcb
JC
2367 if (!$userpicture->visibletoscreenreaders) {
2368 $attributes['role'] = 'presentation';
2369 $attributes['tabindex'] = '-1';
2370 $attributes['aria-hidden'] = 'true';
2371 }
5d0c95a5
PS
2372
2373 if ($userpicture->popup) {
2374 $id = html_writer::random_id('userpicture');
2375 $attributes['id'] = $id;
c80877aa 2376 $this->add_action_handler(new popup_action('click', $url), $id);
5d0c95a5
PS
2377 }
2378
26acc814 2379 return html_writer::tag('a', $output, $attributes);
d9c8f425 2380 }
b80ef420 2381
b80ef420
DC
2382 /**
2383 * Internal implementation of file tree viewer items rendering.
7a3c215b 2384 *
b80ef420
DC
2385 * @param array $dir
2386 * @return string
2387 */
2388 public function htmllize_file_tree($dir) {
2389 if (empty($dir['subdirs']) and empty($dir['files'])) {
2390 return '';
2391 }
2392 $result = '<ul>';
2393 foreach ($dir['subdirs'] as $subdir) {
2394 $result .= '<li>'.s($subdir['dirname']).' '.$this->htmllize_file_tree($subdir).'</li>';
2395 }
2396 foreach ($dir['files'] as $file) {
2397 $filename = $file->get_filename();
2398 $result .= '<li><span>'.html_writer::link($file->fileurl, $filename).'</span></li>';
2399 }
2400 $result .= '</ul>';
2401
2402 return $result;
2403 }
7a3c215b 2404
bb496de7 2405 /**
7a3c215b 2406 * Returns HTML to display the file picker
bb496de7
DC
2407 *
2408 * <pre>
2409 * $OUTPUT->file_picker($options);
2410 * </pre>
2411 *
2fada290
MG
2412 * Theme developers: DO NOT OVERRIDE! Please override function
2413 * {@link core_renderer::render_file_picker()} instead.
2414 *
bb496de7
DC
2415 * @param array $options associative array with file manager options
2416 * options are:
2417 * maxbytes=>-1,
2418 * itemid=>0,
2419 * client_id=>uniqid(),
2420 * acepted_types=>'*',
2421 * return_types=>FILE_INTERNAL,
2422 * context=>$PAGE->context
2423 * @return string HTML fragment
2424 */
2425 public function file_picker($options) {
2426 $fp = new file_picker($options);
2427 return $this->render($fp);
2428 }
7a3c215b 2429
b80ef420
DC
2430 /**
2431 * Internal implementation of file picker rendering.
7a3c215b 2432 *
b80ef420
DC
2433 * @param file_picker $fp
2434 * @return string
2435 */
bb496de7
DC
2436 public function render_file_picker(file_picker $fp) {
2437 global $CFG, $OUTPUT, $USER;
2438 $options = $fp->options;
2439 $client_id = $options->client_id;
2440 $strsaved = get_string('filesaved', 'repository');
2441 $straddfile = get_string('openpicker', 'repository');
2442 $strloading = get_string('loading', 'repository');
adce0230 2443 $strdndenabled = get_string('dndenabled_inbox', 'moodle');
906e7d89 2444 $strdroptoupload = get_string('droptoupload', 'moodle');
bb496de7
DC
2445 $icon_progress = $OUTPUT->pix_icon('i/loading_small', $strloading).'';
2446
2447 $currentfile = $options->currentfile;
2448 if (empty($currentfile)) {
322945e9
FM
2449 $currentfile = '';
2450 } else {
2451 $currentfile .= ' - ';
bb496de7 2452 }
b817205b
DC
2453 if ($options->maxbytes) {
2454 $size = $options->maxbytes;
2455 } else {
2456 $size = get_max_upload_file_size();
2457 }
513aed3c 2458 if ($size == -1) {
831399c4 2459 $maxsize = '';
513aed3c
DC
2460 } else {
2461 $maxsize = get_string('maxfilesize', 'moodle', display_size($size));
2462 }
f50a61fb 2463 if ($options->buttonname) {
4b72f9eb
AW
2464 $buttonname = ' name="' . $options->buttonname . '"';
2465 } else {
2466 $buttonname = '';
2467 }
bb496de7
DC
2468 $html = <<<EOD
2469<div class="filemanager-loading mdl-align" id='filepicker-loading-{$client_id}'>
2470$icon_progress
2471</div>
2472<div id="filepicker-wrapper-{$client_id}" class="mdl-left" style="display:none">
2473 <div>
c81f3328 2474 <input type="button" class="fp-btn-choose" id="filepicker-button-{$client_id}" value="{$straddfile}"{$buttonname}/>
fa7f2a45 2475 <span> $maxsize </span>
bb496de7
DC
2476 </div>
2477EOD;
2478 if ($options->env != 'url') {
2479 $html .= <<<EOD
50597880 2480 <div id="file_info_{$client_id}" class="mdl-left filepicker-filelist" style="position: relative">
a9352f1f 2481 <div class="filepicker-filename">
08a6a19d 2482 <div class="filepicker-container">$currentfile<div class="dndupload-message">$strdndenabled <br/><div class="dndupload-arrow"></div></div></div>
0f94289c 2483 <div class="dndupload-progressbars"></div>
a9352f1f 2484 </div>
08a6a19d 2485 <div><div class="dndupload-target">{$strdroptoupload}<br/><div class="dndupload-arrow"></div></div></div>
f08fac7c 2486 </div>
bb496de7
DC
2487EOD;
2488 }
2489 $html .= '</div>';
2490 return $html;
2491 }
d9c8f425 2492
2493 /**
7a3c215b 2494 * Returns HTML to display the 'Update this Modulename' button that appears on module pages.
d9c8f425 2495 *
2496 * @param string $cmid the course_module id.
2497 * @param string $modulename the module name, eg. "forum", "quiz" or "workshop"
2498 * @return string the HTML for the button, if this user has permission to edit it, else an empty string.
2499 */
2500 public function update_module_button($cmid, $modulename) {
2501 global $CFG;
b0c6dc1c 2502 if (has_capability('moodle/course:manageactivities', context_module::instance($cmid))) {
d9c8f425 2503 $modulename = get_string('modulename', $modulename);
2504 $string = get_string('updatethis', '', $modulename);
3ba60ee1
PS
2505 $url = new moodle_url("$CFG->wwwroot/course/mod.php", array('update' => $cmid, 'return' => true, 'sesskey' => sesskey()));
2506 return $this->single_button($url, $string);
d9c8f425 2507 } else {
2508 return '';
2509 }
2510 }
2511
2512 /**
7a3c215b
SH
2513 * Returns HTML to display a "Turn editing on/off" button in a form.
2514 *
d9c8f425 2515 * @param moodle_url $url The URL + params to send through when clicking the button
2516 * @return string HTML the button
2517 */
2518 public function edit_button(moodle_url $url) {
3362dfdc
EL
2519
2520 $url->param('sesskey', sesskey());
2521 if ($this->page->user_is_editing()) {
2522 $url->param('edit', 'off');
2523 $editstring = get_string('turneditingoff');
d9c8f425 2524 } else {
3362dfdc
EL
2525 $url->param('edit', 'on');
2526 $editstring = get_string('turneditingon');
d9c8f425 2527 }
2528
3362dfdc 2529 return $this->single_button($url, $editstring);
d9c8f425 2530 }
2531
d9c8f425 2532 /**
7a3c215b 2533 * Returns HTML to display a simple button to close a window
d9c8f425 2534 *
d9c8f425 2535 * @param string $text The lang string for the button's label (already output from get_string())
3ba60ee1 2536 * @return string html fragment
d9c8f425 2537 */
7a5c78e0 2538 public function close_window_button($text='') {
d9c8f425 2539 if (empty($text)) {
2540 $text = get_string('closewindow');
2541 }
a6855934
PS
2542 $button = new single_button(new moodle_url('#'), $text, 'get');
2543 $button->add_action(new component_action('click', 'close_window'));
3ba60ee1
PS
2544
2545 return $this->container($this->render($button), 'closewindow');
d9c8f425 2546 }
2547
d9c8f425 2548 /**
2549 * Output an error message. By default wraps the error message in <span class="error">.
2550 * If the error message is blank, nothing is output.
7a3c215b 2551 *
d9c8f425 2552 * @param string $message the error message.
2553 * @return string the HTML to output.
2554 */
2555 public function error_text($message) {
2556 if (empty($message)) {
2557 return '';
2558 }
3246648b 2559 $message = $this->pix_icon('i/warning', get_string('error'), '', array('class' => 'icon icon-pre', 'title'=>'')) . $message;
26acc814 2560 return html_writer::tag('span', $message, array('class' => 'error'));
d9c8f425 2561 }
2562
2563 /**
2564 * Do not call this function directly.
2565 *
f8129210 2566 * To terminate the current script with a fatal error, call the {@link print_error}
d9c8f425 2567 * function, or throw an exception. Doing either of those things will then call this
2568 * function to display the error, before terminating the execution.
2569 *
2570 * @param string $message The message to output
2571 * @param string $moreinfourl URL where more info can be found about the error
2572 * @param string $link Link for the Continue button
2573 * @param array $backtrace The execution backtrace
2574 * @param string $debuginfo Debugging information
d9c8f425 2575 * @return string the HTML to output.
2576 */
83267ec0 2577 public function fatal_error($message, $moreinfourl, $link, $backtrace, $debuginfo = null) {
6bd8d7e7 2578 global $CFG;
d9c8f425 2579
2580 $output = '';
6f8f4d83 2581 $obbuffer = '';
e57c283d 2582
d9c8f425 2583 if ($this->has_started()) {
50764d37
PS
2584 // we can not always recover properly here, we have problems with output buffering,
2585 // html tables, etc.
d9c8f425 2586 $output .= $this->opencontainers->pop_all_but_last();
50764d37 2587
d9c8f425 2588 } else {
50764d37
PS
2589 // It is really bad if library code throws exception when output buffering is on,
2590 // because the buffered text would be printed before our start of page.
2591 // NOTE: this hack might be behave unexpectedly in case output buffering is enabled in PHP.ini
6bd8d7e7 2592 error_reporting(0); // disable notices from gzip compression, etc.
50764d37 2593 while (ob_get_level() > 0) {
2cadd443
PS
2594 $buff = ob_get_clean();
2595 if ($buff === false) {
2596 break;
2597 }
2598 $obbuffer .= $buff;
50764d37 2599 }
6bd8d7e7 2600 error_reporting($CFG->debug);
6f8f4d83 2601
f22f1caf
PS
2602 // Output not yet started.
2603 $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
2604 if (empty($_SERVER['HTTP_RANGE'])) {
2605 @header($protocol . ' 404 Not Found');
2606 } else {
2607 // Must stop byteserving attempts somehow,
2608 // this is weird but Chrome PDF viewer can be stopped only with 407!
2609 @header($protocol . ' 407 Proxy Authentication Required');
85309744 2610 }
f22f1caf 2611
eb5bdb35 2612 $this->page->set_context(null); // ugly hack - make sure page context is set to something, we do not want bogus warnings here
7fde1e4b 2613 $this->page->set_url('/'); // no url
191b267b 2614 //$this->page->set_pagelayout('base'); //TODO: MDL-20676 blocks on error pages are weird, unfortunately it somehow detect the pagelayout from URL :-(
dcfb9b78 2615 $this->page->set_title(get_string('error'));
8093188f 2616 $this->page->set_heading($this->page->course->fullname);
d9c8f425 2617 $output .= $this->header();
2618 }
2619
2620 $message = '<p class="errormessage">' . $message . '</p>'.
2621 '<p class="errorcode"><a href="' . $moreinfourl . '">' .
2622 get_string('moreinformation') . '</a></p>';
1ad8143a
PS
2623 if (empty($CFG->rolesactive)) {
2624 $message .= '<p class="errormessage">' . get_string('installproblem', 'error') . '</p>';
2625 //It is usually not possible to recover from errors triggered during installation, you may need to create a new database or use a different database prefix for new installation.
2626 }
4c2892c6 2627 $output .= $this->box($message, 'errorbox', null, array('data-rel' => 'fatalerror'));
d9c8f425 2628
96f81ea3 2629 if ($CFG->debugdeveloper) {
6f8f4d83 2630 if (!empty($debuginfo)) {
c5d18164
PS
2631 $debuginfo = s($debuginfo); // removes all nasty JS
2632 $debuginfo = str_replace("\n", '<br />', $debuginfo); // keep newlines
2633 $output .= $this->notification('<strong>Debug info:</strong> '.$debuginfo, 'notifytiny');
6f8f4d83
PS
2634 }
2635 if (!empty($backtrace)) {
2636 $output .= $this->notification('<strong>Stack trace:</strong> '.format_backtrace($backtrace), 'notifytiny');
2637 }
2638 if ($obbuffer !== '' ) {
2639 $output .= $this->notification('<strong>Output buffer:</strong> '.s($obbuffer), 'notifytiny');
2640 }
d9c8f425 2641 }
2642
3efe6bbb
PS
2643 if (empty($CFG->rolesactive)) {
2644 // continue does not make much sense if moodle is not installed yet because error is most probably not recoverable
2645 } else if (!empty($link)) {
d9c8f425 2646 $output .= $this->continue_button($link);
2647 }
2648
2649 $output .= $this->footer();
2650
2651 // Padding to encourage IE to display our error page, rather than its own.
2652 $output .= str_repeat(' ', 512);
2653
2654 return $output;
2655 }
2656
2657 /**
2658 * Output a notification (that is, a status message about something that has
2659 * just happened).
2660 *
2661 * @param string $message the message to print out
2662 * @param string $classes normally 'notifyproblem' or 'notifysuccess'.
2663 * @return string the HTML to output.
2664 */
2665 public function notification($message, $classes = 'notifyproblem') {
26acc814 2666 return html_writer::tag('div', clean_text($message), array('class' => renderer_base::prepare_classes($classes)));
d9c8f425 2667 }
2668
2669 /**
7a3c215b 2670 * Returns HTML to display a continue button that goes to a particular URL.
d9c8f425 2671 *
3ba60ee1 2672 * @param string|moodle_url $url The url the button goes to.
d9c8f425 2673 * @return string the HTML to output.
2674 */
3ba60ee1
PS
2675 public function continue_button($url) {
2676 if (!($url instanceof moodle_url)) {
2677 $url = new moodle_url($url);
d9c8f425 2678 }
3ba60ee1
PS
2679 $button = new single_button($url, get_string('continue'), 'get');
2680 $button->class = 'continuebutton';
d9c8f425 2681
3ba60ee1 2682 return $this->render($button);
d9c8f425 2683 }
2684
2685 /**
7a3c215b 2686 * Returns HTML to display a single paging bar to provide access to other pages (usually in a search)
d9c8f425 2687 *
2fada290
MG
2688 * Theme developers: DO NOT OVERRIDE! Please override function
2689 * {@link core_renderer::render_paging_bar()} instead.
2690 *
71c03ac1 2691 * @param int $totalcount The total number of entries available to be paged through
929d7a83
PS
2692 * @param int $page The page you are currently viewing
2693 * @param int $perpage The number of entries that should be shown per page
2694 * @param string|moodle_url $baseurl url of the current page, the $pagevar parameter is added
2695 * @param string $pagevar name of page parameter that holds the page number
d9c8f425 2696 * @return string the HTML to output.
2697 */
929d7a83
PS
2698 public function paging_bar($totalcount, $page, $perpage, $baseurl, $pagevar = 'page') {
2699 $pb = new paging_bar($totalcount, $page, $perpage, $baseurl, $pagevar);
2700 return $this->render($pb);
2701 }
2702
2703 /**
2704 * Internal implementation of paging bar rendering.
7a3c215b 2705 *
929d7a83
PS
2706 * @param paging_bar $pagingbar
2707 * @return string
2708 */
2709 protected function render_paging_bar(paging_bar $pagingbar) {
d9c8f425 2710 $output = '';
2711 $pagingbar = clone($pagingbar);
34059565 2712 $pagingbar->prepare($this, $this->page, $this->target);
d9c8f425 2713
2714 if ($pagingbar->totalcount > $pagingbar->perpage) {
2715 $output .= get_string('page') . ':';
2716
2717 if (!empty($pagingbar->previouslink)) {
56ddb719 2718 $output .= '&#160;(' . $pagingbar->previouslink . ')&#160;';
d9c8f425 2719 }
2720
2721 if (!empty($pagingbar->firstlink)) {
56ddb719 2722 $output .= '&#160;' . $pagingbar->firstlink . '&#160;...';
d9c8f425 2723 }
2724
2725 foreach ($pagingbar->pagelinks as $link) {
56ddb719 2726 $output .= "&#160;&#160;$link";
d9c8f425 2727 }
2728
2729 if (!empty($pagingbar->lastlink)) {
56ddb719 2730 $output .= '&#160;...' . $pagingbar->lastlink . '&#160;';
d9c8f425 2731 }
2732
2733 if (!empty($pagingbar->nextlink)) {
56ddb719 2734 $output .= '&#160;&#160;(' . $pagingbar->nextlink . ')';
d9c8f425 2735 }
2736 }
2737
26acc814 2738 return html_writer::tag('div', $output, array('class' => 'paging'));
d9c8f425 2739 }
2740
d9c8f425 2741 /**
2742 * Output the place a skip link goes to.
7a3c215b 2743 *
d9c8f425 2744 * @param string $id The target name from the corresponding $PAGE->requires->skip_link_to($target) call.
2745 * @return string the HTML to output.
2746 */
fe213365 2747 public function skip_link_target($id = null) {
26acc814 2748 return html_writer::tag('span', '', array('id' => $id));
d9c8f425 2749 }
2750
2751 /**
2752 * Outputs a heading
7a3c215b 2753 *
d9c8f425 2754 * @param string $text The text of the heading
2755 * @param int $level The level of importance of the heading. Defaulting to 2
699e2fd0 2756 * @param string $classes A space-separated list of CSS classes. Defaulting to null
d9c8f425 2757 * @param string $id An optional ID
2758 * @return string the HTML to output.
2759 */
699e2fd0 2760 public function heading($text, $level = 2, $classes = null, $id = null) {
d9c8f425 2761 $level = (integer) $level;
2762 if ($level < 1 or $level > 6) {
2763 throw new coding_exception('Heading level must be an integer between 1 and 6.');
2764 }
26acc814 2765 return html_writer::tag('h' . $level, $text, array('id' => $id, 'class' => renderer_base::prepare_classes($classes)));
d9c8f425 2766 }
2767
2768 /**
2769 * Outputs a box.
7a3c215b 2770 *
d9c8f425 2771 * @param string $contents The contents of the box
2772 * @param string $classes A space-separated list of CSS classes
2773 * @param string $id An optional ID
3e76c7fa 2774 * @param array $attributes An array of other attributes to give the box.
d9c8f425 2775 * @return string the HTML to output.
2776 */
3e76c7fa
SH
2777 public function box($contents, $classes = 'generalbox', $id = null, $attributes = array()) {
2778 return $this->box_start($classes, $id, $attributes) . $contents . $this->box_end();
d9c8f425 2779 }
2780
2781 /**
3d3fae72 2782 * Outputs the opening section of a box.
7a3c215b 2783 *
d9c8f425 2784 * @param string $classes A space-separated list of CSS classes
2785 * @param string $id An optional ID
3e76c7fa 2786 * @param array $attributes An array of other attributes to give the box.
d9c8f425 2787 * @return string the HTML to output.
2788 */
3e76c7fa 2789 public function box_start($classes = 'generalbox', $id = null, $attributes = array()) {
5d0c95a5 2790 $this->opencontainers->push('box', html_writer::end_tag('div'));
3e76c7fa
SH
2791 $attributes['id'] = $id;
2792 $attributes['class'] = 'box ' . renderer_base::prepare_classes($classes);
2793 return html_writer::start_tag('div', $attributes);
d9c8f425 2794 }
2795
2796 /**
2797 * Outputs the closing section of a box.
7a3c215b 2798 *
d9c8f425 2799 * @return string the HTML to output.
2800 */
2801 public function box_end() {
2802 return $this->opencontainers->pop('box');
2803 }
2804
2805 /**
2806 * Outputs a container.
7a3c215b 2807 *
d9c8f425 2808 * @param string $contents The contents of the box
2809 * @param string $classes A space-separated list of CSS classes
2810 * @param string $id An optional ID
2811 * @return string the HTML to output.
2812 */
fe213365 2813 public function container($contents, $classes = null, $id = null) {
d9c8f425 2814 return $this->container_start($classes, $id) . $contents . $this->container_end();
2815 }
2816
2817 /**
2818 * Outputs the opening section of a container.
7a3c215b 2819 *
d9c8f425 2820 * @param string $classes A space-separated list of CSS classes
2821 * @param string $id An optional ID
2822 * @return string the HTML to output.
2823 */
fe213365 2824 public function container_start($classes = null, $id = null) {
5d0c95a5
PS
2825 $this->opencontainers->push('container', html_writer::end_tag('div'));
2826 return html_writer::start_tag('div', array('id' => $id,
78946b9b 2827 'class' => renderer_base::prepare_classes($classes)));
d9c8f425 2828 }
2829
2830 /**
2831 * Outputs the closing section of a container.
7a3c215b 2832 *
d9c8f425 2833 * @return string the HTML to output.
2834 */
2835 public function container_end() {
2836 return $this->opencontainers->pop('container');
2837 }
7d2a0492 2838
3406acde 2839 /**
7d2a0492 2840 * Make nested HTML lists out of the items
2841 *
2842 * The resulting list will look something like this:
2843 *
2844 * <pre>
2845 * <<ul>>
2846 * <<li>><div class='tree_item parent'>(item contents)</div>
2847 * <<ul>
2848 * <<li>><div class='tree_item'>(item contents)</div><</li>>
2849 * <</ul>>
2850 * <</li>>
2851 * <</ul>>
2852 * </pre>
2853 *
7a3c215b
SH
2854 * @param array $items
2855 * @param array $attrs html attributes passed to the top ofs the list
7d2a0492 2856 * @return string HTML
2857 */
7a3c215b 2858 public function tree_block_contents($items, $attrs = array()) {
7d2a0492 2859 // exit if empty, we don't want an empty ul element
2860 if (empty($items)) {
2861 return '';
2862 }
2863 // array of nested li elements
2864 $lis = array();
2865 foreach ($items as $item) {
2866 // this applies to the li item which contains all child lists too
2867 $content = $item->content($this);
2868 $liclasses = array($item->get_css_type());
3406acde 2869 if (!$item->forceopen || (!$item->forceopen && $item->collapse) || ($item->children->count()==0 && $item->nodetype==navigation_node::NODETYPE_BRANCH)) {
7d2a0492 2870 $liclasses[] = 'collapsed';
2871 }
2872 if ($item->isactive === true) {
2873 $liclasses[] = 'current_branch';
2874 }
2875 $liattr = array('class'=>join(' ',$liclasses));
2876 // class attribute on the div item which only contains the item content
2877 $divclasses = array('tree_item');
3406acde 2878 if ($item->children->count()>0 || $item->nodetype==navigation_node::NODETYPE_BRANCH) {
7d2a0492 2879 $divclasses[] = 'branch';
2880 } else {
2881 $divclasses[] = 'leaf';
2882 }
2883 if (!empty($item->classes) && count($item->classes)>0) {
2884 $divclasses[] = join(' ', $item->classes);
2885 }
2886 $divattr = array('class'=>join(' ', $divclasses));
2887 if (!empty($item->id)) {
2888 $divattr['id'] = $item->id;
2889 }
26acc814 2890 $content = html_writer::tag('p', $content, $divattr) . $this->tree_block_contents($item->children);
7d2a0492 2891 if (!empty($item->preceedwithhr) && $item->preceedwithhr===true) {
26acc814 2892 $content = html_writer::empty_tag('hr') . $content;
7d2a0492 2893 }
26acc814 2894 $content = html_writer::tag('li', $content, $liattr);
7d2a0492 2895 $lis[] = $content;
2896 }
26acc814 2897 return html_writer::tag('ul', implode("\n", $lis), $attrs);
7d2a0492 2898 }
2899
2900 /**
2901 * Return the navbar content so that it can be echoed out by the layout
7a3c215b 2902 *
7d2a0492 2903 * @return string XHTML navbar
2904 */
2905 public function navbar() {
3406acde 2906 $items = $this->page->navbar->get_items();
06a72e01
SH
2907 $itemcount = count($items);
2908 if ($itemcount === 0) {
2909 return '';
2910 }
3406acde 2911
3406acde
SH
2912 $htmlblocks = array();
2913 // Iterate the navarray and display each node
ffca6f4b
SH
2914 $separator = get_separator();
2915 for ($i=0;$i < $itemcount;$i++) {
2916 $item = $items[$i];
493a48f3 2917 $item->hideicon = true;
ffca6f4b
SH
2918 if ($i===0) {
2919 $content = html_writer::tag('li', $this->render($item));
2920 } else {
2921 $content = html_writer::tag('li', $separator.$this->render($item));
2922 }
2923 $htmlblocks[] = $content;
3406acde
SH
2924 }
2925
dcfb9b78
RW
2926 //accessibility: heading for navbar list (MDL-20446)
2927 $navbarcontent = html_writer::tag('span', get_string('pagepath'), array('class'=>'accesshide'));
90395bbf 2928 $navbarcontent .= html_writer::tag('nav', html_writer::tag('ul', join('', $htmlblocks)));
3406acde 2929 // XHTML
dcfb9b78 2930 return $navbarcontent;
3406acde
SH
2931 }
2932
7a3c215b
SH
2933 /**
2934 * Renders a navigation node object.
2935 *
2936 * @param navigation_node $item The navigation node to render.
2937 * @return string HTML fragment
2938 */
3406acde
SH
2939 protected function render_navigation_node(navigation_node $item) {
2940 $content = $item->get_content();
2941 $title = $item->get_title();
493a48f3 2942 if ($item->icon instanceof renderable && !$item->hideicon) {
3406acde 2943 $icon = $this->render($item->icon);
48fa9484 2944 $content = $icon.$content; // use CSS for spacing of icons
3406acde
SH
2945 }
2946 if ($item->helpbutton !== null) {
32561caf 2947 $content = trim($item->helpbutton).html_writer::tag('span', $content, array('class'=>'clearhelpbutton', 'tabindex'=>'0'));
3406acde
SH
2948 }
2949 if ($content === '') {
b4c458a3 2950 return '';
3406acde
SH
2951 }
2952 if ($item->action instanceof action_link) {
3406acde
SH
2953 $link = $item->action;
2954 if ($item->hidden) {
2955 $link->add_class('dimmed');
2956 }
0064f3cf
SH
2957 if (!empty($content)) {
2958 // Providing there is content we will use that for the link content.
2959 $link->text = $content;
2960 }
62594358 2961 $content = $this->render($link);
3406acde
SH
2962 } else if ($item->action instanceof moodle_url) {
2963 $attributes = array();
2964 if ($title !== '') {
2965 $attributes['title'] = $title;
2966 }
2967 if ($item->hidden) {
2968 $attributes['class'] = 'dimmed_text';
2969 }
2970 $content = html_writer::link($item->action, $content, $attributes);
2971
2972 } else if (is_string($item->action) || empty($item->action)) {
32561caf 2973 $attributes = array('tabindex'=>'0'); //add tab support to span but still maintain character stream sequence.
3406acde
SH
2974 if ($title !== '') {
2975 $attributes['title'] = $title;
2976 }
2977 if ($item->hidden) {
2978 $attributes['class'] = 'dimmed_text';
2979 }
2980 $content = html_writer::tag('span', $content, $attributes);
2981 }
2982 return $content;
7d2a0492 2983 }
92e01ab7
PS
2984
2985 /**
2986 * Accessibility: Right arrow-like character is
2987 * used in the breadcrumb trail, course navigation menu
2988 * (previous/next activity), calendar, and search forum block.
2989 * If the theme does not set characters, appropriate defaults
2990 * are set automatically. Please DO NOT
2991 * use &lt; &gt; &raquo; - these are confusing for blind users.
7a3c215b 2992 *
92e01ab7
PS
2993 * @return string
2994 */
2995 public function rarrow() {
2996 return $this->page->theme->rarrow;
2997 }
2998
2999 /**
3000 * Accessibility: Right arrow-like character is
3001 * used in the breadcrumb trail, course navigation menu
3002 * (previous/next activity), calendar, and search forum block.
3003 * If the theme does not set characters, appropriate defaults
3004 * are set automatically. Please DO NOT
3005 * use &lt; &gt; &raquo; - these are confusing for blind users.
7a3c215b 3006 *
92e01ab7
PS
3007 * @return string
3008 */
3009 public function larrow() {
3010 return $this->page->theme->larrow;
3011 }
088ccb43 3012
d2dbd0c0
SH
3013 /**
3014 * Returns the custom menu if one has been set
3015 *
71c03ac1 3016 * A custom menu can be configured by browsing to
d2dbd0c0
SH
3017 * Settings: Administration > Appearance > Themes > Theme settings
3018 * and then configuring the custommenu config setting as described.
4d2ee4c2 3019 *
2fada290
MG
3020 * Theme developers: DO NOT OVERRIDE! Please override function
3021 * {@link core_renderer::render_custom_menu()} instead.
3022 *
0f6d9349 3023 * @param string $custommenuitems - custom menuitems set by theme instead of global theme settings
d2dbd0c0
SH
3024 * @return string
3025 */
0f6d9349 3026 public function custom_menu($custommenuitems = '') {
12cc75ae 3027 global $CFG;
0f6d9349
DM
3028 if (empty($custommenuitems) && !empty($CFG->custommenuitems)) {
3029 $custommenuitems = $CFG->custommenuitems;
3030 }
3031 if (empty($custommenuitems)) {
12cc75ae
SH
3032 return '';
3033 }
0f6d9349 3034 $custommenu = new custom_menu($custommenuitems, current_language());
2fada290 3035 return $this->render($custommenu);
d2dbd0c0
SH
3036 }
3037
3038 /**
3039 * Renders a custom menu object (located in outputcomponents.php)
3040 *
3041 * The custom menu this method produces makes use of the YUI3 menunav widget
3042 * and requires very specific html elements and classes.
3043 *
3044 * @staticvar int $menucount
3045 * @param custom_menu $menu
3046 * @return string
3047 */
3048 protected function render_custom_menu(custom_menu $menu) {
3049 static $menucount = 0;
3050 // If the menu has no children return an empty string
3051 if (!$menu->has_children()) {
3052 return '';
3053 }
3054 // Increment the menu count. This is used for ID's that get worked with
3055 // in JavaScript as is essential
3056 $menucount++;