Skip to content

Commit

Permalink
navigation MDL-23852 Added is_role_switched method to accesslib and i…
Browse files Browse the repository at this point in the history
…mplemented use of it
  • Loading branch information
Sam Hemelryk committed Aug 27, 2010
1 parent 00fa663 commit f5c1e62
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
21 changes: 18 additions & 3 deletions lib/accesslib.php
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/moodlelib.php
Expand Up @@ -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)) {
Expand Down
11 changes: 10 additions & 1 deletion lib/navigationlib.php
Expand Up @@ -982,7 +982,8 @@ public function initialise() {
// 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;
Expand Down Expand Up @@ -1525,6 +1526,14 @@ protected function load_for_user($user=null, $forceforcontext=false) {
$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);

Expand Down
2 changes: 1 addition & 1 deletion lib/outputrenderers.php
Expand Up @@ -421,7 +421,7 @@ public function login_info() {
if (isguestuser()) {
$loggedinas = $realuserinfo.get_string('loggedinasguest').
" (<a href=\"$loginurl\">".get_string('login').'</a>)';
} 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);
Expand Down
3 changes: 1 addition & 2 deletions user/view.php
Expand Up @@ -59,6 +59,7 @@
}

$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');
Expand All @@ -73,14 +74,12 @@
// 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");
Expand Down

0 comments on commit f5c1e62

Please sign in to comment.