edit.php 6.93 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
5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
Martyn Smith's avatar
Martyn Smith committed
6
 *
Francois Marier's avatar
Francois Marier committed
7
8
9
10
 * 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
11
 *
Francois Marier's avatar
Francois Marier committed
12
13
14
15
 * 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
16
 *
Francois Marier's avatar
Francois Marier committed
17
18
 * 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
19
20
21
 *
 * @package    mahara
 * @subpackage core
22
 * @author     Catalyst IT Ltd
Martyn Smith's avatar
Martyn Smith committed
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
Martyn Smith's avatar
Martyn Smith committed
25
26
27
28
 *
 */

define('INTERNAL', 1);
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
36
require_once(get_config('libroot') . 'view.php');
require_once(get_config('libroot') . 'group.php');
Martyn Smith's avatar
Martyn Smith committed
37

Richard Mansfield's avatar
Richard Mansfield committed
38
$view = new View(param_integer('id'));
39

Richard Mansfield's avatar
Richard Mansfield committed
40
41
if (!$USER->can_edit_view($view)) {
    throw new AccessDeniedException();
42
}
43

44
45
46
47
// If the view has been submitted, disallow editing
if ($view->is_submitted()) {
    $submittedto = $view->submitted_to();
    throw new AccessDeniedException(get_string('canteditsubmitted', 'view', $submittedto['name']));
48
49
}

Richard Mansfield's avatar
Richard Mansfield committed
50
51
$group = $view->get('group');
$institution = $view->get('institution');
52
View::set_nav($group, $institution);
53

Richard Mansfield's avatar
Richard Mansfield committed
54
55
56
$new = param_boolean('new', 0);

if ($new) {
57
    define('TITLE', get_string('edittitleanddescription', 'view'));
58
59
}
else {
60
    define('TITLE', $view->get('title') . ': ' . get_string('edittitleanddescription', 'view'));
61
}
Martyn Smith's avatar
Martyn Smith committed
62

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

65
require_once('pieforms/pieform.php');
Martyn Smith's avatar
Martyn Smith committed
66

67
68
69
70
71
$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
72
73
);

74
75
76
$displayname = display_name($USER);
if ($displayname !== '') {
    $ownerformatoptions[FORMAT_NAME_DISPLAYNAME] = sprintf($formatstring, get_string('preferredname'), $displayname);
77
78
79
80
}
$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
81
}
82

83
$editview = array(
84
85
86
    'name'     => 'editview',
    'method'   => 'post',
    'autofocus' => 'title',
87
    'autoselect' => $new ? 'title' : null,
88
89
90
    'plugintype' => 'core',
    'pluginname' => 'view',
    'elements' => array(
91
        'id' => array(
92
            'type'  => 'hidden',
Richard Mansfield's avatar
Richard Mansfield committed
93
            'value' => $view->get('id'),
94
        ),
95
96
97
98
        'new' => array(
            'type' => 'hidden',
            'value' => $new,
        ),
99
100
101
        'title' => array(
            'type'         => 'text',
            'title'        => get_string('title','view'),
Richard Mansfield's avatar
Richard Mansfield committed
102
            'defaultvalue' => $view->get('title'),
103
104
105
106
107
108
109
            'rules'        => array( 'required' => true ),
        ),
        'description' => array(
            'type'         => 'wysiwyg',
            'title'        => get_string('description','view'),
            'rows'         => 10,
            'cols'         => 70,
Richard Mansfield's avatar
Richard Mansfield committed
110
            'defaultvalue' => $view->get('description'),
111
112
113
114
        ),
        'tags'        => array(
            'type'         => 'tags',
            'title'        => get_string('tags'),
115
            'description'  => get_string('tagsdescprofile'),
Richard Mansfield's avatar
Richard Mansfield committed
116
            'defaultvalue' => $view->get('tags'),
117
            'help'         => true,
118
119
        ),
    ),
120
121
);

