MDL-55415 course: Move options permission check to API

This commit is contained in:
Juan Leyva 2016-08-02 11:49:14 +02:00
parent 5c33a0db21
commit d6cd89b110
3 changed files with 180 additions and 27 deletions

View File

@ -4061,3 +4061,53 @@ function course_get_tagged_course_modules($tag, $exclusivemode = false, $fromcon
$exclusivemode, $fromcontextid, $contextid, $recursivecontext, $page, $totalpages);
}
}
/**
* Return an object with the list of navigation options in a course that are avaialable or not for the current user.
* This function also handles the frontpage course.
*
* @param stdClass $context context object (it can be a course context or the system context for frontpage settings)
* @param stdClass $course the course where the settings are being rendered (only used when $context is set to frontpage)
* @return stdClass the navigation options in a course and their availability status
* @since Moodle 3.2
*/
function course_get_user_navigation_options($context, $course = null) {
global $CFG;
$isloggedin = isloggedin();
$isguestuser = isguestuser();
$isfrontpage = $context->contextlevel == CONTEXT_SYSTEM;
if ($isfrontpage) {
$sitecontext = $context;
} else {
$sitecontext = context_system::instance();
}
$options = new stdClass;
$options->blogs = !empty($CFG->enableblogs) &&
($CFG->bloglevel == BLOG_GLOBAL_LEVEL ||
($CFG->bloglevel == BLOG_SITE_LEVEL and ($isloggedin and !$isguestuser)))
&& has_capability('moodle/blog:view', $sitecontext);
$options->notes = !empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $context);
// Frontpage settings?
if ($isfrontpage) {
if ($course->id == SITEID) {
$options->participants = has_capability('moodle/site:viewparticipants', $sitecontext);
} else {
$options->participants = has_capability('moodle/course:viewparticipants', context_course::instance($course->id));
}
$options->badges = !empty($CFG->enablebadges) && has_capability('moodle/badges:viewbadges', $sitecontext);
$options->tags = !empty($CFG->usetags) && $isloggedin;
$options->search = !empty($CFG->enableglobalsearch) && has_capability('moodle/search:query', $sitecontext);
$options->calendar = $isloggedin;
} else {
$options->participants = has_capability('moodle/course:viewparticipants', $context);
$options->badges = !empty($CFG->enablebadges) && !empty($CFG->badges_allowcoursebadges) &&
has_capability('moodle/badges:viewbadges', $context);
}
return $options;
}

View File

