Skip to content

Commit

Permalink
navigation MDL-20651 Added config setting to hide course categories o…
Browse files Browse the repository at this point in the history
…n the navigation

As requested and voted on admin can now choose not to include course categories within the navbar and global navigation block.
At the same time I also introduced a second setting, showallcourses, which ensures all of the courses a user is registered in are shown on the navigation at all times.
  • Loading branch information
Sam Hemelryk committed Nov 4, 2009
1 parent 8bdc9ca commit da3ab9c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 9 deletions.
6 changes: 6 additions & 0 deletions admin/settings/appearance.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
$temp->add(new admin_setting_configtext('maxexternalblogsperuser', get_string('maxexternalblogsperuser','blog'), get_string('configmaxexternalblogsperuser', 'blog'), 1));
$ADMIN->add('appearance', $temp);

// Navigation settings
$temp = new admin_settingpage('navigation', get_string('navigation'));
$temp->add(new admin_setting_configcheckbox('navhidecategories', get_string('navhidecategories', 'admin'), get_string('confignavhidecategories', 'admin'), 0));
$temp->add(new admin_setting_configcheckbox('navshowallcourses', get_string('navshowallcourses', 'admin'), get_string('confignavshowallcourses', 'admin'), 0));
$ADMIN->add('appearance', $temp);

/* TODO: reimplement editor settings and preferences, editors are now full plugins ;-)
// "htmleditor" settingpage
$ADMIN->add('appearance', new admin_category('htmleditor', get_string('htmleditor', 'admin')));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ function get_content() {
$this->remove_empty_section_branches();
}

// Load the my courses branch if the user has selected to
if (!empty($CFG->navhidecategories)) {
$this->page->navigation->collapse_course_categories();
}

// Load the my courses branch if the user has selected to
if (!empty($this->config->showmycourses) && $this->config->showmycourses=='yes') {
$this->showmycourses();
Expand Down Expand Up @@ -359,7 +364,7 @@ protected function showmycourses() {
// Add a branch labelled something like My Courses
$mycoursesbranch = $PAGE->navigation->add(get_string('mycourses'), null,navigation_node::TYPE_CATEGORY, null, 'mycourses');
$PAGE->navigation->add_courses($courses, $mycoursesbranch);

$PAGE->navigation->get($mycoursesbranch)->type = navigation_node::TYPE_SETTING;
return true;
}

Expand Down
4 changes: 4 additions & 0 deletions lang/en_utf8/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@
$string['condifmodeditdefaults'] = 'The values you set here define the default values that are used in the activity settings form when you create a new activity. You can also configure which activity settings are considered advanced.';
$string['configmycoursesperpage'] = 'Maximum number of courses to display in any list of a user\'s own courses';
$string['configmymoodleredirect'] = 'This setting forces redirects to /my on login for non-admins and replaces the top level site navigation with /my';
$string['confignavhidecategories'] = 'Do not show course categories in the navigation bar or navigation blocks';
$string['confignavshowallcourses'] = 'Setting this ensures that all courses a user is registered in are shown on the navigation at all times. By default once a user browses to a course only that course is shown on the navigation.';
$string['confignodefaultuserrolelists'] = 'This setting prevents all users from being returned from the database from deprecated calls of get_course_user, etc., for the site course if the default role provides that access. Check this, if you suffer a performance hit.';
$string['confignonmetacoursesyncroleids'] = 'By default all role assignments from child courses are synchronised to metacourses. Roles that are selected here will not be included in the synchronisation process.';
$string['confignoreplyaddress'] = 'Emails are sometimes sent out on behalf of a user (eg forum posts). The email address you specify here will be used as the \"From\" address in those cases when the recipients should not be able to reply directly to the user (eg when a user chooses to keep their address private).';
Expand Down Expand Up @@ -645,6 +647,8 @@
$string['navigationupgrade'] = 'This upgrade introduces two new navigation blocks that will replace these blocks: Administration, Courses, Activities and Participants. If you had set any special permissions on those blocks you should check to make sure everything
is behaving as you want it.<br /><br />
You should also "Shift-Refresh" your browser to load the new styles, otherwise the new blocks will not work correctly.';
$string['navhidecategories'] = 'Hide course categories';
$string['navshowallcourses'] = 'Show all users courses';
$string['nobookmarksforuser'] = 'You do not have any bookmarks.';
$string['nochanges'] = 'No changes';
$string['nodatabase'] = 'No database';
Expand Down
1 change: 1 addition & 0 deletions lang/en_utf8/moodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,7 @@
$string['nametopics'] = 'topic';
$string['nameweeks'] = 'week';
$string['nameweekscss'] = 'week';
$string['navigation'] = 'Navigation';
$string['needed'] = 'Needed';
$string['never'] = 'Never';
$string['neverdeletelogs'] = 'Never delete logs';
Expand Down
68 changes: 60 additions & 8 deletions lib/navigationlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,24 @@ public function find_child_depth($key, $type=self::TYPE_CATEGORY) {
return $depth;
}

/**
* Finds all nodes that have the specified type
*
* @param int $type One of navigation_node::TYPE_*
* @return array An array of navigation_node references for nodes of type $type
*/
public function get_children_by_type($type) {
$nodes = array();
if (count($this->children)>0) {
foreach ($this->children as &$child) {
if ($child->type === $type) {
$nodes[] = $child;
}
}
}
return $nodes;
}

