Commit f342e41b authored by Cecilia Vela Gurovic's avatar Cecilia Vela Gurovic Committed by Robert Lyon

Bug 1813987: Copying a page (in old or new layout) creates a page with new layout

- Copy a page when original view is using
grid layout
- Copy a page when original view is using
old layout: Translates the original page layout
without modifying it,
and creates a new page with new grid layout
The blocks in the new page will not have height
assigned. The height of the blocks is updated
on first load of the page in the browser.
This can happen when editing the page or displaying it.
- Copy a collection with old layout: creates the new collection
with new grid layout pages. Each page needs to be loaded on the
browser to update the block heights

behatnotneeded

Change-Id: I43b5f9050be192c057cb1803dcd69575c137a9cc
parent 32de0b18
...@@ -2139,13 +2139,18 @@ class BlockInstance { ...@@ -2139,13 +2139,18 @@ class BlockInstance {
$obj->height = $height; $obj->height = $height;
$obj->width = $width; $obj->width = $width;
//TODO: move this inside of the commit if (isset($obj->positionx) && isset($obj->positiony) && isset($obj->height) && isset($obj->width)) {
ensure_record_exists('block_instance_dimension', (object) array('block' => $this->id), $obj); //TODO: move this inside of the commit
ensure_record_exists('block_instance_dimension', (object) array('block' => $this->id), $obj);
$this->set('positionx', $positionx);
$this->set('positiony', $positiony);
$this->set('height', $height);
$this->set('width', $width);
}
else {
throw new \Exception(get_string('dimensionsnotset', 'view'));
}
$this->set('positionx', $positionx);
$this->set('positiony', $positiony);
$this->set('height', $height);
$this->set('width', $width);
} }
} }
......
...@@ -66,7 +66,7 @@ function loadGrid(grid, blocks) { ...@@ -66,7 +66,7 @@ function loadGrid(grid, blocks) {
function updateTranslatedGridRows(blocks) { function updateTranslatedGridRows(blocks) {
var height = [], maxheight = [], realheight; var height = [], maxheight = [], realheight, updatedGrid = [];
height[0] = 0; height[0] = 0;
maxheight[0] = 0; maxheight[0] = 0;
...@@ -100,16 +100,17 @@ function updateTranslatedGridRows(blocks) { ...@@ -100,16 +100,17 @@ function updateTranslatedGridRows(blocks) {
el, el,
block.positionx, block.positionx,
block.positiony block.positiony
); );
var id = block.id, var updatedBlock = {};
dimensions = { updatedBlock.id = block.id;
newx: block.positionx, updatedBlock.dimensions = {
newy: block.positiony, newx: +block.positionx,
newwidth: block.width, newy: +block.positiony,
newheight: realheight, newwidth: +block.width,
} newheight: +realheight,
moveBlock(id, dimensions); };
updatedGrid.push(updatedBlock);
if (height[block.row][block.column] == 0) { if (height[block.row][block.column] == 0) {
height[block.row][block.column] = realheight; height[block.row][block.column] = realheight;
...@@ -119,7 +120,8 @@ function updateTranslatedGridRows(blocks) { ...@@ -119,7 +120,8 @@ function updateTranslatedGridRows(blocks) {
} }
maxheight[block.row] = Math.max(...height[block.row]); maxheight[block.row] = Math.max(...height[block.row]);
}); });
// update all blocks together
moveBlocks(updatedGrid);
} }
function updateBlockSizes() { function updateBlockSizes() {
...@@ -196,6 +198,15 @@ function moveBlock(id, whereTo) { ...@@ -196,6 +198,15 @@ function moveBlock(id, whereTo) {
sendjsonrequest(config['wwwroot'] + 'view/blocks.json.php', pd, 'POST'); sendjsonrequest(config['wwwroot'] + 'view/blocks.json.php', pd, 'POST');
} }
function moveBlocks(grid) {
var pd = {
'id': $('#viewid').val(),
'blocks': JSON.stringify(grid),
};
sendjsonrequest(config['wwwroot'] + 'view/grid.json.php', pd, 'POST');
}
var serializeWidgetMap = function(items) { var serializeWidgetMap = function(items) {
// get the block id // get the block id
// json call to update new position and/or dimension // json call to update new position and/or dimension
......
...@@ -512,6 +512,8 @@ $string['gotonextversion'] = 'Go to the next version '; ...@@ -512,6 +512,8 @@ $string['gotonextversion'] = 'Go to the next version ';
$string['gotopreviousversion'] = 'Go to the previous version'; $string['gotopreviousversion'] = 'Go to the previous version';
$string['loadingtimelinecontent'] = 'Loading timeline for "%s". If the page has many versions, this may take a while.'; $string['loadingtimelinecontent'] = 'Loading timeline for "%s". If the page has many versions, this may take a while.';
// layout // layout strings
$string['bottom'] = 'Bottom'; $string['bottom'] = 'Bottom';
$string['top'] = 'Top'; $string['top'] = 'Top';
$string['blockssizeupdated'] = 'Block sizes were updated successfully';
$string['dimensionsnotset'] = 'Block dimensions not set';
...@@ -474,26 +474,6 @@ class View { ...@@ -474,26 +474,6 @@ class View {
$view->commit(); $view->commit();
// if layout is set, and it's not a default layout
// add an entry to usr_custom_layout if one does not already exist
if ($template->get('layout') !== null) {
$customlayout = get_record('view_layout', 'id', $template->get('layout'), 'iscustom', 1);
if ($customlayout !== false) {
// is the owner of the copy going to be a group or institution or not?
$group = $view->group;
$institution = $view->institution;
$owner = (!empty($institution) || !empty($group)) ? null : $view->owner;
$data = (object) array(
'usr' => $owner,
'group' => $group,
'institution' => $institution,
'layout' => $template->get('layout'),
);
$where = clone $data;
ensure_record_exists('usr_custom_layout', $where, $data);
}
}
$blocks = get_records_array('block_instance', 'view', $view->get('id')); $blocks = get_records_array('block_instance', 'view', $view->get('id'));
if ($blocks) { if ($blocks) {
foreach ($blocks as $b) { foreach ($blocks as $b) {
...@@ -2314,6 +2294,28 @@ class View { ...@@ -2314,6 +2294,28 @@ class View {
return ($usesnewlayout || !$hasblocks); return ($usesnewlayout || !$hasblocks);
} }
/*
* Checks if the block dimension heights of the page are set to default = 1
* and they need to be reset when loading the page
* This can happen when we copy a view that has an old layout
*/
function needs_block_resize_on_load() {
$viewid = $this->get('id');
$sql = "SELECT * FROM {block_instance} bi
JOIN {block_instance_dimension} bd
ON bi.id = bd.block
WHERE bi.view = ?";
$hasblocks = record_exists_sql($sql, array($viewid));
$sql = "SELECT * FROM {block_instance} bi
JOIN {block_instance_dimension} bd
ON bi.id = bd.block
WHERE bd.height > 1 AND bi.view = ?";
$blockshavedefaultheights = !record_exists_sql($sql, array($viewid));
return ($hasblocks && $blockshavedefaultheights);
}
/** /**
* Returns the HTML for the columns of this view * Returns the HTML for the columns of this view
*/ */
...@@ -2682,9 +2684,10 @@ class View { ...@@ -2682,9 +2684,10 @@ class View {
* *
* @param array $values parameters for this function * @param array $values parameters for this function
* id => int of block instance to move * id => int of block instance to move
* row => int current row * newx => int x position to move to
* column => int column to move to * newy => int y position to move to
* order => position in new column to insert at * newheight => int height of the block
* newwidth => int width of the block
*/ */
public function moveblockinstance($values) { public function moveblockinstance($values) {
$requires = array('id', 'newx', 'newy', 'newheight', 'newwidth'); $requires = array('id', 'newx', 'newy', 'newheight', 'newwidth');
...@@ -5951,8 +5954,6 @@ class View { ...@@ -5951,8 +5954,6 @@ class View {
public function copy_contents($template, &$artefactcopies) { public function copy_contents($template, &$artefactcopies) {
$this->set('lockblocks', $template->get('lockblocks')); $this->set('lockblocks', $template->get('lockblocks'));
$this->set('numrows', $template->get('numrows'));
$this->set('layout', $template->get('layout'));
if ($template->get('template') == self::SITE_TEMPLATE if ($template->get('template') == self::SITE_TEMPLATE
&& $template->get('type') == 'portfolio') { && $template->get('type') == 'portfolio') {
$this->set('description', ''); $this->set('description', '');
...@@ -5964,8 +5965,39 @@ class View { ...@@ -5964,8 +5965,39 @@ class View {
$this->set('instructions', EmbeddedImage::prepare_embedded_images($this->copy_setting_info($template, $artefactcopies, 'instructions'), 'instructions', $this->get('id'))); $this->set('instructions', EmbeddedImage::prepare_embedded_images($this->copy_setting_info($template, $artefactcopies, 'instructions'), 'instructions', $this->get('id')));
} }
$this->set('tags', $template->get('tags')); $this->set('tags', $template->get('tags'));
$this->set('columnsperrow', $template->get('columnsperrow'));
$blocks = get_records_array('block_instance', 'view', $template->get('id')); // If the template uses the gridstack layout
if ($template->uses_new_layout()) {
// then recover info from block_instance_dimension too
$sql = "
SELECT * FROM {block_instance} bi
INNER JOIN {block_instance_dimension} bd
ON bi.id = bd.block
WHERE bi.view = ?";
$blocks = get_records_sql_array($sql, array($template->get('id')));
}
else {
require_once(get_config('libroot') . 'gridstacklayout.php');
// get blocks in old layout
$blocks = get_records_array('block_instance', 'view', $template->get('id'));
// translate layout
$oldlayoutcontent = get_blocks_in_old_layout($template->get('id'));
$newlayoutcontent = translate_to_new_layout($oldlayoutcontent);
foreach ($newlayoutcontent as $block) {
$dimensions[$block->block] = $block;
}
foreach ($blocks as $block) {
$block->positionx = $dimensions[$block->id]->positionx;
$block->positiony = $dimensions[$block->id]->positiony;
$block->width = $dimensions[$block->id]->width;
$block->height = $dimensions[$block->id]->height;
}
}
$numcopied = array('blocks' => 0); $numcopied = array('blocks' => 0);
if ($blocks) { if ($blocks) {
......
{include file="header.tpl" headertype="page"} {include file="header.tpl" headertype="page"}
{include file='modal-details.tpl'} {include file='modal-details.tpl'}
<input type="hidden" id="viewid" name="id" value="{$viewid}">
<div id="view-description" class="view-description {if $toolbarhtml}with-toolbar{/if}"> <div id="view-description" class="view-description {if $toolbarhtml}with-toolbar{/if}">
{$viewdescription|clean_html|safe} {$viewdescription|clean_html|safe}
</div> </div>
......
...@@ -163,11 +163,15 @@ require_once('pieforms/pieform/elements/select.php'); ...@@ -163,11 +163,15 @@ require_once('pieforms/pieform/elements/select.php');
$inlinejs .= pieform_element_select_get_inlinejs(); $inlinejs .= pieform_element_select_get_inlinejs();
$inlinejs .= "jQuery(window).on('pageupdated', {}, function() { dock.init(jQuery(document)); });"; $inlinejs .= "jQuery(window).on('pageupdated', {}, function() { dock.init(jQuery(document)); });";
$needstranslate = "false"; $blockresizeonload = "false";
if ($view->uses_new_layout() && $view->needs_block_resize_on_load()) {
// we're copying from an old layout view and need to resize blocks
$blockresizeonload = "true";
}
if (!$view->uses_new_layout()) { if (!$view->uses_new_layout()) {
// if it's old rowa layout, we need to translate to grid layout // if it's old row layout, we need to translate to grid layout
save_blocks_in_new_layout($view->get('id')); save_blocks_in_new_layout($view->get('id'));
$needstranslate = "true"; $blockresizeonload = "true";
} }
$blocks = $view->get_blocks(true); $blocks = $view->get_blocks(true);
$blocksencode = json_encode($blocks); $blocksencode = json_encode($blocks);
...@@ -192,7 +196,7 @@ $(function () { ...@@ -192,7 +196,7 @@ $(function () {
grid.resizable('.grid-stack-item', true); grid.resizable('.grid-stack-item', true);
// should add the blocks one by one // should add the blocks one by one
var blocks = {$blocksencode}; var blocks = {$blocksencode};
if ({$needstranslate}) { if ({$blockresizeonload}) {
// update block heights when they are loaded // update block heights when they are loaded
loadGridTranslate(grid, blocks); loadGridTranslate(grid, blocks);
} }
...@@ -304,7 +308,6 @@ if ($collection = $view->get('collection')) { ...@@ -304,7 +308,6 @@ if ($collection = $view->get('collection')) {
} }
$smarty->assign('collectionid', $collectionid); $smarty->assign('collectionid', $collectionid);
$smarty->assign('needstranslate', ($needstranslate ? 1 : 0));
$smarty->assign('issiteview', isset($institution) && ($institution == 'mahara')); $smarty->assign('issiteview', isset($institution) && ($institution == 'mahara'));
$smarty->assign('issitetemplate', $view->is_site_template()); $smarty->assign('issitetemplate', $view->is_site_template());
......
<?php
/**
*
* @package mahara
* @subpackage core
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
define('INTERNAL', 1);
define('JSON', 1);
require(dirname(dirname(__FILE__)) . '/init.php');
require_once('view.php');
$view = new View(param_integer('id'));
$blocks = param_variable('blocks', null);
$blocks = json_decode($blocks);
if (!$USER->can_edit_view($view)) {
throw new AccessDeniedException();
}
require_once(get_config('docroot') . 'blocktype/lib.php');
try {
foreach ($blocks as $block) {
$id = $block->id;
$dimensions = $block->dimensions;
$bi = new BlockInstance($id);
// Check if the block_instance belongs to this view
if ($bi->get('view') != $view->get('id')) {
throw new AccessDeniedException(get_string('blocknotinview', 'view', $bi->get('id')));
}
$bi->set('positionx', $dimensions->newx);
$bi->set('positiony', $dimensions->newy);
$bi->set('width', $dimensions->newwidth);
$bi->set('height', $dimensions->newheight);
$bi->commit();
}
json_reply(false, get_string('blockssizeupdated', 'view'));
}
catch(Exception $e) {
json_reply(true, $e->getMessage());
}
...@@ -349,12 +349,18 @@ if ($owner && $owner == $USER->get('id')) { ...@@ -349,12 +349,18 @@ if ($owner && $owner == $USER->get('id')) {
} }
} }
// Don't show page content to a user with peer role // Don't show page content to a user with peer role
// if the view doesn't have a peer assessment block // if the view doesn't have a peer assessment block
if (!$USER->has_peer_role_only($view) || $view->has_peer_assessement_block() if (!$USER->has_peer_role_only($view) || $view->has_peer_assessement_block()
|| ($USER->is_admin_for_user($view->get('owner')) && $view->is_objectionable())) { || ($USER->is_admin_for_user($view->get('owner')) && $view->is_objectionable())) {
if ($newlayout = $view->uses_new_layout()) { if ($newlayout = $view->uses_new_layout()) {
$blockresizeonload = "false";
if ($view->uses_new_layout() && $view->needs_block_resize_on_load()) {
// we're copying from an old layout view and need to resize blocks
$blockresizeonload = "true";
}
$blocks = $view->get_blocks(); $blocks = $view->get_blocks();
$blocks = json_encode($blocks); $blocks = json_encode($blocks);
$blocksjs = <<<EOF $blocksjs = <<<EOF
...@@ -368,9 +374,15 @@ $(function () { ...@@ -368,9 +374,15 @@ $(function () {
grid.gridstack(options); grid.gridstack(options);
grid = $('.grid-stack').data('gridstack'); grid = $('.grid-stack').data('gridstack');
// should add the blocks one by one
var blocks = {$blocks}; var blocks = {$blocks};
loadGrid(grid, blocks); if ({$blockresizeonload}) {
// the page was copied from an old layout page
// and the blocks still need to be resized
loadGridTranslate(grid, blocks);
}
else {
loadGrid(grid, blocks);
}
}); });
EOF; EOF;
} }
...@@ -649,6 +661,7 @@ if ($titletext !== $title) { ...@@ -649,6 +661,7 @@ if ($titletext !== $title) {
$smarty->assign('userisowner', ($owner && $owner == $USER->get('id'))); $smarty->assign('userisowner', ($owner && $owner == $USER->get('id')));
$smarty->assign('viewid', $view->get('id'));
$smarty->display('view/view.tpl'); $smarty->display('view/view.tpl');
mahara_touch_record('view', $viewid); // Update record 'atime' mahara_touch_record('view', $viewid); // Update record 'atime'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment