block-navigation MDL-23838 There is now an option that you can enable so that categor...
authorSam Hemelryk <sam@moodle.com>
Thu, 2 Dec 2010 07:13:07 +0000 (07:13 +0000)
committerSam Hemelryk <sam@moodle.com>
Thu, 2 Dec 2010 07:13:07 +0000 (07:13 +0000)
blocks/navigation/block_navigation.php
blocks/navigation/edit_form.php
blocks/navigation/lang/en/block_navigation.php
blocks/navigation/renderer.php
lib/ajax/getnavbranch.php
lib/navigationlib.php

index 89afeea..73522fe 100644 (file)
@@ -180,9 +180,12 @@ class block_navigation extends block_base {
         $arguments = array($this->instance->id, array('expansions'=>$expandable, 'instance'=>$this->instance->id, 'candock'=>$this->instance_can_be_docked(), 'courselimit'=>$limit));
         $this->page->requires->js_init_call('M.block_navigation.init_add_tree', $arguments, false, $module);
 
+        $options = array();
+        $options['linkcategories'] = (!empty($this->config->linkcategories) && $this->config->linkcategories == 'yes');
+        
         // Grab the items to display
         $renderer = $this->page->get_renderer('block_navigation');
-        $this->content->text = $renderer->navigation_tree($navigation, $expansionlimit);
+        $this->content->text = $renderer->navigation_tree($navigation, $expansionlimit, $options);
 
         // Set content generated to true so that we know it has been done
         $this->contentgenerated = true;
index 91733e3..0d36b6b 100644 (file)
@@ -39,7 +39,7 @@ class block_navigation_edit_form extends block_edit_form {
         global $CFG;
         $mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));
 