/**
* Finds all nodes (recursivily) that have the specified type, regardless of
* assumed order or position.
Expand Down Expand Up @@ -895,7 +913,7 @@ public function content() {
* @return bool Returns true
*/
public function initialise($jsargs = null) {
global $PAGE, $SITE;
global $PAGE, $SITE, $CFG;
if ($this->initialised || during_initial_install()) {
return true;
}
Expand Down Expand Up @@ -941,7 +959,7 @@ public function initialise($jsargs = null) {
* This gets called by {@link initialise()} when the context is CONTEXT_USER
*/
protected function load_for_user() {
global $DB, $SITE, $PAGE;
global $DB, $SITE, $PAGE, $CFG;
if (!empty($PAGE->course->id)) {
$courseid = $PAGE->course->id;
} else {
Expand All @@ -951,6 +969,9 @@ protected function load_for_user() {
$course = $DB->get_record('course', array('id'=>$courseid));
}
if (isset($course) && $course) {
if (!empty($CFG->navshowallcourses)) {
$this->load_categories();
}
$this->load_for_course();
} else {
$this->load_categories();
Expand All @@ -968,6 +989,9 @@ protected function load_for_category($lookforid=true) {
global $PAGE, $CFG;
$id = optional_param('id', null);
if ($lookforid && $id!==null) {
if (!empty($CFG->navshowallcourses)) {
$this->load_categories();
}
$this->load_categories($id);
$depth = $this->find_child_depth($id);
} else {
Expand All @@ -984,6 +1008,9 @@ protected function load_for_category($lookforid=true) {
protected function load_for_course() {
global $PAGE, $CFG, $USER;
$keys = array();
if (!empty($CFG->navshowallcourses)) {
$this->load_categories();
}
$depth = $this->load_course_categories($keys);
$depth += $this->load_course($keys);
if (!$this->format_display_course_content($PAGE->course->format)) {
Expand Down Expand Up @@ -1182,7 +1209,7 @@ protected function module_extends_navigation($modname) {
* @return int
*/
protected function load_for_activity() {
global $PAGE, $DB;
global $PAGE, $DB, $CFG;
$keys = array();

$sectionnum = false;
Expand All @@ -1193,6 +1220,10 @@ protected function load_for_activity() {
}
}

if (!empty($CFG->navshowallcourses)) {
$this->load_categories();
}

$depth = $this->load_course_categories($keys);
$depth += $this->load_course($keys);
$depth += $this->load_course_activities($keys);
Expand Down Expand Up @@ -1312,7 +1343,9 @@ public function add_courses($courses, $categoryid=null) {
// Process this course into the nav structure
$url = new moodle_url($CFG->wwwroot.'/course/view.php', array('id'=>$course->id));
if ($categoryid===null) {
$category = $this->find_child($course->category);
$category = $this->find_child($course->category, self::TYPE_CATEGORY);
} else if ($categoryid === false) {
$category = $this;
} else {
$category = $this->find_child($categoryid);
}
Expand Down Expand Up @@ -1563,8 +1596,13 @@ protected function load_course_categories(&$keys) {
if (is_array($categories) && count($categories)>0) {
$categories = array_reverse($categories);
foreach ($categories as $category) {
$url = new moodle_url($CFG->wwwroot.'/course/category.php', array('id'=>$category->id, 'categoryedit'=>'on', 'sesskey'=>sesskey()));
$keys[] = $this->add_to_path($keys, $category->id, $category->name, $category->name, self::TYPE_CATEGORY, $url);
$key = $category->id.':'.self::TYPE_CATEGORY;
if (!$this->get_by_path(array_merge($keys, array($key)))) {
$url = new moodle_url($CFG->wwwroot.'/course/category.php', array('id'=>$category->id, 'categoryedit'=>'on', 'sesskey'=>sesskey()));
$keys[] = $this->add_to_path($keys, $category->id, $category->name, $category->name, self::TYPE_CATEGORY, $url);
} else {
$keys[] = $key;
}
}
}
return count($categories);
Expand Down Expand Up @@ -1626,7 +1664,7 @@ protected function load_categories($categoryid=0) {
$categorypathids = explode('/',trim($course->categorypath,' /'));
// If no category has been specified limit the depth we display immediatly to
// that of the nav var depthforwards
if ($categoryid===0 && count($categorypathids)>($this->depthforward+1)) {
if ($categoryid===0 && count($categorypathids)>($this->depthforward+1) && empty($CFG->navshowallcourses)) {
$categorypathids = array_slice($categorypathids, 0, ($this->depthforward+1));
}
$categoryids = array_merge($categoryids, $categorypathids);
Expand Down Expand Up @@ -1716,6 +1754,17 @@ public function load_empty_categories() {
}
}
}

public function collapse_course_categories() {
$categories = $this->get_children_by_type(self::TYPE_CATEGORY);
while (count($categories) > 0) {
foreach ($categories as $category) {
$this->children = array_merge($this->children, $category->children);
$this->remove_child($category->key, self::TYPE_CATEGORY);
}
$categories = $this->get_children_by_type(self::TYPE_CATEGORY);
}
}
}

/**
Expand Down Expand Up @@ -2170,6 +2219,7 @@ public function content() {
* @return string HTML
*/
protected function parse_branch_to_html($navarray, $firstnode=true, $moreafterthis=false) {
global $CFG;
$separator = get_separator();
$output = '';
if ($firstnode===true) {
Expand All @@ -2189,7 +2239,7 @@ protected function parse_branch_to_html($navarray, $firstnode=true, $moreafterth
return $output;
}
$child = false;
// Iterate the nodes in navarray and finde the active node
// Iterate the nodes in navarray and find the active node
foreach ($navarray as $tempchild) {
if ($tempchild->isactive || $tempchild->contains_active_node()) {
$child = $tempchild;
Expand All @@ -2211,8 +2261,10 @@ protected function parse_branch_to_html($navarray, $firstnode=true, $moreafterth
$oldaction = $child->action;
$child->action = null;
}
if (empty($CFG->navhidecategories) || $child->type !== navigation_node::TYPE_CATEGORY) {
// Now display the node
$output .= '<li>'.$separator.' '.$child->content(true).'</li>';
}
if (isset($oldaction)) {
$child->action = $oldaction;
}
Expand Down

0 comments on commit da3ab9c

Please sign in to comment.