@ -2920,4 +2920,110 @@ class core_course_courselib_testcase extends advanced_testcase {
$this->assertNotEmpty($res->prevpageurl);
$this->assertEmpty($res->nextpageurl);
}
/**
* Test course_get_user_navigation_options for frontpage.
*/
public function test_course_get_user_navigation_options_for_frontpage() {
global $CFG, $SITE, $DB;
$this->resetAfterTest();
$context = context_system::instance();
$course = clone $SITE;
$this->setAdminUser();
$navoptions = course_get_user_navigation_options($context, $course);
$this->assertTrue($navoptions->blogs);
$this->assertTrue($navoptions->notes);
$this->assertTrue($navoptions->participants);
$this->assertTrue($navoptions->badges);
$this->assertTrue($navoptions->tags);
$this->assertFalse($navoptions->search);
$this->assertTrue($navoptions->calendar);
// Enable global search now.
$CFG->enableglobalsearch = 1;
$navoptions = course_get_user_navigation_options($context, $course);
$this->assertTrue($navoptions->search);
// Now try with a standard user.
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$navoptions = course_get_user_navigation_options($context, $course);
$this->assertTrue($navoptions->blogs);
$this->assertFalse($navoptions->notes);
$this->assertFalse($navoptions->participants);
$this->assertTrue($navoptions->badges);
$this->assertTrue($navoptions->tags);
$this->assertTrue($navoptions->search);
$this->assertTrue($navoptions->calendar);
// Standar using viewing frontpage settings from a course where is enrolled.
$course = self::getDataGenerator()->create_course();
// Create a viewer user.
$viewer = self::getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($viewer->id, $course->id, $studentrole->id);
$this->setUser($viewer);
$navoptions = course_get_user_navigation_options($context, $course);
$this->assertTrue($navoptions->blogs);
$this->assertFalse($navoptions->notes);
$this->assertTrue($navoptions->participants);
$this->assertTrue($navoptions->badges);
$this->assertTrue($navoptions->tags);
$this->assertTrue($navoptions->search);
$this->assertTrue($navoptions->calendar);
}
/**
* Test course_get_user_navigation_options for managers in a normal course.
*/
public function test_course_get_user_navigation_options_for_managers() {
global $CFG;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$context = context_course::instance($course->id);
$this->setAdminUser();
$navoptions = course_get_user_navigation_options($context);
$this->assertTrue($navoptions->blogs);
$this->assertTrue($navoptions->notes);
$this->assertTrue($navoptions->participants);
$this->assertTrue($navoptions->badges);
}
/**
* Test course_get_user_navigation_options for students in a normal course.
*/
public function test_course_get_user_navigation_options_for_students() {
global $DB, $CFG;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$context = context_course::instance($course->id);
$user = $this->getDataGenerator()->create_user();
$roleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $roleid);
$this->setUser($user);
$navoptions = course_get_user_navigation_options($context);
$this->assertTrue($navoptions->blogs);
$this->assertFalse($navoptions->notes);
$this->assertTrue($navoptions->participants);
$this->assertTrue($navoptions->badges);
// Disable some options.
$CFG->badges_allowcoursebadges = 0;
$CFG->enableblogs = 0;
// Disable view participants capability.
assign_capability('moodle/course:viewparticipants', CAP_PROHIBIT, $roleid, $context);
$context->mark_dirty();
$navoptions = course_get_user_navigation_options($context);
$this->assertFalse($navoptions->blogs);
$this->assertFalse($navoptions->notes);
$this->assertFalse($navoptions->participants);
$this->assertFalse($navoptions->badges);
}
}

View File

