Completed
Push — master ( 562606...354044 )
by Julito
09:19
created

sort_users()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 39
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 31
c 1
b 0
f 0
nc 9
nop 2
dl 0
loc 39
rs 8.0555
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * This script displays an area where teachers can edit the group properties and member list.
6
 * Groups are also often called "teams" in the Dokeos code.
7
 *
8
 * @author various contributors
9
 * @author Roan Embrechts (VUB), partial code cleanup, initial virtual course support
10
 *
11
 * @package chamilo.group
12
 *
13
 * @todo course admin functionality to create groups based on who is in which course (or class).
14
 */
15
require_once __DIR__.'/../inc/global.inc.php';
16
$this_section = SECTION_COURSES;
17
$current_course_tool = TOOL_GROUP;
18
19
// Notice for unauthorized people.
20
api_protect_course_script(true);
21
22
$group_id = api_get_group_id();
23
$current_group = GroupManager::get_group_properties($group_id);
24
25
$nameTools = get_lang('EditGroup');
26
$interbreadcrumb[] = ['url' => 'group.php?'.api_get_cidreq(), 'name' => get_lang('Groups')];
27
$interbreadcrumb[] = ['url' => 'group_space.php?'.api_get_cidreq(), 'name' => $current_group['name']];
28
29
$is_group_member = GroupManager::is_tutor_of_group(api_get_user_id(), $current_group);
30
31
if (!api_is_allowed_to_edit(false, true) && !$is_group_member) {
32
    api_not_allowed(true);
33
}
34
35
/**
36
 * Function to sort users after getting the list in the DB.
37
 * Necessary because there are 2 or 3 queries. Called by usort().
38
 */
39
function sort_users($user_a, $user_b)
40
{
41
    $orderListByOfficialCode = api_get_setting('order_user_list_by_official_code');
42
    if ($orderListByOfficialCode === 'true') {
43
        $cmp = api_strcmp($user_a['official_code'], $user_b['official_code']);
44
        if ($cmp !== 0) {
45
            return $cmp;
46
        } else {
47
            $cmp = api_strcmp($user_a['lastname'], $user_b['lastname']);
48
            if ($cmp !== 0) {
49
                return $cmp;
50
            } else {
51
                return api_strcmp($user_a['username'], $user_b['username']);
52
            }
53
        }
54
    }
55
56
    if (api_sort_by_first_name()) {
57
        $cmp = api_strcmp($user_a['firstname'], $user_b['firstname']);
58
        if ($cmp !== 0) {
59
            return $cmp;
60
        } else {
61
            $cmp = api_strcmp($user_a['lastname'], $user_b['lastname']);
62
            if ($cmp !== 0) {
63
                return $cmp;
64
            } else {
65
                return api_strcmp($user_a['username'], $user_b['username']);
66
            }
67
        }
68
    } else {
69
        $cmp = api_strcmp($user_a['lastname'], $user_b['lastname']);
70
        if ($cmp !== 0) {
71
            return $cmp;
72
        } else {
73
            $cmp = api_strcmp($user_a['firstname'], $user_b['firstname']);
74
            if ($cmp !== 0) {
75
                return $cmp;
76
            } else {
77
                return api_strcmp($user_a['username'], $user_b['username']);
78
            }
79
        }
80
    }
81
}
82
83
$htmlHeadXtra[] = '<script>
84
$(function() {
85
    $("#max_member").on("focus", function() {
86
        $("#max_member_selected").attr("checked", true);
87
    });
88
});
89
 </script>';
