]> git.mjollnir.org Git - moodle.git/commitdiff
blocklib: cache (and used cached) block instances properly in $pageblocks
authormartinlanghoff <martinlanghoff>
Wed, 17 May 2006 04:39:43 +0000 (04:39 +0000)
committermartinlanghoff <martinlanghoff>
Wed, 17 May 2006 04:39:43 +0000 (04:39 +0000)
Now blocks_have_content() caches instantiated block objects inside $pageblocks
and blocks_print_blocks() uses them if available. This behaviour now matches
the documentation: blocks instances are created only once, get_content()
may be invoked several times.

A better fix would be to cache the _output_ of the block (the ->content
property) but it may bite us if any block is counting on being called twice.

Discussion at: http://moodle.org/mod/forum/discuss.php?d=45867

lib/blocklib.php

index 34a5d6c4dd05ce45f7ca0350d0321d857811f78d..c152fd44b1171dead8d0b370ae18b1206b2269af 100644 (file)
@@ -224,7 +224,10 @@ function blocks_have_content(&$pageblocks, $position) {
     if (empty($pageblocks) || !is_array($pageblocks) || !array_key_exists($position,$pageblocks)) {
         return false;
     }
-    foreach($pageblocks[$position] as $instance) {
+    // use a for() loop to get references to the array elements
+    // foreach() cannot fetch references in PHP v4.x
+    for ($n=0; $n<count($pageblocks[$position]);$n++) {
+        $instance = &$pageblocks[$position][$n];
         if(!$instance->visible) {
             continue;
         }
@@ -235,6 +238,10 @@ function blocks_have_content(&$pageblocks, $position) {
             continue;
         }
         if(!$obj->is_empty()) {
+            // cache rec and obj 
+            // for blocks_print_group()
+            $instance->rec = $rec;
+            $instance->obj = $obj; 
             return true;
         }
     }
@@ -263,7 +270,15 @@ function blocks_print_group(&$page, &$pageblocks, $position) {
     $isediting = $page->user_is_editing();
 
     foreach($pageblocks[$position] as $instance) {
-        $block = blocks_get_record($instance->blockid);
+
+        // $instance may have ->rec and ->obj
+        // cached from when we walked $pageblocks
+        // in blocks_have_content()
+        if (empty($instance->rec)) {
+            $block = blocks_get_record($instance->blockid);
+        } else {
+            $block = $instance->rec;
+        }
 
         if (empty($block)) {
             // Block doesn't exist! We should delete this instance!
@@ -274,10 +289,14 @@ function blocks_print_group(&$page, &$pageblocks, $position) {
             // Disabled by the admin
             continue;
         }
-
-        if (!$obj = block_instance($block->name, $instance)) {
-            // Invalid block
-            continue;
+            
+        if (empty($instance->obj)) {
+            if (!$obj = block_instance($block->name, $instance)) {
+                // Invalid block
+                continue;
+            }
+        } else {
+            $obj = $instance->obj;
         }
 
         $editalways = $page->edit_always();