PageRenderTime 62ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/blocks/navigation/renderer.php

https://github.com/pauln/moodle
PHP | 211 lines | 128 code | 20 blank | 63 comment | 52 complexity | 2201b3987a39b18838ff40092e32c564 MD5 | raw file
  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. * Outputs the navigation tree.
  18. *
  19. * @since Moodle 2.0
  20. * @package block_navigation
  21. * @copyright 2009 Sam Hemelryk
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. /**
  25. * Renderer for block navigation
  26. *
  27. * @package block_navigation
  28. * @category navigation
  29. * @copyright 2009 Sam Hemelryk
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class block_navigation_renderer extends plugin_renderer_base {
  33. /**
  34. * Returns the content of the navigation tree.
  35. *
  36. * @param global_navigation $navigation
  37. * @param int $expansionlimit
  38. * @param array $options
  39. * @return string $content
  40. */
  41. public function navigation_tree(global_navigation $navigation, $expansionlimit, array $options = array()) {
  42. $navigation->add_class('navigation_node');
  43. $navigationattrs = array(
  44. 'class' => 'block_tree list',
  45. 'role' => 'tree',
  46. 'data-ajax-loader' => 'block_navigation/nav_loader');
  47. $content = $this->navigation_node(array($navigation), $navigationattrs, $expansionlimit, $options);
  48. if (isset($navigation->id) && !is_numeric($navigation->id) && !empty($content)) {
  49. $content = $this->output->box($content, 'block_tree_box', $navigation->id);
  50. }
  51. return $content;
  52. }
  53. /**
  54. * Produces a navigation node for the navigation tree
  55. *
  56. * @param navigation_node[] $items
  57. * @param array $attrs
  58. * @param int $expansionlimit
  59. * @param array $options
  60. * @param int $depth
  61. * @return string
  62. */
  63. protected function navigation_node($items, $attrs=array(), $expansionlimit=null, array $options = array(), $depth=1) {
  64. // Exit if empty, we don't want an empty ul element.
  65. if (count($items) === 0) {
  66. return '';
  67. }
  68. // Turn our navigation items into list items.
  69. $lis = array();
  70. // Set the number to be static for unique id's.
  71. static $number = 0;
  72. foreach ($items as $item) {
  73. $number++;
  74. if (!$item->display && !$item->contains_active_node()) {
  75. continue;
  76. }
  77. $content = $item->get_content();
  78. $title = $item->get_title();
  79. $isexpandable = (empty($expansionlimit) || ($item->type > navigation_node::TYPE_ACTIVITY || $item->type < $expansionlimit) || ($item->contains_active_node() && $item->children->count() > 0));
  80. $isbranch = $isexpandable && ($item->children->count() > 0 || ($item->has_children() && (isloggedin() || $item->type <= navigation_node::TYPE_CATEGORY)));
  81. // Skip elements which have no content and no action - no point in showing them
  82. if (!$isexpandable && empty($item->action)) {
  83. continue;
  84. }
  85. $hasicon = ((!$isbranch || $item->type == navigation_node::TYPE_ACTIVITY || $item->type == navigation_node::TYPE_RESOURCE) && $item->icon instanceof renderable);
  86. if ($hasicon) {
  87. $icon = $this->output->render($item->icon);
  88. // Because an icon is being used we're going to wrap the actual content in a span.
  89. // This will allow designers to create columns for the content, as we've done in styles.css.
  90. $content = $icon . html_writer::span($content, 'item-content-wrap');
  91. } else {
  92. $icon = '';
  93. }
  94. if ($item->helpbutton !== null) {
  95. $content = trim($item->helpbutton).html_writer::tag('span', $content, array('class'=>'clearhelpbutton'));
  96. }
  97. if ($content === '') {
  98. continue;
  99. }
  100. $nodetextid = 'label_' . $depth . '_' . $number;
  101. $attributes = array('tabindex' => '-1', 'id' => $nodetextid);
  102. if ($title !== '') {
  103. $attributes['title'] = $title;
  104. }
  105. if ($item->hidden) {
  106. $attributes['class'] = 'dimmed_text';
  107. }
  108. if (is_string($item->action) || empty($item->action) ||
  109. (($item->type === navigation_node::TYPE_CATEGORY || $item->type === navigation_node::TYPE_MY_CATEGORY) &&
  110. empty($options['linkcategories']))) {
  111. $content = html_writer::tag('span', $content, $attributes);
  112. } else if ($item->action instanceof action_link) {
  113. //TODO: to be replaced with something else
  114. $link = $item->action;
  115. $link->text = $icon.html_writer::span($link->text, 'item-content-wrap');
  116. $link->attributes = array_merge($link->attributes, $attributes);
  117. $content = $this->output->render($link);
  118. } else if ($item->action instanceof moodle_url) {
  119. $content = html_writer::link($item->action, $content, $attributes);
  120. }
  121. // This applies to the li item which contains all child lists too.
  122. $liclasses = array($item->get_css_type(), 'depth_'.$depth);
  123. // Class attribute on the div item which only contains the item content.
  124. $divclasses = array('tree_item');
  125. $liexpandable = array();
  126. $lirole = array('role' => 'treeitem');
  127. if ($isbranch) {
  128. $liclasses[] = 'contains_branch';
  129. if ($depth == 1) {
  130. $liexpandable = array(
  131. 'data-expandable' => 'false',
  132. 'data-collapsible' => 'false'
  133. );
  134. } else {
  135. $liexpandable = array(
  136. 'aria-expanded' => ($item->has_children() &&
  137. (!$item->forceopen || $item->collapse)) ? "false" : "true");
  138. }
  139. if ($item->requiresajaxloading) {
  140. $liexpandable['data-requires-ajax'] = 'true';
  141. $liexpandable['data-loaded'] = 'false';
  142. $liexpandable['data-node-id'] = $item->id;
  143. $liexpandable['data-node-key'] = $item->key;
  144. $liexpandable['data-node-type'] = $item->type;
  145. }
  146. $divclasses[] = 'branch';
  147. } else {
  148. $divclasses[] = 'leaf';
  149. }
  150. if ($hasicon) {
  151. // Add this class if the item has an icon, whether it is a branch or not.
  152. $liclasses[] = 'item_with_icon';
  153. $divclasses[] = 'hasicon';
  154. }
  155. if ($item->isactive === true) {
  156. $liclasses[] = 'current_branch';
  157. }
  158. if (!empty($item->classes) && count($item->classes)>0) {
  159. $divclasses[] = join(' ', $item->classes);
  160. }
  161. // Now build attribute arrays.
  162. $liattr = array('class' => join(' ', $liclasses)) + $liexpandable + $lirole;
  163. $divattr = array('class'=>join(' ', $divclasses));
  164. if (!empty($item->id)) {
  165. $divattr['id'] = $item->id;
  166. }
  167. // Create the structure.
  168. $content = html_writer::tag('p', $content, $divattr);
  169. if ($isexpandable) {
  170. $content .= $this->navigation_node($item->children, array('role' => 'group'), $expansionlimit,
  171. $options, $depth + 1);
  172. }
  173. if (!empty($item->preceedwithhr) && $item->preceedwithhr===true) {
  174. $content = html_writer::empty_tag('hr') . $content;
  175. }
  176. if ($depth == 1) {
  177. $liattr['tabindex'] = '0';
  178. }
  179. $liattr['aria-labelledby'] = $nodetextid;
  180. $content = html_writer::tag('li', $content, $liattr);
  181. $lis[] = $content;
  182. }
  183. if (count($lis) === 0) {
  184. // There is still a chance, despite having items, that nothing had content and no list items were created.
  185. return '';
  186. }
  187. // We used to separate using new lines, however we don't do that now, instead we'll save a few chars.
  188. // The source is complex already anyway.
  189. return html_writer::tag('ul', implode('', $lis), $attrs);
  190. }
  191. }