navigation MDL-22044 Added setting to limit content that is shown on the navigation...
authorSam Hemelryk <sam@moodle.com>
Wed, 21 Apr 2010 09:44:16 +0000 (09:44 +0000)
committerSam Hemelryk <sam@moodle.com>
Wed, 21 Apr 2010 09:44:16 +0000 (09:44 +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/blocklib.php
lib/navigationlib.php

index c71ec12..31daa95 100644 (file)
@@ -146,6 +146,11 @@ class block_navigation extends block_base {
         // Initialise (only actually happens if it hasn't already been done yet
         $this->page->navigation->initialise();
         $navigation = clone($this->page->navigation);
+        $expansionlimit = null;
+        if (!empty($this->config->expansionlimit)) {
+            $expansionlimit = $this->config->expansionlimit;
+            $navigation->set_expansion_limit($this->config->expansionlimit);
+        }
         $this->trim($navigation, $trimmode, $trimlength, ceil($trimlength/2));
 
         if (!empty($this->config->showmyhistory) && $this->config->showmyhistory=='yes') {
@@ -154,7 +159,7 @@ class block_navigation extends block_base {
 
         // Get the expandable items so we can pass them to JS
         $expandable = array();
-        $this->page->navigation->find_expandable($expandable);
+        $navigation->find_expandable($expandable);
 
         // Initialise the JS tree object
         $module = array('name'=>'block_navigation', 'fullpath'=>'/blocks/navigation/navigation.js', 'requires'=>array('core_dock', 'io', 'node', 'dom', 'event-custom', 'json-parse'));
@@ -163,7 +168,7 @@ class block_navigation extends block_base {
 
         // Grab the items to display
         $renderer = $this->page->get_renderer('block_navigation');
-        $this->content->text = $renderer->navigation_tree($this->page->navigation);
+        $this->content->text = $renderer->navigation_tree($navigation, $expansionlimit);
 
         $reloadlink = new moodle_url($this->page->url, array('regenerate'=>'navigation'));
 
index b739b13..64b1320 100644 (file)
@@ -57,5 +57,15 @@ class block_navigation_edit_form extends block_edit_form {
         $mform->addElement('text', 'config_trimlength', get_string('trimlength', $this->block->blockname));
         $mform->setDefault('config_trimlength', 50);
         $mform->setType('config_trimlength', PARAM_INT);
+
+        $options = array(
+            0 => get_string('everything', $this->block->blockname),
+            global_navigation::TYPE_COURSE => get_string('courses', $this->block->blockname),
+            global_navigation::TYPE_SECTION => get_string('coursestructures', $this->block->blockname),
+            global_navigation::TYPE_ACTIVITY => get_string('courseactivities', $this->block->blockname)
+        );
+        $mform->addElement('select', 'config_expansionlimit', get_string('expansionlimit', $this->block->blockname), $options);
+        $mform->setType('config_expansionlimit', PARAM_INT);
+
     }
 }
\ No newline at end of file
index 7bf88fb..796e754 100644 (file)
@@ -30,6 +30,7 @@ $string['coursestructures'] = 'Categories, courses, and course structures';
 $string['courseactivities'] = 'Categories, courses, and course Activities';
 $string['enablehoverexpansiondesc'] = 'Enable mouseover expansion of this block';
 $string['enabledockdesc'] = 'Allow the user to dock this block';
+$string['expansionlimit'] = 'Generate navigation for the following';
 $string['pluginname'] = 'Navigation';
 $string['showmyhistorydesc'] = 'Show my history as a branch in the navigation';
 $string['showmyhistorytitle'] = 'My history';
index 47b5fd3..56b80c6 100644 (file)
@@ -2,15 +2,15 @@
 
 class block_navigation_renderer extends plugin_renderer_base {
 
-    public function navigation_tree(global_navigation $navigation) {
-        $content = $this->navigation_node(array($navigation), array('class'=>'block_tree list'));
+    public function navigation_tree(global_navigation $navigation, $expansionlimit) {
+        $content = $this->navigation_node(array($navigation), array('class'=>'block_tree list'), $expansionlimit);
         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()) {
+    protected function navigation_node($items, $attrs=array(), $expansionlimit=null) {
 
         // exit if empty, we don't want an empty ul element
         if (count($items)==0) {
@@ -76,7 +76,7 @@ class block_navigation_renderer extends plugin_renderer_base {
             $liattr = array('class'=>join(' ',$liclasses));
             // class attribute on the div item which only contains the item content
             $divclasses = array('tree_item');
-            if ($item->children->count() > 0 || ($item->nodetype == navigation_node::NODETYPE_BRANCH && $item->children->count()==0 && isloggedin())) {
+            if ((empty($expansionlimit) || $item->type != $expansionlimit) && ($item->children->count() > 0 || ($item->nodetype == navigation_node::NODETYPE_BRANCH && $item->children->count()==0 && isloggedin()))) {
                 $divclasses[] = 'branch';
             } else {
                 $divclasses[] = 'leaf';
@@ -88,7 +88,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);
+            $content = html_writer::tag('p', $content, $divattr) . $this->navigation_node($item->children, array(), $expansionlimit);
             if (!empty($item->preceedwithhr) && $item->preceedwithhr===true) {
                 $content = html_writer::empty_tag('hr') . $content;
             }
index be918d5..ccd089d 100644 (file)
@@ -43,15 +43,13 @@ try {
     // Create a global nav object
     $navigation = new global_navigation_for_ajax($PAGE);
 
-    // If set to true then we need to call toggle display
-    $toggledisplay = false;
     if ($instanceid!==null) {
         // Get the db record for the block instance
-        $blockrecords = $DB->get_record('block_instances', array('id'=>$instanceid,'blockname'=>'navigation'));
-        if ($blockrecords!=false) {
+        $blockrecord = $DB->get_record('block_instances', array('id'=>$instanceid,'blockname'=>'navigation'));
+        if ($blockrecord!=false) {
 
             // Instantiate a block_instance object so we can access config
-            $block = block_instance('navigation', $blockrecords);
+            $block = block_instance('navigation', $blockrecord);
 
             $trimmode = block_navigation::TRIM_RIGHT;
             $trimlength = 50;
@@ -70,6 +68,9 @@ try {
     // Create a navigation object to use, we can't guarantee PAGE will be complete
 
     $expandable = $navigation->initialise($branchtype, $branchid);
+    if (isset($block) && !empty($block->config->expansionlimit)) {
+        $navigation->set_expansion_limit($block->config->expansionlimit);
+    }
     if (isset($block)) {
         $block->trim($navigation, $trimmode, $trimlength, ceil($trimlength/2));
     }
index e08dd34..15c2784 100644 (file)
@@ -1057,6 +1057,9 @@ class block_manager {
         $editpage->set_pagelayout('admin');
         $editpage->set_course($this->page->course);
         $editpage->set_context($block->context);
+        if ($this->page->cm) {
+            $editpage->set_cm($this->page->cm);
+        }
         $editurlbase = str_replace($CFG->wwwroot . '/', '/', $this->page->url->out_omit_querystring());
         $editurlparams = $this->page->url->params();
         $editurlparams['bui_editid'] = $blockid;
index 2bdd0f9..e4cbf05 100644 (file)
@@ -512,7 +512,7 @@ class navigation_node implements renderable {
             return;
         }
         foreach ($this->children as &$child) {
-            if ($child->nodetype == self::NODETYPE_BRANCH && $child->children->count()==0) {
+            if ($child->nodetype == self::NODETYPE_BRANCH && $child->children->count()==0 && $child->display) {
                 $child->id = 'expandable_branch_'.(count($expandable)+1);
                 $this->add_class('canexpand');
                 $expandable[] = array('id'=>$child->id,'branchid'=>$child->key,'type'=>$child->type);
@@ -520,6 +520,15 @@ class navigation_node implements renderable {
             $child->find_expandable($expandable);
         }
     }
+
+    public function find_all_of_type($type) {
+        $nodes = $this->children->type($type);
+        foreach ($this->children as &$node) {
+            $childnodes = $node->find_all_of_type($type);
+            $nodes = array_merge($nodes, $childnodes);
+        }
+        return $nodes;
+    }
 }
 
 /**
@@ -744,6 +753,9 @@ class global_navigation extends navigation_node {
     protected $cache;
     /** @var array */
     protected $addedcourses = array();
+    /** @var int */
+    protected $expansionlimit = 0;
+
     /**
      * Constructs a new global navigation
      *
@@ -1481,6 +1493,16 @@ class global_navigation extends navigation_node {
     public function clear_cache() {
         $this->cache->clear();
     }
+
+    public function set_expansion_limit($type) {
+        $nodes = $this->find_all_of_type($type);
+        foreach ($nodes as &$node) {
+            foreach ($node->children as &$child) {
+                $child->display = false;
+            }
+        }
+        return true;
+    }
 }
 
 /**