create4.php 5.24 KB
Newer Older
Nigel McNie's avatar
Nigel McNie committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
/**
 * This program is part of Mahara
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * @package    mahara
 * @subpackage core
 * @author     Nigel McNie <nigel@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

define('INTERNAL', 1);
define('MENUITEM', 'view');
require(dirname(dirname(__FILE__)) . '/init.php');
require_once('pieforms/pieform.php');
require_once('pieforms/pieform/elements/calendar.php');
$smarty = smarty(array(), pieform_get_headdata_calendar(pieform_configure_calendar(array())));
33
$createid = param_integer('createid', null);
34
$data = $SESSION->get('create_' . $createid);
Nigel McNie's avatar
Nigel McNie committed
35

36
37

log_debug($data);
Nigel McNie's avatar
Nigel McNie committed
38
39
40
$form = array(
    'name' => 'createview4',
    'elements' => array(
41
42
43
        'accesslist' => array(
            'type'         => 'viewacl',
            'defaultvalue' => isset($data['accesslist']) ? $data['accesslist'] : null
Nigel McNie's avatar
Nigel McNie committed
44
45
        ),
        'submit' => array(
46
47
            'type' => 'cancelbackcreate',
            'value' => array(get_string('cancel'), get_string('back'), get_string('createview'))
Nigel McNie's avatar
Nigel McNie committed
48
49
50
51
        )
    )
);

52
53
54
function createview4_submit_cancel() {
    redirect(get_config('wwwroot') . 'view/');
}
Nigel McNie's avatar
Nigel McNie committed
55

56
function createview4_submit($values) {
57
    global $SESSION, $USER, $createid, $data;
58
59
60
61
    log_debug($values);
    log_debug($data);


62
    if (param_boolean('back')) {
Nigel McNie's avatar
Nigel McNie committed
63
        $data['accesslist'] = array_values((array)$values['accesslist']);
64
65
66
67
68
69
70
        log_debug($data);
        $SESSION->set('create_' . $createid, $data);
        redirect(get_config('wwwroot') . 'view/create3.php?createid=' . $createid);
    }



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    db_begin();
    $time = db_format_timestamp(time());

    $view = new StdClass;
    $view->title = $data['title'];
    $view->description = $data['description'];
    $view->owner = $USER->get('id');
    $view->ownerformat = $data['ownerformat'];
    $view->template = $data['template'];
    $view->startdate = db_format_timestamp($data['startdate']);
    $view->stopdate = db_format_timestamp($data['stopdate']);
    $view->ctime = $view->mtime = $view->atime = $time;
    log_debug($view);
    $viewid = insert_record('view', $view, 'id', true);
    log_debug('inserted view as id ' . $viewid);

    foreach ($data['artefacts'] as $block => $blockdata) {
        if ($blockdata['type'] == 'label') {
            $viewcontent          = new StdClass;
            $viewcontent->view    = $viewid;
            $viewcontent->content = $blockdata['value'];
            $viewcontent->block   = $block;
            $viewcontent->ctime   = $time;
            insert_record('view_content', $viewcontent);
        }
        else if ($blockdata['type'] == 'artefact') {
97
98
99
100
101
102
103
104
105
106
            $blockdata['id'] = (array)$blockdata['id'];
            foreach ($blockdata['id'] as $id) {
                $viewartefact           = new StdClass;
                $viewartefact->view     = $viewid;
                $viewartefact->artefact = $id;
                $viewartefact->block    = $block;
                $viewartefact->ctime    = $time;
                $viewartefact->format   = $blockdata['format'];
                insert_record('view_artefact', $viewartefact);
            }
107
108
109
110
111
112
        }
        else {
            throw new OMGWTFException();
        }
    }

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    // View access
    foreach ($values['accesslist'] as $item) {
        $accessrecord = new StdClass;
        $accessrecord->view = $viewid;
        $accessrecord->startdate = db_format_timestamp($item['startdate']);
        $accessrecord->stopdate  = db_format_timestamp($item['stopdate']);
        switch ($item['type']) {
            case 'public':
            case 'loggedin':
            case 'friends':
                $accessrecord->accesstype = $item['type'];
                insert_record('view_access', $accessrecord);
                break;
            case 'user':
                $accessrecord->usr = $item['id'];
                insert_record('view_access_usr', $accessrecord);
                break;
            case 'group':
                $accessrecord->grp = $item['id'];
                insert_record('view_access_group', $accessrecord);
                break;
            case 'community':
                $accessrecord->community = $item['id'];
                insert_record('view_access_community', $accessrecord);
                break;
        }
    }
140
141
142
143
144
145
146
147
148
    db_commit();
    $SESSION->add_ok_msg(get_string('viewcreatedsuccessfully'));
    redirect(get_config('wwwroot') . 'view/');
}

function createview4_cancel() {
    redirect(get_config('wwwroot') . 'view/');
}

Nigel McNie's avatar
Nigel McNie committed
149
150
151
152
$smarty->assign('createview4form', pieform($form));
$smarty->display('view/create4.tpl');

?>