edit.php 7.52 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
    $institution = param_alphanum('institution', null);
45
46
}
else {
47
    $view = new View($id);
48
    if (!$USER->can_edit_view($view)) {
49
50
        throw new AccessDeniedException(get_string('canteditdontown', 'view'));
    }
51
52
53
54
55
56

    // 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)));
    }
57
58

    $group = $view->get('group');
59
    $institution = $view->get('institution');
60
61
}

62
63
if ($group && !group_user_can_edit_views($group)
    || $institution && !$USER->can_edit_institution($institution)) {
64
    throw new AccessDeniedException();
65
66
67
68
69
70
71
}

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

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

76
require_once('pieforms/pieform.php');
Martyn Smith's avatar
Martyn Smith committed
77

78
79
80
81
82
$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
83
84
);

85
86
87
88
89
90
91
$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
92
}
93
94
$ownerformatoptions[FORMAT_NAME_DISPLAYNAME] = sprintf($formatstring, get_string('displayname'), display_name($USER));

95
$editview = array(
96
97
98
99
100
101
    'name'     => 'editview',
    'method'   => 'post',
    'autofocus' => 'title',
    'plugintype' => 'core',
    'pluginname' => 'view',
    'elements' => array(
102
        'id' => array(
103
            'type'  => 'hidden',
104
            'value' => $id,
105
        ),
106
107
108
109
        'new' => array(
            'type' => 'hidden',
            'value' => $new,
        ),
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
        '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'),
126
            'description'  => get_string('tagsdescprofile'),
127
            'defaultvalue' => isset($view) ? $view->get('tags') : null,
128
            'help'         => true,
129
130
131
        ),
        'submit'   => array(
            'type'  => 'submitcancel',
132
            'value' => array(empty($new) ? get_string('save') : get_string('next'), get_string('cancel')),
133
134
            'confirm' => $new && isset($view) ? array(null, get_string('confirmcancelcreatingview', 'view')) : null,
        )
135
    ),
136
137
138
139
140
141
142
143
);

if ($group) {
    $editview['elements']['group'] = array(
        'type'  => 'hidden',
        'value' => $group
    );
}
144
145
146
147
148
149
else if ($institution) {
    $editview['elements']['institution'] = array(
        'type'  => 'hidden',
        'value' => $institution
    );
}
150
151
152
153
154
155
156
157
158
159
else {
    $editview['elements']['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),
    );
}
160
161

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

163
function editview_cancel_submit() {
164
165
166
167
168
169
170
171
172
173
    global $view, $new, $group, $institution;
    if (isset($view) && $new) {
        $view->delete();
    }
    if ($group) {
        redirect('/view/groupviews.php?group='.$group);
    }
    if ($institution) {
        redirect('/view/institutionviews.php?institution='.$institution);
    }
174
175
    redirect('/view');
}
Martyn Smith's avatar
Martyn Smith committed
176

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

179
    global $USER, $SESSION, $group, $institution;
Martyn Smith's avatar
Martyn Smith committed
180

181
182
    $editing = !empty($values['id']);
    $view = new View($values['id'], $values);
Martyn Smith's avatar
Martyn Smith committed
183

184
185
    if (empty($editing)) {
        $view->set('numcolumns', 3); // default
186
187
188
        if ($group) {
            $view->set('group', $group);
        }
189
190
191
        else if ($institution) {
            $view->set('institution', $institution);
        }
192
193
194
        else {
            $view->set('owner', $USER->get('id'));
        }
Martyn Smith's avatar
Martyn Smith committed
195
    }
196
197
    else {
        $view->set('dirty', true);
Martyn Smith's avatar
Martyn Smith committed
198
199
    }

200
    $view->commit();
201
202
203
    if (empty($editing) && $group) {
        // By default, group views should be visible to the group
        $view->set_access(array(array(
204
205
            'type'      => 'group',
            'id'        => $group,
206
            'startdate' => null,
207
208
            'stopdate'  => null,
            'role'      => null
209
210
211
        )));
        $view->commit();
    }
Martyn Smith's avatar
Martyn Smith committed
212

213
    if ($values['new']) {
214
215
216
217
        $redirecturl = '/view/blocks.php?id=' . $view->get('id') . '&new=1';
    } 
    else {
        $SESSION->add_ok_msg(get_string('viewsavedsuccessfully', 'view'));
218
219
220
        if ($group) {
            $redirecturl = '/view/groupviews.php?group='.$group;
        }
221
222
223
        else if ($institution) {
            $redirecturl = '/view/institutionviews.php?institution=' . $institution;
        }
224
225
226
        else {
            $redirecturl = '/view/index.php';
        }
227
    }
Martyn Smith's avatar
Martyn Smith committed
228

229
    redirect($redirecturl);
Martyn Smith's avatar
Martyn Smith committed
230
231
232

}

233
234
235
236
$smarty = smarty();
$smarty->assign('heading', $heading);
$smarty->assign('editview', $editview);
$smarty->display('view/edit.tpl');
Martyn Smith's avatar
Martyn Smith committed
237
238

?>