90
91
// Build form
92
$form = new FormValidator('group_edit', 'post', api_get_self().'?'.api_get_cidreq());
93
$form->addElement('hidden', 'action');
94
95
// Group tutors
96
$group_tutor_list = GroupManager::get_subscribed_tutors($current_group);
97
98
$selected_tutors = [];
99
foreach ($group_tutor_list as $index => $user) {
100
    $selected_tutors[] = $user['user_id'];
101
}
102
103
$complete_user_list = CourseManager::get_user_list_from_course_code(
104
    api_get_course_id(),
105
    api_get_session_id()
106
);
107
108
$possible_users = [];
109
$userGroup = new UserGroup();
110
111
$subscribedUsers = GroupManager::get_subscribed_users($current_group);
112
if ($subscribedUsers) {
113
    $subscribedUsers = array_column($subscribedUsers, 'user_id');
114
}
115
116
$orderUserListByOfficialCode = api_get_setting('order_user_list_by_official_code');
117
if (!empty($complete_user_list)) {
118
    usort($complete_user_list, 'sort_users');
119
    foreach ($complete_user_list as $index => $user) {
120
        if (in_array($user['user_id'], $subscribedUsers)) {
121
            continue;
122
        }
123
124
        //prevent invitee users add to groups or tutors - see #8091
125
        if ($user['status'] != INVITEE) {
126
            $officialCode = !empty($user['official_code']) ? ' - '.$user['official_code'] : null;
127
128
            $groups = $userGroup->getUserGroupListByUser($user['user_id']);
129
            $groupNameListToString = '';
130
            if (!empty($groups)) {
131
                $groupNameList = array_column($groups, 'name');
132
                $groupNameListToString = ' - ['.implode(', ', $groupNameList).']';
133
            }
134
135
            $name = api_get_person_name(
136
                $user['firstname'],
137
                $user['lastname']
138
            ).' ('.$user['username'].')'.$officialCode;
139
140
            if ($orderUserListByOfficialCode === 'true') {
141
                $officialCode = !empty($user['official_code']) ? $user['official_code']." - " : '? - ';
142
                $name = $officialCode.' '.api_get_person_name(
143
                    $user['firstname'],
144
                    $user['lastname']
145
                ).' ('.$user['username'].')';
146
            }
147
148
            $possible_users[$user['user_id']] = $name.$groupNameListToString;
149
        }
150
    }
151
}
152
153
$group_tutors_element = $form->addElement(
154
    'advmultiselect',
155
    'group_tutors',
156
    get_lang('GroupTutors'),
157
    $possible_users,
158
    'style="width: 280px;"'
159
);
160
161
// submit button
162
$form->addButtonSave(get_lang('SaveSettings'));
163
164
if ($form->validate()) {
165
    $values = $form->exportValues();
166
167
    // Storing the tutors (we first remove all the tutors and then add only those who were selected)
168
    GroupManager :: unsubscribe_all_tutors($current_group['iid']);
169
    if (isset($_POST['group_tutors']) && count($_POST['group_tutors']) > 0) {
170
        GroupManager::subscribe_tutors($values['group_tutors'], $current_group);
171
    }
172
173
    // Returning to the group area (note: this is inconsistent with the rest of chamilo)
174
    $cat = GroupManager::get_category_from_group($current_group['iid']);
175
176
    if (isset($_POST['group_members']) &&
177
        count($_POST['group_members']) > $max_member &&
178
        $max_member != GroupManager::MEMBER_PER_GROUP_NO_LIMIT
179
    ) {
180
        Display::addFlash(Display::return_message(get_lang('GroupTooMuchMembers'), 'warning'));
181
        header('Location: group.php?'.api_get_cidreq(true, false));
182
    } else {
183
        Display::addFlash(Display::return_message(get_lang('GroupSettingsModified'), 'success'));
184
        header('Location: group.php?'.api_get_cidreq(true, false).'&category='.$cat['id']);
185
    }
186
    exit;
187
}
188
189
$defaults = $current_group;
190
$defaults['group_tutors'] = $selected_tutors;
191
$action = isset($_GET['action']) ? $_GET['action'] : '';
192
$defaults['action'] = $action;
193
194
if (!empty($_GET['keyword']) && !empty($_GET['submit'])) {
195
    $keyword_name = Security::remove_XSS($_GET['keyword']);
196
    echo '<br/>'.get_lang('SearchResultsFor').' <span style="font-style: italic ;"> '.$keyword_name.' </span><br>';
197
}
198
199
Display :: display_header($nameTools, 'Group');
200
$form->setDefaults($defaults);
201
echo GroupManager::getSettingBar('tutor');
202
$form->display();
203
204
Display :: display_footer();
205