Completed
Push — 1.11.x ( 70c96c...4a31a7 )
by Angel Fernando Quiroz
01:22 queued 43s
created

get_user_list()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 41
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 41
rs 8.5226
cc 7
nc 12
nop 4
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
$cidReset = true;
6
7
require_once __DIR__.'/../inc/global.inc.php';
8
9
$id_session = isset($_GET['id_session']) ? (int) $_GET['id_session'] : 0;
10
11
SessionManager::protectSession($id_session);
12
13
if (empty($id_session)) {
14
    api_not_allowed();
15
}
16
17
$action = $_REQUEST['action'] ?? null;
18
$idChecked = isset($_REQUEST['idChecked']) && is_array($_REQUEST['idChecked']) ? $_REQUEST['idChecked'] : [];
19
20
$course_code = Database::escape_string(trim($_GET['course_code']));
21
$courseInfo = api_get_course_info($course_code);
22
$courseId = $courseInfo['real_id'];
23
$apiIsWesternNameOrder = api_is_western_name_order();
24
25
$check = Security::check_token('get');
26
27
if ($check) {
28
    switch ($action) {
29
        case 'delete':
30
            foreach ($idChecked as $userId) {
31
                SessionManager::unSubscribeUserFromCourseSession((int) $userId, $courseId, $id_session);
32
            }
33
            header(
34
                'Location: '.api_get_self().'?'
35
                .http_build_query(['id_session' => $id_session, 'course_code' => $course_code])
36
            );
37
            exit;
38
        case 'add':
39
            SessionManager::subscribe_users_to_session_course($idChecked, $id_session, $course_code);
40
            header(
41
                'Location: '.api_get_self().'?'
42
                .http_build_query(['id_session' => $id_session, 'course_code' => $course_code])
43
            );
44
            exit;
45
    }
46
    Security::clear_token();
47
}
48
49
$tblSessionRelUser = Database::get_main_table(TABLE_MAIN_SESSION_USER);
50
$tblUser = Database::get_main_table(TABLE_MAIN_USER);
51
$tblCourse = Database::get_main_table(TABLE_MAIN_COURSE);
52
$tblSession = Database::get_main_table(TABLE_MAIN_SESSION);
53
$urlTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
54
$tblSessionRelCourseRelUser = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
55
$tblSessionRelCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
56
57
$sql = "SELECT s.name, c.title
58
        FROM $tblSessionRelCourse src
59
		INNER JOIN $tblSession s ON s.id = src.session_id
60
		INNER JOIN $tblCourse c ON c.id = src.c_id
61
		WHERE src.session_id='$id_session' AND src.c_id='$courseId' ";
