[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/theme/bootstrapbase/renderers/ -> core_renderer.php (source)

   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  
  17  /**
  18   * Renderers to align Moodle's HTML with that expected by Bootstrap
  19   *
  20   * @package    theme_bootstrapbase
  21   * @copyright  2012 Bas Brands, www.basbrands.nl
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  class theme_bootstrapbase_core_renderer extends core_renderer {
  26  
  27      /** @var custom_menu_item language The language menu if created */
  28      protected $language = null;
  29  
  30      /**
  31       * The standard tags that should be included in the <head> tag
  32       * including a meta description for the front page
  33       *
  34       * @return string HTML fragment.
  35       */
  36      public function standard_head_html() {
  37          global $SITE, $PAGE;
  38  
  39          $output = parent::standard_head_html();
  40          if ($PAGE->pagelayout == 'frontpage') {
  41              $summary = s(strip_tags(format_text($SITE->summary, FORMAT_HTML)));
  42              if (!empty($summary)) {
  43                  $output .= "<meta name=\"description\" content=\"$summary\" />\n";
  44              }
  45          }
  46  
  47          return $output;
  48      }
  49  
  50      /*
  51       * This renders the navbar.
  52       * Uses bootstrap compatible html.
  53       */
  54      public function navbar() {
  55          $items = $this->page->navbar->get_items();
  56          if (empty($items)) {
  57              return '';
  58          }
  59  
  60          $breadcrumbs = array();
  61          foreach ($items as $item) {
  62              $item->hideicon = true;
  63              $breadcrumbs[] = $this->render($item);
  64          }
  65          $divider = '<span class="divider">'.get_separator().'</span>';
  66          $list_items = '<li>'.join(" $divider</li><li>", $breadcrumbs).'</li>';
  67          $title = '<span class="accesshide" id="navbar-label">'.get_string('pagepath').'</span>';
  68          return $title . '<nav aria-labelledby="navbar-label"><ul class="breadcrumb">' .
  69                  $list_items . '</ul></nav>';
  70      }
  71  
  72      /*
  73       * Overriding the custom_menu function ensures the custom menu is
  74       * always shown, even if no menu items are configured in the global
  75       * theme settings page.
  76       */
  77      public function custom_menu($custommenuitems = '') {
  78          global $CFG;
  79  
  80          if (empty($custommenuitems) && !empty($CFG->custommenuitems)) {
  81              $custommenuitems = $CFG->custommenuitems;
  82          }
  83          $custommenu = new custom_menu($custommenuitems, current_language());
  84          return $this->render_custom_menu($custommenu);
  85      }
  86  
  87      /*
  88       * This renders the bootstrap top menu.
  89       *
  90       * This renderer is needed to enable the Bootstrap style navigation.
  91       */
  92      protected function render_custom_menu(custom_menu $menu) {
  93          global $CFG;
  94  
  95          $langs = get_string_manager()->get_list_of_translations();
  96          $haslangmenu = $this->lang_menu() != '';
  97  
  98          if (!$menu->has_children() && !$haslangmenu) {
  99              return '';
 100          }
 101  
 102          if ($haslangmenu) {
 103              $strlang =  get_string('language');
 104              $currentlang = current_language();
 105              if (isset($langs[$currentlang])) {
 106                  $currentlang = $langs[$currentlang];
 107              } else {
 108                  $currentlang = $strlang;
 109              }
 110              $this->language = $menu->add($currentlang, new moodle_url(''), $strlang, 10000);
 111              foreach ($langs as $langtype => $langname) {
 112                  $this->language->add($langname, new moodle_url($this->page->url, array('lang' => $langtype)), $langname);
 113              }
 114          }
 115  
 116          $content = '<ul class="nav">';
 117          foreach ($menu->get_children() as $item) {
 118              $content .= $this->render_custom_menu_item($item, 1);
 119          }
 120  
 121          return $content.'</ul>';
 122      }
 123  
 124      /*
 125       * This code renders the custom menu items for the
 126       * bootstrap dropdown menu.
 127       */
 128      protected function render_custom_menu_item(custom_menu_item $menunode, $level = 0 ) {
 129          static $submenucount = 0;
 130  
 131          $content = '';
 132          if ($menunode->has_children()) {
 133  
 134              if ($level == 1) {
 135                  $class = 'dropdown';
 136              } else {
 137                  $class = 'dropdown-submenu';
 138              }
 139  
 140              if ($menunode === $this->language) {
 141                  $class .= ' langmenu';
 142              }
 143              $content = html_writer::start_tag('li', array('class' => $class));
 144              // If the child has menus render it as a sub menu.
 145              $submenucount++;
 146              if ($menunode->get_url() !== null) {
 147                  $url = $menunode->get_url();
 148              } else {
 149                  $url = '#cm_submenu_'.$submenucount;
 150              }
 151              $content .= html_writer::start_tag('a', array('href'=>$url, 'class'=>'dropdown-toggle', 'data-toggle'=>'dropdown', 'title'=>$menunode->get_title()));
 152              $content .= $menunode->get_text();
 153              if ($level == 1) {
 154                  $content .= '<b class="caret"></b>';
 155              }
 156              $content .= '</a>';
 157              $content .= '<ul class="dropdown-menu">';
 158              foreach ($menunode->get_children() as $menunode) {
 159                  $content .= $this->render_custom_menu_item($menunode, 0);
 160              }
 161              $content .= '</ul>';
 162          } else {
 163              // The node doesn't have children so produce a final menuitem.
 164              // Also, if the node's text matches '####', add a class so we can treat it as a divider.
 165              if (preg_match("/^#+$/", $menunode->get_text())) {
 166                  // This is a divider.
 167                  $content = '<li class="divider">&nbsp;</li>';
 168              } else {
 169                  $content = '<li>';
 170                  if ($menunode->get_url() !== null) {
 171                      $url = $menunode->get_url();
 172                  } else {
 173                      $url = '#';
 174                  }
 175                  $content .= html_writer::link($url, $menunode->get_text(), array('title' => $menunode->get_title()));
 176                  $content .= '</li>';
 177              }
 178          }
 179          return $content;
 180      }
 181  
 182      /**
 183       * This code renders the navbar button to control the display of the custom menu
 184       * on smaller screens.
 185       *
 186       * Do not display the button if the menu is empty.
 187       *
 188       * @return string HTML fragment
 189       */
 190      protected function navbar_button() {
 191          global $CFG;
 192  
 193          if (empty($CFG->custommenuitems) && $this->lang_menu() == '') {
 194              return '';
 195          }
 196  
 197          $iconbar = html_writer::tag('span', '', array('class' => 'icon-bar'));
 198          $button = html_writer::tag('a', $iconbar . "\n" . $iconbar. "\n" . $iconbar, array(
 199              'class'       => 'btn btn-navbar',
 200              'data-toggle' => 'collapse',
 201              'data-target' => '.nav-collapse'
 202          ));
 203          return $button;
 204      }
 205  
 206      /**
 207       * Renders tabtree
 208       *
 209       * @param tabtree $tabtree
 210       * @return string
 211       */
 212      protected function render_tabtree(tabtree $tabtree) {
 213          if (empty($tabtree->subtree)) {
 214              return '';
 215          }
 216          $firstrow = $secondrow = '';
 217          foreach ($tabtree->subtree as $tab) {
 218              $firstrow .= $this->render($tab);
 219              if (($tab->selected || $tab->activated) && !empty($tab->subtree) && $tab->subtree !== array()) {
 220                  $secondrow = $this->tabtree($tab->subtree);
 221              }
 222          }
 223          return html_writer::tag('ul', $firstrow, array('class' => 'nav nav-tabs')) . $secondrow;
 224      }
 225  
 226      /**
 227       * Renders tabobject (part of tabtree)
 228       *
 229       * This function is called from {@link core_renderer::render_tabtree()}
 230       * and also it calls itself when printing the $tabobject subtree recursively.
 231       *
 232       * @param tabobject $tabobject
 233       * @return string HTML fragment
 234       */
 235      protected function render_tabobject(tabobject $tab) {
 236          if (($tab->selected and (!$tab->linkedwhenselected)) or $tab->activated) {
 237              return html_writer::tag('li', html_writer::tag('a', $tab->text), array('class' => 'active'));
 238          } else if ($tab->inactive) {
 239              return html_writer::tag('li', html_writer::tag('a', $tab->text), array('class' => 'disabled'));
 240          } else {
 241              if (!($tab->link instanceof moodle_url)) {
 242                  // backward compartibility when link was passed as quoted string
 243                  $link = "<a href=\"$tab->link\" title=\"$tab->title\">$tab->text</a>";
 244              } else {
 245                  $link = html_writer::link($tab->link, $tab->text, array('title' => $tab->title));
 246              }
 247              $params = $tab->selected ? array('class' => 'active') : null;
 248              return html_writer::tag('li', $link, $params);
 249          }
 250      }
 251  }
 252  
 253  /**
 254   * Overridden core maintenance renderer.
 255   *
 256   * This renderer gets used instead of the standard core_renderer during maintenance
 257   * tasks such as installation and upgrade.
 258   * We override it in order to style those scenarios consistently with the regular
 259   * bootstrap look and feel.
 260   *
 261   * @package    theme_bootstrapbase
 262   * @copyright  2014 Sam Hemelryk
 263   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 264   */
 265  class theme_bootstrapbase_core_renderer_maintenance extends core_renderer_maintenance {
 266  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1