online_logout()   F
last analyzed

Complexity

Conditions 20
Paths 1440

Size

Total Lines 96
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 56
nc 1440
nop 2
dl 0
loc 96
rs 0
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
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Entity\UserAuthSource;
5
use Chamilo\CoreBundle\Entity\UserRelUser;
6
use Chamilo\CoreBundle\Framework\Container;
7
use ChamiloSession as Session;
8
9
/**
10
 * Code library for showing Who is online.
11
 *
12
 * @author Istvan Mandak, principal author
13
 * @author Denes Nagy, principal author
14
 * @author Bart Mollet
15
 * @author Roan Embrechts, cleaning and bugfixing
16
 * Insert a login reference for the current user into the track_e_online stats
17
 * table. This table keeps trace of the last login. Nothing else matters (we
18
 * don't keep traces of anything older).
19
 *
20
 * @param int user id
21
 */
22
function LoginCheck($uid)
23
{
24
    $uid = (int) $uid;
25
    if (!empty($uid)) {
26
        $online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
27
        $_course = api_get_course_info();
28
        $user_ip = '';
29
        if (!empty($_SERVER['REMOTE_ADDR'])) {
30
            $user_ip = Database::escape_string(api_get_real_ip());
31
        }
32
33
        $login_date = api_get_utc_datetime();
34
        $access_url_id = 1;
35
        if (api_get_multiple_access_url() && -1 != api_get_current_access_url_id()) {
36
            $access_url_id = api_get_current_access_url_id();
37
        }
38
        $session_id = api_get_session_id();
39
        $cid = 0;
40
        if (is_array($_course) && count($_course) > 0 && !empty($_course['real_id'])) {
41
            $cid = intval($_course['real_id']);
42
        }
43
        $query = "SELECT login_id FROM $online_table WHERE login_user_id = $uid";
44
        $resLogin = Database::query($query);
45
        if (Database::num_rows($resLogin) > 0) {
46
            $query = "UPDATE $online_table SET
47
                      login_date = '$login_date',
48
                      user_ip = '$user_ip',
49
                      c_id = $cid,
50
                      session_id = $session_id,
51
                      access_url_id = $access_url_id
52
                      WHERE login_user_id = $uid";
53
            Database::query($query);
54
        } else {
55
            $query = "INSERT $online_table (
56
                login_user_id,
57
                login_date,
58
                user_ip,
59
                c_id,
60
                session_id,
61
                access_url_id
62
            ) values (
63
                $uid,
64
                '$login_date',
65
                '$user_ip',
66
                $cid,
67
                $session_id,
68
                $access_url_id
69
            )";
70
            Database::query($query);
71
        }
72
    }
73
}
74
75
/**
76
 * @param int $userId
77
 */
78
function preventMultipleLogin($userId)
79
{
80
    $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
81
    $userId = (int) $userId;
82
    if ('true' === api_get_setting('prevent_multiple_simultaneous_login')) {
83
        if (!empty($userId) && !api_is_anonymous()) {
84
            $isFirstLogin = Session::read('first_user_login');
85
            $currentIp = Session::read('current_ip');
86
            $differentIp = false;
87
            if (!empty($currentIp) && api_get_real_ip() !== $currentIp) {
88
                $isFirstLogin = null;
89
                $differentIp = true;
90
            }
91
92
            if (empty($isFirstLogin)) {
93
                $sql = "SELECT login_id FROM $table
94
                        WHERE login_user_id = $userId
95
                        LIMIT 1";
96
97
                $result = Database::query($sql);
98
                $loginData = [];
99
                if (Database::num_rows($result)) {
100
                    $loginData = Database::fetch_array($result);
101
                }
102
103
                $userIsReallyOnline = user_is_online($userId);
104
105
                // Trying double login.
106
                if (!empty($loginData) && true == $userIsReallyOnline || $differentIp) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (! empty($loginData) && ...Online) || $differentIp, Probably Intended Meaning: ! empty($loginData) && (...Online || $differentIp)
Loading history...
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
107
                    session_regenerate_id();
108
                    Session::destroy();
109
                    header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=multiple_connection_not_allowed');
110
                    exit;
111
                } else {
112
                    // First time
113
                    Session::write('first_user_login', 1);
114
                    Session::write('current_ip', api_get_real_ip());
115
                }
116
            }
117
        }
118
    }