62
63
$result = Database::query($sql);
64
if (!list($session_name, $course_title) = Database::fetch_row($result)) {
65
    header('Location: session_course_list.php?id_session='.$id_session);
66
    exit();
67
}
68
69
function get_number_of_users(): int
70
{
71
    $tblSessionRelUser = $GLOBALS['tblSessionRelUser'];
72
    $tblUser = $GLOBALS['tblUser'];
73
    $urlTable = $GLOBALS['urlTable'];
74
    $tblSessionRelCourseRelUser = $GLOBALS['tblSessionRelCourseRelUser'];
75
76
    $sessionId = (int) $GLOBALS['id_session'];
77
    $courseId = (int) $GLOBALS['courseId'];
78
    $urlId = api_get_current_access_url_id();
79
80
    $sql = "SELECT COUNT(DISTINCT u.user_id) AS nbr
81
        FROM $tblSessionRelUser s
82
        INNER JOIN $tblUser u ON (u.id = s.user_id)
83
        INNER JOIN $urlTable url ON (url.user_id = u.id)
84
        LEFT JOIN $tblSessionRelCourseRelUser scru
85
            ON (s.session_id = scru.session_id AND s.user_id = scru.user_id AND scru.c_id = $courseId)
86
        WHERE
87
            s.session_id = $sessionId AND
88
            url.access_url_id = $urlId";
89
90
    $row = Database::fetch_assoc(Database::query($sql));
91
92
    return (int) $row['nbr'];
93
}
94
95
function get_user_list(int $from, int $limit, int $column, string $direction): array
96
{
97
    $tblSessionRelUser = $GLOBALS['tblSessionRelUser'];
98
    $tblUser = $GLOBALS['tblUser'];
99
    $urlTable = $GLOBALS['urlTable'];
100
    $tblSessionRelCourseRelUser = $GLOBALS['tblSessionRelCourseRelUser'];
101
    $apiIsWesternNameOrder = $GLOBALS['apiIsWesternNameOrder'];
102
103
    $sessionId = (int) $GLOBALS['id_session'];
104
    $courseId = (int) $GLOBALS['courseId'];
105
    $urlId = api_get_current_access_url_id();
106
107
    $orderBy = "is_subscribed $direction, u.lastname";
108
109
    if ($column == 1) {
110
        $orderBy = $apiIsWesternNameOrder ? "u.firstname $direction, u.lastname" : "u.lastname $direction, u.firstname";
111
    } elseif ($column == 2) {
112
        $orderBy = $apiIsWesternNameOrder ? "u.lastname $direction, u.firstname" : "u.firstname $direction, u.lastname";
113
    } elseif (3 == $column) {
114
        $orderBy = "u.username $direction";
115
    }
116
117
    $sql = "SELECT DISTINCT u.user_id,"
118
        .($apiIsWesternNameOrder ? 'u.firstname, u.lastname' : 'u.lastname, u.firstname')
119
        .", u.username, scru.user_id as is_subscribed
120
        FROM $tblSessionRelUser s
121
        INNER JOIN $tblUser u
122
            ON (u.id = s.user_id)
123
        INNER JOIN $urlTable url
124
            ON (url.user_id = u.id)
125
        LEFT JOIN $tblSessionRelCourseRelUser scru
126
            ON (s.session_id = scru.session_id AND s.user_id = scru.user_id AND scru.c_id = $courseId)
127
        WHERE
128
            s.session_id = $sessionId AND
129
            url.access_url_id = $urlId
130
        ORDER BY $orderBy
131
        LIMIT $from, $limit";
132
133
    $result = Database::query($sql);
134
135
    return Database::store_result($result);
136
}
137
138
function actions_filter(?int $sessionCourseSubscriptionId, string $urlParams, array $row): string
139
{
140
    $params = [
141
        'idChecked[]' => $row['user_id'],
142
        'action' => 'add',
143
    ];
144
145
    $icon = Display::return_icon('add.png', get_lang('Add'));
146
147
    if ($sessionCourseSubscriptionId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sessionCourseSubscriptionId of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
148
        $params['action'] = 'delete';
149
150
        $icon = Display::return_icon('delete.png', get_lang('Delete'));
151
    }
152
153
    return Display::url(
154
        $icon,
155
        api_get_self().'?'.http_build_query($params)."&$urlParams",
156
        [
157
            'onclick' => 'javascript:if(!confirm(\''.get_lang('ConfirmYourChoice').'\')) return false;',
158
        ]
159
    );
160
}
161
162
$table = new SortableTable(
163
    'users',
164
    'get_number_of_users',
165
    'get_user_list'
166
);
167
$table->set_additional_parameters(
168
    [
169
        'sec_token' => Security::get_token(),
170
        'id_session' => $id_session,
171
        'course_code' => $course_code,
172
    ]
173
);
174
$table->set_header(0, '&nbsp;', false);
175
176
if ($apiIsWesternNameOrder) {
177
    $table->set_header(1, get_lang('FirstName'));
178
    $table->set_header(2, get_lang('LastName'));
179
} else {
180
    $table->set_header(1, get_lang('LastName'));
181
    $table->set_header(2, get_lang('FirstName'));
182
}
183
184
$table->set_header(3, get_lang('LoginName'));
185
$table->set_header(4, get_lang('Action'));
186
$table->set_column_filter(4, 'actions_filter');
187
$table->set_form_actions(
188
    [
189
        'delete' => get_lang('UnsubscribeSelectedUsersFromSession'),
190
        'add' => get_lang('AddUsers'),
191
    ],
192
    'idChecked'
193
);
194
195
$tool_name = get_lang('Session').': '.$session_name.' - '.get_lang('Course').': '.$course_title;
196
197
$interbreadcrumb[] = ['url' => 'session_list.php', 'name' => get_lang('SessionList')];
198
$interbreadcrumb[] = [
199
    'url' => "resume_session.php?id_session=".$id_session,
200
    'name' => get_lang('SessionOverview'),
201
];
202
203
Display::display_header($tool_name);
204
echo Display::page_header($tool_name);
205
206
$table->display();
207
208
Display::display_footer();
209