PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/theme/clean/classes/core_renderer.php

https://bitbucket.org/moodle/moodle
PHP | 167 lines | 67 code | 21 blank | 79 comment | 17 complexity | a64e7f4028acecc5b86049337d611919 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. require_once($CFG->dirroot . '/theme/bootstrapbase/renderers.php');
  17. /**
  18. * Clean core renderers.
  19. *
  20. * @package theme_clean
  21. * @copyright 2015 FrĂŠdĂŠric Massart - FMCorz.net
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. class theme_clean_core_renderer extends theme_bootstrapbase_core_renderer {
  25. /**
  26. * Either returns the parent version of the header bar, or a version with the logo replacing the header.
  27. *
  28. * @since Moodle 2.9
  29. * @param array $headerinfo An array of header information, dependant on what type of header is being displayed. The following
  30. * array example is user specific.
  31. * heading => Override the page heading.
  32. * user => User object.
  33. * usercontext => user context.
  34. * @param int $headinglevel What level the 'h' tag will be.
  35. * @return string HTML for the header bar.
  36. */
  37. public function context_header($headerinfo = null, $headinglevel = 1) {
  38. if ($this->should_render_logo($headinglevel)) {
  39. return html_writer::tag('div', '', array('class' => 'logo'));
  40. }
  41. return parent::context_header($headerinfo, $headinglevel);
  42. }
  43. /**
  44. * Determines if we should render the logo.
  45. *
  46. * @param int $headinglevel What level the 'h' tag will be.
  47. * @return bool Should the logo be rendered.
  48. */
  49. protected function should_render_logo($headinglevel = 1) {
  50. global $PAGE;
  51. // Only render the logo if we're on the front page or login page
  52. // and the theme has a logo.
  53. $logo = $this->get_logo_url();
  54. if ($headinglevel == 1 && !empty($logo)) {
  55. if ($PAGE->pagelayout == 'frontpage' || $PAGE->pagelayout == 'login') {
  56. return true;
  57. }
  58. }
  59. return false;
  60. }
  61. /**
  62. * Returns the navigation bar home reference.
  63. *
  64. * The small logo is only rendered on pages where the logo is not displayed.
  65. *
  66. * @param bool $returnlink Whether to wrap the icon and the site name in links or not
  67. * @return string The site name, the small logo or both depending on the theme settings.
  68. */
  69. public function navbar_home($returnlink = true) {
  70. global $CFG;
  71. $imageurl = $this->get_compact_logo_url(null, 35);
  72. if ($this->should_render_logo() || empty($imageurl)) {
  73. // If there is no small logo we always show the site name.
  74. return $this->get_home_ref($returnlink);
  75. }
  76. $image = html_writer::img($imageurl, get_string('sitelogo', 'theme_' . $this->page->theme->name),
  77. array('class' => 'small-logo'));
  78. if ($returnlink) {
  79. $logocontainer = html_writer::link(new moodle_url('/'), $image,
  80. array('class' => 'small-logo-container', 'title' => get_string('home')));
  81. } else {
  82. $logocontainer = html_writer::tag('span', $image, array('class' => 'small-logo-container'));
  83. }
  84. // Sitename setting defaults to true.
  85. if (!isset($this->page->theme->settings->sitename) || !empty($this->page->theme->settings->sitename)) {
  86. return $logocontainer . $this->get_home_ref($returnlink);
  87. }
  88. return $logocontainer;
  89. }
  90. /**
  91. * Returns a reference to the site home.
  92. *
  93. * It can be either a link or a span.
  94. *
  95. * @param bool $returnlink
  96. * @return string
  97. */
  98. protected function get_home_ref($returnlink = true) {
  99. global $CFG, $SITE;
  100. $sitename = format_string($SITE->shortname, true, array('context' => context_course::instance(SITEID)));
  101. if ($returnlink) {
  102. return html_writer::link(new moodle_url('/'), $sitename, array('class' => 'brand', 'title' => get_string('home')));
  103. }
  104. return html_writer::tag('span', $sitename, array('class' => 'brand'));
  105. }
  106. /**
  107. * Return the theme logo URL, else the site's logo URL, if any.
  108. *
  109. * Note that maximum sizes are not applied to the theme logo.
  110. *
  111. * @param int $maxwidth The maximum width, or null when the maximum width does not matter.
  112. * @param int $maxheight The maximum height, or null when the maximum height does not matter.
  113. * @return moodle_url|false
  114. */
  115. public function get_logo_url($maxwidth = null, $maxheight = 100) {
  116. global $CFG;
  117. if (!empty($this->page->theme->settings->logo)) {
  118. $url = $this->page->theme->setting_file_url('logo', 'logo');
  119. // Get a URL suitable for moodle_url.
  120. $relativebaseurl = preg_replace('|^https?://|i', '//', $CFG->wwwroot);
  121. $url = str_replace($relativebaseurl, '', $url);
  122. return new moodle_url($url);
  123. }
  124. return parent::get_logo_url($maxwidth, $maxheight);
  125. }
  126. /**
  127. * Return the theme's compact logo URL, else the site's compact logo URL, if any.
  128. *
  129. * Note that maximum sizes are not applied to the theme logo.
  130. *
  131. * @param int $maxwidth The maximum width, or null when the maximum width does not matter.
  132. * @param int $maxheight The maximum height, or null when the maximum height does not matter.
  133. * @return moodle_url|false
  134. */
  135. public function get_compact_logo_url($maxwidth = 100, $maxheight = 100) {
  136. global $CFG;
  137. if (!empty($this->page->theme->settings->smalllogo)) {
  138. $url = $this->page->theme->setting_file_url('smalllogo', 'smalllogo');
  139. // Get a URL suitable for moodle_url.
  140. $relativebaseurl = preg_replace('|^https?://|i', '//', $CFG->wwwroot);
  141. $url = str_replace($relativebaseurl, '', $url);
  142. return new moodle_url($url);
  143. }
  144. return parent::get_compact_logo_url($maxwidth, $maxheight);
  145. }
  146. }