119
}
120
121
/**
122
 * This function handles the logout and is called whenever there is a $_GET['logout'].
123
 *
124
 * @param int  $user_id
125
 * @param bool $logout_redirect
126
 *
127
 * @author Fernando P. García <[email protected]>
128
 */
129
function online_logout($user_id = null, $logout_redirect = false)
130
{
131
    global $extAuthSource;
132
133
    // Database table definition
134
    $tbl_track_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
135
136
    if (empty($user_id)) {
137
        $user_id = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
138
    }
139
140
    // Changing global chat status to offline
141
    if (api_is_global_chat_enabled()) {
142
        $chat = new Chat();
143
        $chat->setUserStatus(0);
144
    }
145
146
    $chat = new Chat();
147
    $chat->close();
148
149
    // selecting the last login of the user
150
    $sql = "SELECT login_id, login_date
151
    		FROM $tbl_track_login
152
    		WHERE login_user_id = $user_id
153
    		ORDER BY login_date DESC
154
    		LIMIT 0,1";
155
    $q_last_connection = Database::query($sql);
156
    $i_id_last_connection = 0;
157
    if (Database::num_rows($q_last_connection) > 0) {
158
        $i_id_last_connection = Database::result($q_last_connection, 0, "login_id");
159
    }
160
161
    if (!isset($_SESSION['login_as']) && !empty($i_id_last_connection)) {
162
        $current_date = api_get_utc_datetime();
163
        $sql = "UPDATE $tbl_track_login SET logout_date='".$current_date."'
164
        		WHERE login_id='$i_id_last_connection'";
165
        Database::query($sql);
166
    }
167
    $logInfo = [
168
        'tool' => 'logout',
169
        'tool_id' => 0,
170
        'tool_id_detail' => 0,
171
    ];
172
    Event::registerLog($logInfo);
0 ignored issues
show
Bug introduced by
The method registerLog() does not exist on Event. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

172
    Event::/** @scrutinizer ignore-call */ 
173
           registerLog($logInfo);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
173
174
    UserManager::loginDelete($user_id);
175
176
    //the following code enables the use of an external logout function.
177
    //example: define a $extAuthSource['ldap']['logout']="file.php" in configuration.php
178
    // then a function called ldap_logout() inside that file
179
    // (using *authent_name*_logout as the function name) and the following code
180
    // will find and execute it
181
    $uinfo = api_get_user_info($user_id);
182
    if (!in_array(UserAuthSource::PLATFORM, $uinfo['auth_sources']) && is_array($extAuthSource)) {
183
        $firstAuthSource = $uinfo['auth_sources'][0];
184
185
        if (is_array($extAuthSource[$firstAuthSource])) {
186
            $subarray = $extAuthSource[$firstAuthSource];
187
            if (!empty($subarray['logout']) && file_exists($subarray['logout'])) {
188
                require_once $subarray['logout'];
189
                $logout_function = $firstAuthSource.'_logout';
190
                if (function_exists($logout_function)) {
191
                    $logout_function($uinfo);
192
                }
193
            }
194
        }
195
    }
196
197
    // After logout redirect to
198
    $url = api_get_path(WEB_PATH).'index.php';
199
200
    if ($logout_redirect && 'true' == api_get_plugin_setting('azure_active_directory', 'enable')) {
201
        if ('azure_active_directory' === ChamiloSession::read('_user_auth_source')) {
202
            $activeDirectoryPlugin = AzureActiveDirectory::create();
203
            $azureLogout = $activeDirectoryPlugin->getUrl(AzureActiveDirectory::URL_TYPE_LOGOUT);
204
            if (!empty($azureLogout)) {
205
                $url = $azureLogout;
206
            }
207
        }
208
    }
209
210
    Session::erase('last_id');
211
    CourseChatUtils::exitChat($user_id);
212
    session_regenerate_id();
213
    Session::destroy();
214
215
    $pluginKeycloak = Container::getPluginHelper()->isPluginEnabled('keycloak');
216
    if ($pluginKeycloak && in_array('keycloak', $uinfo['auth_sources'])) {
217
        $pluginUrl = api_get_path(WEB_PLUGIN_PATH).'keycloak/start.php?slo';
218
        header('Location: '.$pluginUrl);
219
        exit;
220
    }
221
222
    if ($logout_redirect) {
223
        header("Location: $url");
224
        exit;
225
    }
226
}
227
228
/**
229
 * @param int $user_id
230
 *
231
 * @return bool
232
 */
233
function user_is_online($user_id)
234
{
235
    $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
236
    $table_user = Database::get_main_table(TABLE_MAIN_USER);
237
238
    $access_url_id = api_get_current_access_url_id();
239
    $time_limit = api_get_setting('time_limit_whosonline');
240
241
    $online_time = time() - $time_limit * 60;
242
    $limit_date = api_get_utc_datetime($online_time);
243
    $user_id = (int) $user_id;
244
245
    $query = " SELECT login_user_id, login_date
246
               FROM $track_online_table track
247
               INNER JOIN $table_user u
248
               ON (u.id=track.login_user_id)
249
               WHERE
250
                    track.access_url_id =  $access_url_id AND
251
                    login_date >= '".$limit_date."'  AND
252
                    u.id =  $user_id
253
               LIMIT 1 ";
254
255
    $result = Database::query($query);
256
    if (Database::num_rows($result)) {
257
        return true;
258
    }
259
260
    return false;
261
}
262
263
/**
264
 * Gives a list of people online now (and in the last $valid minutes).
265
 *
266
 * @param $from
267
 * @param $number_of_items
268
 * @param null $column
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $column is correct as it would always require null to be passed?
Loading history...
269
 * @param null $direction
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $direction is correct as it would always require null to be passed?
Loading history...
270
 * @param null $time_limit
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $time_limit is correct as it would always require null to be passed?
Loading history...
271
 * @param bool $friends
272
 *
273
 * @return array|bool For each line, a list of user IDs and login dates, or FALSE on error or empty results
274
 */
