. namespace theme_boost\output; use coding_exception; use html_writer; use tabobject; use tabtree; use custom_menu_item; use custom_menu; use block_contents; use stdClass; use moodle_url; use preferences_groups; use action_menu; use help_icon; use single_button; use single_select; use paging_bar; use url_select; use context_course; use pix_icon; defined('MOODLE_INTERNAL') || die; /** * Renderers to align Moodle's HTML with that expected by Bootstrap * * @package theme_boost * @copyright 2012 Bas Brands, www.basbrands.nl * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class core_renderer extends \core_renderer { /** @var custom_menu_item language The language menu if created */ protected $language = null; /** * Outputs the opening section of a box. * * @param string $classes A space-separated list of CSS classes * @param string $id An optional ID * @param array $attributes An array of other attributes to give the box. * @return string the HTML to output. */ public function box_start($classes = 'generalbox', $id = null, $attributes = array()) { if (is_array($classes)) { $classes = implode(' ', $classes); } return parent::box_start($classes . ' p-a-1', $id, $attributes); } /** * Wrapper for header elements. * * @return string HTML to display the main header. */ public function full_header() { $html = html_writer::start_tag('header', array('id' => 'page-header', 'class' => 'row')); $html .= html_writer::start_div('col-xs-12 p-t-1 p-b-1'); $html .= $this->context_header(); $html .= html_writer::start_div('clearfix', array('id' => 'page-navbar')); $html .= html_writer::tag('div', $this->navbar(), array('class' => 'breadcrumb-nav')); $html .= html_writer::div($this->page_heading_button(), 'breadcrumb-button'); $html .= html_writer::end_div(); $html .= html_writer::tag('div', $this->course_header(), array('id' => 'course-header')); $html .= html_writer::end_div(); $html .= html_writer::end_tag('header'); return $html; } /** * The standard tags that should be included in the tag * including a meta description for the front page * * @return string HTML fragment. */ public function standard_head_html() { global $SITE, $PAGE; $output = parent::standard_head_html(); if ($PAGE->pagelayout == 'frontpage') { $summary = s(strip_tags(format_text($SITE->summary, FORMAT_HTML))); if (!empty($summary)) { $output .= "\n"; } } return $output; } /* * This renders the navbar. * Uses bootstrap compatible html. */ public function navbar() { return $this->render_from_template('core/navbar', $this->page->navbar); } /** * Override to inject the logo. * * @param array $headerinfo The header info. * @param int $headinglevel What level the 'h' tag will be. * @return string HTML for the header bar. */ public function context_header($headerinfo = null, $headinglevel = 1) { global $SITE; if ($this->should_display_main_logo($headinglevel)) { $sitename = format_string($SITE->fullname, true, array('context' => context_course::instance(SITEID))); return html_writer::div(html_writer::empty_tag('img', [ 'src' => $this->get_logo_url(null, 75), 'alt' => $sitename]), 'logo'); } return parent::context_header($headerinfo, $headinglevel); } /** * Get the compact logo URL. * * @return string */ public function get_compact_logo_url($maxwidth = 100, $maxheight = 100) { return parent::get_compact_logo_url(null, 35); } /** * Whether we should display the main logo. * * @return bool */ public function should_display_main_logo($headinglevel = 1) { global $PAGE; // Only render the logo if we're on the front page or login page and the we have a logo. $logo = $this->get_logo_url(); if ($headinglevel == 1 && !empty($logo)) { if ($PAGE->pagelayout == 'frontpage' || $PAGE->pagelayout == 'login') { return true; } } return false; } /** * Whether we should display the logo in the navbar. * * We will when there are no main logos, and we have compact logo. * * @return bool */ public function should_display_navbar_logo() { $logo = $this->get_compact_logo_url(); return !empty($logo) && !$this->should_display_main_logo(); } /* * Overriding the custom_menu function ensures the custom menu is * always shown, even if no menu items are configured in the global * theme settings page. */ public function custom_menu($custommenuitems = '') { global $CFG; if (empty($custommenuitems) && !empty($CFG->custommenuitems)) { $custommenuitems = $CFG->custommenuitems; } $custommenu = new custom_menu($custommenuitems, current_language()); return $this->render_custom_menu($custommenu); } /* * This renders the bootstrap top menu. * * This renderer is needed to enable the Bootstrap style navigation. */ protected function render_custom_menu(custom_menu $menu) { global $CFG; $langs = get_string_manager()->get_list_of_translations(); $haslangmenu = $this->lang_menu() != ''; if (!$menu->has_children() && !$haslangmenu) { return ''; } if ($haslangmenu) { $strlang = get_string('language'); $currentlang = current_language(); if (isset($langs[$currentlang])) { $currentlang = $langs[$currentlang]; } else { $currentlang = $strlang; } $this->language = $menu->add($currentlang, new moodle_url('#'), $strlang, 10000); foreach ($langs as $langtype => $langname) { $this->language->add($langname, new moodle_url($this->page->url, array('lang' => $langtype)), $langname); } } $content = ''; foreach ($menu->get_children() as $item) { $context = $item->export_for_template($this); $content .= $this->render_from_template('core/custom_menu_item', $context); } return $content; } /** * This code renders the navbar button to control the display of the custom menu * on smaller screens. * * Do not display the button if the menu is empty. * * @return string HTML fragment */ public function navbar_button() { global $CFG; if (empty($CFG->custommenuitems) && $this->lang_menu() == '') { return ''; } $iconbar = html_writer::tag('span', '', array('class' => 'icon-bar')); $button = html_writer::tag('a', $iconbar . "\n" . $iconbar. "\n" . $iconbar, array( 'class' => 'btn btn-navbar', 'data-toggle' => 'collapse', 'data-target' => '.nav-collapse' )); return $button; } /** * Renders tabtree * * @param tabtree $tabtree * @return string */ protected function render_tabtree(tabtree $tabtree) { if (empty($tabtree->subtree)) { return ''; } $data = $tabtree->export_for_template($this); return $this->render_from_template('core/tabtree', $data); } /** * Renders tabobject (part of tabtree) * * This function is called from {@link core_renderer::render_tabtree()} * and also it calls itself when printing the $tabobject subtree recursively. * * @param tabobject $tabobject * @return string HTML fragment */ protected function render_tabobject(tabobject $tab) { throw new coding_exception('Tab objects should not be directly rendered.'); } /** * Prints a nice side block with an optional header. * * @param block_contents $bc HTML for the content * @param string $region the region the block is appearing in. * @return string the HTML to be output. */ public function block(block_contents $bc, $region) { $bc = clone($bc); // Avoid messing up the object passed in. if (empty($bc->blockinstanceid) || !strip_tags($bc->title)) { $bc->collapsible = block_contents::NOT_HIDEABLE; } $id = !empty($bc->attributes['id']) ? $bc->attributes['id'] : uniqid('block-'); $context = new stdClass(); $context->skipid = $bc->skipid; $context->blockinstanceid = $bc->blockinstanceid; $context->dockable = $bc->dockable; $context->id = $id; $context->hidden = $bc->collapsible == block_contents::HIDDEN; $context->skiptitle = strip_tags($bc->title); $context->showskiplink = !empty($context->skiptitle); $context->arialabel = $bc->arialabel; $context->ariarole = !empty($bc->attributes['role']) ? $bc->attributes['role'] : 'complementary'; $context->type = $bc->attributes['data-block']; $context->title = $bc->title; $context->content = $bc->content; $context->annotation = $bc->annotation; $context->footer = $bc->footer; $context->hascontrols = !empty($bc->controls); if ($context->hascontrols) { $context->controls = $this->block_controls($bc->controls, $id); } return $this->render_from_template('core/block', $context); } /** * Returns the CSS classes to apply to the body tag. * * @since Moodle 2.5.1 2.6 * @param array $additionalclasses Any additional classes to apply. * @return string */ public function body_css_classes(array $additionalclasses = array()) { return $this->page->bodyclasses; } /** * Renders preferences groups. * * @param preferences_groups $renderable The renderable * @return string The output. */ public function render_preferences_groups(preferences_groups $renderable) { return $this->render_from_template('core/preferences_groups', $renderable); } /** * Renders an action menu component. * * @param action_menu $menu * @return string HTML */ public function render_action_menu(action_menu $menu) { // We don't want the class icon there! foreach ($menu->get_secondary_actions() as $action) { if ($action instanceof \action_menu_link && $action->has_class('icon')) { $action->attributes['class'] = preg_replace('/(^|\s+)icon(\s+|$)/i', '', $action->attributes['class']); } } $context = $menu->export_for_template($this); // We do not want the icon with the caret, the caret is added by Bootstrap. if (empty($context->primary->menutrigger)) { $newurl = $this->pix_url('t/edit', 'moodle'); $context->primary->icon['attributes'] = array_reduce($context->primary->icon['attributes'], function($carry, $item) use ($newurl) { if ($item['name'] === 'src') { $item['value'] = $newurl->out(false); } $carry[] = $item; return $carry; }, [] ); } return $this->render_from_template('core/action_menu', $context); } /** * Implementation of user image rendering. * * @param help_icon $helpicon A help icon instance * @return string HTML fragment */ protected function render_help_icon(help_icon $helpicon) { $context = $helpicon->export_for_template($this); return $this->render_from_template('core/help_icon', $context); } /** * Renders a single button widget. * * This will return HTML to display a form containing a single button. * * @param single_button $button * @return string HTML fragment */ protected function render_single_button(single_button $button) { return $this->render_from_template('core/single_button', $button->export_for_template($this)); } /** * Renders a single select. * * @param single_select $select The object. * @return string HTML */ protected function render_single_select(single_select $select) { return $this->render_from_template('core/single_select', $select->export_for_template($this)); } /** * Renders a paging bar. * * @param paging_bar $pagingbar The object. * @return string HTML */ protected function render_paging_bar(paging_bar $pagingbar) { // Any more than 10 is not usable and causes wierd wrapping of the pagination in this theme. $pagingbar->maxdisplay = 10; return $this->render_from_template('core/paging_bar', $pagingbar->export_for_template($this)); } /** * Renders a url select. * * @param url_select $select The object. * @return string HTML */ protected function render_url_select(url_select $select) { return $this->render_from_template('core/url_select', $select->export_for_template($this)); } /** * Renders a pix_icon widget and returns the HTML to display it. * * @param pix_icon $icon * @return string HTML fragment */ protected function render_pix_icon(pix_icon $icon) { $data = $icon->export_for_template($this); foreach ($data['attributes'] as $key => $item) { $name = $item['name']; $value = $item['value']; if ($name == 'class') { $data['extraclasses'] = $value; unset($data['attributes'][$key]); $data['attributes'] = array_values($data['attributes']); break; } } return $this->render_from_template('core/pix_icon', $data); } }