manage.php 4.64 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage admin
 * @author     Catalyst IT Ltd
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
9
10
11
12
13
14
 *
 */

define('INTERNAL', 1);
define('ADMIN', 1);
define('MENUITEM', 'managegroups/groups');
15
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
16
17
18

require_once('pieforms/pieform.php');

19
$group = get_record_select('group', 'id = ? AND deleted = 0', array(param_integer('id')));
20

Hugh Davenport's avatar
Hugh Davenport committed
21
22
23
24
define('TITLE', get_string('administergroups', 'admin'));

$quotasform = pieform(array(
    'name'       => 'groupquotasform',
25
26
    'class'      => 'form-inline',
    'renderer'   => 'div',
Hugh Davenport's avatar
Hugh Davenport committed
27
28
29
30
31
32
33
    'elements'   => array(
        'groupid' => array(
            'type' => 'hidden',
            'value' => $group->id,
        ),
        'quota'  => array(
            'type' => 'bytes',
34
            'title' => get_string('filequota1', 'admin'),
Hugh Davenport's avatar
Hugh Davenport committed
35
36
37
            'defaultvalue' => $group->quota,
        ),
        'submit' => array(
38
39
            'type' => 'button',
            'usebuttontag' => true,
40
            'class' => 'btn-primary ',
Hugh Davenport's avatar
Hugh Davenport committed
41
            'value' => get_string('save'),
42
43
44
45
46
47
        ),
        'description' => array(
            'type' => 'html',
            'class' => 'text-block mtm',
            'value' => '<p class="metadata">' .get_string('groupfilequotadescription', 'admin') . '</p>'
        ),
Hugh Davenport's avatar
Hugh Davenport committed
48
49
50
51
52
53
    ),
));

function groupquotasform_submit(Pieform $form, $values) {
    global $SESSION;

54
    $oldquota = get_field('group', 'quota', 'id', $values['groupid']);
Hugh Davenport's avatar
Hugh Davenport committed
55
56
57
58
59
    $group = new StdClass;
    $group->id = $values['groupid'];
    $group->quota = $values['quota'];
    update_record('group', $group);

60
61
62
63
64
65
66
67
68
69
70
71
72
    if (!empty($values['quota']) && $values['quota'] != $oldquota) {
        // We need to alert group admins that the group may now be over the threshold
        $quotanotifylimit = get_config_plugin('artefact', 'file', 'quotanotifylimit');
        $sqlwhere = " ((g.quotaused / g.quota) * 100) ";
        if (is_postgres()) {
            $sqlwhere = " ((CAST(g.quotaused AS float) / CAST(g.quota AS float)) * 100) ";
        }
        if ($groups = get_records_sql_assoc("SELECT g.id, g.name, g.quota, " . $sqlwhere . " AS quotausedpercent FROM {group} g WHERE " . $sqlwhere . " >= ? AND id = ?", array($quotanotifylimit, $values['groupid']))) {
            require_once(get_config('docroot') . 'artefact/file/lib.php');
            ArtefactTypeFile::notify_groups_threshold_exceeded($groups);
        }
    }

Hugh Davenport's avatar
Hugh Davenport committed
73
74
75
76
    $SESSION->add_ok_msg(get_string('groupquotaupdated', 'admin'));
    redirect(get_config('wwwroot').'admin/groups/groups.php');
}

77

78
79
$admins = get_column_sql(
    "SELECT gm.member FROM {group_member} gm WHERE gm.role = 'admin' AND gm.group = ?", array($group->id)
80
81
);

Hugh Davenport's avatar
Hugh Davenport committed
82
$groupadminsform = pieform(array(
83
    'name'       => 'groupadminsform',
84
    'renderer'   => 'div',
85
86
87
88
89
    'plugintype' => 'core',
    'pluginname' => 'admin',
    'elements'   => array(
        'admins' => array(
            'type' => 'userlist',
90
91
92
93
            'title' => get_string('groupadmins', 'group'),
            'defaultvalue' => $admins,
            'lefttitle' => get_string('potentialadmins', 'admin'),
            'righttitle' => get_string('currentadmins', 'admin'),
94
95
96
        ),
        'submit' => array(
            'type' => 'submit',
97
            'class' => 'btn-success',
98
            'value' => get_string('save'),
99
100
        ),
    ),
101
));
102
103

function groupadminsform_submit(Pieform $form, $values) {
104
    global $SESSION, $group, $admins;
105

106
107
108
109
110
111
112
113
114
115
116
117
118
    $newadmins = array_diff($values['admins'], $admins);
    $demoted = array_diff($admins, $values['admins']);

    db_begin();
    if ($demoted) {
        $demoted = join(',', array_map('intval', $demoted));
        execute_sql("
            UPDATE {group_member}
            SET role = 'member'
            WHERE role = 'admin' AND \"group\" = ?
                AND member IN ($demoted)",
            array($group->id)
        );
119
    }
120
121
    $dbnow = db_format_timestamp(time());
    foreach ($newadmins as $id) {
122
123
        if (group_user_access($group->id, $id)) {
            group_change_role($group->id, $id, 'admin');
124
125
        }
        else {
126
            group_add_user($group->id, $id, 'admin');
127
128
129
130
        }
    }
    db_commit();

131
    $SESSION->add_ok_msg(get_string('groupadminsupdated', 'admin'));
132
133
134
135
    redirect(get_config('wwwroot').'admin/groups/groups.php');
}

$smarty = smarty();
136
137
setpageicon($smarty, 'icon-users');

138
$smarty->assign('PAGEHEADING', TITLE);
Hugh Davenport's avatar
Hugh Davenport committed
139
140
141
$smarty->assign('quotasform', $quotasform);
$smarty->assign('groupname', $group->name);
$smarty->assign('managegroupform', $groupadminsform);
142
$smarty->display('admin/groups/manage.tpl');