MDL-48930 navigation: Using user headers in user pages.
authorAdrian Greeve <adrian@moodle.com>
Mon, 16 Mar 2015 05:13:07 +0000 (13:13 +0800)
committerAdrian Greeve <adrian@moodle.com>
Fri, 10 Apr 2015 09:17:07 +0000 (17:17 +0800)
Pages that are percieved to be in a user context are now
using a user context for the header.

Part of MDL-45774

26 files changed:
admin/roles/permissions.php
badges/mybackpack.php
badges/mybadges.php
badges/preferences.php
blog/edit.php
blog/external_blog_edit.php
blog/external_blogs.php
blog/index.php
blog/preferences.php
grade/report/overview/index.php
login/change_password.php
message/edit.php
message/index.php
mod/forum/classes/event/user_report_viewed.php
mod/forum/tests/events_test.php
mod/forum/user.php
my/index.php
notes/index.php
report/log/user.php
report/outline/user.php
report/usersessions/user.php
user/edit.php
user/editadvanced.php
user/files.php
user/preferences.php
user/profile.php

index 3670a86..01ddbaa 100644 (file)
@@ -60,7 +60,7 @@ $PAGE->set_url($url);
 
 if ($context->contextlevel == CONTEXT_USER and $USER->id != $context->instanceid) {
     $PAGE->navigation->extend_for_user($user);
-    $PAGE->set_context(context_course::instance($course->id));
+    $PAGE->set_context(context_user::instance($user->id));
 } else {
     $PAGE->set_context($context);
 }
index 875d025..c6bb7e8 100644 (file)
@@ -49,7 +49,7 @@ $PAGE->set_context($context);
 
 $title = get_string('mybackpack', 'badges');
 $PAGE->set_title($title);
-$PAGE->set_heading($title);
+$PAGE->set_heading(fullname($USER));
 $PAGE->set_pagelayout('mydashboard');
 
 $backpack = $DB->get_record('badge_backpack', array('userid' => $USER->id));
@@ -131,5 +131,6 @@ if ($backpack) {
 }
 
 echo $OUTPUT->header();
+echo $OUTPUT->heading($title);
 $form->display();
 echo $OUTPUT->footer();
index a1454da..e7c7361 100644 (file)
@@ -88,7 +88,7 @@ $PAGE->set_context($context);
 
 $title = get_string('mybadges', 'badges');
 $PAGE->set_title($title);
-$PAGE->set_heading($title);
+$PAGE->set_heading(fullname($USER));
 $PAGE->set_pagelayout('mydashboard');
 
 // Include JS files for backpack support.
index edb7718..a35337d 100644 (file)
@@ -30,7 +30,7 @@ require_once('preferences_form.php');
 $url = new moodle_url('/badges/preferences.php');
 
 require_login();
-$PAGE->set_context(context_system::instance());
+$PAGE->set_context(context_user::instance($USER->id));
 $PAGE->set_url($url);
 $PAGE->set_pagelayout('standard');
 
@@ -55,7 +55,7 @@ $strbadges      = get_string('badges');
 
 $title = "$strbadges: $strpreferences";
 $PAGE->set_title($title);
-$PAGE->set_heading($title);
+$PAGE->set_heading(fullname($USER));
 
 echo $OUTPUT->header();
 echo $OUTPUT->heading("$strbadges: $strpreferences", 2);
index c3052f7..783a2bd 100644 (file)
@@ -48,9 +48,9 @@ if (!empty($id) && $action == 'add') {
     $id = null;
 }
 
-// Blogs are always in system context.
 $sitecontext = context_system::instance();
-$PAGE->set_context($sitecontext);
+$usercontext = context_user::instance($USER->id);
+$PAGE->set_context($usercontext);
 
 require_login($courseid);
 
@@ -142,10 +142,10 @@ if ($action === 'delete') {
     }
 } else if ($action == 'add') {
     $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('addnewentry', 'blog'));
-    $PAGE->set_heading($SITE->shortname);
+    $PAGE->set_heading(fullname($USER));
 } else if ($action == 'edit') {
     $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('editentry', 'blog'));
