edit.php 5.65 KB
Newer Older
Martyn Smith's avatar
Martyn Smith committed
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
4
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
 * Copyright (C) 2006-2007 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     Martyn Smith <martyn@catalyst.net.nz>
Martyn Smith's avatar
Martyn Smith committed
22
23
24
25
26
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

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');
Martyn Smith's avatar
Martyn Smith committed
36

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

40
if (empty($id)) {
41
    define('TITLE', get_string('createview', 'view'));
42
    $new = true;
43
44
}
else {
45
    $view = new View($id);
46
47
48
    if ($view->get('owner') != $USER->get('id')) {
        throw new AccessDeniedException(get_string('canteditdontown', 'view'));
    }
49
    define('TITLE', get_string('editview', 'view', $view->get('title')));
50
}
Martyn Smith's avatar
Martyn Smith committed
51

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

54
require_once('pieforms/pieform.php');
Martyn Smith's avatar
Martyn Smith committed
55

56
57
58
59
60
$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
61
62
);

63
64
65
66
67
68
69
$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
70
}
71
72
73
74
75
76
77
78
79
$ownerformatoptions[FORMAT_NAME_DISPLAYNAME] = sprintf($formatstring, get_string('displayname'), display_name($USER));

$editview = pieform(array(
    'name'     => 'editview',
    'method'   => 'post',
    'autofocus' => 'title',
    'plugintype' => 'core',
    'pluginname' => 'view',
    'elements' => array(
80
        'id' => array(
81
            'type'  => 'hidden',
82
            'value' => $id,
83
        ),
84
85
86
87
        'new' => array(
            'type' => 'hidden',
            'value' => $new,
        ),
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
        'title' => array(
            'type'         => 'text',
            'title'        => get_string('title','view'),
            'defaultvalue' => isset($view) ? $view->get('title') : null,
            'rules'        => array( 'required' => true ),
            'help'         => true,
        ),
        'description' => array(
            'type'         => 'wysiwyg',
            'title'        => get_string('description','view'),
            'rows'         => 10,
            'cols'         => 70,
            'defaultvalue' => isset($view) ? $view->get('description') : null,
            'help'         => true,
        ),
        'tags'        => array(
            'type'         => 'tags',
            'title'        => get_string('tags'),
106
            'description'  => get_string('tagsdescprofile'),
107
108
109
110
111
112
113
114
115
116
117
118
119
120
            'defaultvalue' => isset($view) ? $view->get('tags') : null,
            'help'        => true,
        ),
        '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),
            'help'         => true,
        ),
        'submit'   => array(
            'type'  => 'submitcancel',
121
            'value' => array(empty($new) ? get_string('save') : get_string('next'), get_string('cancel')),
122
123
            'confirm' => $new && isset($view) ? array(null, get_string('confirmcancelcreatingview', 'view')) : null,
        )
124
125
    ),
));
Martyn Smith's avatar
Martyn Smith committed
126

127
function editview_cancel_submit() {
128
129
130
131
	global $view, $new;
	if (isset($view) && $new) {
	    $view->delete();
	}
132
133
    redirect('/view');
}
Martyn Smith's avatar
Martyn Smith committed
134

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

137
    global $USER, $SESSION;
Martyn Smith's avatar
Martyn Smith committed
138

139
140
    $editing = !empty($values['id']);
    $view = new View($values['id'], $values);
Martyn Smith's avatar
Martyn Smith committed
141
142


143
144
145
    if (empty($editing)) {
        $view->set('numcolumns', 3); // default
        $view->set('owner', $USER->get('id'));
Martyn Smith's avatar
Martyn Smith committed
146
    }
147
148
    else {
        $view->set('dirty', true);
Martyn Smith's avatar
Martyn Smith committed
149
150
    }

151
    $view->commit();
Martyn Smith's avatar
Martyn Smith committed
152

153
    if ($values['new']) {
154
155
156
157
158
159
        $redirecturl = '/view/blocks.php?id=' . $view->get('id') . '&new=1';
    } 
    else {
        $redirecturl = '/view/index.php';
        $SESSION->add_ok_msg(get_string('viewsavedsuccessfully', 'view'));
    }
Martyn Smith's avatar
Martyn Smith committed
160

161
    redirect($redirecturl);
Martyn Smith's avatar
Martyn Smith committed
162
163
164

}

165
166
167
168
$smarty = smarty();
$smarty->assign('heading', $heading);
$smarty->assign('editview', $editview);
$smarty->display('view/edit.tpl');
Martyn Smith's avatar
Martyn Smith committed
169
170

?>