MDL-47189 my: Fix the addition of default blocks to my/ page
authorFrederic Massart <fred@moodle.com>
Mon, 6 Oct 2014 10:57:17 +0000 (18:57 +0800)
committerFrederic Massart <fred@moodle.com>
Tue, 14 Oct 2014 08:03:12 +0000 (16:03 +0800)
Fixing the upgrade script so that they attach the new blocks
to the right my_pages entry. An upgrade script has also been
added to fix the existing bad data.

blocks/badges/db/upgrade.php
blocks/calendar_month/db/upgrade.php
blocks/calendar_upcoming/db/upgrade.php
lib/blocklib.php
lib/db/upgrade.php
version.php

index 3be12fd..3b3ec4d 100644 (file)
@@ -49,20 +49,25 @@ function xmldb_block_badges_upgrade($oldversion, $block) {
         // Add this block the default blocks on /my.
         $blockname = 'badges';
 
-        $page = new moodle_page();
-        $page->set_context(context_system::instance());
+        // Do not try to add the block if we cannot find the default my_pages entry.
+        // Private => 1 refers to MY_PAGE_PRIVATE.
+        if ($systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => 1))) {
+            $page = new moodle_page();
+            $page->set_context(context_system::instance());
 
-        // Check to see if this block is already on the default /my page.
-        $criteria = array(
-            'blockname' => $blockname,
-            'parentcontextid' => $page->context->id,
-            'pagetypepattern' => 'my-index'
-        );
+            // Check to see if this block is already on the default /my page.
+            $criteria = array(
+                'blockname' => $blockname,
+                'parentcontextid' => $page->context->id,
+                'pagetypepattern' => 'my-index',
+                'subpagepattern' => $systempage->id,
+            );
 
-        if (!$DB->record_exists('block_instances', $criteria)) {
-            // Add the block to the default /my.
-            $page->blocks->add_region(BLOCK_POS_RIGHT);
-            $page->blocks->add_block($blockname, BLOCK_POS_RIGHT, 0, false, 'my-index');
+            if (!$DB->record_exists('block_instances', $criteria)) {
+                // Add the block to the default /my.
+                $page->blocks->add_region(BLOCK_POS_RIGHT);
+                $page->blocks->add_block($blockname, BLOCK_POS_RIGHT, 0, false, 'my-index', $systempage->id);
+            }
         }
 
         upgrade_block_savepoint(true, 2014062600, $blockname);
index d4ae9b1..a03a0e1 100644 (file)
@@ -49,20 +49,25 @@ function xmldb_block_calendar_month_upgrade($oldversion, $block) {
         // Add this block the default blocks on /my.
         $blockname = 'calendar_month';
 
-        $page = new moodle_page();
-        $page->set_context(context_system::instance());
+        // Do not try to add the block if we cannot find the default my_pages entry.
+        // Private => 1 refers to MY_PAGE_PRIVATE.
+        if ($systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => 1))) {
+            $page = new moodle_page();
+            $page->set_context(context_system::instance());
 
-        // Check to see if this block is already on the default /my page.
-        $criteria = array(
-            'blockname' => $blockname,
-            'parentcontextid' => $page->context->id,
-            'pagetypepattern' => 'my-index'
-        );
+            // Check to see if this block is already on the default /my page.
+            $criteria = array(
+                'blockname' => $blockname,
+                'parentcontextid' => $page->context->id,
+                'pagetypepattern' => 'my-index',
+                'subpagepattern' => $systempage->id,
+            );
 
-        if (!$DB->record_exists('block_instances', $criteria)) {
-            // Add the block to the default /my.
-            $page->blocks->add_region(BLOCK_POS_RIGHT);
-            $page->blocks->add_block($blockname, BLOCK_POS_RIGHT, 0, false, 'my-index');
+            if (!$DB->record_exists('block_instances', $criteria)) {
+                // Add the block to the default /my.
+                $page->blocks->add_region(BLOCK_POS_RIGHT);
+                $page->blocks->add_block($blockname, BLOCK_POS_RIGHT, 0, false, 'my-index', $systempage->id);
+            }
         }
 
         upgrade_block_savepoint(true, 2014062600, $blockname);
index 947d37c..ecce6e4 100644 (file)
@@ -49,20 +49,25 @@ function xmldb_block_calendar_upcoming_upgrade($oldversion, $block) {
         // Add this block the default blocks on /my.
         $blockname = 'calendar_upcoming';
 
-        $page = new moodle_page();
-        $page->set_context(context_system::instance());
+        // Do not try to add the block if we cannot find the default my_pages entry.
+        // Private => 1 refers to MY_PAGE_PRIVATE.
+        if ($systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => 1))) {
+            $page = new moodle_page();
+            $page->set_context(context_system::instance());
 
-        // Check to see if this block is already on the default /my page.
-        $criteria = array(
-            'blockname' => $blockname,
-            'parentcontextid' => $page->context->id,
-            'pagetypepattern' => 'my-index'
-        );
+            // Check to see if this block is already on the default /my page.
+            $criteria = array(
+                'blockname' => $blockname,
+                'parentcontextid' => $page->context->id,
+                'pagetypepattern' => 'my-index',
+                'subpagepattern' => $systempage->id,
+            );
 
-        if (!$DB->record_exists('block_instances', $criteria)) {
-            // Add the block to the default /my.
-            $page->blocks->add_region(BLOCK_POS_RIGHT);
-            $page->blocks->add_block($blockname, BLOCK_POS_RIGHT, 0, false, 'my-index');
+            if (!$DB->record_exists('block_instances', $criteria)) {
+                // Add the block to the default /my.
+                $page->blocks->add_region(BLOCK_POS_RIGHT);
+                $page->blocks->add_block($blockname, BLOCK_POS_RIGHT, 0, false, 'my-index', $systempage->id);
+            }
         }
 
         upgrade_block_savepoint(true, 2014062600, $blockname);
index 9c9dc65..f8d12bf 100644 (file)
@@ -2178,7 +2178,13 @@ function blocks_add_default_system_blocks() {
     $page->blocks->add_blocks(array(BLOCK_POS_LEFT => array('navigation', 'settings')), '*', null, true);
     $page->blocks->add_blocks(array(BLOCK_POS_LEFT => array('admin_bookmarks')), 'admin-*', null, null, 2);
 
+    if ($defaultmypage = $DB->get_record('my_pages', array('userid' => null, 'name' => '__default', 'private' => 1))) {
+        $subpagepattern = $defaultmypage->id;
+    } else {
+        $subpagepattern = null;
+    }
+
     $newblocks = array('private_files', 'online_users', 'badges', 'calendar_month', 'calendar_upcoming');
     $newcontent = array('course_overview');
-    $page->blocks->add_blocks(array(BLOCK_POS_RIGHT => $newblocks, 'content' => $newcontent), 'my-index');
+    $page->blocks->add_blocks(array(BLOCK_POS_RIGHT => $newblocks, 'content' => $newcontent), 'my-index', $subpagepattern);
 }
index 7f8cbc7..ec35ffa 100644 (file)
@@ -3980,5 +3980,43 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2014100800.00);
     }
 