275
function who_is_online(
276
    $from,
277
    $number_of_items,
278
    $column = null,
279
    $direction = null,
280
    $time_limit = null,
281
    $friends = false
282
) {
283
    // Time limit in seconds?
284
    if (empty($time_limit)) {
285
        $time_limit = api_get_setting('time_limit_whosonline');
286
    } else {
287
        $time_limit = intval($time_limit);
288
    }
289
290
    $from = intval($from);
291
    $number_of_items = intval($number_of_items);
292
293
    if (empty($column)) {
294
        $column = 'picture_uri';
295
        if ($friends) {
296
            $column = 'login_date';
297
        }
298
    }
299
300
    $direction = strtolower($direction);
301
    if (empty($direction)) {
302
        $direction = 'DESC';
303
    } else {
304
        if (!in_array($direction, ['asc', 'desc'])) {
305
            $direction = 'DESC';
306
        }
307
    }
308
309
    $online_time = time() - $time_limit * 60;
310
    $current_date = api_get_utc_datetime($online_time);
311
    $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
312
    $friend_user_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
313
    $table_user = Database::get_main_table(TABLE_MAIN_USER);
314
315
    if ($friends) {
316
        // 	who friends from social network is online
317
        $query = "SELECT DISTINCT login_user_id, login_date
318
                  FROM $track_online_table INNER JOIN $friend_user_table
319
                  ON (friend_user_id = login_user_id)
320
                  WHERE
321
                    login_date >= '".$current_date."' AND
322
                    friend_user_id <> '".api_get_user_id()."' AND
323
                    relation_type='".UserRelUser::USER_RELATION_TYPE_FRIEND."' AND
324
                    user_id = '".api_get_user_id()."'
325
                  ORDER BY `$column` $direction
326
                  LIMIT $from, $number_of_items";
327
    } else {
328
        $query = "SELECT DISTINCT login_user_id, login_date
329
                    FROM ".$track_online_table." e
330
                    INNER JOIN ".$table_user." u ON (u.id = e.login_user_id)
331
                  WHERE u.active <> ".USER_SOFT_DELETED." AND u.status != ".ANONYMOUS." AND login_date >= '".$current_date."'
332
                  ORDER BY `$column` $direction
333
                  LIMIT $from, $number_of_items";
334
    }
335
336
    if (api_get_multiple_access_url()) {
337
        $access_url_id = api_get_current_access_url_id();
338
        if (-1 != $access_url_id) {
339
            if ($friends) {
340
                // 	friends from social network is online
341
                $query = "SELECT distinct login_user_id, login_date
342
                            FROM $track_online_table track INNER JOIN $friend_user_table
343
                            ON (friend_user_id = login_user_id)
344
                            WHERE   track.access_url_id =  $access_url_id AND
345
                                    login_date >= '".$current_date."' AND
346
                                    friend_user_id <> '".api_get_user_id()."' AND
347
                                    relation_type='".UserRelUser::USER_RELATION_TYPE_FRIEND."'
348
                            ORDER BY `$column` $direction
349
                            LIMIT $from, $number_of_items";
350
            } else {
351
                // all users online
352
                $query = "SELECT login_user_id, login_date
353
                          FROM ".$track_online_table." track
354
                          INNER JOIN ".$table_user." u
355
                          ON (u.id=track.login_user_id)
356
                          WHERE u.active <> ".USER_SOFT_DELETED." AND u.status != ".ANONYMOUS." AND track.access_url_id =  $access_url_id AND
357
                                login_date >= '".$current_date."'
358
                          ORDER BY `$column` $direction
359
                          LIMIT $from, $number_of_items";
360
            }
361
        }
362
    }
363
364
    //This query will show all registered users. Only for dev purposes.
365
    /*$query = "SELECT DISTINCT u.id as login_user_id, login_date
366
            FROM $track_online_table e, $table_user u
367
            GROUP by u.id
368
            ORDER BY $column $direction
369
            LIMIT $from, $number_of_items";*/
370
371
    $result = Database::query($query);
372
    if ($result) {
373
        $users_online = [];
374
        while (list($login_user_id, $login_date) = Database::fetch_row($result)) {
375
            $users_online[] = $login_user_id;
376
        }
377
378
        return $users_online;
379
    } else {
380
        return false;
381
    }
382
}
383
384
/**
385
 * @param string $time_limit
386
 */