@ -2536,6 +2536,7 @@ class global_navigation extends navigation_node {
*/
public function add_course_essentials($coursenode, stdClass $course) {
global $CFG, $SITE;
require_once($CFG->dirroot . '/course/lib.php');
if ($course->id == $SITE->id) {
return $this->add_front_page_course_essentials($coursenode, $course);
@ -2545,22 +2546,23 @@ class global_navigation extends navigation_node {
return true;
}
$navoptions = course_get_user_navigation_options($this->page->context);
//Participants
if (has_capability('moodle/course:viewparticipants', $this->page->context)) {
if ($navoptions->participants) {
$participants = $coursenode->add(get_string('participants'), new moodle_url('/user/index.php?id='.$course->id), self::TYPE_CONTAINER, get_string('participants'), 'participants');
if (!empty($CFG->enableblogs)) {
if (($CFG->bloglevel == BLOG_GLOBAL_LEVEL or ($CFG->bloglevel == BLOG_SITE_LEVEL and (isloggedin() and !isguestuser())))
and has_capability('moodle/blog:view', context_system::instance())) {
$blogsurls = new moodle_url('/blog/index.php');
if ($currentgroup = groups_get_course_group($course, true)) {
$blogsurls->param('groupid', $currentgroup);
} else {
$blogsurls->param('courseid', $course->id);
}
$participants->add(get_string('blogscourse', 'blog'), $blogsurls->out(), self::TYPE_SETTING, null, 'courseblogs');
if ($navoptions->blogs) {
$blogsurls = new moodle_url('/blog/index.php');
if ($currentgroup = groups_get_course_group($course, true)) {
$blogsurls->param('groupid', $currentgroup);
} else {
$blogsurls->param('courseid', $course->id);
}
$participants->add(get_string('blogscourse', 'blog'), $blogsurls->out(), self::TYPE_SETTING, null, 'courseblogs');
}
if (!empty($CFG->enablenotes) && (has_capability('moodle/notes:manage', $this->page->context) || has_capability('moodle/notes:view', $this->page->context))) {
if ($navoptions->notes) {
$participants->add(get_string('notes', 'notes'), new moodle_url('/notes/index.php', array('filtertype' => 'course', 'filterselect' => $course->id)), self::TYPE_SETTING, null, 'currentcoursenotes');
}
} else if (count($this->extendforuser) > 0 || $this->page->course->id == $course->id) {
@ -2568,8 +2570,7 @@ class global_navigation extends navigation_node {
}
// Badges.
if (!empty($CFG->enablebadges) && !empty($CFG->badges_allowcoursebadges) &&
has_capability('moodle/badges:viewbadges', $this->page->context)) {
if ($navoptions->badges) {
$url = new moodle_url('/badges/view.php', array('type' => 2, 'id' => $course->id));
$coursenode->add(get_string('coursebadges', 'badges'), null,
@ -2594,30 +2595,26 @@ class global_navigation extends navigation_node {
*/
public function add_front_page_course_essentials(navigation_node $coursenode, stdClass $course) {
global $CFG;
require_once($CFG->dirroot . '/course/lib.php');
if ($coursenode == false || $coursenode->get('frontpageloaded', navigation_node::TYPE_CUSTOM)) {
return true;
}
$sitecontext = context_system::instance();
$isfrontpage = ($course->id == SITEID);
$navoptions = course_get_user_navigation_options($sitecontext, $course);
// Hidden node that we use to determine if the front page navigation is loaded.
// This required as there are not other guaranteed nodes that may be loaded.
$coursenode->add('frontpageloaded', null, self::TYPE_CUSTOM, null, 'frontpageloaded')->display = false;
// Participants.
// If this is the site course, they need to have moodle/site:viewparticipants at the site level.
// If no, then they need to have moodle/course:viewparticipants at the course level.
if (($isfrontpage && has_capability('moodle/site:viewparticipants', $sitecontext)) ||
(!$isfrontpage && has_capability('moodle/course:viewparticipants', context_course::instance($course->id)))) {
if ($navoptions->participants) {
$coursenode->add(get_string('participants'), new moodle_url('/user/index.php?id='.$course->id), self::TYPE_CUSTOM, get_string('participants'), 'participants');
}
// Blogs.
if (!empty($CFG->enableblogs)
and ($CFG->bloglevel == BLOG_GLOBAL_LEVEL or ($CFG->bloglevel == BLOG_SITE_LEVEL and (isloggedin() and !isguestuser())))
and has_capability('moodle/blog:view', $sitecontext)) {
if ($navoptions->blogs) {
$blogsurls = new moodle_url('/blog/index.php');
$coursenode->add(get_string('blogssite', 'blog'), $blogsurls->out(), self::TYPE_SYSTEM, null, 'siteblog');
}
@ -2625,30 +2622,30 @@ class global_navigation extends navigation_node {
$filterselect = 0;
// Badges.
if (!empty($CFG->enablebadges) && has_capability('moodle/badges:viewbadges', $sitecontext)) {
if ($navoptions->badges) {
$url = new moodle_url($CFG->wwwroot . '/badges/view.php', array('type' => 1));
$coursenode->add(get_string('sitebadges', 'badges'), $url, navigation_node::TYPE_CUSTOM);
}
// Notes.
if (!empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $sitecontext)) {
if ($navoptions->notes) {
$coursenode->add(get_string('notes', 'notes'), new moodle_url('/notes/index.php',
array('filtertype' => 'course', 'filterselect' => $filterselect)), self::TYPE_SETTING, null, 'notes');
}
// Tags
if (!empty($CFG->usetags) && isloggedin()) {
if ($navoptions->tags) {
$node = $coursenode->add(get_string('tags', 'tag'), new moodle_url('/tag/search.php'),
self::TYPE_SETTING, null, 'tags');
}
// Search.
if (!empty($CFG->enableglobalsearch) && has_capability('moodle/search:query', $sitecontext)) {
if ($navoptions->search) {
$node = $coursenode->add(get_string('search', 'search'), new moodle_url('/search/index.php'),
self::TYPE_SETTING, null, 'search');
}
if (isloggedin()) {
if ($navoptions->calendar) {
// Calendar
$calendarurl = new moodle_url('/calendar/view.php', array('view' => 'month'));
$coursenode->add(get_string('calendar', 'calendar'), $calendarurl, self::TYPE_CUSTOM, null, 'calendar');