Passed
Push — master ( 7f9d4e...b9a584 )
by Julito
18:35 queued 06:54
created

get_group_user_data()   C

Complexity

Conditions 10
Paths 26

Size

Total Lines 99
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 67
nc 26
nop 4
dl 0
loc 99
rs 6.8533
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Entity\User;
6
use Chamilo\CoreBundle\Framework\Container;
7
8
/**
9
 * This script shows the group space for one specific group, possibly displaying
10
 * a list of users in the group, subscribe or unsubscribe option, tutors...
11
 *
12
 * @todo    Display error message if no group ID specified
13
 */
14
require_once __DIR__.'/../inc/global.inc.php';
15
$current_course_tool = TOOL_GROUP;
16
17
// Notice for unauthorized people.
18
api_protect_course_script(true, false, 'group');
19
20
$group_id = api_get_group_id();
21
$user_id = api_get_user_id();
22
$groupEntity = null;
23
if (!empty($group_id)) {
24
    $groupEntity = api_get_group_entity($group_id);
25
}
26
27
if (null === $groupEntity) {
28
    api_not_allowed(true);
29
}
30
31
$this_section = SECTION_COURSES;
32
$nameTools = get_lang('Group area');
33
$interbreadcrumb[] = [
34
    'url' => 'group.php?'.api_get_cidreq(),
35
    'name' => get_lang('Groups'),
36
];
37
38
/*	Ensure all private groups // Juan Carlos Raña Trabado */
39
$forums = get_forums_of_group($groupEntity);
40
if (!GroupManager::userHasAccessToBrowse($user_id, $groupEntity, api_get_session_id())) {
41
    api_not_allowed(true);
42
}
43
44
/*
45
 * User wants to register in this group
46
 */
47
if (!empty($_GET['selfReg']) &&
48
    GroupManager::is_self_registration_allowed($user_id, $groupEntity)
49
) {
50
    GroupManager::subscribeUsers($user_id, $groupEntity);
51
    Display::addFlash(Display::return_message(get_lang('You are now a member of this group.')));
52
}
53
54
/*
55
 * User wants to unregister from this group
56
 */
57
if (!empty($_GET['selfUnReg']) &&
58
    GroupManager::is_self_unregistration_allowed($user_id, $groupEntity)
59
) {
60
    GroupManager::unsubscribeUsers($user_id, $groupEntity);
61
    Display::addFlash(
62
        Display::return_message(get_lang('You\'re now unsubscribed.'), 'normal')
63
    );
64
}
65
66
Display::display_header(
67
    $nameTools.' '.Security::remove_XSS($groupEntity->getName()),
68
    'Group'
69
);
70
71
Display::display_introduction_section(TOOL_GROUP);
72
73
$actions = '<a href="'.api_get_path(WEB_CODE_PATH).'group/group.php?'.api_get_cidreq().'">'.
74
    Display::return_icon(
75
        'back.png',
76
        get_lang('Back to Groups list'),
77
        '',
78
        ICON_SIZE_MEDIUM
79
    ).
80
    '</a>';
