[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 * Returns the content of the navigation tree. 37 * 38 * @param global_navigation $navigation 39 * @param int $expansionlimit 40 * @param array $options 41 * @return string $content 42 */ 43 public function navigation_tree(global_navigation $navigation, $expansionlimit, array $options = array()) { 44 $navigation->add_class('navigation_node'); 45 $content = $this->navigation_node(array($navigation), array('class'=>'block_tree list'), $expansionlimit, $options); 46 if (isset($navigation->id) && !is_numeric($navigation->id) && !empty($content)) { 47 $content = $this->output->box($content, 'block_tree_box', $navigation->id); 48 } 49 return $content; 50 } 51 /** 52 * Produces a navigation node for the navigation tree 53 * 54 * @param array $items 55 * @param array $attrs 56 * @param int $expansionlimit 57 * @param array $options 58 * @param int $depth 59 * @return string 60 */ 61 protected function navigation_node($items, $attrs=array(), $expansionlimit=null, array $options = array(), $depth=1) { 62 63 // exit if empty, we don't want an empty ul element 64 if (count($items)==0) { 65 return ''; 66 } 67 68 // array of nested li elements 69 $lis = array(); 70 foreach ($items as $item) { 71 if (!$item->display && !$item->contains_active_node()) { 72 continue; 73 } 74 $content = $item->get_content(); 75 $title = $item->get_title(); 76 77 $isexpandable = (empty($expansionlimit) || ($item->type > navigation_node::TYPE_ACTIVITY || $item->type < $expansionlimit) || ($item->contains_active_node() && $item->children->count() > 0)); 78 $isbranch = $isexpandable && ($item->children->count() > 0 || ($item->has_children() && (isloggedin() || $item->type <= navigation_node::TYPE_CATEGORY))); 79 80 // Skip elements which have no content and no action - no point in showing them 81 if (!$isexpandable && empty($item->action)) { 82 continue; 83 } 84 85 $hasicon = ((!$isbranch || $item->type == navigation_node::TYPE_ACTIVITY || $item->type == navigation_node::TYPE_RESOURCE) && $item->icon instanceof renderable); 86 87 if ($hasicon) { 88 $icon = $this->output->render($item->icon); 89 } else { 90 $icon = ''; 91 } 92 $content = $icon.$content; // use CSS for spacing of icons 93 if ($item->helpbutton !== null) { 94 $content = trim($item->helpbutton).html_writer::tag('span', $content, array('class'=>'clearhelpbutton')); 95 } 96 97 if ($content === '') { 98 continue; 99 } 100 101 $attributes = array(); 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 $attributes['tabindex'] = '0'; //add tab support to span but still maintain character stream sequence. 112 $content = html_writer::tag('span', $content, $attributes); 113 } else if ($item->action instanceof action_link) { 114 //TODO: to be replaced with something else 115 $link = $item->action; 116 $link->text = $icon.$link->text; 117 $link->attributes = array_merge($link->attributes, $attributes); 118 $content = $this->output->render($link); 119 $linkrendered = true; 120 } else if ($item->action instanceof moodle_url) { 121 $content = html_writer::link($item->action, $content, $attributes); 122 } 123 124 // this applies to the li item which contains all child lists too 125 $liclasses = array($item->get_css_type(), 'depth_'.$depth); 126 $liexpandable = array(); 127 if ($item->has_children() && (!$item->forceopen || $item->collapse)) { 128 $liclasses[] = 'collapsed'; 129 } 130 if ($isbranch) { 131 $liclasses[] = 'contains_branch'; 132 $liexpandable = array('aria-expanded' => in_array('collapsed', $liclasses) ? "false" : "true"); 133 } else if ($hasicon) { 134 $liclasses[] = 'item_with_icon'; 135 } 136 if ($item->isactive === true) { 137 $liclasses[] = 'current_branch'; 138 } 139 $liattr = array('class' => join(' ',$liclasses)) + $liexpandable; 140 // class attribute on the div item which only contains the item content 141 $divclasses = array('tree_item'); 142 if ($isbranch) { 143 $divclasses[] = 'branch'; 144 } else { 145 $divclasses[] = 'leaf'; 146 } 147 if ($hasicon) { 148 $divclasses[] = 'hasicon'; 149 } 150 if (!empty($item->classes) && count($item->classes)>0) { 151 $divclasses[] = join(' ', $item->classes); 152 } 153 $divattr = array('class'=>join(' ', $divclasses)); 154 if (!empty($item->id)) { 155 $divattr['id'] = $item->id; 156 } 157 $content = html_writer::tag('p', $content, $divattr); 158 if ($isexpandable) { 159 $content .= $this->navigation_node($item->children, array(), $expansionlimit, $options, $depth+1); 160 } 161 if (!empty($item->preceedwithhr) && $item->preceedwithhr===true) { 162 $content = html_writer::empty_tag('hr') . $content; 163 } 164 $content = html_writer::tag('li', $content, $liattr); 165 $lis[] = $content; 166 } 167 168 if (count($lis)) { 169 return html_writer::tag('ul', implode("\n", $lis), $attrs); 170 } else { 171 return ''; 172 } 173 } 174 175 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |