Commit 7b17b833 authored by Cecilia Vela Gurovic's avatar Cecilia Vela Gurovic Committed by Robert Lyon
Browse files

Bug 1813987: Make view versioning work with gridstack

- Only allow to create a new version if the view
is using new gridstack layout.

In timeline version section:
- For view versions that were created with old layout,
show them as they are
(we dont translate old view versions to new layout)
- For view versions that are created with new grid layout,
they are also displayed in the timeline view with the new grid
layout

behatnotneeded

Change-Id: I1abdae9e5e547dcc9bce97cac127085786dcac8a
parent f342e41b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -268,6 +268,7 @@ $string['cantversionviewinvalid'] = 'The supplied page identifier is invalid.';
$string['cantversionvieweditpermissions'] = 'You do not permission to edit this page.';
$string['cantversionviewsubmitted'] = 'You cannot edit this page because it has been submitted for assessment. You will have to wait until it is released.';
$string['cantversionviewgroupeditwindow'] = 'You cannot edit this page. It is outside of the group editable date window.';
$string['cantversionoldlayout'] = 'You cannot save a timeline version of a view with an old layout. You need to convert it to the new grid layout. To do this simply edit the page and the layout will be converted.';

$string['invalidcolumn'] = 'Column %s out of range';

+105 −66
Original line number Diff line number Diff line
@@ -2164,7 +2164,8 @@ class View {
* Gets the view blocks in an array to be easily loaded in js gridstack
* @param boolean $editing    whether we are in the edit more or not
*/
    public function get_blocks($editing=false) {
public function get_blocks($editing=false, $exporting=false, $versioning=false) {
    if (!$versioning) {
        $sql = '
        SELECT bi.id, bi.view, bi.row, bi.column, bi.order,
        positionx, positiony, width, height, blocktype, title, configdata
@@ -2174,19 +2175,36 @@ class View {
        WHERE bi.view = ?
        ORDER BY bi.row, bi.column, bi.order';
        $blocks = get_records_sql_array($sql, array($this->get('id')));
    }
    else {
        $blocks = $versioning->blocks;
    }

    if (is_array($blocks) || is_object($blocks)) {
        foreach ($blocks as $block) {
            require_once(get_config('docroot') . 'blocktype/lib.php');
            $block = (object)$block;
            $block->view = $this->get('id');
            $block->view_obj = $this;
                $b = new BlockInstance($block->id, (array)$block);
            if (!$versioning) {
                $blockid = $block->id;
            }
            else {
                $blockid = $block->originalblockid;
            }
            $b = new BlockInstance($blockid, (array)$block);
            if (isset($versioning->newlayout)) {
                $b->set('positionx', $block->positionx);
                $b->set('positiony', $block->positiony);
                $b->set('width', $block->width);
                $b->set('height', $block->height);
                $b->set('configdata', (array)$block->configdata);
            }
            else {
                $b->set('row', $block->row);
                $b->set('column', $block->column);
                $b->set('order', $block->order);
            }
            $this->grid[]=$b;
        }
    }
@@ -2199,7 +2217,7 @@ class View {
            $result = $result['html'];
        }
        else {
                $result = $blockinstance->render_viewing();
            $result = $blockinstance->render_viewing($exporting, $versioning);
            if (call_static_method(generate_class_name('blocktype', $blockinstance->get('blocktype')), 'has_static_content')) {
                $block['class'] = 'staticblock';
            }
@@ -7165,10 +7183,11 @@ class View {

        $data = json_decode($data);
        $data->version = $versionnumber;
        $this->numrows = isset($data->numrows) ? $data->numrows : $this->numrows;
        $this->layout = isset($data->layout) ? $data->layout : $this->layout;
        $this->description = isset($data->description) ? $data->description : '';
        $this->tags = isset($data->tags) && is_array($data->tags) ? $data->tags : array();
        if (!isset($data->newlayout)) {
            $this->numrows = isset($data->numrows) ? $data->numrows : $this->numrows;
            $this->layout = isset($data->layout) ? $data->layout : $this->layout;
            $colsperrow = array();
            if (isset($data->columnsperrow)) {
                foreach ($data->columnsperrow as $k => $v) {
@@ -7185,33 +7204,53 @@ class View {
                    $this->columns[$i][$j]['width'] = $widths[$j-1];
                }
            }
        }

        $html = '';
        if (!empty($data->blocks)) {
            require_once(get_config('docroot') . 'blocktype/lib.php');
            if (!isset($data->newlayout)) {
                usort($data->blocks, function($a, $b) { return $a->order > $b->order; });
            }
            foreach ($data->blocks as $k => $v) {
                safe_require('blocktype', $v->blocktype);
                $bi = new BlockInstance(0,
                    array(
                $blockdata = array(
                    'id'          => $v->originalblockid,
                    'blocktype'   => $v->blocktype,
                    'title'       => $v->title,
                    'view'        => $this->get('id'),
                    'view_obj'    => $this,
                        'row'         => $v->row,
                        'column'      => $v->column,
                        'order'       => $v->order,
                    'configdata'  => serialize((array)$v->configdata),
                    )
                );
                if (!isset($data->newlayout)) {
                    $blockdata['row']    = $v->row;
                    $blockdata['column'] = $v->column;
                    $blockdata['order']  = $v->order;
                }
                else {
                    $blockdata['positionx'] = $v->positionx;
                    $blockdata['positiony'] = $v->positiony;
                    $blockdata['height']    = $v->height;
                    $blockdata['width']     = $v->width;
                }
                $bi = new BlockInstance(0, $blockdata);
                // Add a fake unique id to allow for pagination etc
                if (!isset($data->newlayout)) {
                    $this->columns[$v->row][$v->column]['blockinstances'][] = $bi;
                }
                else {
                    $this->blocks[] = $bi;
                }
            }
        }
        if (!$USER->has_peer_role_only($this) || $this->has_peer_assessement_block()) {
            if (!isset($data->newlayout)) {
                $html = $this->build_rows(false, false, $data);
            }
            else {
                $html = $this->get_blocks(false, false, $data);
            }
        }
        else {
            $html = '<div class="alert alert-info">
                        <span class="icon icon-lg icon-info-circle left" role="presentation" aria-hidden="true"></span>' .
+19 −1
Original line number Diff line number Diff line
@@ -39,7 +39,25 @@
            },
            formatTitle: function (title, obj) { return '<h3>' + title + '</h3>'; },
            formatSubTitle: function (subTitle, obj) { return '<div class="metadata">' + subTitle + '</div>'; },
            formatBodyContent: function (bodyCnt, obj) { return bodyCnt;}
            formatBodyContent: function (bodyCnt, obj) {
              if (obj.gridlayout) {
                  var grid = $('<div class="grid-stack"></div>');
                  var options = {
                      verticalMargin: 10,
                      float: true,
                      ddPlugin: false,
                  };
                  grid.gridstack(options);
                  grid = grid.data('gridstack');
                  loadGrid(grid, bodyCnt);

                  var container = $('<div class="container-fluid"></div>').append(grid.container);
                  return container[0].outerHTML;
              }
              else {
                  return bodyCnt;
              }
            }
        });
    });
    </script>
+9 −6
Original line number Diff line number Diff line
@@ -31,13 +31,15 @@ if ($groupid && !group_within_edit_window($groupid)) {
    throw new AccessDeniedException(get_string('cantversionviewgroupeditwindow', 'view'));
}

if (!$view->uses_new_layout()) {
    throw new AccessDeniedException(get_string('cantversionoldlayout', 'view'));
}

$version = new stdClass();
$version->numrows = $view->get('numrows');
$version->layout = $view->get('layout');
$version->description = $view->get('description');
$version->title = $view->get('title');
$version->tags = $view->get('tags');
$version->columnsperrow = $view->get('columnsperrow');
$version->newlayout = true;
$version->blocks = array();
$blocks = get_records_array('block_instance', 'view', $view->get('id'));

@@ -51,9 +53,10 @@ if ($blocks) {
            $bi->blocktype = $oldblock->get('blocktype');
            $bi->title = $oldblock->get('title');
            $bi->configdata = $oldblock->get('configdata');
            $bi->row = $oldblock->get('row');
            $bi->column = $oldblock->get('column');
            $bi->order = $oldblock->get('order');
            $bi->positionx = $oldblock->get('positionx');
            $bi->positiony = $oldblock->get('positiony');
            $bi->width = $oldblock->get('width');
            $bi->height = $oldblock->get('height');

            $classname = generate_class_name('blocktype', $oldblock->get('blocktype'));
            if (is_callable($classname . '::'. 'get_current_artefacts')) {
+1 −0
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ if ($versions->count > 0) {
            "assignID" => $value->id,
            "taskShortDate" => date('j F', strtotime($value->ctime)),
            "taskDetails" => $value->blockdata_formatted->html,
            "gridlayout" => isset($value->blockdata_formatted->newlayout),
        );
    }
}
Loading