MDL-32652 - Make block drag-drop work throughout Moodle
authorPaul Nicholls <paul.nicholls@canterbury.ac.nz>
Tue, 15 Jan 2013 00:08:25 +0000 (13:08 +1300)
committerPaul Nicholls <paul.nicholls@canterbury.ac.nz>
Tue, 29 Jan 2013 01:57:28 +0000 (14:57 +1300)
The central "content" region on My Home / user profile pages also needs to
be initialised in lib/ajax/blocks.php in order to allow blocks to be
dropped into that region successfully.  Some tweaks were also necessary in
order to correct the "content" region's structure in order for drag-drop
to function on this region on these pages.

Non-standard block regions (including the central "content" region
used by My Home and user profile pages) were causing the temporary empty
block region used as a drop target to sometimes appear in the wrong place.

The call to init_requirements_data (which includes the block drag-drop JS)
happens at a late enough stage in page generation that we can simply
pass the context ID through to the JS, so that it can be sent as part of
the AJAX request.  This prevents lib/ajax/blocks.php from having to try
to guess the context from the other bits of information it receives, so
block drag-drop should now work everywhere without issues.

course/lib.php
lib/ajax/blocks.php
lib/outputrequirementslib.php
lib/yui/blocks/blocks.js
user/profile.php

index 0673ade..0240760 100644 (file)
@@ -3832,16 +3832,6 @@ function include_course_ajax($course, $usedmodules = array(), $enabledmodules =
             )), null, true);
     }
 
