MDL-30811 core: Initiate notifications on first use only

This commit is contained in:
Andrew Nicols 2016-03-02 13:03:59 +08:00
parent ce878562f3
commit 2f244f1cc6
4 changed files with 14 additions and 10 deletions

View File

@ -81,6 +81,9 @@ class notification {
// Add the notification directly to the session.
// This will either be fetched in the header, or by JS in the footer.
if (!isset($SESSION->notifications) || !array($SESSION->notifications)) {
$SESSION->notifications = [];
}
$SESSION->notifications[] = (object) array(
'message' => $message,
'type' => $level,
@ -100,7 +103,7 @@ class notification {
}
$notifications = $SESSION->notifications;
$SESSION->notifications = [];
unset($SESSION->notifications);
$renderables = [];
foreach ($notifications as $notification) {

View File

@ -157,9 +157,8 @@ class manager {
public static function init_empty_session() {
global $CFG;
// Backup notifications. These should be preserved across session changes until the user fetches and clears them.
$notifications = [];
if (isset($GLOBALS['SESSION']->notifications)) {
// Backup notifications. These should be preserved across session changes until the user fetches and clears them.
$notifications = $GLOBALS['SESSION']->notifications;
}
$GLOBALS['SESSION'] = new \stdClass();
@ -167,8 +166,10 @@ class manager {
$GLOBALS['USER'] = new \stdClass();
$GLOBALS['USER']->id = 0;
// Restore notifications.
$GLOBALS['SESSION']->notifications = $notifications;
if (!empty($notifications)) {
// Restore notifications.
$GLOBALS['SESSION']->notifications = $notifications;
}
if (isset($CFG->mnet_localhost_id)) {
$GLOBALS['USER']->mnethostid = $CFG->mnet_localhost_id;
} else {

View File

@ -59,7 +59,7 @@ class core_session_manager_testcase extends advanced_testcase {
\core\session\manager::init_empty_session();
$this->assertInstanceOf('stdClass', $SESSION);
$this->assertCount(1, (array)$SESSION);
$this->assertEmpty((array)$SESSION);
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
$this->assertSame($GLOBALS['SESSION'], $SESSION);
@ -149,7 +149,7 @@ class core_session_manager_testcase extends advanced_testcase {
$this->assertEquals(0, $USER->id);
$this->assertInstanceOf('stdClass', $SESSION);
$this->assertCount(1, (array)$SESSION);
$this->assertEmpty((array)$SESSION);
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
$this->assertSame($GLOBALS['SESSION'], $SESSION);

View File

@ -76,7 +76,7 @@ class core_sessionlib_testcase extends advanced_testcase {
$this->assertSame($PAGE->context, context_course::instance($SITE->id));
$this->assertNotSame($adminsession, $SESSION);
$this->assertObjectNotHasAttribute('test1', $SESSION);
$this->assertCount(1, (array)$SESSION);
$this->assertEmpty((array)$SESSION);
$usersession1 = $SESSION;
$SESSION->test2 = true;
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
@ -99,7 +99,7 @@ class core_sessionlib_testcase extends advanced_testcase {
$this->assertSame($PAGE->context, context_course::instance($SITE->id));
$this->assertNotSame($adminsession, $SESSION);
$this->assertNotSame($usersession1, $SESSION);
$this->assertCount(1, (array)$SESSION);
$this->assertEmpty((array)$SESSION);
$usersession2 = $SESSION;
$usersession2->test3 = true;
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
@ -123,7 +123,7 @@ class core_sessionlib_testcase extends advanced_testcase {
$this->assertSame($PAGE->context, context_course::instance($SITE->id));
$this->assertNotSame($adminsession, $SESSION);
$this->assertNotSame($usersession1, $SESSION);
$this->assertCount(1, (array)$SESSION);
$this->assertEmpty((array)$SESSION);
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
$this->assertSame($GLOBALS['SESSION'], $SESSION);
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);