|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 class block_navigation_renderer extends plugin_renderer_base { 00004 00005 public function navigation_tree(global_navigation $navigation, $expansionlimit, array $options = array()) { 00006 $navigation->add_class('navigation_node'); 00007 $content = $this->navigation_node(array($navigation), array('class'=>'block_tree list'), $expansionlimit, $options); 00008 if (isset($navigation->id) && !is_numeric($navigation->id) && !empty($content)) { 00009 $content = $this->output->box($content, 'block_tree_box', $navigation->id); 00010 } 00011 return $content; 00012 } 00013 00014 protected function navigation_node($items, $attrs=array(), $expansionlimit=null, array $options = array(), $depth=1) { 00015 00016 // exit if empty, we don't want an empty ul element 00017 if (count($items)==0) { 00018 return ''; 00019 } 00020 00021 // array of nested li elements 00022 $lis = array(); 00023 foreach ($items as $item) { 00024 if (!$item->display && !$item->contains_active_node()) { 00025 continue; 00026 } 00027 $content = $item->get_content(); 00028 $title = $item->get_title(); 00029 00030 $isexpandable = (empty($expansionlimit) || ($item->type > navigation_node::TYPE_ACTIVITY || $item->type < $expansionlimit) || ($item->contains_active_node() && $item->children->count() > 0)); 00031 $isbranch = $isexpandable && ($item->children->count() > 0 || ($item->has_children() && (isloggedin() || $item->type <= navigation_node::TYPE_CATEGORY))); 00032 00033 $hasicon = ((!$isbranch || $item->type == navigation_node::TYPE_ACTIVITY || $item->type == navigation_node::TYPE_RESOURCE) && $item->icon instanceof renderable); 00034 00035 if ($hasicon) { 00036 $icon = $this->output->render($item->icon); 00037 } else { 00038 $icon = ''; 00039 } 00040 $content = $icon.$content; // use CSS for spacing of icons 00041 if ($item->helpbutton !== null) { 00042 $content = trim($item->helpbutton).html_writer::tag('span', $content, array('class'=>'clearhelpbutton')); 00043 } 00044 00045 if ($content === '') { 00046 continue; 00047 } 00048 00049 $attributes = array(); 00050 if ($title !== '') { 00051 $attributes['title'] = $title; 00052 } 00053 if ($item->hidden) { 00054 $attributes['class'] = 'dimmed_text'; 00055 } 00056 if (is_string($item->action) || empty($item->action) || ($item->type === navigation_node::TYPE_CATEGORY && empty($options['linkcategories']))) { 00057 $attributes['tabindex'] = '0'; //add tab support to span but still maintain character stream sequence. 00058 $content = html_writer::tag('span', $content, $attributes); 00059 } else if ($item->action instanceof action_link) { 00060 //TODO: to be replaced with something else 00061 $link = $item->action; 00062 $link->text = $icon.$link->text; 00063 $link->attributes = array_merge($link->attributes, $attributes); 00064 $content = $this->output->render($link); 00065 $linkrendered = true; 00066 } else if ($item->action instanceof moodle_url) { 00067 $content = html_writer::link($item->action, $content, $attributes); 00068 } 00069 00070 // this applies to the li item which contains all child lists too 00071 $liclasses = array($item->get_css_type(), 'depth_'.$depth); 00072 if ($item->has_children() && (!$item->forceopen || $item->collapse)) { 00073 $liclasses[] = 'collapsed'; 00074 } 00075 if ($isbranch) { 00076 $liclasses[] = 'contains_branch'; 00077 } else if ($hasicon) { 00078 $liclasses[] = 'item_with_icon'; 00079 } 00080 if ($item->isactive === true) { 00081 $liclasses[] = 'current_branch'; 00082 } 00083 $liattr = array('class'=>join(' ',$liclasses)); 00084 // class attribute on the div item which only contains the item content 00085 $divclasses = array('tree_item'); 00086 if ($isbranch) { 00087 $divclasses[] = 'branch'; 00088 } else { 00089 $divclasses[] = 'leaf'; 00090 } 00091 if ($hasicon) { 00092 $divclasses[] = 'hasicon'; 00093 } 00094 if (!empty($item->classes) && count($item->classes)>0) { 00095 $divclasses[] = join(' ', $item->classes); 00096 } 00097 $divattr = array('class'=>join(' ', $divclasses)); 00098 if (!empty($item->id)) { 00099 $divattr['id'] = $item->id; 00100 } 00101 $content = html_writer::tag('p', $content, $divattr); 00102 if ($isexpandable) { 00103 $content .= $this->navigation_node($item->children, array(), $expansionlimit, $options, $depth+1); 00104 } 00105 if (!empty($item->preceedwithhr) && $item->preceedwithhr===true) { 00106 $content = html_writer::empty_tag('hr') . $content; 00107 } 00108 $content = html_writer::tag('li', $content, $liattr); 00109 $lis[] = $content; 00110 } 00111 00112 if (count($lis)) { 00113 return html_writer::tag('ul', implode("\n", $lis), $attrs); 00114 } else { 00115 return ''; 00116 } 00117 } 00118 00119 }