edit.php 6.75 KB
Newer Older
Martyn Smith's avatar
Martyn Smith committed
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
Martyn Smith's avatar
Martyn Smith committed
5
 *
Francois Marier's avatar
Francois Marier committed
6
7
8
9
 * 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 3 of the License, or
 * (at your option) any later version.
Martyn Smith's avatar
Martyn Smith committed
10
 *
Francois Marier's avatar
Francois Marier committed
11
12
13
14
 * 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.
Martyn Smith's avatar
Martyn Smith committed
15
 *
Francois Marier's avatar
Francois Marier committed
16
17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Martyn Smith's avatar
Martyn Smith committed
18
19
20
 *
 * @package    mahara
 * @subpackage core
21
 * @author     Catalyst IT Ltd
Martyn Smith's avatar
Martyn Smith committed
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
23
 * @copyright  (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
Martyn Smith's avatar
Martyn Smith committed
24
25
26
27
 *
 */

define('INTERNAL', 1);
28
define('MENUITEM', 'myportfolio/views');
29

30
31
32
33
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'view');
define('SECTION_PAGE', 'edit');

Martyn Smith's avatar
Martyn Smith committed
34
require(dirname(dirname(__FILE__)) . '/init.php');
35
require_once(get_config('docroot') . 'lib/view.php');
36
require_once(get_config('docroot') . 'lib/group.php');
Martyn Smith's avatar
Martyn Smith committed
37

38
$id = param_integer('id', 0); // if 0, we're creating a new view
39
40
$new = param_boolean('new');

41
if (empty($id)) {
42
    $new = true;
43
    $group = param_integer('group', null);
44
45
}
else {
46
    $view = new View($id);
47
    if (!$USER->can_edit_view($view)) {
48
49
        throw new AccessDeniedException(get_string('canteditdontown', 'view'));
    }
50
51
52
53
54
55

    // If the view has been submitted to a group, disallow editing
    $submittedto = $view->get('submittedto');
    if ($submittedto) {
        throw new AccessDeniedException(get_string('canteditsubmitted', 'view', get_field('group', 'name', 'id', $submittedto)));
    }
56
57
58
59
60
61

    $group = $view->get('group');
}

if ($group && !group_user_access($group)) {
    throw new AccessDeniedException();
62
63
64
65
66
67
68
}

if ($new || empty($id)) {
    define('TITLE', get_string('createviewstepone', 'view'));
}
else {
    define('TITLE', get_string('editviewdetails', 'view', $view->get('title')));
69
}
Martyn Smith's avatar
Martyn Smith committed
70

71
$heading = TITLE; // for the smarty template
Martyn Smith's avatar
Martyn Smith committed
72

73
require_once('pieforms/pieform.php');
Martyn Smith's avatar
Martyn Smith committed
74

75
76
77
78
79
$formatstring = '%s (%s)';
$ownerformatoptions = array(
    FORMAT_NAME_FIRSTNAME => sprintf($formatstring, get_string('firstname'), $USER->get('firstname')),
    FORMAT_NAME_LASTNAME => sprintf($formatstring, get_string('lastname'), $USER->get('lastname')),
    FORMAT_NAME_FIRSTNAMELASTNAME => sprintf($formatstring, get_string('fullname'), full_name())
Martyn Smith's avatar
Martyn Smith committed
80
81
);

82
83
84
85
86
87
88
$preferredname = $USER->get('preferredname');
if ($preferredname !== '') {
    $ownerformatoptions[FORMAT_NAME_PREFERREDNAME] = sprintf($formatstring, get_string('preferredname'), $preferredname);
}
$studentid = (string)get_field('artefact', 'title', 'owner', $USER->get('id'), 'artefacttype', 'studentid');
if ($studentid !== '') {
    $ownerformatoptions[FORMAT_NAME_STUDENTID] = sprintf($formatstring, get_string('studentid'), $studentid);
Martyn Smith's avatar
Martyn Smith committed
89
}
90
91
$ownerformatoptions[FORMAT_NAME_DISPLAYNAME] = sprintf($formatstring, get_string('displayname'), display_name($USER));