Richard Mansfield's avatar
Richard Mansfield committed
122
123
124
125
126
if (!($group || $institution)) {
    $default = $view->get('ownerformat');
    if (!$default) {
        $default = FORMAT_NAME_DISPLAYNAME;
    }
127
128
129
130
131
    $editview['elements']['ownerformat'] = array(
        'type'         => 'select',
        'title'        => get_string('ownerformat','view'),
        'description'  => get_string('ownerformatdescription','view'),
        'options'      => $ownerformatoptions,
Richard Mansfield's avatar
Richard Mansfield committed
132
        'defaultvalue' => $default,
133
134
135
        'rules'        => array('required' => true),
    );
}
136

137
138
$editview['elements']['allowcomments'] = array(
    'type'         => 'checkbox',
139
140
    'title'        => get_string('allowcomments','artefact.comment'),
    'description'  => get_string('allowcommentsonview','view'),
141
142
143
    'defaultvalue' => $view->get('allowcomments'),
);

Richard Mansfield's avatar
Richard Mansfield committed
144
145
146
if ($new) {
    $editview['elements']['submit'] = array(
        'type'  => 'cancelbackcreate',
147
        'value' => array(get_string('cancel'), get_string('back','view'), get_string('next') . ': ' . get_string('editaccess', 'view')),
Richard Mansfield's avatar
Richard Mansfield committed
148
149
150
151
152
153
154
        'confirm' => array(get_string('confirmcancelcreatingview', 'view'), null, null),
    );
}
else {
    $editview['elements']['submit'] = array(
        'type'  => 'submit',
        'value' => get_string('done'),
155
156
157
    );
}

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

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

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

176
    global $new, $view, $SESSION;
Martyn Smith's avatar
Martyn Smith committed
177

Richard Mansfield's avatar
Richard Mansfield committed
178
179
    if (param_boolean('back')) {
        redirect('/view/blocks.php?id=' . $view->get('id') . '&new=' . $new);
Martyn Smith's avatar
Martyn Smith committed
180
    }
Richard Mansfield's avatar
Richard Mansfield committed
181
182
183
184

    $view->set('title', $values['title']);
    $view->set('description', $values['description']);
    $view->set('tags', $values['tags']);
185
    $view->set('allowcomments', (int) $values['allowcomments']);
Richard Mansfield's avatar
Richard Mansfield committed
186
187
    if (isset($values['ownerformat']) && $view->get('owner')) {
        $view->set('ownerformat', $values['ownerformat']);
Martyn Smith's avatar
Martyn Smith committed
188
189
    }

190
    $view->commit();
Richard Mansfield's avatar
Richard Mansfield committed
191

192
    if ($values['new']) {
Richard Mansfield's avatar
Richard Mansfield committed
193
        $redirecturl = '/view/access.php?id=' . $view->get('id') . '&new=1';
194
195
196
    } 
    else {
        $SESSION->add_ok_msg(get_string('viewsavedsuccessfully', 'view'));
Richard Mansfield's avatar
Richard Mansfield committed
197
198
        if ($view->get('group')) {
            $redirecturl = '/view/groupviews.php?group='.$view->get('group');
199
        }
Richard Mansfield's avatar
Richard Mansfield committed
200
201
        else if ($view->get('institution')) {
            $redirecturl = '/view/institutionviews.php?institution=' . $view->get('institution');
202
        }
203
204
205
        else {
            $redirecturl = '/view/index.php';
        }
206
    }
Martyn Smith's avatar
Martyn Smith committed
207

208
    redirect($redirecturl);
Martyn Smith's avatar
Martyn Smith committed
209
210
211

}

212
213
$smarty = smarty(array(), array(), array(), array('sidebars' => false));
$smarty->assign('PAGEHEADING', hsc($heading));
214
215
$smarty->assign('editview', $editview);
$smarty->display('view/edit.tpl');
Martyn Smith's avatar
Martyn Smith committed
216
217

?>