# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Apply Diff Patch action on respective folder.
# It uses platform neutral UTF-8 encoding.
# Above lines and this line are ignored by the patching process.
Index: moodle/lib/accesslib.php
--- moodle/lib/accesslib.php Base (1.658)
+++ moodle/lib/accesslib.php Locally Modified (Based On 1.658)
@@ -5282,13 +5282,28 @@
     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;
Index: moodle/lib/moodlelib.php
--- moodle/lib/moodlelib.php Base (1.1440)
+++ moodle/lib/moodlelib.php Locally Modified (Based On 1.1440)
@@ -2318,7 +2318,7 @@
     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)) {
Index: moodle/lib/navigationlib.php
--- moodle/lib/navigationlib.php Base (1.179)
+++ moodle/lib/navigationlib.php Locally Modified (Based On 1.179)
@@ -982,7 +982,8 @@
                 // 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 @@
                 $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);
 
Index: moodle/lib/outputrenderers.php
--- moodle/lib/outputrenderers.php Base (1.240)
+++ moodle/lib/outputrenderers.php Locally Modified (Based On 1.240)
@@ -421,7 +421,7 @@
             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);
Index: moodle/user/view.php
--- moodle/user/view.php Base (1.243)
+++ moodle/user/view.php Locally Modified (Based On 1.243)
@@ -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');
@@ -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");
