From 78d27a9049c62de4a8a91387f4480f09d987c528 Mon Sep 17 00:00:00 2001
From: tjhunt <tjhunt>
Date: Wed, 22 Jul 2009 05:38:28 +0000
Subject: [PATCH] blocks MDL-19902: were seeing empty block columns on the
 front page when not logged in.

It turns out the only reliable way to find out whether blocks will appear is to get the contents. :-(
---
 lib/blocklib.php  | 16 +++++++++++++---
 lib/outputlib.php |  4 ++--
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/lib/blocklib.php b/lib/blocklib.php
index 2cb56eb97d..82520e6b7c 100644
--- a/lib/blocklib.php
+++ b/lib/blocklib.php
@@ -263,21 +263,31 @@ class block_manager {
     /**
      * Determine whether a region contains anything. (Either any real blocks, or
      * the add new block UI.)
+     *
+     * (You may wonder why the $output parameter is required. Unfortunately,
+     * becuase of the way that blocks work, the only reliable way to find out
+     * if a block will be visible is to get the content for output, and to
+     * get the content, you need a renderer. Fortunately, this is not a
+     * performance problem, becuase we cache the output that is generated, and
+     * in almost every case where we call region_has_content, we are about to
+     * output the blocks anyway, so we are not doing wasted effort.)
+     *
      * @param string $region a block region that exists on this page.
+     * @param object $output a moodle_core_renderer. normally the global $OUTPUT. 
      * @return boolean Whether there is anything in this region.
      */
-    public function region_has_content($region) {
+    public function region_has_content($region, $output) {
         if (!$this->is_known_region($region)) {
             return false;
         }
         $this->check_is_loaded();
-        $this->ensure_instances_exist($region);
+        $this->ensure_content_created($region, $output);
         if ($this->page->user_is_editing() && $this->page->user_can_edit_blocks()) {
             // If editing is on, we need all the block regions visible, for the
             // move blocks UI.
             return true;
         }
-        return !empty($this->blockinstances[$region]) || !empty($this->extracontent[$region]);
+        return !empty($this->visibleblockcontent[$region]) || !empty($this->extracontent[$region]);
     }
 
     /**
diff --git a/lib/outputlib.php b/lib/outputlib.php
index e69df92d88..726fb111bb 100644
--- a/lib/outputlib.php
+++ b/lib/outputlib.php
@@ -1972,7 +1972,7 @@ class moodle_core_renderer extends moodle_renderer_base {
 
         echo '<table id="layout-table"><tr>';
         foreach ($lt as $column) {
-            if ($column == 'left' && $this->page->blocks->region_has_content(BLOCK_POS_LEFT)) {
+            if ($column == 'left' && $this->page->blocks->region_has_content(BLOCK_POS_LEFT, $OUTPUT)) {
                 echo '<td id="left-column" class="block-region" style="width: ' . $preferredwidthright . 'px; vertical-align: top;">';
                 echo $this->container_start();
                 echo $this->blocks_for_region(BLOCK_POS_LEFT);
@@ -1987,7 +1987,7 @@ class moodle_core_renderer extends moodle_renderer_base {
                 echo $this->container_end();
                 echo '</td>';
 
-            } else if ($column == 'right' && $this->page->blocks->region_has_content(BLOCK_POS_RIGHT)) {
+            } else if ($column == 'right' && $this->page->blocks->region_has_content(BLOCK_POS_RIGHT, $OUTPUT)) {
                 echo '<td id="right-column" class="block-region" style="width: ' . $preferredwidthright . 'px; vertical-align: top;">';
                 echo $this->container_start();
                 echo $this->blocks_for_region(BLOCK_POS_RIGHT);
-- 
2.39.5