Skip to content

Commit

Permalink
MDL-21097 Correctly check capabilities for course settings items
Browse files Browse the repository at this point in the history
- Make sure that items in course settings (and frontpage settings) are shown depending on user capabilities
- Make sure user is able to turn editing on on the page even if he has only limited number of seciton/modules managing capabilities
  • Loading branch information
marinaglancy committed May 31, 2013
1 parent 73f560c commit 1fda836
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
15 changes: 10 additions & 5 deletions course/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ protected function add_modchoosertoggle() {
$this->page->course->id == SITEID ||
!$this->page->user_is_editing() ||
!($context = context_course::instance($this->page->course->id)) ||
!has_capability('moodle/course:update', $context) ||
!has_capability('moodle/course:manageactivities', $context) ||
!course_ajax_enabled($this->page->course) ||
!($coursenode = $this->page->settingsnav->find('courseadmin', navigation_node::TYPE_COURSE)) ||
!$coursenode->get('editsettings')) {
// too late or we are on site page or we could not find the course settings node
!($turneditingnode = $coursenode->get('turneditingonoff'))) {
// too late or we are on site page or we could not find the adjacent nodes in course settings menu
// or we are not allowed to edit
return;
}
Expand All @@ -97,8 +97,13 @@ protected function add_modchoosertoggle() {
$modchoosertogglestring = get_string('modchooserenable', 'moodle');
$modchoosertoggleurl->param('modchooser', 'on');
}
$modchoosertoggle = navigation_node::create($modchoosertogglestring, $modchoosertoggleurl, navigation_node::TYPE_SETTING);
$coursenode->add_node($modchoosertoggle, 'editsettings');
$modchoosertoggle = navigation_node::create($modchoosertogglestring, $modchoosertoggleurl, navigation_node::TYPE_SETTING, null, 'modchoosertoggle');

// Insert the modchoosertoggle after the settings node 'turneditingonoff' (navigation_node only has function to insert before, so we insert before and then swap).
$coursenode->add_node($modchoosertoggle, 'turneditingonoff');
$turneditingnode->remove();
$coursenode->add_node($turneditingnode, 'modchoosertoggle');

$modchoosertoggle->add_class('modchoosertoggle');
$modchoosertoggle->add_class('visibleifjs');
user_preference_allow_ajax_update('usemodchooser', PARAM_BOOL);
Expand Down
5 changes: 5 additions & 0 deletions course/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
$PAGE->set_pagelayout('course');
$PAGE->set_pagetype('course-view-' . $course->format);
$PAGE->set_other_editing_capability('moodle/course:manageactivities');
$PAGE->set_other_editing_capability('moodle/course:activityvisibility');
if (course_format_uses_sections($course->format)) {
$PAGE->set_other_editing_capability('moodle/course:sectionvisibility');
$PAGE->set_other_editing_capability('moodle/course:movesections');
}

// Preload course format renderer before output starts.
// This is a little hacky but necessary since
Expand Down
1 change: 1 addition & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@

$PAGE->set_pagetype('site-index');
$PAGE->set_other_editing_capability('moodle/course:manageactivities');
$PAGE->set_other_editing_capability('moodle/course:activityvisibility');
$PAGE->set_docs_path('');
$PAGE->set_pagelayout('frontpage');
$editing = $PAGE->user_is_editing();
Expand Down
10 changes: 7 additions & 3 deletions lib/navigationlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3489,7 +3489,7 @@ protected function load_course_settings($forceopen = false) {
$coursenode->force_open();
}

if (has_capability('moodle/course:update', $coursecontext)) {
if ($this->page->user_allowed_editing()) {
// Add the turn on/off settings

if ($this->page->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
Expand All @@ -3509,8 +3509,10 @@ protected function load_course_settings($forceopen = false) {
$editurl->param('edit', 'on');
$editstring = get_string('turneditingon');
}
$coursenode->add($editstring, $editurl, self::TYPE_SETTING, null, null, new pix_icon('i/edit', ''));
$coursenode->add($editstring, $editurl, self::TYPE_SETTING, null, 'turneditingonoff', new pix_icon('i/edit', ''));
}

if (has_capability('moodle/course:update', $coursecontext)) {
// Add the course settings link
$url = new moodle_url('/course/edit.php', array('id'=>$course->id));
$coursenode->add(get_string('editsettings'), $url, self::TYPE_SETTING, null, 'editsettings', new pix_icon('i/settings', ''));
Expand Down Expand Up @@ -4244,7 +4246,7 @@ protected function load_front_page_settings($forceopen = false) {
}
$frontpage->id = 'frontpagesettings';

if (has_capability('moodle/course:update', $coursecontext)) {
if ($this->page->user_allowed_editing()) {

// Add the turn on/off settings
$url = new moodle_url('/course/view.php', array('id'=>$course->id, 'sesskey'=>sesskey()));
Expand All @@ -4256,7 +4258,9 @@ protected function load_front_page_settings($forceopen = false) {
$editstring = get_string('turneditingon');
}
$frontpage->add($editstring, $url, self::TYPE_SETTING, null, null, new pix_icon('i/edit', ''));
}

if (has_capability('moodle/course:update', $coursecontext)) {
// Add the course settings link
$url = new moodle_url('/admin/settings.php', array('section'=>'frontpagesettings'));
$frontpage->add(get_string('editsettings'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/settings', ''));
Expand Down

0 comments on commit 1fda836

Please sign in to comment.