-    // Include blocks dragdrop
-    $params = array(
-        'courseid' => $course->id,
-        'pagetype' => $PAGE->pagetype,
-        'pagelayout' => $PAGE->pagelayout,
-        'subpage' => $PAGE->subpage,
-        'regions' => $PAGE->blocks->get_regions(),
-    );
-    $PAGE->requires->yui_module('moodle-core-blocks', 'M.core_blocks.init_dragdrop', array($params), null, true);
-
     // Require various strings for the command toolbox
     $PAGE->requires->strings_for_js(array(
             'moveleft',
index 470106d..ab24525 100644 (file)
@@ -30,6 +30,7 @@ require_once(dirname(__FILE__) . '/../../config.php');
 $courseid = required_param('courseid', PARAM_INT);
 $pagelayout = required_param('pagelayout', PARAM_ALPHAEXT);
 $pagetype = required_param('pagetype', PARAM_ALPHAEXT);
+$contextid = required_param('contextid', PARAM_INT);
 $subpage = optional_param('subpage', '', PARAM_ALPHANUMEXT);
 $cmid = optional_param('cmid', null, PARAM_INT);
 $action = optional_param('action', '', PARAM_ALPHA);
@@ -50,9 +51,33 @@ if (!is_null($cmid)) {
 require_login($courseid, false, $cm);
 require_sesskey();
 
+// Set context from ID, so we don't have to guess it from other info.
+$PAGE->set_context(context::instance_by_id($contextid));
+
 // Setting layout to replicate blocks configuration for the page we edit
 $PAGE->set_pagelayout($pagelayout);
 $PAGE->set_subpage($subpage);
+$pagetype = explode('-', $pagetype);
+switch ($pagetype[0]) {
+    case 'my':
+        // My Home page needs to have 'content' block region set up.
+        $PAGE->set_blocks_editing_capability('moodle/my:manageblocks');
+        $PAGE->blocks->add_region('content');
+        break;
+    case 'user':
+        if ($pagelayout == 'mydashboard') {
+            // User profile pages also need the 'content' block region set up.
+            $PAGE->blocks->add_region('content');
+            // If it's not the current user's profile, we need a different capability.
+            if ($PAGE->context->contextlevel == CONTEXT_USER && $PAGE->context->instanceid != $USER->id) {
+                $PAGE->set_blocks_editing_capability('moodle/user:manageblocks');
+            } else {
+                $PAGE->set_blocks_editing_capability('moodle/user:manageownblocks');
+            }
+        }
+        break;
+}
+
 echo $OUTPUT->header(); // send headers
 
 switch ($action) {
index d4eff1c..8c97eac 100644 (file)
@@ -282,6 +282,22 @@ class page_requirements_manager {
         if ($page->pagelayout === 'frametop') {
             $this->js_init_call('M.util.init_frametop');
         }
+
+        // Include block drag/drop if editing is on
+        if ($page->user_is_editing()) {
+            $params = array(
+                'courseid' => $page->course->id,
+                'pagetype' => $page->pagetype,
+                'pagelayout' => $page->pagelayout,
+                'subpage' => $page->subpage,
+                'regions' => $page->blocks->get_regions(),
+                'contextid' => $page->context->id,
+            );
+            if (!empty($page->cm->id)) {
+                $params['cmid'] = $page->cm->id;
+            }
+            $page->requires->yui_module('moodle-core-blocks', 'M.core_blocks.init_dragdrop', array($params), null, true);
+        }
     }
 
     /**
index edb655f..baa06be 100644 (file)
@@ -10,7 +10,9 @@ YUI.add('moodle-core-blocks', function(Y) {
         LIGHTBOX : 'lightbox',
         REGIONCONTENT : 'region-content',
         SKIPBLOCK : 'skip-block',
-        SKIPBLOCKTO : 'skip-block-to'
+        SKIPBLOCKTO : 'skip-block-to',
+        MYINDEX : 'page-my-index',
+        REGIONMAIN : 'region-main'
     }
 
     var DRAGBLOCK = function() {
@@ -26,6 +28,15 @@ YUI.add('moodle-core-blocks', function(Y) {
             this.samenodeclass = CSS.BLOCK;
             this.parentnodeclass = CSS.REGIONCONTENT;
 
+            // Add relevant classes and ID to 'content' block region on My Home page.
+            var myhomecontent = Y.Node.all('body#'+CSS.MYINDEX+' #'+CSS.REGIONMAIN+' > .'+CSS.REGIONCONTENT);
+            if (myhomecontent.size() > 0) {
+                var contentregion = myhomecontent.item(0);
+                contentregion.addClass(CSS.BLOCKREGION);
+                contentregion.set('id', CSS.REGIONCONTENT);
+                contentregion.one('div').addClass(CSS.REGIONCONTENT);
+            }
+
             // Initialise blocks dragging
             // Find all block regions on the page
             var blockregionlist = Y.Node.all('div.'+CSS.BLOCKREGION);
@@ -42,17 +53,18 @@ YUI.add('moodle-core-blocks', function(Y) {
                 var regioncontent = Y.Node.create('<div></div>')
                     .addClass(CSS.REGIONCONTENT);
                 blockregion.appendChild(regioncontent);
+                var pre = blockregionlist.filter('#region-pre');
+                var post = blockregionlist.filter('#region-post');
 
-                var regionid = this.get_region_id(blockregionlist.item(0));
-                if (regionid === 'post') {
+                if (pre.size() === 0 && post.size() === 1) {
                     // pre block is missing, instert it before post
                     blockregion.setAttrs({id : 'region-pre'});
-                    blockregionlist.item(0).insert(blockregion, 'before');
+                    post.item(0).insert(blockregion, 'before');
                     blockregionlist.unshift(blockregion);
-                } else {
+                } else if (post.size() === 0 && pre.size() === 1) {
                     // post block is missing, instert it after pre
                     blockregion.setAttrs({id : 'region-post'});
-                    blockregionlist.item(0).insert(blockregion, 'after');
+                    pre.item(0).insert(blockregion, 'after');
                     blockregionlist.push(blockregion);
                 }
             }
@@ -214,6 +226,7 @@ YUI.add('moodle-core-blocks', function(Y) {
                 pagelayout : this.get('pagelayout'),
                 pagetype : this.get('pagetype'),
                 subpage : this.get('subpage'),
+                contextid : this.get('contextid'),
                 action : 'move',
                 bui_moveid : this.get_block_id(dragnode),
                 bui_newregion : this.get_block_region(dropnode)
@@ -263,6 +276,9 @@ YUI.add('moodle-core-blocks', function(Y) {
             cmid : {
                 value : null
             },
+            contextid : {
+                value : null
+            },
             pagelayout : {
                 value : null
             },
index 54d65f2..9439c4e 100644 (file)
@@ -377,8 +377,9 @@ if (!isset($hiddenfields['suspended'])) {
 
 echo "</table></div></div>";
 
-
+echo '<div id="region-content" class="block-region"><div class="region-content">';
 echo $OUTPUT->blocks_for_region('content');
+echo '</div></div>';
 
 // Print messaging link if allowed
 if (isloggedin() && has_capability('moodle/site:sendmessage', $context)