387
function who_is_online_count($time_limit = null, $friends = false)
388
{
389
    if (empty($time_limit)) {
390
        $time_limit = api_get_setting('time_limit_whosonline');
391
    } else {
392
        $time_limit = intval($time_limit);
393
    }
394
    $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
395
    $friend_user_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
396
    $table_user = Database::get_main_table(TABLE_MAIN_USER);
397
    $online_time = time() - $time_limit * 60;
398
    $current_date = api_get_utc_datetime($online_time);
399
400
    if ($friends) {
401
        // who friends from social network is online
402
        $query = "SELECT DISTINCT count(login_user_id) as count
403
				  FROM $track_online_table INNER JOIN $friend_user_table
404
                  ON (friend_user_id = login_user_id)
405
				  WHERE
406
				        login_date >= '$current_date' AND
407
				        friend_user_id <> '".api_get_user_id()."' AND
408
				        relation_type='".UserRelUser::USER_RELATION_TYPE_FRIEND."' AND
409
				        user_id = '".api_get_user_id()."' ";
410
    } else {
411
        // All users online
412
        $query = "SELECT count(login_id) as count
413
                  FROM $track_online_table track INNER JOIN $table_user u
414
                  ON (u.id=track.login_user_id)
415
                  WHERE u.active <> ".USER_SOFT_DELETED." AND u.status != ".ANONYMOUS." AND login_date >= '$current_date'  ";
416
    }
417
418
    if (api_get_multiple_access_url()) {
419
        $access_url_id = api_get_current_access_url_id();
420
        if (-1 != $access_url_id) {
421
            if ($friends) {
422
                // friends from social network is online
423
                $query = "SELECT DISTINCT count(login_user_id) as count
424
							FROM $track_online_table track
425
							INNER JOIN $friend_user_table ON (friend_user_id = login_user_id)
426
							WHERE
427
							    track.access_url_id = $access_url_id AND
428
							    login_date >= '".$current_date."' AND
429
							    friend_user_id <> '".api_get_user_id()."' AND
430
							    relation_type='".UserRelUser::USER_RELATION_TYPE_FRIEND."'  ";
431
            } else {
432
                // all users online
433
                $query = "SELECT count(login_id) as count FROM $track_online_table  track
434
                          INNER JOIN $table_user u ON (u.id=track.login_user_id)
435
						  WHERE
436
						    u.active <> ".USER_SOFT_DELETED." AND
437
						    u.status != ".ANONYMOUS." AND
438
						    track.access_url_id =  $access_url_id AND
439
						    login_date >= '$current_date' ";
440
            }
441
        }
442
    }
443
444
    // Dev purposes show all users online
445
    /*$table_user = Database::get_main_table(TABLE_MAIN_USER);
446
    $query = "SELECT count(*)  as count FROM ".$table_user;*/
447
448
    $result = Database::query($query);
449
    if (Database::num_rows($result) > 0) {
450
        $row = Database::fetch_array($result);
451
452
        return $row['count'];
453
    } else {
454
        return false;
455
    }
456
}
457
458
/**
459
 * Returns a list (array) of users who are online and in this course.
460
 *
461
 * @param    int User ID
462
 * @param    int Number of minutes
463
 * @param    string  Course code (could be empty, but then the function returns false)
464
 *
465
 * @return array Each line gives a user id and a login time
466
 */
467
function who_is_online_in_this_course($from, $number_of_items, $uid, $time_limit, $course_code)
468
{
469
    if (empty($course_code)) {
470
        return false;
471
    }
472
473
    $time_limit = (int) $time_limit;
474
    if (empty($time_limit)) {
475
        $time_limit = api_get_setting('time_limit_whosonline');
476
    }
477
478
    $online_time = time() - $time_limit * 60;
479
    $current_date = api_get_utc_datetime($online_time);
480
    $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
481
    $tableUser = Database::get_main_table(TABLE_MAIN_USER);
482
    $course_code = Database::escape_string($course_code);
483
    $courseInfo = api_get_course_info($course_code);
484
    $courseId = $courseInfo['real_id'];
485
486
    $from = (int) $from;
487
    $number_of_items = (int) $number_of_items;
488
489
    $urlCondition = '';
490
    $urlJoin = '';
491
    if (api_is_multiple_url_enabled()) {
0 ignored issues
show
Deprecated Code introduced by
The function api_is_multiple_url_enabled() has been deprecated: Use AccessUrlUtil::isMultiple ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

491
    if (/** @scrutinizer ignore-deprecated */ api_is_multiple_url_enabled()) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
492
        $accessUrlUser = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
493
        $urlId = api_get_current_access_url_id();
494
        $urlJoin = " INNER JOIN $accessUrlUser a ON (a.user_id = u.id) ";
495
        $urlCondition = " AND a.access_url_id = $urlId ";
496
    }
497
498
    $query = "SELECT o.login_user_id, o.login_date
499
              FROM $track_online_table o
500
              INNER JOIN $tableUser u
501
              ON (o.login_user_id = u.id)
502
              $urlJoin
503
              WHERE
504
                u.active <> ".USER_SOFT_DELETED." AND
505
                u.status <> '".ANONYMOUS."' AND
506
                o.c_id = $courseId AND
507
                o.login_date >= '$current_date'
508
                $urlCondition
509
              LIMIT $from, $number_of_items ";
510
511
    $result = Database::query($query);
512
    if ($result) {
513
        $users_online = [];
514
        while (list($login_user_id, $login_date) = Database::fetch_row($result)) {
515
            $users_online[] = $login_user_id;
516
        }
517
518
        return $users_online;
519
    } else {
520
        return false;
521
    }
522
}
523
524
/**
525
 * @param int    $uid
526
 * @param string $time_limit
527
 */