81
82
$confirmationMessage = addslashes(api_htmlentities(get_lang('Please confirm your choice'), ENT_QUOTES));
83
84
// Register to group.
85
$subscribe_group = '';
86
if (GroupManager::is_self_registration_allowed($user_id, $groupEntity)) {
87
    $subscribe_group = '<a
88
            class="btn btn-default"
89
            href="'.api_get_self().'?selfReg=1&group_id='.$group_id.'"
90
            onclick="javascript: if(!confirm('."'".$confirmationMessage."'".')) return false;">'.
91
        get_lang('Add me to this group').
92
    '</a>';
93
}
94
95
// Unregister from group.
96
$unsubscribe_group = '';
97
if (GroupManager::is_self_unregistration_allowed($user_id, $groupEntity)) {
98
    $unsubscribe_group = '<a
99
        class="btn btn-default" href="'.api_get_self().'?selfUnReg=1"
100
        onclick="javascript: if(!confirm('."'".$confirmationMessage."'".')) return false;">'.
101
        get_lang('Unsubscribe me from this group.').'</a>';
102
}
103
104
echo Display::toolbarAction('toolbar', [$actions]);
105
106
$edit_url = '';
107
if (api_is_allowed_to_edit(false, true) ||
108
    GroupManager::isTutorOfGroup($user_id, $groupEntity)
109
) {
110
    $edit_url = '<a
111
        href="'.api_get_path(WEB_CODE_PATH).'group/settings.php?'.api_get_cidreq().'">'.
112
        Display::return_icon('edit.png', get_lang('Edit this group'), '', ICON_SIZE_SMALL).
113
        '</a>';
114
}
115
116
echo Display::page_header(
117
    Security::remove_XSS($groupEntity->getName()).' '.$edit_url.' '.$subscribe_group.' '.$unsubscribe_group
118
);
119
120
if (!empty($groupEntity->getDescription())) {
121
    echo '<p>'.Security::remove_XSS($groupEntity->getDescription()).'</p>';
122
}
123
124
if (api_is_allowed_to_edit(false, true) ||
125
    GroupManager::userHasAccessToBrowse($user_id, $groupEntity, api_get_session_id())
126
) {
127
    $actions_array = [];
128
    if (is_array($forums)) {
129
        if (GroupManager::TOOL_NOT_AVAILABLE != $groupEntity->getForumState()) {
130
            foreach ($forums as $forum) {
131
                if ('public' === $forum->getForumGroupPublicPrivate() ||
132
                    ('private' === $forum->getForumGroupPublicPrivate()) ||
133
                    !empty($user_is_tutor) ||
134
                    api_is_allowed_to_edit(false, true)
135
                ) {
136
                    $actions_array[] = [
137
                        'url' => api_get_path(WEB_CODE_PATH).
138
                            'forum/viewforum.php?forum='.$forum->getIid().'&'.api_get_cidreq().'&origin=group',
139
                        'content' => Display::return_icon(
140
                            'forum.png',
141
                            get_lang('Forum').': '.$forum->getForumTitle(),
142
                            [],
143
                            32
144
                        ),
145
                    ];
146
                }
147
            }
148
        }
149
    }
150
151
    if (GroupManager::TOOL_NOT_AVAILABLE != $groupEntity->getDocState()) {
152
        // Link to the documents area of this group
153
        $actions_array[] = [
154
            'url' => api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq(),
155
            'content' => Display::return_icon('folder.png', get_lang('Documents'), [], 32),
156
        ];
157
    }
158
159
    if (GroupManager::TOOL_NOT_AVAILABLE != $groupEntity->getCalendarState()) {
160
        $groupFilter = '';
161
        if (!empty($group_id)) {
162
            $groupFilter = "&type=course&user_id=GROUP:$group_id";
163
        }
164
        // Link to a group-specific part of agenda
165
        $actions_array[] = [
166
            'url' => api_get_path(WEB_CODE_PATH).'calendar/agenda_js.php?'.api_get_cidreq().$groupFilter,
167
            'content' => Display::return_icon('agenda.png', get_lang('Agenda'), [], 32),
168
        ];
169
    }
170
171
    if (GroupManager::TOOL_NOT_AVAILABLE != $groupEntity->getWorkState()) {
172
        // Link to the works area of this group
173
        $actions_array[] = [
174
            'url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(),
175
            'content' => Display::return_icon('work.png', get_lang('Assignments'), [], 32),
176
        ];
177
    }
178
    if (GroupManager::TOOL_NOT_AVAILABLE != $groupEntity->getAnnouncementsState()) {
179
        // Link to a group-specific part of announcements
180
        $actions_array[] = [
181
            'url' => api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.api_get_cidreq(),
182
            'content' => Display::return_icon('announce.png', get_lang('Announcements'), [], 32),
183
        ];
184
    }
185
186
    if (GroupManager::TOOL_NOT_AVAILABLE != $groupEntity->getWikiState()) {
187
        // Link to the wiki area of this group
188
        $actions_array[] = [
189
            'url' => api_get_path(WEB_CODE_PATH).
190
                'wiki/index.php?'.api_get_cidreq().'&action=show&title=index&sid='.api_get_session_id().'&group_id='.$groupEntity->getIid(),
191
            'content' => Display::return_icon('wiki.png', get_lang('Wiki'), [], 32),
192
        ];
193
    }
194
195
    if (GroupManager::TOOL_NOT_AVAILABLE != $groupEntity->getChatState()) {
196
        // Link to the chat area of this group
197
        if (api_get_course_setting('allow_open_chat_window')) {
198
            $actions_array[] = [
199
                'url' => 'javascript: void(0);',
200
                'content' => Display::return_icon('chat.png', get_lang('Chat'), [], 32),
201
                'url_attributes' => [
202
                    'onclick' => " window.open('../chat/chat.php?".api_get_cidreq().'&toolgroup='.$groupEntity->getIid()."','window_chat_group_".api_get_course_id().'_'.api_get_group_id()."','height=380, width=625, left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no')",
203
                ],
204
            ];
205
        } else {
206
            $actions_array[] = [
207
                'url' => api_get_path(WEB_CODE_PATH).'chat/chat.php?'.api_get_cidreq().'&gid='.$groupEntity->getIid(),
208
                'content' => Display::return_icon('chat.png', get_lang('Chat'), [], 32),
209
            ];
210
        }
211
    }
212
213
    $enabled = api_get_plugin_setting('bbb', 'tool_enable');
214
    if ('true' === $enabled) {
215
        $bbb = new bbb();
216
        if ($bbb->hasGroupSupport()) {
217
            $actions_array[] = [
218
                'url' => api_get_path(WEB_PLUGIN_PATH).'bbb/start.php?'.api_get_cidreq(),
219
                'content' => Display::return_icon('bbb.png', get_lang('Videoconference'), [], 32),
220
            ];
221
        }
222
    }
223
224
    $enabled = api_get_plugin_setting('zoom', 'tool_enable');
225
    if ('true' === $enabled) {
226
        $actions_array[] = [
227
            'url' => api_get_path(WEB_PLUGIN_PATH).'zoom/start.php?'.api_get_cidreq(),
228
            'content' => Display::return_icon('bbb.png', get_lang('VideoConference'), [], 32),
229
        ];
230
    }
231
232
    if (!empty($actions_array)) {
233
        echo Display::actions($actions_array);
234
    }
235
} else {
236
    $actions_array = [];
237
    if (is_array($forums)) {
238
        if (GroupManager::TOOL_PUBLIC == $groupEntity->getForumState()) {
239
            foreach ($forums as $forum) {
240
                if ('public' === $forum->getForumGroupPublicPrivate()) {
241
                    $actions_array[] = [
242
                        'url' => api_get_path(WEB_CODE_PATH).
243
                            'forum/viewforum.php?cid='.api_get_course_int_id().
244
                            '&forum='.$forum->getIid().'&gid='.$groupEntity->getIid().'&origin=group',
245
                        'content' => Display::return_icon(
246
                            'forum.png',
247
                            get_lang('Group Forum'),
248
                            [],
249
                            ICON_SIZE_MEDIUM
250
                        ),
251
                    ];
252
                }
253
            }
254
        }
255
    }
256
257
    if (GroupManager::TOOL_PUBLIC == $groupEntity->getDocState()) {
258
        // Link to the documents area of this group
259
        $actions_array[] = [
260
            'url' => api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq(),
261
            'content' => Display::return_icon('folder.png', get_lang('Documents'), [], ICON_SIZE_MEDIUM),
262
        ];
263
    }
264
265
    if (GroupManager::TOOL_PUBLIC == $groupEntity->getCalendarState()) {
266
        $groupFilter = '';
267
        if (!empty($group_id)) {
268
            $groupFilter = "&type=course&user_id=GROUP:$group_id";
269
        }
270
        // Link to a group-specific part of agenda
271
        $actions_array[] = [
272
            'url' => api_get_path(WEB_CODE_PATH).'calendar/agenda_js.php?'.api_get_cidreq().$groupFilter,
273
            'content' => Display::return_icon('agenda.png', get_lang('Agenda'), [], 32),
274
        ];
275
    }
276
277
    if (GroupManager::TOOL_PUBLIC == $groupEntity->getWorkState()) {
278
        // Link to the works area of this group
279
        $actions_array[] = [
280
            'url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(),
281
            'content' => Display::return_icon('work.png', get_lang('Assignments'), [], ICON_SIZE_MEDIUM),
282
        ];
283
    }
284
285
    if (GroupManager::TOOL_PUBLIC == $groupEntity->getAnnouncementsState()) {
286
        // Link to a group-specific part of announcements
287
        $actions_array[] = [
288
            'url' => api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.api_get_cidreq(),
289
            'content' => Display::return_icon('announce.png', get_lang('Announcements'), [], ICON_SIZE_MEDIUM),
290
        ];
291
    }
292
293
    if (GroupManager::TOOL_PUBLIC == $groupEntity->getWikiState()) {
294
        // Link to the wiki area of this group
295
        $actions_array[] = [
296
            'url' => api_get_path(WEB_CODE_PATH).'wiki/index.php?'.
297
                api_get_cidreq().'&action=show&title=index&sid='.api_get_session_id().'&gid='.$group_id,
298
            'content' => Display::return_icon('wiki.png', get_lang('Wiki'), [], 32),
299
        ];
300
    }
301
302
    if (GroupManager::TOOL_PUBLIC == $groupEntity->getChatState()) {
303
        // Link to the chat area of this group
304
        if (api_get_course_setting('allow_open_chat_window')) {
305
            $actions_array[] = [
306
                'url' => "javascript: void(0);\" onclick=\"window.open('../chat/chat.php?".api_get_cidreq().'&toolgroup='.$group_id."','window_chat_group_".api_get_course_id().'_'.api_get_group_id()."','height=380, width=625, left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no') \"",
307
                'content' => Display::return_icon('chat.png', get_lang('Chat'), [], 32),
308
            ];
309
        } else {
310
            $actions_array[] = [
311
                'url' => api_get_path(WEB_CODE_PATH).'chat/chat.php?'.api_get_cidreq().'&toolgroup='.$group_id,
312
                'content' => Display::return_icon('chat.png', get_lang('Chat'), [], 32),
313
            ];
314
        }
315
    }
316
317
    if (!empty($actions_array)) {
318
        echo Display::actions($actions_array);
319
    }
320
}
321
322
/*
323
 * List all the tutors of the current group
324
 */
