From d7c29c22572314680350ccdcf801f188aca541e4 Mon Sep 17 00:00:00 2001 From: Paul Nicholls Date: Tue, 15 Jan 2013 13:08:25 +1300 Subject: [PATCH] MDL-32652 - Make block drag-drop work throughout Moodle Other than shifting the initialisation from course/lib.php to lib/outputrequirementslib.php, some workarounds/tweaks were required in order to make it work correctly on admin pages and My Home. --- course/lib.php | 10 ---------- lib/ajax/blocks.php | 14 ++++++++++++++ lib/outputrequirementslib.php | 15 +++++++++++++++ lib/yui/blocks/blocks.js | 13 ++++++++++++- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/course/lib.php b/course/lib.php index 52302409346..c0f8945df35 100644 --- a/course/lib.php +++ b/course/lib.php @@ -4503,16 +4503,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', diff --git a/lib/ajax/blocks.php b/lib/ajax/blocks.php index 470106d071c..861540518e6 100644 --- a/lib/ajax/blocks.php +++ b/lib/ajax/blocks.php @@ -53,6 +53,20 @@ require_sesskey(); // 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 'admin': + // Admin pages need to be in the system context, not Site Course context. + $PAGE->set_context(context_system::instance()); + break; + case 'my': + // My Home page needs to be in user context, and to have 'content' block region set up. + $PAGE->set_context(context_user::instance($USER->id)); + $PAGE->set_blocks_editing_capability('moodle/my:manageblocks'); + $PAGE->blocks->add_region('content'); + break; +} + echo $OUTPUT->header(); // send headers switch ($action) { diff --git a/lib/outputrequirementslib.php b/lib/outputrequirementslib.php index d4eff1ccc2a..008d091ebd2 100644 --- a/lib/outputrequirementslib.php +++ b/lib/outputrequirementslib.php @@ -282,6 +282,21 @@ 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(), + ); + 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); + } } /** diff --git a/lib/yui/blocks/blocks.js b/lib/yui/blocks/blocks.js index edb655fa048..1c4ba8381a4 100644 --- a/lib/yui/blocks/blocks.js +++ b/lib/yui/blocks/blocks.js @@ -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);