createversion.php 4.01 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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);
require(dirname(dirname(__FILE__)) . '/init.php');
require_once('view.php');
$viewid = param_integer('view');

$view = new View($viewid, null);

19
20
if (!$view ) {
    throw new AccessDeniedException(get_string('cantversionviewinvalid', 'view'));
21
}
22
23
24
25
26
27
28
if (!$USER->can_edit_view($view)) {
    throw new AccessDeniedException(get_string('cantversionvieweditpermissions', 'view'));
}
if ($view->is_submitted()) {
    throw new AccessDeniedException(get_string('cantversionviewsubmitted', 'view'));
}

29
30
$groupid = $view->get('group');
if ($groupid && !group_within_edit_window($groupid)) {
31
    throw new AccessDeniedException(get_string('cantversionviewgroupeditwindow', 'view'));
32
33
34
35
36
37
}

$version = new stdClass();
$version->numrows = $view->get('numrows');
$version->layout = $view->get('layout');
$version->description = $view->get('description');
38
$version->title = $view->get('title');
39
40
41
42
43
44
45
46
47
48
49
$version->tags = $view->get('tags');
$version->columnsperrow = $view->get('columnsperrow');
$version->blocks = array();
$blocks = get_records_array('block_instance', 'view', $view->get('id'));

if ($blocks) {
    foreach ($blocks as $k => $b) {
        if (safe_require('blocktype', $b->blocktype, 'lib.php', 'require_once', true) !== false) {
            $oldblock = new BlockInstance($b->id, $b);

            $bi = new stdClass();
50
            $bi->originalblockid = $oldblock->get('id');
51
52
53
54
55
56
            $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');
57
58
            $classname = generate_class_name('blocktype', $oldblock->get('blocktype'));
            if (is_callable($classname . '::'. 'get_current_artefacts')) {
59
                // The block is for one artefact so lets see if it displays more than one artefact
60
                if ($artefacts = call_static_method($classname, 'get_current_artefacts', $oldblock)) {
61
62
                    // We need to ignore the parent artefactid
                    foreach ($artefacts as $key => $artefact) {
63
                        if (isset($bi->configdata['artefactid']) && $bi->configdata['artefactid'] == $artefact) {
64
65
66
67
68
69
70
71
                            unset($artefacts[$key]);
                        }
                    }
                    if (!empty($artefacts)) {
                        $bi->configdata['existing_artefacts'] = $artefacts;
                    }
                }
            }
72
73
74
75
76
77
78
79
80
            if ($oldblock->get('blocktype') == 'annotation' || $oldblock->get('blocktype') ==  'textbox') {
                $configdata = $oldblock->get('configdata');
                if (!empty($configdata['artefactid'])) {
                    safe_require('artefact', 'file');
                    $artefactid = $configdata['artefactid'];
                    $artefact = $oldblock->get_artefact_instance($artefactid);
                    $bi->configdata['text'] = $artefact->get('description');
                }
            }
81
82
83
84
            if ($oldblock->get('blocktype') == 'taggedposts') {
                $tagrecords = get_records_array('blocktype_taggedposts_tags', 'block_instance', $oldblock->get('id'), 'tagtype desc, tag', 'tag, tagtype');
                $bi->configdata['tagrecords'] = $tagrecords;
            }
85
86
87
88
89
90
91
92
93
94
95
96
97
            $version->blocks[$k] = $bi;
        }
    }
}

$fordb = new stdClass();
$fordb->view = $view->get('id');
$fordb->ctime = db_format_timestamp(time());
$fordb->blockdata = json_encode($version);
$fordb->owner = $view->get('owner');
$id = insert_record('view_versioning', $fordb, 'id', true);
$SESSION->add_ok_msg(get_string('savedtotimeline', 'view'));
redirect('/view/view.php?id=' . $view->get('id'));