### Eclipse Workspace Patch 1.0
#P moodle19b
Index: lib/blocklib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/blocklib.php,v
retrieving revision 1.129.2.3
diff -u -r1.129.2.3 blocklib.php
--- lib/blocklib.php	2 Jul 2008 05:12:27 -0000	1.129.2.3
+++ lib/blocklib.php	5 Jul 2008 10:32:54 -0000
@@ -224,25 +224,25 @@
     if (empty($pageblocks) || !is_array($pageblocks) || !array_key_exists($position,$pageblocks)) {
         return false;
     }
-    // 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 (empty($instance->visible)) {
-            continue;
-        }
-        if(!$record = blocks_get_record($instance->blockid)) {
-            continue;
-        }
-        if(!$obj = block_instance($record->name, $instance)) {
-            continue;
-        }
-        if(!$obj->is_empty()) {
-            // cache rec and obj
-            // for blocks_print_group()
-            $instance->rec = $record;
-            $instance->obj = $obj;
-            return true;
+    foreach ($pageblocks as $position=>$unused) {
+        foreach ($pageblocks[$position] as $n=>$unused2) {
+            $instance = &$pageblocks[$position][$n];
+            if (empty($instance->visible)) {
+                continue;
+            }
+            if(!$record = blocks_get_record($instance->blockid)) {
+                continue;
+            }
+            if(!$obj = block_instance($record->name, $instance)) {
+                continue;
+            }
+            if(!$obj->is_empty()) {
+                // cache rec and obj
+                // for blocks_print_group()
+                $instance->rec = $record;
+                $instance->obj = $obj;
+                return true;
+            }
         }
     }
 
@@ -894,27 +894,27 @@
  */
 function blocks_get_by_page_pinned($page) {
     $pinned = blocks_get_pinned($page);
-    $user = blocks_get_by_page($page);
+    $normal = blocks_get_by_page($page);
 
-    $weights = array();
+    $blocks = array();
 
-    foreach ($pinned as $pos => $arr) {
-        $weights[$pos] = count($arr);
-    }
-
-    foreach ($user as $pos => $blocks) {
-        if (!array_key_exists($pos,$pinned)) {
-             $pinned[$pos] = array();
-        }
-        if (!array_key_exists($pos,$weights)) {
-            $weights[$pos] = 0;
+    foreach ($normal as $pos=>$unused) {
+        $blocks[$pos] = array();
+        //first add all pinned with negative wights - we wnat the mto be first, we never move them if both dsiplayed
+        if (!empty($pinned[$pos])) {
+            $keys = array_keys($pinned[$pos]);
+            $max = array_pop($keys); // max weight
+            foreach ($pinned[$pos] as $weight=>$block) {
+                $blocks[$pos][$weight-$max-1] = $block;
+            }
         }
-        foreach ($blocks as $block) {
-            $pinned[$pos][$weights[$pos]] = $block;
-            $weights[$pos]++;
+        //now add normal blocks
+        foreach ($normal[$pos] as $weight=>$block) {
+            $blocks[$pos][$weight] = $block;
         }
     }
-    return $pinned;
+
+    return $blocks;
 }
 
 

