[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/blocks/navigation/ -> block_navigation.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   * This file contains classes used to manage the navigation structures in Moodle
  19   * and was introduced as part of the changes occuring in Moodle 2.0
  20   *
  21   * @since     Moodle 2.0
  22   * @package   block_navigation
  23   * @copyright 2009 Sam Hemelryk
  24   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  /**
  28   * The global navigation tree block class
  29   *
  30   * Used to produce the global navigation block new to Moodle 2.0
  31   *
  32   * @package   block_navigation
  33   * @category  navigation
  34   * @copyright 2009 Sam Hemelryk
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class block_navigation extends block_base {
  38  
  39      /** @var int This allows for multiple navigation trees */
  40      public static $navcount;
  41      /** @var string The name of the block */
  42      public $blockname = null;
  43      /** @var bool A switch to indicate whether content has been generated or not. */
  44      protected $contentgenerated = false;
  45      /** @var bool|null variable for checking if the block is docked*/
  46      protected $docked = null;
  47  
  48      /** @var int Trim characters from the right */
  49      const TRIM_RIGHT = 1;
  50      /** @var int Trim characters from the left */
  51      const TRIM_LEFT = 2;
  52      /** @var int Trim characters from the center */
  53      const TRIM_CENTER = 3;
  54  
  55      /**
  56       * Set the initial properties for the block
  57       */
  58      function init() {
  59          $this->blockname = get_class($this);
  60          $this->title = get_string('pluginname', $this->blockname);
  61      }
  62  
  63      /**
  64       * All multiple instances of this block
  65       * @return bool Returns false
  66       */
  67      function instance_allow_multiple() {
  68          return false;
  69      }
  70  
  71      /**
  72       * Set the applicable formats for this block to all
  73       * @return array
  74       */
  75      function applicable_formats() {
  76          return array('all' => true);
  77      }
  78  
  79      /**
  80       * Allow the user to configure a block instance
  81       * @return bool Returns true
  82       */
  83      function instance_allow_config() {
  84          return true;
  85      }
  86  
  87      /**
  88       * The navigation block cannot be hidden by default as it is integral to
  89       * the navigation of Moodle.
  90       *
  91       * @return false
  92       */
  93      function  instance_can_be_hidden() {
  94          return false;
  95      }
  96  
  97      /**
  98       * Find out if an instance can be docked.
  99       *
 100       * @return bool true or false depending on whether the instance can be docked or not.
 101       */
 102      function instance_can_be_docked() {
 103          return (parent::instance_can_be_docked() && (empty($this->config->enabledock) || $this->config->enabledock=='yes'));
 104      }
 105  
 106      /**
 107       * Gets Javascript that may be required for navigation
 108       */
 109      function get_required_javascript() {
 110          global $CFG;
 111          parent::get_required_javascript();
 112          $limit = 20;
 113          if (!empty($CFG->navcourselimit)) {
 114              $limit = $CFG->navcourselimit;
 115          }
 116          $expansionlimit = 0;
 117          if (!empty($this->config->expansionlimit)) {
 118              $expansionlimit = $this->config->expansionlimit;
 119          }
 120          $arguments = array(
 121              'id'             => $this->instance->id,
 122              'instance'       => $this->instance->id,
 123              'candock'        => $this->instance_can_be_docked(),
 124              'courselimit'    => $limit,
 125              'expansionlimit' => $expansionlimit
 126          );
 127          $this->page->requires->string_for_js('viewallcourses', 'moodle');
 128          $this->page->requires->yui_module('moodle-block_navigation-navigation', 'M.block_navigation.init_add_tree', array($arguments));
 129      }
 130  
 131      /**
 132       * Gets the content for this block by grabbing it from $this->page
 133       *
 134       * @return object $this->content
 135       */
 136      function get_content() {
 137          // First check if we have already generated, don't waste cycles
 138          if ($this->contentgenerated === true) {
 139              return $this->content;
 140          }
 141          // JS for navigation moved to the standard theme, the code will probably have to depend on the actual page structure
 142          // $this->page->requires->js('/lib/javascript-navigation.js');
 143          // Navcount is used to allow us to have multiple trees although I dont' know why
 144          // you would want two trees the same
 145  
 146          block_navigation::$navcount++;
 147  
 148          // Check if this block has been docked
 149          if ($this->docked === null) {
 150              $this->docked = get_user_preferences('nav_in_tab_panel_globalnav'.block_navigation::$navcount, 0);
 151          }
 152  
 153          // Check if there is a param to change the docked state
 154          if ($this->docked && optional_param('undock', null, PARAM_INT)==$this->instance->id) {
 155              unset_user_preference('nav_in_tab_panel_globalnav'.block_navigation::$navcount);
 156              $url = $this->page->url;
 157              $url->remove_params(array('undock'));
 158              redirect($url);
 159          } else if (!$this->docked && optional_param('dock', null, PARAM_INT)==$this->instance->id) {
 160              set_user_preferences(array('nav_in_tab_panel_globalnav'.block_navigation::$navcount=>1));
 161              $url = $this->page->url;
 162              $url->remove_params(array('dock'));
 163              redirect($url);
 164          }
 165  
 166          $trimmode = self::TRIM_RIGHT;
 167          $trimlength = 50;
 168  
 169          if (!empty($this->config->trimmode)) {
 170              $trimmode = (int)$this->config->trimmode;
 171          }
 172  
 173          if (!empty($this->config->trimlength)) {
 174              $trimlength = (int)$this->config->trimlength;
 175          }
 176  
 177          // Get the navigation object or don't display the block if none provided.
 178          if (!$navigation = $this->get_navigation()) {
 179              return null;
 180          }
 181          $expansionlimit = null;
 182          if (!empty($this->config->expansionlimit)) {
 183              $expansionlimit = $this->config->expansionlimit;
 184              $navigation->set_expansion_limit($this->config->expansionlimit);
 185          }
 186          $this->trim($navigation, $trimmode, $trimlength, ceil($trimlength/2));
 187  
 188          // Get the expandable items so we can pass them to JS
 189          $expandable = array();
 190          $navigation->find_expandable($expandable);
 191          if ($expansionlimit) {
 192              foreach ($expandable as $key=>$node) {
 193                  if ($node['type'] > $expansionlimit && !($expansionlimit == navigation_node::TYPE_COURSE && $node['type'] == $expansionlimit && $node['branchid'] == SITEID)) {
 194                      unset($expandable[$key]);
 195                  }
 196              }
 197          }
 198  
 199          $this->page->requires->data_for_js('navtreeexpansions'.$this->instance->id, $expandable);
 200  
 201          $options = array();
 202          $options['linkcategories'] = (!empty($this->config->linkcategories) && $this->config->linkcategories == 'yes');
 203  
 204          // Grab the items to display
 205          $renderer = $this->page->get_renderer($this->blockname);
 206          $this->content = new stdClass();
 207          $this->content->text = $renderer->navigation_tree($navigation, $expansionlimit, $options);
 208  
 209          // Set content generated to true so that we know it has been done
 210          $this->contentgenerated = true;
 211  
 212          return $this->content;
 213      }
 214  
 215      /**
 216       * Returns the navigation
 217       *
 218       * @return navigation_node The navigation object to display
 219       */
 220      protected function get_navigation() {
 221          // Initialise (only actually happens if it hasn't already been done yet)
 222          $this->page->navigation->initialise();
 223          return clone($this->page->navigation);
 224      }
 225  
 226      /**
 227       * Returns the attributes to set for this block
 228       *
 229       * This function returns an array of HTML attributes for this block including
 230       * the defaults.
 231       * {@link block_tree::html_attributes()} is used to get the default arguments
 232       * and then we check whether the user has enabled hover expansion and add the
 233       * appropriate hover class if it has.
 234       *
 235       * @return array An array of HTML attributes
 236       */
 237      public function html_attributes() {
 238          $attributes = parent::html_attributes();
 239          if (!empty($this->config->enablehoverexpansion) && $this->config->enablehoverexpansion == 'yes') {
 240              $attributes['class'] .= ' block_js_expansion';
 241          }
 242          return $attributes;
 243      }
 244  
 245      /**
 246       * Trims the text and shorttext properties of this node and optionally
 247       * all of its children.
 248       *
 249       * @param navigation_node $node
 250       * @param int $mode One of navigation_node::TRIM_*
 251       * @param int $long The length to trim text to
 252       * @param int $short The length to trim shorttext to
 253       * @param bool $recurse Recurse all children
 254       */
 255      public function trim(navigation_node $node, $mode=1, $long=50, $short=25, $recurse=true) {
 256          switch ($mode) {
 257              case self::TRIM_RIGHT :
 258                  if (core_text::strlen($node->text)>($long+3)) {
 259                      // Truncate the text to $long characters
 260                      $node->text = $this->trim_right($node->text, $long);
 261                  }
 262                  if (is_string($node->shorttext) && core_text::strlen($node->shorttext)>($short+3)) {
 263                      // Truncate the shorttext
 264                      $node->shorttext = $this->trim_right($node->shorttext, $short);
 265                  }
 266                  break;
 267              case self::TRIM_LEFT :
 268                  if (core_text::strlen($node->text)>($long+3)) {
 269                      // Truncate the text to $long characters
 270                      $node->text = $this->trim_left($node->text, $long);
 271                  }
 272                  if (is_string($node->shorttext) && core_text::strlen($node->shorttext)>($short+3)) {
 273                      // Truncate the shorttext
 274                      $node->shorttext = $this->trim_left($node->shorttext, $short);
 275                  }
 276                  break;
 277              case self::TRIM_CENTER :
 278                  if (core_text::strlen($node->text)>($long+3)) {
 279                      // Truncate the text to $long characters
 280                      $node->text = $this->trim_center($node->text, $long);
 281                  }
 282                  if (is_string($node->shorttext) && core_text::strlen($node->shorttext)>($short+3)) {
 283                      // Truncate the shorttext
 284                      $node->shorttext = $this->trim_center($node->shorttext, $short);
 285                  }
 286                  break;
 287          }
 288          if ($recurse && $node->children->count()) {
 289              foreach ($node->children as &$child) {
 290                  $this->trim($child, $mode, $long, $short, true);
 291              }
 292          }
 293      }
 294      /**
 295       * Truncate a string from the left
 296       * @param string $string The string to truncate
 297       * @param int $length The length to truncate to
 298       * @return string The truncated string
 299       */
 300      protected function trim_left($string, $length) {
 301          return '...'.core_text::substr($string, core_text::strlen($string)-$length, $length);
 302      }
 303      /**
 304       * Truncate a string from the right
 305       * @param string $string The string to truncate
 306       * @param int $length The length to truncate to
 307       * @return string The truncated string
 308       */
 309      protected function trim_right($string, $length) {
 310          return core_text::substr($string, 0, $length).'...';
 311      }
 312      /**
 313       * Truncate a string in the center
 314       * @param string $string The string to truncate
 315       * @param int $length The length to truncate to
 316       * @return string The truncated string
 317       */
 318      protected function trim_center($string, $length) {
 319          $trimlength = ceil($length/2);
 320          $start = core_text::substr($string, 0, $trimlength);
 321          $end = core_text::substr($string, core_text::strlen($string)-$trimlength);
 322          $string = $start.'...'.$end;
 323          return $string;
 324      }
 325  
 326      /**
 327       * Returns the role that best describes the navigation block... 'navigation'
 328       *
 329       * @return string 'navigation'
 330       */
 331      public function get_aria_role() {
 332          return 'navigation';
 333      }
 334  }


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