325
$tutors = $groupEntity->getTutors();
326
$userRepo = Container::getUserRepository();
327
$tutor_info = '';
328
if (0 == count($tutors)) {
329
    $tutor_info = get_lang('(none)');
330
} else {
331
    $tutor_info .= '<ul class="thumbnails">';
332
    foreach ($tutors as $tutor) {
333
        $user = $tutor->getUser();
334
        $username = api_htmlentities(sprintf(get_lang('Login: %s'), $user->getUsername()), ENT_QUOTES);
335
        $completeName = UserManager::formatUserFullName($user);
336
        $avatar = Container::getIllustrationRepository()->getIllustrationUrl($user);
337
        $photo = '<img src="'.$avatar.'" alt="'.$completeName.'" width="32" height="32" title="'.$completeName.'" />';
338
        $tutor_info .= '<li>';
339
        //$tutor_info .= $userInfo['complete_name_with_message_link'];
340
        $tutor_info .= $photo.$completeName;
341
        $tutor_info .= '</li>';
342
    }
343
    $tutor_info .= '</ul>';
344
}
345
346
echo Display::page_subheader(get_lang('Coaches'));
347
if (!empty($tutor_info)) {
348
    echo $tutor_info;
349
}
350
echo '<br />';
351
352
/*
353
 * List all the members of the current group
354
 */
