edit.php 8.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
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
define('MENUITEM', 'groups/groupsiown');
30
require(dirname(dirname(__FILE__)) . '/init.php');
Martyn Smith's avatar
Martyn Smith committed
31
require_once('pieforms/pieform.php');
32
require_once('group.php');
33
define('TITLE', get_string('editgroup', 'group'));
Martyn Smith's avatar
Martyn Smith committed
34
35

$id = param_integer('id');
36
define('GROUP', $id);
Martyn Smith's avatar
Martyn Smith committed
37

38
$group_data = get_record_sql("SELECT g.*
39
40
41
42
    FROM {group} g
    INNER JOIN {group_member} gm ON (gm.group = g.id AND gm.member = ? AND gm.role = 'admin')
    WHERE g.id = ?
    AND g.deleted = 0", array($USER->get('id'), $id));
Martyn Smith's avatar
Martyn Smith committed
43

44
if (!$group_data) {
Martyn Smith's avatar
Martyn Smith committed
45
    $SESSION->add_error_msg(get_string('canteditdontown'));
46
    redirect('/group/mygroups.php');
Martyn Smith's avatar
Martyn Smith committed
47
}
48
49
$elements = array();
$elements['name'] = array(
Martyn Smith's avatar
Martyn Smith committed
50
            'type'         => 'text',
51
            'title'        => get_string('groupname', 'group'),
Penny Leach's avatar
Penny Leach committed
52
            'rules'        => array( 'required' => true, 'maxlength' => 128 ),
53
54
            'defaultvalue' => $group_data->name);
$elements['description'] = array(
Martyn Smith's avatar
Martyn Smith committed
55
            'type'         => 'wysiwyg',
56
            'title'        => get_string('groupdescription', 'group'),
Martyn Smith's avatar
Martyn Smith committed
57
            'rows'         => 10,
58
            'cols'         => 55,
59
            'defaultvalue' => $group_data->description);
60
61
62
63
64
65
66
67
68
69

$grouptypeoptions = group_get_grouptype_options($group_data->grouptype);
$currenttype = $group_data->grouptype . '.' . $group_data->jointype;
if (!isset($grouptypeoptions[$currenttype])) {
    // The user can't create groups of this type.  Probably a non-staff user
    // who's been promoted to admin of a controlled group.  Just don't let
    // them change it.
    $grouptypeoptions = array($currenttype => get_string('membershiptype.' . $group_data->jointype, 'group'));
}

70
$elements['grouptype'] = array(
71
72
73
74
75
76
    'type'         => 'select',
    'title'        => get_string('grouptype', 'group'),
    'options'      => $grouptypeoptions,
    'defaultvalue' => $currenttype,
    'help'         => true
);
77
78
79
if (get_config('allowgroupcategories')
    && $groupcategories = get_records_menu('group_category','','','displayorder', 'id,title')
) {
80
    $elements['category'] = array(
81
82
                'type'         => 'select',
                'title'        => get_string('groupcategory', 'group'),
83
                'options'      => array('0'=>get_string('nocategoryselected', 'group')) + $groupcategories,
84
                'defaultvalue' => $group_data->category);
85
86
}
$elements['public'] = array(
87
88
89
90
91
92
93
            'type'         => 'select',
            'title'        => get_string('publiclyviewablegroup', 'group'),
            'description'  => get_string('publiclyviewablegroupdescription', 'group'),
            'options'      => array(true  => get_string('yes'),
                                    false => get_string('no')),
            'defaultvalue' => $group_data->public,
            'help'         => true,
94
95
            'ignore'       => !(get_config('createpublicgroups') == 'all' || get_config('createpublicgroups') == 'admins' && $USER->get('admin')));
$elements['usersautoadded'] = array(
96
97
98
99
100
            'type'         => 'select',
            'title'        => get_string('usersautoadded', 'group'),
            'description'  => get_string('usersautoaddeddescription', 'group'),
            'options'      => array(true  => get_string('yes'),
                                    false => get_string('no')),
101
            'defaultvalue' => $group_data->usersautoadded,
102
            'help'         => true,
103
            'ignore'       => !$USER->get('admin'));
104
105
106
107
108
109
$elements['viewnotify'] = array(
    'type' => 'checkbox',
    'title' => get_string('viewnotify', 'group'),
    'description' => get_string('viewnotifydescription', 'group'),
    'defaultvalue' => $group_data->viewnotify
);
110
$elements['id'] = array(
Martyn Smith's avatar
Martyn Smith committed
111
            'type'         => 'hidden',
112
113
            'value'        => $id);
$elements['submit'] = array(
Martyn Smith's avatar
Martyn Smith committed
114
            'type'  => 'submitcancel',
115
116
117
118
119
120
121
122
            'value' => array(get_string('savegroup', 'group'), get_string('cancel')));

$editgroup = pieform(array(
    'name'     => 'editgroup',
    'method'   => 'post',
    'plugintype' => 'core',
    'pluginname' => 'groups',
    'elements' => $elements));
Martyn Smith's avatar
Martyn Smith committed
123

124
function editgroup_validate(Pieform $form, $values) {
125
    $cid = get_field('group', 'id', 'name', $values['name']);
Martyn Smith's avatar
Martyn Smith committed
126
127

    if ($cid && $cid != $values['id']) {
128
        $form->set_error('name', get_string('groupalreadyexists', 'group'));
Martyn Smith's avatar
Martyn Smith committed
129
130
131
    }
}

132
function editgroup_cancel_submit() {
133
    redirect('/group/mygroups.php');
Martyn Smith's avatar
Martyn Smith committed
134
135
}

136
function editgroup_submit(Pieform $form, $values) {
137
    global $USER, $SESSION, $group_data;
Martyn Smith's avatar
Martyn Smith committed
138
139
140
141
142

    db_begin();

    $now = db_format_timestamp(time());

143
    list($grouptype, $jointype) = explode('.', $values['grouptype']);
144
    $values['public'] = (isset($values['public'])) ? $values['public'] : 0;
145
    $values['usersautoadded'] = (isset($values['usersautoadded'])) ? $values['usersautoadded'] : 0;
146

Martyn Smith's avatar
Martyn Smith committed
147
    update_record(
148
        'group',
Martyn Smith's avatar
Martyn Smith committed
149
150
151
152
        (object) array(
            'id'             => $values['id'],
            'name'           => $values['name'],
            'description'    => $values['description'],
153
            'grouptype'      => $grouptype,
154
            'category'       => empty($values['category']) ? null : intval($values['category']),
155
            'jointype'       => $jointype,
Martyn Smith's avatar
Martyn Smith committed
156
            'mtime'          => $now,
157
            'usersautoadded' => intval($values['usersautoadded']),
158
            'public'         => intval($values['public']),
159
            'viewnotify'     => intval($values['viewnotify']),
Martyn Smith's avatar
Martyn Smith committed
160
161
162
163
        ),
        'id'
    );

164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    // When jointype changes from invite/request to anything else,
    // remove all open invitations/requests, ---
    // Except for when jointype changes from request to open. Then
    // we can just add group membership for everyone with an open
    // request.

    if ($group_data->jointype == 'invite' && $jointype != 'invite') {
        delete_records('group_member_invite', 'group', $group_data->id);
    }
    else if ($group_data->jointype == 'request') {
        if ($jointype == 'open') {
            $userids = get_column_sql('
                SELECT u.id
                FROM {usr} u JOIN {group_member_request} r ON u.id = r.member
                WHERE r.group = ? AND u.deleted = 0',
                array($group_data->id)
            );
            if ($userids) {
                foreach ($userids as $uid) {
                    group_add_user($group_data->id, $uid);
                }
            }
        }
        else if ($jointype != 'request') {
            delete_records('group_member_request', 'group', $group_data->id);
        }
    }
191
192
193
194
195
    // When group type changes from course to standard, make sure that tutors
    // are demoted to members.
    if ($group_data->grouptype == 'course' && $grouptype != 'course') {
        set_field('group_member', 'role', 'member', 'group', $values['id'], 'role', 'tutor');
    }
196

197
198
199
200
201
202
203
204
205
206
207
208
209
210
    // When a group changes from public -> private or vice versa, set the
    // appropriate access permissions on the group homepage view.
    if ($group_data->public != $values['public']) {
        $homepageid = get_field('view', 'id', 'type', 'grouphomepage', 'group', $group_data->id);
        if ($group_data->public && !$values['public']) {
            delete_records('view_access', 'view', $homepageid, 'accesstype', 'public');
            insert_record('view_access', (object) array('view' => $homepageid, 'accesstype' => 'loggedin'));
        }
        else if (!$group_data->public && $values['public']) {
            delete_records('view_access', 'view', $homepageid, 'accesstype', 'loggedin');
            insert_record('view_access', (object) array('view' => $homepageid, 'accesstype' => 'public'));
        }
    }

211
    $SESSION->add_ok_msg(get_string('groupsaved', 'group'));
Martyn Smith's avatar
Martyn Smith committed
212
213
214

    db_commit();

215
    redirect('/group/view.php?id=' . $values['id']);
Martyn Smith's avatar
Martyn Smith committed
216
217
218
}

$smarty = smarty();
219
$smarty->assign('editgroup', $editgroup);
220
$smarty->display('group/edit.tpl');
Martyn Smith's avatar
Martyn Smith committed
221
222

?>