-    $PAGE->set_heading($SITE->shortname);
+    $PAGE->set_heading(fullname($USER));
 }
 
 if (!empty($entry->id)) {
index 0aa122b..069ef96 100644 (file)
@@ -44,7 +44,7 @@ if ($id !== null) {
     $url->param('id', $id);
 }
 $PAGE->set_url($url);
-$PAGE->set_context($context);
+$PAGE->set_context(context_user::instance($USER->id));
 $PAGE->set_pagelayout('admin');
 
 $returnurl = new moodle_url('/blog/external_blogs.php');
@@ -125,7 +125,7 @@ if ($externalblogform->is_cancelled()) {
     redirect($returnurl);
 }
 
-$PAGE->set_heading("$SITE->shortname: $strblogs: $strexternalblogs", $SITE->fullname);
+$PAGE->set_heading(fullname($USER));
 $PAGE->set_title("$SITE->shortname: $strblogs: $strexternalblogs");
 
 echo $OUTPUT->header();
index 800c85c..4b0d408 100644 (file)
@@ -29,7 +29,7 @@ require_once('lib.php');
 
 require_login();
 $context = context_system::instance();
-$PAGE->set_context($context);
+$PAGE->set_context(context_user::instance($USER->id));
 $PAGE->set_url(new moodle_url('/blog/external_blogs.php'));
 require_capability('moodle/blog:manageexternal', $context);
 
@@ -61,7 +61,7 @@ if ($delete && confirm_sesskey()) {
 
 $blogs = $DB->get_records('blog_external', array('userid' => $USER->id));
 
-$PAGE->set_heading("$SITE->shortname: $strblogs: $strexternalblogs", $SITE->fullname);
+$PAGE->set_heading(fullname($USER));
 $PAGE->set_title("$SITE->shortname: $strblogs: $strexternalblogs");
 $PAGE->set_pagelayout('standard');
 
index 4c58f34..0c9a7f6 100644 (file)
@@ -57,9 +57,14 @@ if (!empty($tag)) {
     }
 }
 
+if (isset($userid)) {
+    $context = context_user::instance($userid);
+} else {
+    $context = context_system::instance();
+}
+$PAGE->set_context($context);
+
 $sitecontext = context_system::instance();
-// Blogs are always in system context.
-$PAGE->set_context($sitecontext);
 
 // Check basic permissions.
 if ($CFG->bloglevel == BLOG_GLOBAL_LEVEL) {
@@ -225,9 +230,11 @@ if ($CFG->enablerssfeeds) {
         blog_rss_add_http_header($rsscontext, $rsstitle, $filtertype, $thingid, $tagid);
     }
 }
+if (isset($userid)) {
+    $PAGE->set_heading(fullname($user));
+}
 
 echo $OUTPUT->header();
-
 echo $OUTPUT->heading($blogheaders['heading'], 2);
 
 $bloglisting = new blog_listing($blogheaders['filters']);
index 59aea44..e0608d0 100644 (file)
@@ -55,7 +55,8 @@ $PAGE->set_url($url);
 $PAGE->set_pagelayout('admin');
 
 $sitecontext = context_system::instance();
-$PAGE->set_context($sitecontext);
+$usercontext = context_user::instance($USER->id);
+$PAGE->set_context($usercontext);
 require_login($courseid);
 
 if (empty($CFG->enableblogs)) {
@@ -90,7 +91,7 @@ $strblogs       = get_string('blogs', 'blog');
 
 $title = "$site->shortname: $strblogs : $strpreferences";
 $PAGE->set_title($title);
-$PAGE->set_heading($title);
+$PAGE->set_heading(fullname($USER));
 
 echo $OUTPUT->header();
 
index c8c11ed..0721d28 100644 (file)
@@ -40,7 +40,6 @@ require_login(null, false);
 $context = context_course::instance($course->id);
 $systemcontext = context_system::instance();
 $personalcontext = null;
-$PAGE->set_context($context);
 
 // If we are accessing the page from a site context then ignore this check.
 if ($courseid != SITEID) {
@@ -57,6 +56,12 @@ if (empty($userid)) {
     $personalcontext = context_user::instance($userid);
 }
 
+if (isset($personalcontext)) {
+    $PAGE->set_context($personalcontext);
+} else {
+    $PAGE->set_context($context);
+}
+
 $access = false;
 if (has_capability('moodle/grade:viewall', $systemcontext)) {
     // Ok - can view all course grades.
@@ -154,9 +159,9 @@ if (has_capability('moodle/grade:viewall', $context) && $courseid != SITEID) {
     // Display a standard page.
     if ($courseid == SITEID) {
         $PAGE->set_pagelayout('standard');
-        $header = get_string('mygrades', 'grades'). ' - '.fullname($report->user);
+        $header = get_string('mygrades', 'grades') . ' - ' . fullname($report->user);
         $PAGE->set_title($header);
-        $PAGE->set_heading($header);
+        $PAGE->set_heading(fullname($report->user));
 
         if ($USER->id != $report->user->id) {
             $PAGE->navigation->extend_for_user($report->user);
index 0dfa342..d36475e 100644 (file)
@@ -134,7 +134,7 @@ if ($mform->is_cancelled()) {
     $fullname = fullname($USER, true);
 
     $PAGE->set_title($strpasswordchanged);
-    $PAGE->set_heading($COURSE->fullname);
+    $PAGE->set_heading(fullname($USER));
     echo $OUTPUT->header();
 
     notice($strpasswordchanged, new moodle_url($PAGE->url, array('return'=>1)));
@@ -151,7 +151,7 @@ $strchangepassword = get_string('changepassword');
 $fullname = fullname($USER, true);
 
 $PAGE->set_title($strchangepassword);
-$PAGE->set_heading($COURSE->fullname);
+$PAGE->set_heading($fullname);
 echo $OUTPUT->header();
 
 if (get_user_preferences('auth_forcepasswordchange')) {
index 7b73ec2..c4172df 100644 (file)
@@ -164,7 +164,7 @@ $preferences->beepnewmessage    =  get_user_preferences( 'message_beepnewmessage
 /// Display page header
 $strmessaging = get_string('messaging', 'message');
 $PAGE->set_title($strmessaging);
-$PAGE->set_heading($strmessaging);
+$PAGE->set_heading(fullname($USER));
 
 // Grab the renderer
 $renderer = $PAGE->get_renderer('core', 'message');
index 1b458de..56070bb 100644 (file)
@@ -125,8 +125,9 @@ if (substr($viewing, 0, 7) == MESSAGE_VIEW_COURSE) {
     $PAGE->set_pagelayout('incourse');
 } else {
     $PAGE->set_pagelayout('standard');
-    $PAGE->set_context(context_user::instance($user1->id));
 }
+// Page context should always be set to user.
+$PAGE->set_context(context_user::instance($user1->id));
 if (!empty($user1->id) && $user1->id != $USER->id) {
     $PAGE->navigation->extend_for_user($user1);
 }
@@ -194,11 +195,10 @@ if ($user2realuser) {
     $user2fullname = fullname($user2);
 
     $PAGE->set_title("$strmessages: $user2fullname");
-    $PAGE->set_heading("$strmessages: $user2fullname");
 } else {
     $PAGE->set_title("{$SITE->shortname}: $strmessages");
-    $PAGE->set_heading("{$SITE->shortname}: $strmessages");
 }
+$PAGE->set_heading(fullname($USER));
 
 //now the page contents
 echo $OUTPUT->header();
index 711ea10..a03fe61 100644 (file)
@@ -119,11 +119,12 @@ class user_report_viewed extends \core\event\base {
         {
             case CONTEXT_COURSE:
             case CONTEXT_SYSTEM:
+            case CONTEXT_USER:
                 // OK, expected context level.
                 break;
             default:
                 // Unexpected contextlevel.
-                throw new \coding_exception('Context level must be either CONTEXT_SYSTEM or CONTEXT_COURSE.');
+                throw new \coding_exception('Context level must be either CONTEXT_SYSTEM, CONTEXT_COURSE or CONTEXT_USER.');
         }
     }
 
index 22edf3b..6073719 100644 (file)
@@ -1040,7 +1040,8 @@ class mod_forum_events_testcase extends advanced_testcase {
             'relateduserid' => $user->id,
         );
 
-        $this->setExpectedException('coding_exception', 'Context level must be either CONTEXT_SYSTEM or CONTEXT_COURSE.');
+        $this->setExpectedException('coding_exception',
+                'Context level must be either CONTEXT_SYSTEM, CONTEXT_COURSE or CONTEXT_USER.');
         \mod_forum\event\user_report_viewed::create($params);
     }
 
index 87aba79..c4bcaa0 100644 (file)
@@ -108,14 +108,13 @@ if ($isspecificcourse) {
     // We are going to search for all of the users posts in all courses!
     // a general require login here as we arn't actually within any course.
     require_login();
-    $PAGE->set_context(context_system::instance());
+    $PAGE->set_context(context_user::instance($user->id));
 
     // Now we need to get all of the courses to search.
     // All courses where the user has posted within a forum will be returned.
     $courses = forum_get_courses_user_posted_in($user, $discussionsonly);
 }
 
-
 $params = array(
     'context' => $PAGE->context,
     'relateduserid' => $user->id,
@@ -202,7 +201,11 @@ if (empty($result->posts)) {
 
     // Display a page letting the user know that there's nothing to display;
     $PAGE->set_title($pagetitle);
-    $PAGE->set_heading($pageheading);
+    if ($isspecificcourse) {
+        $PAGE->set_heading($pageheading);
+    } else {
+        $PAGE->set_heading(fullname($user));
+    }
     echo $OUTPUT->header();
     echo $OUTPUT->heading($pagetitle);
     echo $OUTPUT->notification($notification);
@@ -323,7 +326,8 @@ if ($isspecificcourse) {
 }
 
 $PAGE->set_title($pagetitle);
-$PAGE->set_heading($pagetitle);
+$PAGE->set_heading($pageheading);
+
 $PAGE->navigation->extend_for_user($user);
 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
 
index 07a808a..676dd22 100644 (file)
@@ -64,7 +64,7 @@ if (isguestuser()) {  // Force them to see system default, no editing allowed
     $userid = $USER->id;  // Owner of the page
     $context = context_user::instance($USER->id);
     $PAGE->set_blocks_editing_capability('moodle/my:manageblocks');
-    $header = "$SITE->shortname: $strmymoodle";
+    $header = fullname($USER);
 }
 
 // Get the My Moodle page info.  Should always return something unless the database is broken.
@@ -72,10 +72,6 @@ if (!$currentpage = my_get_page($userid, MY_PAGE_PRIVATE)) {
     print_error('mymoodlesetup');
 }
 
-if (!$currentpage->userid) {
-    $context = context_system::instance();  // So we even see non-sticky blocks
-}
-
 // Start setting up the page
 $params = array();
 $PAGE->set_context($context);
@@ -109,17 +105,6 @@ if ($PAGE->user_allowed_editing()) {
         }
     } else if ($edit !== null) {             // Editing state was specified
         $USER->editing = $edit;       // Change editing state
-        if (!$currentpage->userid && $edit) {
-            // If we are viewing a system page as ordinary user, and the user turns
-            // editing on, copy the system pages as new user pages, and get the
-            // new page record
-            if (!$currentpage = my_copy_page($USER->id, MY_PAGE_PRIVATE)) {
-                print_error('mymoodlesetup');
-            }
-            $context = context_user::instance($USER->id);
-            $PAGE->set_context($context);
-            $PAGE->set_subpage($currentpage->id);
-        }
     } else {                          // Editing state is in session
         if ($currentpage->userid) {   // It's a page we can edit, so load from session
             if (!empty($USER->editing)) {
@@ -127,7 +112,16 @@ if ($PAGE->user_allowed_editing()) {
             } else {
                 $edit = 0;
             }
-        } else {                      // It's a system page and they are not allowed to edit system pages
+        } else {
+            // For the page to display properly with the user context header the page blocks need to
+            // be copied over to the user context.
+            if (!$currentpage = my_copy_page($USER->id, MY_PAGE_PRIVATE)) {
+                print_error('mymoodlesetup');
+            }
+            $context = context_user::instance($USER->id);
+            $PAGE->set_context($context);
+            $PAGE->set_subpage($currentpage->id);
+            // It's a system page and they are not allowed to edit system pages
             $USER->editing = $edit = 0;          // Disable editing completely, just to be safe
         }
     }
@@ -158,12 +152,6 @@ if ($PAGE->user_allowed_editing()) {
     $USER->editing = $edit = 0;
 }
 
-// HACK WARNING!  This loads up all this page's blocks in the system context
-if ($currentpage->userid == 0) {
-    $CFG->blockmanagerclass = 'my_syspage_block_manager';
-}
-
-
 echo $OUTPUT->header();
 
 echo $OUTPUT->custom_block_region('content');
index abab4e9..8e8d846 100644 (file)
@@ -104,7 +104,7 @@ if ($userid) {
 
 $PAGE->set_pagelayout('incourse');
 $PAGE->set_title($course->shortname . ': ' . $strnotes);
-$PAGE->set_heading($course->fullname);
+$PAGE->set_heading(fullname($USER));
 
 echo $OUTPUT->header();
 if ($userid) {
index c339e6c..7e94953 100644 (file)
@@ -43,6 +43,10 @@ $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
 $coursecontext   = context_course::instance($course->id);
 $personalcontext = context_user::instance($user->id);
 
+if ($courseid == SITEID) {
+    $PAGE->set_context($personalcontext);
+}
+
 if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
         and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
     //TODO: do not require parents to be enrolled in courses - this is a hack!
@@ -71,7 +75,11 @@ $PAGE->set_url('/report/log/user.php', array('id' => $user->id, 'course' => $cou
 $PAGE->navigation->extend_for_user($user);
 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
 $PAGE->set_title("$course->shortname: $stractivityreport");
-$PAGE->set_heading($course->fullname);
+if ($courseid == SITEID) {
+    $PAGE->set_heading(fullname($user));
+} else {
+    $PAGE->set_heading($course->fullname);
+}
 
 // Trigger a user logs viewed event.
 $event = \report_log\event\user_report_viewed::create(array('context' => $coursecontext, 'relateduserid' => $userid,
index ed4ac77..227ab97 100644 (file)
@@ -41,6 +41,10 @@ $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
 $coursecontext   = context_course::instance($course->id);
 $personalcontext = context_user::instance($user->id);
 
+if ($courseid == SITEID) {
+    $PAGE->set_context($personalcontext);
+}
+
 if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
         and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
     //TODO: do not require parents to be enrolled in courses - this is a hack!
@@ -62,7 +66,11 @@ $PAGE->set_url('/report/outline/user.php', array('id'=>$user->id, 'course'=>$cou
 $PAGE->navigation->extend_for_user($user);
 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
 $PAGE->set_title("$course->shortname: $stractivityreport");
-$PAGE->set_heading($course->fullname);
+if ($courseid == SITEID) {
+    $PAGE->set_heading(fullname($user));
+} else {
+    $PAGE->set_heading($course->fullname);
+}
 
 // Trigger a report viewed event.
 $event = \report_outline\event\report_viewed::create(array('context' => context_course::instance($course->id),
index 7af5d93..7c91197 100644 (file)
@@ -47,6 +47,7 @@ $delete = optional_param('delete', 0, PARAM_INT);
 $PAGE->set_url('/report/usersessions/user.php');
 $PAGE->set_context($context);
 $PAGE->set_title(get_string('navigationlink', 'report_usersessions'));
+$PAGE->set_heading(fullname($USER));
 $PAGE->set_pagelayout('admin');
 
 if ($delete and confirm_sesskey()) {
index 0b1ce5d..3966c1f 100644 (file)
@@ -298,7 +298,7 @@ $strparticipants  = get_string('participants');
 $userfullname     = fullname($user, true);
 
 $PAGE->set_title("$course->shortname: $streditmyprofile");
-$PAGE->set_heading($course->fullname);
+$PAGE->set_heading($userfullname);
 
 echo $OUTPUT->header();
 echo $OUTPUT->heading($userfullname);
index d0136b4..5e6c486 100644 (file)
@@ -310,9 +310,9 @@ if ($user->id == -1 or ($user->id != $USER->id)) {
     if ($user->id == -1) {
         echo $OUTPUT->header();
     } else {
-        $PAGE->set_heading($SITE->fullname);
-        echo $OUTPUT->header();
         $userfullname = fullname($user, true);
+        $PAGE->set_heading($userfullname);
+        echo $OUTPUT->header();
         echo $OUTPUT->heading($userfullname);
     }
 } else if (!empty($USER->newadminuser)) {
@@ -334,7 +334,7 @@ if ($user->id == -1 or ($user->id != $USER->id)) {
     $userfullname     = fullname($user, true);
 
     $PAGE->set_title("$course->shortname: $streditmyprofile");
-    $PAGE->set_heading($course->fullname);
+    $PAGE->set_heading($userfullname);
 
     echo $OUTPUT->header();
     echo $OUTPUT->heading($userfullname);
index 00d1eba..0417b03 100644 (file)
@@ -47,7 +47,7 @@ $struser = get_string('user');
 $PAGE->set_url('/user/files.php');
 $PAGE->set_context($context);
 $PAGE->set_title($title);
-$PAGE->set_heading($title);
+$PAGE->set_heading(fullname($USER));
 $PAGE->set_pagelayout('mydashboard');
 $PAGE->set_pagetype('user-files');
 
index d65676e..c53de77 100644 (file)
@@ -48,17 +48,13 @@ $PAGE->set_context(context_user::instance($userid));
 $PAGE->set_url('/user/preferences.php', array('userid' => $userid));
 $PAGE->set_pagelayout('admin');
 $PAGE->set_pagetype('user-preferences');
+$PAGE->set_title(fullname($user));
+$PAGE->set_heading(fullname($user));
 
 if (!$currentuser) {
-    $PAGE->set_title(fullname($user));
-    $PAGE->set_heading(fullname($user));
-
     $PAGE->navigation->extend_for_user($user);
     $settings = $PAGE->settingsnav->get('userviewingsettings' . $user->id);
 } else {
-    $PAGE->set_title(get_string('preferences'));
-    $PAGE->set_heading(get_string('preferences'));
-
     $settings = $PAGE->settingsnav->get('usercurrentsettings');
 }
 
index ca6fea1..a579185 100644 (file)
@@ -85,7 +85,7 @@ if (!$currentuser &&
     $struser = get_string('user');
     $PAGE->set_context(context_system::instance());
     $PAGE->set_title("$SITE->shortname: $struser");  // Do not leak the name.
-    $PAGE->set_heading("$SITE->shortname: $struser");
+    $PAGE->set_heading($struser);
     $PAGE->set_url('/user/profile.php', array('id' => $userid));
     $PAGE->navbar->add($struser);
     echo $OUTPUT->header();
@@ -99,10 +99,6 @@ if (!$currentpage = my_get_page($userid, MY_PAGE_PUBLIC)) {
     print_error('mymoodlesetup');
 }
 
-if (!$currentpage->userid) {
-    $context = context_system::instance();  // A trick so that we even see non-sticky blocks.
-}
-
 $PAGE->set_context($context);
 $PAGE->set_pagelayout('mypublic');
 $PAGE->set_pagetype('user-profile');
@@ -144,7 +140,7 @@ $strpublicprofile = get_string('publicprofile');
 $PAGE->blocks->add_region('content');
 $PAGE->set_subpage($currentpage->id);
 $PAGE->set_title(fullname($user).": $strpublicprofile");
-$PAGE->set_heading(fullname($user).": $strpublicprofile");
+$PAGE->set_heading(fullname($user));
 
 if (!$currentuser) {
     $PAGE->navigation->extend_for_user($user);
@@ -170,16 +166,6 @@ if ($PAGE->user_allowed_editing()) {
         }
     } else if ($edit !== null) {             // Editing state was specified.
         $USER->editing = $edit;       // Change editing state.
-        if (!$currentpage->userid && $edit) {
-            // If we are viewing a system page as ordinary user, and the user turns
-            // editing on, copy the system pages as new user pages, and get the
-            // new page record.
-            if (!$currentpage = my_copy_page($userid, MY_PAGE_PUBLIC, 'user-profile')) {
-                print_error('mymoodlesetup');
-            }
-            $PAGE->set_context($usercontext);
-            $PAGE->set_subpage($currentpage->id);
-        }
     } else {                          // Editing state is in session.
         if ($currentpage->userid) {   // It's a page we can edit, so load from session.
             if (!empty($USER->editing)) {
@@ -187,7 +173,15 @@ if ($PAGE->user_allowed_editing()) {
             } else {
                 $edit = 0;
             }
-        } else {                      // It's a system page and they are not allowed to edit system pages.
+        } else {
+            // For the page to display properly with the user context header the page blocks need to
+            // be copied over to the user context.
+            if (!$currentpage = my_copy_page($userid, MY_PAGE_PUBLIC, 'user-profile')) {
+                print_error('mymoodlesetup');
+            }
+            $PAGE->set_context($usercontext);
+            $PAGE->set_subpage($currentpage->id);
+            // It's a system page and they are not allowed to edit system pages.
             $USER->editing = $edit = 0;          // Disable editing completely, just to be safe.
         }
     }
@@ -219,11 +213,6 @@ if ($PAGE->user_allowed_editing()) {
     $USER->editing = $edit = 0;
 }
 
-// HACK WARNING!  This loads up all this page's blocks in the system context.
-if ($currentpage->userid == 0) {
-    $CFG->blockmanagerclass = 'my_syspage_block_manager';
-}
-
 // Trigger a user profile viewed event.
 profile_view($user, $usercontext);