355
echo Display::page_subheader(get_lang('Group members'));
356
357
$table = new SortableTable(
358
    'group_users',
359
    'get_number_of_group_users',
360
    'get_group_user_data',
361
    api_is_western_name_order() xor api_sort_by_first_name() ? 2 : 1
362
);
363
$origin = api_get_origin();
364
$my_cidreq = isset($_GET['cid']) ? (int) $_GET['cid'] : 0;
365
$my_gidreq = isset($_GET['gid']) ? (int) $_GET['gid'] : 0;
366
$parameters = ['cid' => $my_cidreq, 'origin' => $origin, 'gid' => $my_gidreq];
367
$table->set_additional_parameters($parameters);
368
$table->set_header(0, '');
369
370
if (api_is_western_name_order()) {
371
    $table->set_header(1, get_lang('First name'));
372
    $table->set_header(2, get_lang('Last name'));
373
} else {
374
    $table->set_header(1, get_lang('Last name'));
375
    $table->set_header(2, get_lang('First name'));
376
}
377
378
if ('true' === api_get_setting('show_email_addresses') || api_is_allowed_to_edit()) {
379
    $table->set_header(3, get_lang('e-mail'));
380
    $table->set_column_filter(3, 'email_filter');
381
    $table->set_header(4, get_lang('active'));
382
    $table->set_column_filter(4, 'activeFilter');
383
} else {
384
    $table->set_header(3, get_lang('active'));
385
    $table->set_column_filter(3, 'activeFilter');
386
}
387
388
$table->display();
389
390
/**
391
 * Get the number of subscribed users to the group.
392
 *
393
 * @return int
394
 *
395
 * @author Patrick Cool <[email protected]>, Ghent University, Belgium
396
 *
397
 * @version April 2008
398
 */
