MDL-11131 - fixed the weight of sticky blocks as it moved around.

This commit is contained in:
Rossiani Wijaya
2010-01-20 04:36:23 +00:00
parent e3fb0b7847
commit 1833b24abb
3 changed files with 121 additions and 86 deletions
+3 -1
View File
@@ -21,6 +21,8 @@ $summary = optional_param('summary', '', PARAM_RAW);
$sequence = optional_param('sequence', '', PARAM_SEQUENCE);
$visible = optional_param('visible', 0, PARAM_INT);
$pageaction = optional_param('action', '', PARAM_ALPHA); // Used to simulate a DELETE command
$positiontoinsert = optional_param('positiontoinsert', '', PARAM_ALPHA);
$positiontoinsertid = optional_param('positiontoinsertid', '', PARAM_RAW);
// Authorise the user and verify some incoming data
if (!$course = get_record('course', 'id', $courseid)) {
@@ -234,7 +236,7 @@ switch($req_method) {
case 'DELETE':
switch ($class) {
case 'block':
blocks_execute_action($PAGE, $pageblocks, 'delete', $blockinstance);
blocks_execute_action($PAGE, $pageblocks, 'delete', $blockinstance, false, false);
break;
case 'resource':
+50 -39
View File
@@ -68,8 +68,8 @@ block_class.prototype.startDrag = function(x, y) {
var dragEl = this.getDragEl();
var clickEl = this.getEl();
dragEl.innerHTML = clickEl.innerHTML;
dragEl.className = clickEl.className;
dragEl.innerHTML = clickEl.innerHTML;
dragEl.className = clickEl.className;
dragEl.style.color = this.DDM.getStyle(clickEl, "color");;
dragEl.style.backgroundColor = this.DDM.getStyle(clickEl, "backgroundColor");
dragEl.style.border = '0px';
@@ -83,14 +83,14 @@ block_class.prototype.startDrag = function(x, y) {
//restyle side boxes to highlight
for (var i=0; i<targets.length; i++) {
var targetEl = targets[i].getEl();
targetEl.style.background = "#fefff0";
targetEl.opacity = .3;
targetEl.filter = "alpha(opacity=30)";
}
}
}
block_class.prototype.endDrag = function() {
// reset the linked element styles
@@ -110,9 +110,9 @@ block_class.prototype.onDragDrop = function(e, id) {
} else {
oDD = YAHOO.util.DDM.getBestMatch(id);
}
var el = this.getEl();
var el = this.getEl();
if (this.debug) {
YAHOO.log("id="+id+" el="+e+" x="+YAHOO.util.Dom.getXY(this.getDragEl()));
}
@@ -120,21 +120,21 @@ block_class.prototype.onDragDrop = function(e, id) {
this.move_block(id);
//YAHOO.util.DDM.moveToEl(el, oDD.getEl());
this.resetTargets();
}
block_class.prototype.find_target = function(column){
var collisions = column.find_sub_collision(YAHOO.util.Region.getRegion(this.getDragEl()));
//determine position
var insertbefore = null;
if(collisions.length == 0)
return;
insertbefore = column.blocks[collisions[0][0]];
return insertbefore;
}
@@ -145,7 +145,7 @@ block_class.prototype.resetTargets = function() {
var targetEl = targets[i].getEl();
targetEl.style.background = "";
targetEl.opacity = 1;
targetEl.filter = "alpha(opacity=100)";
targetEl.filter = "alpha(opacity=100)";
}
}
@@ -156,47 +156,58 @@ block_class.prototype.move_block = function(columnid){
var inserttarget = this.find_target(column);
if(this.debug && inserttarget != null)YAHOO.log("moving "+this.getEl().id+" before "+inserttarget.getEl().id+" - parentNode="+this.getEl().parentNode.id);
if(this == inserttarget){
if (this.getEl() == inserttarget) {
if(this.debug)YAHOO.log("Dropping on self, resetting");
this.endDrag();
return;
}
//remove from document
if(this.getEl().parentNode != null)
this.getEl().parentNode.removeChild(this.getEl());
//remove from document
if (this.getEl().parentNode != null) {
this.getEl().parentNode.removeChild(this.getEl());
}
//insert into correct place
if(inserttarget != null ){
inserttarget.getEl().parentNode.insertBefore(this.getEl(),inserttarget.getEl());
if (inserttarget != null && inserttarget.getEl().parentNode != null) {
inserttarget.getEl().parentNode.insertBefore(this.getEl(),inserttarget.getEl());
positiontoinsert = "before";
if (inserttarget.getEl().id != 'rinst0' || inserttarget.getEl().id == 'rinst0'){
positiontoinsertid = inserttarget.getEl().id;
} else if (main.adminBlock.parentNode.nextSibling != null) {
positiontoinsertid = main.adminBlock.parentNode.nextSibling.id;
} else {
positiontoinsertid = inserttarget.getEl().id;
}
}else if(column == main.rightcolumn){//if right side insert before admin block
column.getEl().insertBefore(this.getEl(),main.adminBlock);
}else{
column.getEl().appendChild(this.getEl());
} else if (column == main.rightcolumn && column.getEl() != null ) {//if right side insert before admin block
column.getEl().insertBefore(this.getEl(),main.tempBlock.childNode);
positiontoinsert = "after";
positiontoinsertid = main.tempBlock.parentNode;
} else {
column.getEl().appendChild(this.getEl());
positiontoinsert = "after";
positiontoinsertid = column.getEl().id;
}
this.reset_regions();
//remove block from current array
if(main.rightcolumn.has_block(this))
if (main.rightcolumn.has_block(this)) {
main.rightcolumn.remove_block(this);
else if(main.leftcolumn.has_block(this))
} else if (main.leftcolumn.has_block(this)) {
main.leftcolumn.remove_block(this);
}
//insert into new array
column.insert_block(this,inserttarget);
}
}
block_class.prototype.reset_regions = function() {
var blockcount = main.blocks.length;
var blockcount = main.blocks.length;
for (i=0; i<blockcount; i++) {
main.blocks[i].region = YAHOO.util.Region.getRegion(main.blocks[i].getEl());
main.blocks[i].region = YAHOO.util.Region.getRegion(main.blocks[i].getEl());
}
}
@@ -247,13 +258,13 @@ block_class.prototype.toggle_hide = function(e, target, isCosmetic) {
}
block_class.prototype.delete_button = function() {
block_class.prototype.delete_button = function() {
// Remove from local model.
if (main.rightcolumn.has_block(this)) {
main.rightcolumn.remove_block(this);
} else if (main.leftcolumn.has_block(this)) {
main.leftcolumn.remove_block(this);
}
}
// Remove block from the drag and drop group in YUI.
this.removeFromGroup('blocks');
@@ -271,8 +282,8 @@ block_class.prototype.delete_button = function() {
block_class.prototype.updatePosition = function(index, columnId) {
//update the db for the position
main.connectQueue_add('POST', 'class=block&field=position', null,
//update the db for the position
main.connectQueue_add('POST', 'class=block&field=position&positiontoinsert='+positiontoinsert +'&positiontoinsertid='+positiontoinsertid, null,
'value='+index+'&column='+columnId+'&instanceId='+this.instanceId);
if (this.debug) {
@@ -305,10 +316,10 @@ column_class.prototype.init_column = function(id, group,config,ident){
}
column_class.prototype.find_sub_collision = function(dragRegion){
column_class.prototype.find_sub_collision = function(dragRegion){
if(this.debug)YAHOO.log("Finding Collisions on "+this.getEl().id+" with "+this.blocks.length+" blocks");
//find collisions with sub_elements(blocks), return array of collisions with regions of collision
var collisions = new Array();
//find collisions with sub_elements(blocks), return array of collisions with regions of collision
var collisions = new Array();
for(i=0;i<this.blocks.length;i++){
if(this.debug)YAHOO.log("testing region "+this.blocks[i].region+" against" + dragRegion + "intersect ="+this.blocks[i].region.intersect(dragRegion));
var intersect = this.blocks[i].region.intersect(dragRegion);
@@ -332,7 +343,7 @@ column_class.prototype.insert_block = function(el,targetel){
var found = -1;
var tempStore = nextStore = null;
for(var i=0;i<blockcount;i++){
if(found > 0){
if (found >= 0) {
tempStore = this.blocks[i];
this.blocks[i] = nextStore;
nextStore = tempStore;
+68 -46
View File
@@ -254,28 +254,27 @@ function blocks_have_content(&$pageblocks, $position) {
// Parameters passed by reference for speed; they are not modified.
function blocks_print_group(&$page, &$pageblocks, $position) {
global $COURSE, $CFG, $USER;
$isediting = $page->user_is_editing();
if (empty($pageblocks[$position])) {
$groupblocks = array();
$maxweight = 0;
$maxweight = 0;
} else {
$groupblocks = $pageblocks[$position];
$maxweight = max(array_keys($groupblocks));
$maxweight = max(array_keys($groupblocks));
}
if (!empty($CFG->ajaxcapable) && $CFG->ajaxcapable && !empty($COURSE->javascriptportal) && $isediting) {
$COURSE->javascriptportal->currentblocksection = $position;
$COURSE->javascriptportal->block_add($position.'inst0', FALSE);
}
foreach ($groupblocks as $instance) {
if (!empty($instance->pinned)) {
$maxweight--;
}
}
$isediting = $page->user_is_editing();
foreach($groupblocks as $instance) {
// $instance may have ->rec and ->obj
// cached from when we walked $pageblocks
// in blocks_have_content()
@@ -331,7 +330,7 @@ function blocks_print_group(&$page, &$pageblocks, $position) {
if ($isediting) {
$obj->_print_shadow();
}
} else {
} else {
global $COURSE;
if(!empty($COURSE->javascriptportal)) {
$COURSE->javascriptportal->currentblocksection = $position;
@@ -339,11 +338,11 @@ function blocks_print_group(&$page, &$pageblocks, $position) {
$obj->_print_block();
}
if (!empty($COURSE->javascriptportal)
&& (empty($instance->pinned) || !$instance->pinned)) {
&& (empty($instance->pinned) || !$instance->pinned)) {
$COURSE->javascriptportal->block_add('inst'.$instance->id, !$instance->visible);
}
}
} // End foreach
// Check if
// we are on the default position/side AND
// we're editing the page AND
@@ -364,8 +363,12 @@ function blocks_print_group(&$page, &$pageblocks, $position) {
if ($page->blocks_default_position() == $position &&
$page->user_is_editing() &&
($managecourseblocks || $editmymoodle || $myownblogpage || defined('ADMIN_STICKYBLOCKS'))) {
print_side_block(NULL,NULL, NULL, NULL, NULL, array('id'=> BLOCK_POS_RIGHT.'inst0', 'class'=>'tempblockhandler'));
blocks_print_adminblock($page, $pageblocks);
} else if ($page->user_is_editing() &&
($managecourseblocks || $editmymoodle || $myownblogpage || defined('ADMIN_STICKYBLOCKS'))) {
print_side_block(NULL,NULL, NULL, NULL, NULL, array('id'=> BLOCK_POS_LEFT.'inst0', 'class'=>'tempblockhandler'));
}
}
@@ -507,7 +510,7 @@ function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid,
// To this data, add anything the page itself needs to display
$hiddendata = array_merge($hiddendata, $page->url_get_parameters());
if($data = data_submitted()) {
if ($data = data_submitted()) {
$remove = array_keys($hiddendata);
foreach($remove as $item) {
unset($data->$item);
@@ -558,31 +561,36 @@ function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid,
blocks_delete_instance($instance, $pinned);
break;
case 'moveup':
if(empty($instance)) {
if (empty($instance)) {
error('Invalid block instance for '. $blockaction);
}
if($instance->weight == 0) {
if ($instance->weight == 0) {
// The block is the first one, so a move "up" probably means it changes position
// Where is the instance going to be moved?
$newpos = $page->blocks_move_position($instance, BLOCK_MOVE_UP);
$newweight = (empty($pageblocks[$newpos]) ? 0 : max(array_keys($pageblocks[$newpos])) + 1);
blocks_execute_repositioning($instance, $newpos, $newweight, $pinned);
}
else {
} else {
// The block is just moving upwards in the same position.
// This configuration will make sure that even if somehow the weights
// become not continuous, block move operations will eventually bring
// the situation back to normal without printing any warnings.
if(!empty($pageblocks[$instance->position][$instance->weight - 1])) {
$other = $pageblocks[$instance->position][$instance->weight - 1];
// the situation back to normal without printing any warnings.
if (!empty($pageblocks[$instance->position][$instance->weight - 1])) {
//define instance's position in the array
foreach ($pageblocks[$instance->position] as $instancekeysindex => $index ){
if ($pageblocks[$instance->position][$instancekeysindex]->blockid == $instance->blockid){
$instanceindex = $instancekeysindex;
}
}
$other = $pageblocks[$instance->position][$instanceindex - 1];
}
if(!empty($other)) {
if (!empty($other)) {
++$other->weight;
if (!empty($pinned)) {
if (!empty($pinned)) {
update_record('block_pinned', $other);
} else {
} else {
update_record('block_instance', $other);
}
}
@@ -591,15 +599,14 @@ function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid,
update_record('block_pinned', $instance);
} else {
update_record('block_instance', $instance);
}
}
}
break;
case 'movedown':
if(empty($instance)) {
if (empty($instance)) {
error('Invalid block instance for '. $blockaction);
}
if($instance->weight == max(array_keys($pageblocks[$instance->position]))) {
if ($instance->weight == max(array_keys($pageblocks[$instance->position]))) {
// The block is the last one, so a move "down" probably means it changes position
// Where is the instance going to be moved?
$newpos = $page->blocks_move_position($instance, BLOCK_MOVE_DOWN);
@@ -612,10 +619,16 @@ function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid,
// This configuration will make sure that even if somehow the weights
// become not continuous, block move operations will eventually bring
// the situation back to normal without printing any warnings.
if(!empty($pageblocks[$instance->position][$instance->weight + 1])) {
$other = $pageblocks[$instance->position][$instance->weight + 1];
}
if(!empty($other)) {
if (!empty($pageblocks[$instance->position][$instance->weight + 1])) {
//define instance's position in the array
foreach ($pageblocks[$instance->position] as $instancekeysindex => $index ){
if ($pageblocks[$instance->position][$instancekeysindex]->blockid == $instance->blockid){
$instanceindex = $instancekeysindex;
}
}
$other = $pageblocks[$instance->position][$instanceindex + 1];
}
if (!empty($other)) {
--$other->weight;
if (!empty($pinned)) {
update_record('block_pinned', $other);
@@ -628,18 +641,22 @@ function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid,
update_record('block_pinned', $instance);
} else {
update_record('block_instance', $instance);
}
}
}
break;
case 'moveleft':
if(empty($instance)) {
error('Invalid block instance for '. $blockaction);
}
// Where is the instance going to be moved?
$newpos = $page->blocks_move_position($instance, BLOCK_MOVE_LEFT);
$newweight = (empty($pageblocks[$newpos]) ? 0 : max(array_keys($pageblocks[$newpos])) + 1);
$newweight = 0;
if (!empty($pinned) && !empty($pageblocks[$newpos]) ){
$newweight = $pageblocks[$newpos][max(array_keys($pageblocks[$newpos])) ]->weight + 1;
} else if(!empty($pageblocks[$newpos]) && (!array_key_exists('pinned', $pageblocks[$newpos][max(array_keys($pageblocks[$newpos]))])) ){
$newweight = $pageblocks[$newpos][max(array_keys($pageblocks[$newpos])) ]->weight + 1;
}
blocks_execute_repositioning($instance, $newpos, $newweight, $pinned);
break;
case 'moveright':
@@ -649,8 +666,13 @@ function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid,
// Where is the instance going to be moved?
$newpos = $page->blocks_move_position($instance, BLOCK_MOVE_RIGHT);
$newweight = (empty($pageblocks[$newpos]) ? 0 : max(array_keys($pageblocks[$newpos])) + 1);
$newweight = 0;
if (!empty($pinned) && !empty($pageblocks[$newpos]) ){
$newweight = $pageblocks[$newpos][max(array_keys($pageblocks[$newpos])) ]->weight + 1;
}else if(!empty($pageblocks[$newpos]) && (!array_key_exists('pinned', $pageblocks[$newpos][max(array_keys($pageblocks[$newpos]))])) ){
$newweight = $pageblocks[$newpos][max(array_keys($pageblocks[$newpos])) ]->weight + 1;
}
blocks_execute_repositioning($instance, $newpos, $newweight, $pinned);
break;
case 'add':
@@ -707,7 +729,7 @@ function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid,
break;
}
if ($redirect) {
if ($redirect) {
// In order to prevent accidental duplicate actions, redirect to a page with a clean url
redirect($page->url_get_full());
}
@@ -716,7 +738,7 @@ function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid,
// You can use this to get the blocks to respond to URL actions without much hassle
function blocks_execute_url_action(&$PAGE, &$pageblocks,$pinned=false) {
$blockaction = optional_param('blockaction', '', PARAM_ALPHA);
if (empty($blockaction) || !$PAGE->user_allowed_editing() || !confirm_sesskey()) {
return;
}
@@ -745,8 +767,8 @@ function blocks_execute_repositioning(&$instance, $newpos, $newweight, $pinned=f
}
// Close the weight gap we 'll leave behind
if (!empty($pinned)) {
$sql = 'UPDATE '. $CFG->prefix .'block_instance SET weight = weight - 1 '.
if (!empty($pinned)) {
$sql = 'UPDATE '. $CFG->prefix .'block_pinned SET weight = weight - 1 '.
'WHERE pagetype = \''. $instance->pagetype.
'\' AND position = \'' .$instance->position.
'\' AND weight > '. $instance->weight;
@@ -799,7 +821,7 @@ function blocks_move_block($page, &$instance, $destpos, $destweight=NULL, $pinne
// First we close the gap that will be left behind when we take out the
// block from it's current column.
if ($pinned) {
$closegapsql = "UPDATE {$CFG->prefix}block_instance
$closegapsql = "UPDATE {$CFG->prefix}block_pinned
SET weight = weight - 1
WHERE weight > '$instance->weight'
AND position = '$instance->position'
@@ -818,7 +840,7 @@ function blocks_move_block($page, &$instance, $destpos, $destweight=NULL, $pinne
// Now let's make space for the block being moved.
if ($pinned) {
$opengapsql = "UPDATE {$CFG->prefix}block_instance
$opengapsql = "UPDATE {$CFG->prefix}block_pinned
SET weight = weight + 1
WHERE weight >= '$destweight'
AND position = '$destpos'
@@ -948,7 +970,7 @@ function blocks_print_adminblock(&$page, &$pageblocks) {
global $USER;
$missingblocks = blocks_get_missing($page, $pageblocks);
if (!empty($missingblocks)) {
$strblocks = '<div class="title"><h2>';
$strblocks .= get_string('blocks');
@@ -968,7 +990,7 @@ function blocks_print_adminblock(&$page, &$pageblocks) {
asort($menu);
$target = $page->url_get_full(array('sesskey' => $USER->sesskey, 'blockaction' => 'add'));
$content = popup_form($target.'&amp;blockid=', $menu, 'add_block', '', $stradd .'...', '', '', true);
$content = popup_form($target.'&amp;blockid=', $menu, 'add_block', '', $stradd .'...', '', '', true);
print_side_block($strblocks, $content, NULL, NULL, NULL, array('class' => 'block_adminblock'));
}
}