+    if ($oldversion < 2014101001.00) {
+        // Some blocks added themselves to the my/ home page, but they did not declare the
+        // subpage of the default my home page. While the upgrade script has been fixed, this
+        // upgrade script will fix the data that was wrongly added.
+
+        // We only proceed if we can find the right entry from my_pages. Private => 1 refers to
+        // the constant value MY_PAGE_PRIVATE.
+        if ($systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => 1))) {
+
+            // Select the blocks there could have been automatically added. showinsubcontexts is hardcoded to 0
+            // because it is possible for administrators to have forced it on the my/ page by adding it to the
+            // system directly rather than updating the default my/ page.
+            $blocks = array('course_overview', 'private_files', 'online_users', 'badges', 'calendar_month', 'calendar_upcoming');
+            list($blocksql, $blockparams) = $DB->get_in_or_equal($blocks, SQL_PARAMS_NAMED);
+            $select = "parentcontextid = :contextid
+                    AND pagetypepattern = :page
+                    AND showinsubcontexts = 0
+                    AND subpagepattern IS NULL
+                    AND blockname $blocksql";
+            $params = array(
+                'contextid' => context_system::instance()->id,
+                'page' => 'my-index'
+            );
+            $params = array_merge($params, $blockparams);
+
+            $DB->set_field_select(
+                'block_instances',
+                'subpagepattern',
+                $systempage->id,
+                $select,
+                $params
+            );
+        }
+
+        // Main savepoint reached.
+        upgrade_main_savepoint(true, 2014101001.00);
+    }
+
     return true;
 }
index e5f6d12..5f7f299 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2014101000.00;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2014101001.00;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.