528
function who_is_online_in_this_course_count(
529
    $uid,
530
    $time_limit,
531
    $coursecode = null
532
) {
533
    if (empty($coursecode)) {
534
        return false;
535
    }
536
    $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
537
    $tableUser = Database::get_main_table(TABLE_MAIN_USER);
538
    $time_limit = Database::escape_string($time_limit);
539
    $online_time = time() - $time_limit * 60;
540
    $current_date = api_get_utc_datetime($online_time);
541
    $courseId = api_get_course_int_id($coursecode);
542
543
    if (empty($courseId)) {
544
        return false;
545
    }
546
547
    $urlCondition = '';
548
    $urlJoin = '';
549
    if (api_is_multiple_url_enabled()) {
0 ignored issues
show
Deprecated Code introduced by
The function api_is_multiple_url_enabled() has been deprecated: Use AccessUrlUtil::isMultiple ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

549
    if (/** @scrutinizer ignore-deprecated */ api_is_multiple_url_enabled()) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
550
        $accessUrlUser = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
551
        $urlId = api_get_current_access_url_id();
552
        $urlJoin = " INNER JOIN $accessUrlUser a ON (a.user_id = u.id) ";
553
        $urlCondition = " AND a.access_url_id = $urlId ";
554
    }
555
556
    $query = "SELECT count(login_user_id) as count
557
              FROM $track_online_table o
558
              INNER JOIN $tableUser u
559
              ON (login_user_id = u.id)
560
              $urlJoin
561
              WHERE
562
                u.active <> ".USER_SOFT_DELETED." AND
563
                u.status <> '".ANONYMOUS."' AND
564
                c_id = $courseId AND
565
                login_date >= '$current_date'
566
                $urlCondition
567
                ";
568
    $result = Database::query($query);
569
    if (Database::num_rows($result) > 0) {
570
        $row = Database::fetch_array($result);
571
572
        return $row['count'];
573
    } else {
574
        return false;
575
    }
576
}
577
578
/**
579
 * @param string $timeLimit
580
 * @param int    $sessionId
581
 *
582
 * @return bool
583
 */
584
function whoIsOnlineInThisSessionCount($timeLimit, $sessionId)
585
{
586
    if (!$sessionId) {
587
        return 0;
588
    }
589
590
    $tblTrackOnline = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
591
    $tableUser = Database::get_main_table(TABLE_MAIN_USER);
592
593
    $timeLimit = Database::escape_string($timeLimit);
594
    $online_time = time() - $timeLimit * 60;
595
    $current_date = api_get_utc_datetime($online_time);
596
597
    $urlCondition = '';
598
    $urlJoin = '';
599
    if (api_is_multiple_url_enabled()) {
0 ignored issues
show
Deprecated Code introduced by
The function api_is_multiple_url_enabled() has been deprecated: Use AccessUrlUtil::isMultiple ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

599
    if (/** @scrutinizer ignore-deprecated */ api_is_multiple_url_enabled()) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
600
        $accessUrlUser = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
601
        $urlId = api_get_current_access_url_id();
602
        $urlJoin = " INNER JOIN $accessUrlUser a ON (a.user_id = u.id) ";
603
        $urlCondition = " AND a.access_url_id = $urlId ";
604
    }
605
606
    $query = "SELECT count(login_user_id) as count
607
              FROM $tblTrackOnline o
608
              INNER JOIN $tableUser u
609
              ON (login_user_id = u.id)
610
              $urlJoin
611
              WHERE
612
                    u.active <> ".USER_SOFT_DELETED." AND
613
                    u.status <> '".ANONYMOUS."' AND
614
                    session_id = $sessionId AND
615
                    login_date >= '$current_date'
616
                    $urlCondition
617
            ";
618
    $result = Database::query($query);
619
620
    if (Database::num_rows($result) > 0) {
621
        $row = Database::fetch_assoc($result);
622
623
        return $row['count'];
624
    }
625
626
    return 0;
627
}
628