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