From 0209a857049e87dac63cc9f44c8d2eab87b16510 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Fri, 14 Nov 2014 17:23:00 +0800 Subject: [PATCH] MDL-46640 navigation: Handle block contexts within a category --- lib/navigationlib.php | 51 ++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/lib/navigationlib.php b/lib/navigationlib.php index cd8d996096a..831db44594e 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -4384,54 +4384,69 @@ class settings_navigation extends navigation_node { protected function load_category_settings() { global $CFG; - $categorynode = $this->add($this->context->get_context_name(), null, null, null, 'categorysettings'); + // We can land here while being in the context of a block, in which case we + // should get the parent context which should be the category one. See self::initialise(). + if ($this->context->contextlevel == CONTEXT_BLOCK) { + $catcontext = $this->context->get_parent_context(); + } else { + $catcontext = $this->context; + } + + // Let's make sure that we always have the right context when getting here. + if ($catcontext->contextlevel != CONTEXT_COURSECAT) { + throw new coding_exception('Unexpected context while loading category settings.'); + } + + $categorynode = $this->add($catcontext->get_context_name(), null, null, null, 'categorysettings'); $categorynode->force_open(); - if (can_edit_in_category($this->context->instanceid)) { - $url = new moodle_url('/course/management.php', array('categoryid' => $this->context->instanceid)); + if (can_edit_in_category($catcontext->instanceid)) { + $url = new moodle_url('/course/management.php', array('categoryid' => $catcontext->instanceid)); $editstring = get_string('managecategorythis'); $categorynode->add($editstring, $url, self::TYPE_SETTING, null, null, new pix_icon('i/edit', '')); } - if (has_capability('moodle/category:manage', $this->context)) { - $editurl = new moodle_url('/course/editcategory.php', array('id' => $this->context->instanceid)); + if (has_capability('moodle/category:manage', $catcontext)) { + $editurl = new moodle_url('/course/editcategory.php', array('id' => $catcontext->instanceid)); $categorynode->add(get_string('editcategorythis'), $editurl, self::TYPE_SETTING, null, 'edit', new pix_icon('i/edit', '')); - $addsubcaturl = new moodle_url('/course/editcategory.php', array('parent' => $this->context->instanceid)); + $addsubcaturl = new moodle_url('/course/editcategory.php', array('parent' => $catcontext->instanceid)); $categorynode->add(get_string('addsubcategory'), $addsubcaturl, self::TYPE_SETTING, null, 'addsubcat', new pix_icon('i/withsubcat', '')); } // Assign local roles - if (has_capability('moodle/role:assign', $this->context)) { - $assignurl = new moodle_url('/'.$CFG->admin.'/roles/assign.php', array('contextid'=>$this->context->id)); + if (has_capability('moodle/role:assign', $catcontext)) { + $assignurl = new moodle_url('/'.$CFG->admin.'/roles/assign.php', array('contextid' => $catcontext->id)); $categorynode->add(get_string('assignroles', 'role'), $assignurl, self::TYPE_SETTING, null, 'roles', new pix_icon('i/assignroles', '')); } // Override roles - if (has_capability('moodle/role:review', $this->context) or count(get_overridable_roles($this->context))>0) { - $url = new moodle_url('/'.$CFG->admin.'/roles/permissions.php', array('contextid'=>$this->context->id)); + if (has_capability('moodle/role:review', $catcontext) or count(get_overridable_roles($catcontext)) > 0) { + $url = new moodle_url('/'.$CFG->admin.'/roles/permissions.php', array('contextid' => $catcontext->id)); $categorynode->add(get_string('permissions', 'role'), $url, self::TYPE_SETTING, null, 'permissions', new pix_icon('i/permissions', '')); } // Check role permissions - if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride','moodle/role:override', 'moodle/role:assign'), $this->context)) { - $url = new moodle_url('/'.$CFG->admin.'/roles/check.php', array('contextid'=>$this->context->id)); + if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride', + 'moodle/role:override', 'moodle/role:assign'), $catcontext)) { + $url = new moodle_url('/'.$CFG->admin.'/roles/check.php', array('contextid' => $catcontext->id)); $categorynode->add(get_string('checkpermissions', 'role'), $url, self::TYPE_SETTING, null, 'checkpermissions', new pix_icon('i/checkpermissions', '')); } // Cohorts - if (has_any_capability(array('moodle/cohort:view', 'moodle/cohort:manage'), $this->context)) { - $categorynode->add(get_string('cohorts', 'cohort'), new moodle_url('/cohort/index.php', array('contextid' => $this->context->id)), self::TYPE_SETTING, null, 'cohort', new pix_icon('i/cohort', '')); + if (has_any_capability(array('moodle/cohort:view', 'moodle/cohort:manage'), $catcontext)) { + $categorynode->add(get_string('cohorts', 'cohort'), new moodle_url('/cohort/index.php', + array('contextid' => $catcontext->id)), self::TYPE_SETTING, null, 'cohort', new pix_icon('i/cohort', '')); } // Manage filters - if (has_capability('moodle/filter:manage', $this->context) && count(filter_get_available_in_context($this->context))>0) { - $url = new moodle_url('/filter/manage.php', array('contextid'=>$this->context->id)); + if (has_capability('moodle/filter:manage', $catcontext) && count(filter_get_available_in_context($catcontext)) > 0) { + $url = new moodle_url('/filter/manage.php', array('contextid' => $catcontext->id)); $categorynode->add(get_string('filters', 'admin'), $url, self::TYPE_SETTING, null, 'filters', new pix_icon('i/filter', '')); } // Restore. - if (has_capability('moodle/course:create', $this->context)) { - $url = new moodle_url('/backup/restorefile.php', array('contextid' => $this->context->id)); + if (has_capability('moodle/course:create', $catcontext)) { + $url = new moodle_url('/backup/restorefile.php', array('contextid' => $catcontext->id)); $categorynode->add(get_string('restorecourse', 'admin'), $url, self::TYPE_SETTING, null, 'restorecourse', new pix_icon('i/restore', '')); }