399
function get_number_of_group_users()
400
{
401
    $groupInfo = GroupManager::get_group_properties(api_get_group_id());
402
    $course_id = api_get_course_int_id();
403
404
    if (empty($groupInfo) || empty($course_id)) {
405
        return 0;
406
    }
407
408
    // Database table definition
409
    $table = Database::get_course_table(TABLE_GROUP_USER);
410
411
    // Query
412
    $sql = "SELECT count(iid) AS number_of_users
413
            FROM $table
414
            WHERE
415
                c_id = $course_id AND
416
                group_id = '".(int) ($groupInfo['iid'])."'";
417
    $result = Database::query($sql);
418
    $return = Database::fetch_array($result, 'ASSOC');
419
420
    return $return['number_of_users'];
421
}
422
423
/**
424
 * Get the details of the users in a group.
425
 *
426
 * @param int $from            starting row
427
 * @param int $number_of_items number of items to be displayed
428
 * @param int $column          sorting colum
429
 * @param int $direction       sorting direction
430
 *
431
 * @return array
432
 *
433
 * @author Patrick Cool <[email protected]>, Ghent University, Belgium
434
 *
435
 * @version April 2008
436
 */
437
function get_group_user_data($from, $number_of_items, $column, $direction)
438
{
439
    $direction = !in_array(strtolower(trim($direction)), ['asc', 'desc']) ? 'asc' : $direction;
440
    $groupInfo = GroupManager::get_group_properties(api_get_group_id());
441
    $course_id = api_get_course_int_id();
442
    $column = (int) $column;
443
444
    if (empty($groupInfo) || empty($course_id)) {
445
        return 0;
446
    }
447
448
    // Database table definition
449
    $table_group_user = Database::get_course_table(TABLE_GROUP_USER);
450
    $table_user = Database::get_main_table(TABLE_MAIN_USER);
451
    $tableGroup = Database::get_course_table(TABLE_GROUP);
452
453
    // Query
454
    if ('true' === api_get_setting('show_email_addresses')) {
455
        $sql = 'SELECT user.id 	AS col0,
456
				'.(
457
            api_is_western_name_order() ?
458
                'user.firstname 	AS col1,
459
				user.lastname 	AS col2,'
460
                :
461
                'user.lastname 	AS col1,
462
				user.firstname 	AS col2,'
463
            )."
464
				user.email		AS col3
465
				, user.active AS col4
466
				FROM $table_user user
467
				INNER JOIN $table_group_user group_rel_user
468
				ON (group_rel_user.user_id = user.id)
469
				INNER JOIN $tableGroup g
470
				ON (group_rel_user.group_id = g.iid)
471
				WHERE
472
				    group_rel_user.c_id = $course_id AND
473
				    g.iid = '".$groupInfo['iid']."'
474
                ORDER BY col$column $direction
475
                LIMIT $from, $number_of_items";
476
    } else {
477
        if (api_is_allowed_to_edit()) {
478
            $sql = 'SELECT DISTINCT
479
                        u.id AS col0,
480
                        '.(api_is_western_name_order() ?
481
                        'u.firstname 	AS col1,
482
                            u.lastname 	AS col2,'
483
                        :
484
                        'u.lastname 	AS col1,
485
                        u.firstname 	AS col2,')."
486
                        u.email		AS col3
487
                        , u.active AS col4
488
                    FROM $table_user u
489
                    INNER JOIN $table_group_user gu
490
                    ON (gu.user_id = u.id)
491
                    INNER JOIN $tableGroup g
492
				    ON (gu.group_id = g.iid)
493
                    WHERE
494
                        g.iid = '".$groupInfo['iid']."' AND
495
                        gu.c_id = $course_id
496
                    ORDER BY col$column $direction
497
                    LIMIT $from, $number_of_items";
498
        } else {
499
            $sql = 'SELECT DISTINCT
500
						user.id 	AS col0,
501
						'.(
502
                api_is_western_name_order() ?
503
                    'user.firstname 	AS col1,
504
						user.lastname 	AS col2 '
505
                    :
506
                    'user.lastname 	AS col1,
507
						user.firstname 	AS col2 '
508
                    )."
509
                    , user.active AS col3
510
                    FROM $table_user user
511
                    INNER JOIN $table_group_user group_rel_user
512
                    ON (group_rel_user.user_id = user.id)
513
                    INNER JOIN $tableGroup g
514
                    ON (group_rel_user.group_id = g.iid)
515
                    WHERE
516
                        g.iid = '".$groupInfo['iid']."' AND
517
                        group_rel_user.c_id = $course_id AND
518
                        group_rel_user.user_id = user.id AND
519
                        g.iid = '".$groupInfo['iid']."'
520
                    ORDER BY col$column $direction
521
                    LIMIT $from, $number_of_items";
522
        }
523
    }
524
525
    $return = [];
526
    $result = Database::query($sql);
527
    while ($row = Database::fetch_row($result)) {
528
        $user = api_get_user_entity($row[0]);
529
        $avatar = Container::getIllustrationRepository()->getIllustrationUrl($user);
530
        $photo = '<img src="'.$avatar.'" width="22" height="22" />';
531
        $row[0] = $photo;
532
        $return[] = $row;
533
    }
534
535
    return $return;
536
}
537
538
/**
539
 * Returns a mailto-link.
540
 *
541
 * @param string $email An email-address
542
 *
543
 * @return string HTML-code with a mailto-link
544
 */
545
function email_filter($email)
546
{
547
    return Display::encrypted_mailto_link($email, $email);
548
}
549
550
function activeFilter($isActive)
551
{
552
    if ($isActive) {
553
        return Display::return_icon('accept.png', get_lang('active'), [], ICON_SIZE_TINY);
554
    }
555
556
    return Display::return_icon('error.png', get_lang('inactive'), [], ICON_SIZE_TINY);
557
}
558
559
if ('learnpath' != $origin) {
560
    Display::display_footer();
561
}
562