92
$editview = array(
93
94
95
96
97
98
    'name'     => 'editview',
    'method'   => 'post',
    'autofocus' => 'title',
    'plugintype' => 'core',
    'pluginname' => 'view',
    'elements' => array(
99
        'id' => array(
100
            'type'  => 'hidden',
101
            'value' => $id,
102
        ),
103
104
105
106
        'new' => array(
            'type' => 'hidden',
            'value' => $new,
        ),
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
        'title' => array(
            'type'         => 'text',
            'title'        => get_string('title','view'),
            'defaultvalue' => isset($view) ? $view->get('title') : null,
            'rules'        => array( 'required' => true ),
        ),
        'description' => array(
            'type'         => 'wysiwyg',
            'title'        => get_string('description','view'),
            'rows'         => 10,
            'cols'         => 70,
            'defaultvalue' => isset($view) ? $view->get('description') : null,
        ),
        'tags'        => array(
            'type'         => 'tags',
            'title'        => get_string('tags'),
123
            'description'  => get_string('tagsdescprofile'),
124
            'defaultvalue' => isset($view) ? $view->get('tags') : null,
125
            'help'         => true,
126
127
128
129
130
131
132
133
134
135
136
        ),
        'ownerformat' => array(
            'type'         => 'select',
            'title'        => get_string('ownerformat','view'),
            'description'  => get_string('ownerformatdescription','view'),
            'options'      => $ownerformatoptions,
            'defaultvalue' => isset($view) ? $view->get('ownerformat') : FORMAT_NAME_DISPLAYNAME,
            'rules'        => array('required' => true),
        ),
        'submit'   => array(
            'type'  => 'submitcancel',
137
            'value' => array(empty($new) ? get_string('save') : get_string('next'), get_string('cancel')),
138
139
            'confirm' => $new && isset($view) ? array(null, get_string('confirmcancelcreatingview', 'view')) : null,
        )
140
    ),
141
142
143
144
145
146
147
148
149
150
);

if ($group) {
    $editview['elements']['group'] = array(
        'type'  => 'hidden',
        'value' => $group
    );
}

$editview = pieform($editview);
Martyn Smith's avatar
Martyn Smith committed
151

152
function editview_cancel_submit() {
153
	global $view, $new, $group;
154
155
156
	if (isset($view) && $new) {
	    $view->delete();
	}
157
158
159
        if ($group) {
            redirect('/view/groupviews.php?group='.$group);
        }
160
161
    redirect('/view');
}
Martyn Smith's avatar
Martyn Smith committed
162

163
function editview_submit(Pieform $form, $values) {
Martyn Smith's avatar
Martyn Smith committed
164

165
    global $USER, $SESSION;
Martyn Smith's avatar
Martyn Smith committed
166

167
168
    $editing = !empty($values['id']);
    $view = new View($values['id'], $values);
169
    $group = isset($values['group']) ? (int)$values['group'] : null;
170
171
172
173
    if ($group && !group_user_access($group)) {
        $SESSION->add_error_msg(get_string('notamember', 'group'));
        redirect('/view/groupviews.php?group='.$group);
    }
Martyn Smith's avatar
Martyn Smith committed
174

175
176
    if (empty($editing)) {
        $view->set('numcolumns', 3); // default
177
178
179
180
181
182
        if ($group) {
            $view->set('group', $group);
        }
        else {
            $view->set('owner', $USER->get('id'));
        }
Martyn Smith's avatar
Martyn Smith committed
183
    }
184
185
    else {
        $view->set('dirty', true);
Martyn Smith's avatar
Martyn Smith committed
186
187
    }

188
    $view->commit();
Martyn Smith's avatar
Martyn Smith committed
189

190
    if ($values['new']) {
191
192
193
194
        $redirecturl = '/view/blocks.php?id=' . $view->get('id') . '&new=1';
    } 
    else {
        $SESSION->add_ok_msg(get_string('viewsavedsuccessfully', 'view'));
195
196
197
198
199
200
        if ($group) {
            $redirecturl = '/view/groupviews.php?group='.$group;
        }
        else {
            $redirecturl = '/view/index.php';
        }
201
    }
Martyn Smith's avatar
Martyn Smith committed
202

203
    redirect($redirecturl);
Martyn Smith's avatar
Martyn Smith committed
204
205
206

}

207
208
209
210
$smarty = smarty();
$smarty->assign('heading', $heading);
$smarty->assign('editview', $editview);
$smarty->display('view/edit.tpl');
Martyn Smith's avatar
Martyn Smith committed
211
212

?>