From f5c1e62149629d0e5f3746c19beb027c82a59866 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Fri, 27 Aug 2010 01:44:25 +0000 Subject: [PATCH] navigation MDL-23852 Added is_role_switched method to accesslib and implemented use of it --- lib/accesslib.php | 21 ++++++++++++++++++--- lib/moodlelib.php | 2 +- lib/navigationlib.php | 11 ++++++++++- lib/outputrenderers.php | 2 +- user/view.php | 3 +-- 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index e026731c0f1..d4069b3d95c 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -5285,13 +5285,28 @@ function role_switch($roleid, $context) { return true; } +/** + * Checks if the user has switched roles within the given course. + * + * Note: You can only switch roles within the course, hence it takes a courseid + * rather than a context. On that note Petr volunteered to implement this across + * all other contexts, all requests for this should be forwarded to him ;) + * + * @param int $courseid The id of the course to check + * @return bool True if the user has switched roles within the course. + */ +function is_role_switched($courseid) { + global $USER; + $context = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST); + return (!empty($USER->access['rsw'][$context->path])); +} /** * Get any role that has an override on exact context * - * @global object - * @param object $context - * @return array + * @global moodle_database + * @param stdClass $context The context to check + * @return array An array of roles */ function get_roles_with_override_on_context($context) { global $DB; diff --git a/lib/moodlelib.php b/lib/moodlelib.php index e6d8d0484a1..584e945c0d7 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -2318,7 +2318,7 @@ function require_login($courseorid = NULL, $autologinguest = true, $cm = NULL, $ if ($course->id == SITEID) { // frontpage can not be hidden } else { - if (!empty($USER->access['rsw'][$coursecontext->path])) { + if (is_role_switched($course->id)) { // when switching roles ignore the hidden flag - user had to be in course to do the switch } else { if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) { diff --git a/lib/navigationlib.php b/lib/navigationlib.php index f341e7af9b8..b7049c2b217 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -982,7 +982,8 @@ class global_navigation extends navigation_node { // If the user is not enrolled then we only want to show the // course node and not populate it. $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); - if (!is_enrolled($coursecontext) && !has_capability('moodle/course:view', $coursecontext)) { + // Not enrolled, can't view, and hasn't switched roles + if (!is_enrolled($coursecontext) && !has_capability('moodle/course:view', $coursecontext) && !is_role_switched($course->id)) { $coursenode->make_active(); $canviewcourseprofile = false; break; @@ -1525,6 +1526,14 @@ class global_navigation extends navigation_node { $usersnode->action = new moodle_url('/user/index.php', array('id'=>$course->id)); $userviewurl = new moodle_url('/user/profile.php', $baseargs); } + if (!$usersnode) { + // We should NEVER get here, if the course hasn't been populated + // with a participants node then the navigaiton either wasn't generated + // for it (you are missing a require_login or set_context call) or + // you don't have access.... in the interests of no leaking informatin + // we simply quit... + return false; + } // Add a branch for the current user $usernode = $usersnode->add(fullname($user, true), $userviewurl, self::TYPE_USER, null, $user->id); diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index df496417931..0224f206584 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -421,7 +421,7 @@ class core_renderer extends renderer_base { if (isguestuser()) { $loggedinas = $realuserinfo.get_string('loggedinasguest'). " (".get_string('login').')'; - } else if (!empty($USER->access['rsw'][$context->path])) { + } else if (is_role_switched($course->id)) { // Has switched roles $rolename = ''; if ($role = $DB->get_record('role', array('id'=>$USER->access['rsw'][$context->path]))) { $rolename = ': '.format_string($role->name); diff --git a/user/view.php b/user/view.php index 9f5076d288a..0c1efa7a8ed 100644 --- a/user/view.php +++ b/user/view.php @@ -59,6 +59,7 @@ if (isguestuser($user)) { } $PAGE->set_context($coursecontext); +$PAGE->set_course($course); $PAGE->set_pagetype('course-view-' . $course->format); // To get the blocks exactly like the course $PAGE->add_body_class('path-user'); // So we can style it independently $PAGE->set_other_editing_capability('moodle/course:manageactivities'); @@ -73,14 +74,12 @@ if (!$currentuser // please note this is just a guess! require_login(); $isparent = true; - } else { // normal course require_login($course); // what to do with users temporary accessing this course? shoudl they see the details? } - $strpersonalprofile = get_string('personalprofile'); $strparticipants = get_string("participants"); $struser = get_string("user");