-        $mods = array('enabledock'=>'yes');
+        $mods = array('enabledock'=>'yes', 'linkcategories'=>'no');
         $yesnooptions = array('yes'=>get_string('yes'), 'no'=>get_string('no'));
         foreach ($mods as $modname=>$default) {
             $mform->addElement('select', 'config_'.$modname, get_string($modname.'desc', $this->block->blockname), $yesnooptions);
index b6496c1..1fb2aa3 100644 (file)
@@ -30,6 +30,7 @@ $string['coursestructures'] = 'Categories, courses, and course structures';
 $string['courseactivities'] = 'Categories, courses, and course Activities';
 $string['enabledockdesc'] = 'Allow the user to dock this block';
 $string['expansionlimit'] = 'Generate navigation for the following';
+$string['linkcategoriesdesc'] = 'Display categories as links';
 $string['pluginname'] = 'Navigation';
 $string['trimmode'] = 'Trim mode';
 $string['trimmoderight'] = 'Trim characters from the right';
index 13d6aa3..6ef43a4 100644 (file)
@@ -1,17 +1,17 @@
 <?php
 
 class block_navigation_renderer extends plugin_renderer_base {
-
-    public function navigation_tree(global_navigation $navigation, $expansionlimit) {
+    
+    public function navigation_tree(global_navigation $navigation, $expansionlimit, array $options = array()) {
         $navigation->add_class('navigation_node');
-        $content = $this->navigation_node(array($navigation), array('class'=>'block_tree list'), $expansionlimit);
+        $content = $this->navigation_node(array($navigation), array('class'=>'block_tree list'), $expansionlimit, $options);
         if (isset($navigation->id) && !is_numeric($navigation->id) && !empty($content)) {
             $content = $this->output->box($content, 'block_tree_box', $navigation->id);
         }
         return $content;
     }
 
-    protected function navigation_node($items, $attrs=array(), $expansionlimit=null, $depth=1) {
+    protected function navigation_node($items, $attrs=array(), $expansionlimit=null, array $options = array(), $depth=1) {
 
         // exit if empty, we don't want an empty ul element
         if (count($items)==0) {
@@ -41,32 +41,23 @@ class block_navigation_renderer extends plugin_renderer_base {
                 continue;
             }
 
-            if ($item->action instanceof action_link) {
+            $attributes = array();
+            if ($title !== '') {
+                $attributes['title'] = $title;
+            }
+            if ($item->hidden) {
+                $attributes['class'] = 'dimmed_text';
+            }
+            if (is_string($item->action) || empty($item->action) || ($item->type === navigation_node::TYPE_CATEGORY && empty($options['linkcategories']))) {
+                $content = html_writer::tag('span', $content, $attributes);
+            } else if ($item->action instanceof action_link) {
                 //TODO: to be replaced with something else
                 $link = $item->action;
-                if ($item->hidden) {
-                    $link->add_class('dimmed');
-                }
+                $link->attributes = array_merge($link->attributes, $attributes);
                 $content = $this->output->render($link);
+                $linkrendered = true;
             } else if ($item->action instanceof moodle_url) {
-                $attributes = array();
-                if ($title !== '') {
-                    $attributes['title'] = $title;
-                }
-                if ($item->hidden) {
-                    $attributes['class'] = 'dimmed_text';
-                }
                 $content = html_writer::link($item->action, $content, $attributes);
-
-            } else if (is_string($item->action) || empty($item->action)) {
-                $attributes = array();
-                if ($title !== '') {
-                    $attributes['title'] = $title;
-                }
-                if ($item->hidden) {
-                    $attributes['class'] = 'dimmed_text';
-                }
-                $content = html_writer::tag('span', $content, $attributes);
             }
 
             // this applies to the li item which contains all child lists too
@@ -100,7 +91,7 @@ class block_navigation_renderer extends plugin_renderer_base {
             if (!empty($item->id)) {
                 $divattr['id'] = $item->id;
             }
-            $content = html_writer::tag('p', $content, $divattr) . $this->navigation_node($item->children, array(), $expansionlimit, $depth+1);
+            $content = html_writer::tag('p', $content, $divattr) . $this->navigation_node($item->children, array(), $expansionlimit, $options, $depth+1);
             if (!empty($item->preceedwithhr) && $item->preceedwithhr===true) {
                 $content = html_writer::empty_tag('hr') . $content;
             }
index 3e4390c..623f790 100644 (file)
@@ -47,6 +47,8 @@ try {
     // Create a global nav object
     $navigation = new global_navigation_for_ajax($PAGE, $branchtype, $branchid);
 
+    $linkcategories = false;
+    
     if ($instanceid!==null) {
         // Get the db record for the block instance
         $blockrecord = $DB->get_record('block_instances', array('id'=>$instanceid,'blockname'=>'navigation'));
@@ -66,6 +68,9 @@ try {
             if (!empty($block->config->trimlength)) {
                 $trimlength = (int)$block->config->trimlength;
             }
+            if (!empty($block->config->linkcategories) && $block->config->linkcategories == 'yes') {
+                $linkcategories = true;
+            }
         }
     }
 
@@ -85,6 +90,13 @@ try {
     // Find the actuall branch we are looking for
     $branch = $navigation->find($branchid, $branchtype);
     
+    // Remove links to categories if required.
+    if (!$linkcategories) {
+        foreach ($branch->find_all_of_type(navigation_node::TYPE_CATEGORY) as $category) {
+            $category->action = null;
+        }
+    }
+    
     // Stop buffering errors at this point
     $html = ob_get_contents();
     ob_end_clean();
index 6b0f163..4e759f2 100644 (file)
@@ -1284,7 +1284,7 @@ class global_navigation extends navigation_node {
         if (!$categorynode) {
             $category = $cat['category'];
             $url = new moodle_url('/course/category.php', array('id'=>$category->id));
-            $categorynode = $parent->add($category->name, null, self::TYPE_CATEGORY, $category->name, $category->id);
+            $categorynode = $parent->add($category->name, $url, self::TYPE_CATEGORY, $category->name, $category->id);
             if (empty($category->visible)) {
                 if (has_capability('moodle/category:viewhiddencategories', get_system_context())) {
                     $categorynode->hidden = true;