[ Index ]

PHP Cross Reference of moodle-2.8

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       * This renders a notification message.
  32       * Uses bootstrap compatible html.
  33       */
  34      public function notification($message, $classes = 'notifyproblem') {
  35          $message = clean_text($message);
  36          $type = '';
  37  
  38          if (($classes == 'notifyproblem') || ($classes == 'notifytiny')) {
  39              $type = 'alert alert-error';
  40          }
  41          if ($classes == 'notifysuccess') {
  42              $type = 'alert alert-success';
  43          }
  44          if ($classes == 'notifymessage') {
  45              $type = 'alert alert-info';
  46          }
  47          if ($classes == 'redirectmessage') {
  48              $type = 'alert alert-block alert-info';
  49          }
  50          return "<div class=\"$type\">$message</div>";
  51      }
  52  
  53      /*
  54       * This renders the navbar.
  55       * Uses bootstrap compatible html.
  56       */
  57      public function navbar() {
  58          $items = $this->page->navbar->get_items();
  59          if (empty($items)) {
  60              return '';
  61          }
  62          $breadcrumbs = array();
  63          foreach ($items as $item) {
  64              $item->hideicon = true;
  65              $breadcrumbs[] = $this->render($item);
  66          }
  67          $divider = '<span class="divider">'.get_separator().'</span>';
  68          $list_items = '<li>'.join(" $divider</li><li>", $breadcrumbs).'</li>';
  69          $title = '<span class="accesshide">'.get_string('pagepath').'</span>';
  70          return $title . "<ul class=\"breadcrumb\">$list_items</ul>";
  71      }
  72  
  73      /*
  74       * Overriding the custom_menu function ensures the custom menu is
  75       * always shown, even if no menu items are configured in the global
  76       * theme settings page.
  77       */
  78      public function custom_menu($custommenuitems = '') {
  79          global $CFG;
  80  
  81          if (empty($custommenuitems) && !empty($CFG->custommenuitems)) {
  82              $custommenuitems = $CFG->custommenuitems;
  83          }
  84          $custommenu = new custom_menu($custommenuitems, current_language());
  85          return $this->render_custom_menu($custommenu);
  86      }
  87  
  88      /*
  89       * This renders the bootstrap top menu.
  90       *
  91       * This renderer is needed to enable the Bootstrap style navigation.
  92       */
  93      protected function render_custom_menu(custom_menu $menu) {
  94          global $CFG;
  95  
  96          // TODO: eliminate this duplicated logic, it belongs in core, not
  97          // here. See MDL-39565.
  98          $addlangmenu = true;
  99          $langs = get_string_manager()->get_list_of_translations();
 100          if (count($langs) < 2
 101              or empty($CFG->langmenu)
 102              or ($this->page->course != SITEID and !empty($this->page->course->lang))) {
 103              $addlangmenu = false;
 104          }
 105  
 106          if (!$menu->has_children() && $addlangmenu === false) {
 107              return '';
 108          }
 109  
 110          if ($addlangmenu) {
 111              $strlang =  get_string('language');
 112              $currentlang = current_language();
 113              if (isset($langs[$currentlang])) {
 114                  $currentlang = $langs[$currentlang];
 115              } else {
 116                  $currentlang = $strlang;
 117              }
 118              $this->language = $menu->add($currentlang, new moodle_url('#'), $strlang, 10000);
 119              foreach ($langs as $langtype => $langname) {
 120                  $this->language->add($langname, new moodle_url($this->page->url, array('lang' => $langtype)), $langname);
 121              }
 122          }
 123  
 124          $content = '<ul class="nav">';
 125          foreach ($menu->get_children() as $item) {
 126              $content .= $this->render_custom_menu_item($item, 1);
 127          }
 128  
 129          return $content.'</ul>';
 130      }
 131  
 132      /*
 133       * This code renders the custom menu items for the
 134       * bootstrap dropdown menu.
 135       */
 136      protected function render_custom_menu_item(custom_menu_item $menunode, $level = 0 ) {
 137          static $submenucount = 0;
 138  
 139          $content = '';
 140          if ($menunode->has_children()) {
 141  
 142              if ($level == 1) {
 143                  $class = 'dropdown';
 144              } else {
 145                  $class = 'dropdown-submenu';
 146              }
 147  
 148              if ($menunode === $this->language) {
 149                  $class .= ' langmenu';
 150              }
 151              $content = html_writer::start_tag('li', array('class' => $class));
 152              // If the child has menus render it as a sub menu.
 153              $submenucount++;
 154              if ($menunode->get_url() !== null) {
 155                  $url = $menunode->get_url();
 156              } else {
 157                  $url = '#cm_submenu_'.$submenucount;
 158              }
 159              $content .= html_writer::start_tag('a', array('href'=>$url, 'class'=>'dropdown-toggle', 'data-toggle'=>'dropdown', 'title'=>$menunode->get_title()));
 160              $content .= $menunode->get_text();
 161              if ($level == 1) {
 162                  $content .= '<b class="caret"></b>';
 163              }
 164              $content .= '</a>';
 165              $content .= '<ul class="dropdown-menu">';
 166              foreach ($menunode->get_children() as $menunode) {
 167                  $content .= $this->render_custom_menu_item($menunode, 0);
 168              }
 169              $content .= '</ul>';
 170          } else {
 171              // The node doesn't have children so produce a final menuitem.
 172              // Also, if the node's text matches '####', add a class so we can treat it as a divider.
 173              if (preg_match("/^#+$/", $menunode->get_text())) {
 174                  // This is a divider.
 175                  $content = '<li class="divider">&nbsp;</li>';
 176              } else {
 177                  $content = '<li>';
 178                  if ($menunode->get_url() !== null) {
 179                      $url = $menunode->get_url();
 180                  } else {
 181                      $url = '#';
 182                  }
 183                  $content .= html_writer::link($url, $menunode->get_text(), array('title' => $menunode->get_title()));
 184                  $content .= '</li>';
 185              }
 186          }
 187          return $content;
 188      }
 189  
 190      /**
 191       * Renders tabtree
 192       *
 193       * @param tabtree $tabtree
 194       * @return string
 195       */
 196      protected function render_tabtree(tabtree $tabtree) {
 197          if (empty($tabtree->subtree)) {
 198              return '';
 199          }
 200          $firstrow = $secondrow = '';
 201          foreach ($tabtree->subtree as $tab) {
 202              $firstrow .= $this->render($tab);
 203              if (($tab->selected || $tab->activated) && !empty($tab->subtree) && $tab->subtree !== array()) {
 204                  $secondrow = $this->tabtree($tab->subtree);
 205              }
 206          }
 207          return html_writer::tag('ul', $firstrow, array('class' => 'nav nav-tabs')) . $secondrow;
 208      }
 209  
 210      /**
 211       * Renders tabobject (part of tabtree)
 212       *
 213       * This function is called from {@link core_renderer::render_tabtree()}
 214       * and also it calls itself when printing the $tabobject subtree recursively.
 215       *
 216       * @param tabobject $tabobject
 217       * @return string HTML fragment
 218       */
 219      protected function render_tabobject(tabobject $tab) {
 220          if (($tab->selected and (!$tab->linkedwhenselected)) or $tab->activated) {
 221              return html_writer::tag('li', html_writer::tag('a', $tab->text), array('class' => 'active'));
 222          } else if ($tab->inactive) {
 223              return html_writer::tag('li', html_writer::tag('a', $tab->text), array('class' => 'disabled'));
 224          } else {
 225              if (!($tab->link instanceof moodle_url)) {
 226                  // backward compartibility when link was passed as quoted string
 227                  $link = "<a href=\"$tab->link\" title=\"$tab->title\">$tab->text</a>";
 228              } else {
 229                  $link = html_writer::link($tab->link, $tab->text, array('title' => $tab->title));
 230              }
 231              $params = $tab->selected ? array('class' => 'active') : null;
 232              return html_writer::tag('li', $link, $params);
 233          }
 234      }
 235  }
 236  
 237  /**
 238   * Overridden core maintenance renderer.
 239   *
 240   * This renderer gets used instead of the standard core_renderer during maintenance
 241   * tasks such as installation and upgrade.
 242   * We override it in order to style those scenarios consistently with the regular
 243   * bootstrap look and feel.
 244   *
 245   * @package    theme_bootstrapbase
 246   * @copyright  2014 Sam Hemelryk
 247   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 248   */
 249  class theme_bootstrapbase_core_renderer_maintenance extends core_renderer_maintenance {
 250      /**
 251       * Renders notifications for maintenance scripts.
 252       *
 253       * We need to override this method in the same way we do for the core_renderer maintenance method
 254       * found above.
 255       * Please note this isn't required of every function, only functions used during maintenance.
 256       * In this case notification is used to print errors and we want pretty errors.
 257       *
 258       * @param string $message
 259       * @param string $classes
 260       * @return string
 261       */
 262      public function notification($message, $classes = 'notifyproblem') {
 263          $message = clean_text($message);
 264          $type = '';
 265  
 266          if (($classes == 'notifyproblem') || ($classes == 'notifytiny')) {
 267              $type = 'alert alert-error';
 268          }
 269          if ($classes == 'notifysuccess') {
 270              $type = 'alert alert-success';
 271          }
 272          if ($classes == 'notifymessage') {
 273              $type = 'alert alert-info';
 274          }
 275          if ($classes == 'redirectmessage') {
 276              $type = 'alert alert-block alert-info';
 277          }
 278          return "<div class=\"$type\">$message</div>";
 279      }
 280  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1