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 ChamiloSession as Session;
7
8
/**
9
 * Code library for showing Who is online.
10
 *
11
 * @author Istvan Mandak, principal author
12
 * @author Denes Nagy, principal author
13
 * @author Bart Mollet
14
 * @author Roan Embrechts, cleaning and bugfixing
15
 * Insert a login reference for the current user into the track_e_online stats
16
 * table. This table keeps trace of the last login. Nothing else matters (we
17
 * don't keep traces of anything older).
18
 *
19
 * @param int user id
20
 */
21
function LoginCheck($uid)
22
{
23
    $uid = (int) $uid;
24
    if (!empty($uid)) {
25
        $online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
26
        $_course = api_get_course_info();
27
        $user_ip = '';
28
        if (!empty($_SERVER['REMOTE_ADDR'])) {
29
            $user_ip = Database::escape_string(api_get_real_ip());
30
        }
31
32
        $login_date = api_get_utc_datetime();
33
        $access_url_id = 1;
34
        if (api_get_multiple_access_url() && -1 != api_get_current_access_url_id()) {
35
            $access_url_id = api_get_current_access_url_id();
36
        }
37
        $session_id = api_get_session_id();
38
        $cid = 0;
39
        if (is_array($_course) && count($_course) > 0 && !empty($_course['real_id'])) {
40
            $cid = intval($_course['real_id']);
41
        }
42
        $query = "SELECT login_id FROM $online_table WHERE login_user_id = $uid";
43
        $resLogin = Database::query($query);
44
        if (Database::num_rows($resLogin) > 0) {
45
            $query = "UPDATE $online_table SET
46
                      login_date = '$login_date',
47
                      user_ip = '$user_ip',
48
                      c_id = $cid,
49
                      session_id = $session_id,
50
                      access_url_id = $access_url_id
51
                      WHERE login_user_id = $uid";
52
            Database::query($query);
53
        } else {
54
            $query = "INSERT $online_table (
55
                login_user_id,
56
                login_date,
57
                user_ip,
58
                c_id,
59
                session_id,
60
                access_url_id
61
            ) values (
62
                $uid,
63
                '$login_date',
64
                '$user_ip',
65
                $cid,
66
                $session_id,
67
                $access_url_id
68
            )";
69
            Database::query($query);
70
        }
71
    }
72
}
73
74
/**
75
 * @param int $userId
76
 */
77
function preventMultipleLogin($userId)
78
{
79
    $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
80
    $userId = (int) $userId;
81
    if ('true' === api_get_setting('prevent_multiple_simultaneous_login')) {
82
        if (!empty($userId) && !api_is_anonymous()) {
83
            $isFirstLogin = Session::read('first_user_login');
84
            $currentIp = Session::read('current_ip');
85
            $differentIp = false;
86
            if (!empty($currentIp) && api_get_real_ip() !== $currentIp) {
87
                $isFirstLogin = null;
88
                $differentIp = true;
89
            }
90
91
            if (empty($isFirstLogin)) {
92
                $sql = "SELECT login_id FROM $table
93
                        WHERE login_user_id = $userId
94
                        LIMIT 1";
95
96
                $result = Database::query($sql);
97
                $loginData = [];
98
                if (Database::num_rows($result)) {
99
                    $loginData = Database::fetch_array($result);
100
                }
101
102
                $userIsReallyOnline = user_is_online($userId);
103
104
                // Trying double login.
105
                if (!empty($loginData) && true == $userIsReallyOnline || $differentIp) {
0 ignored issues
show
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...
introduced by
Consider adding parentheses for clarity. Current Interpretation: (! empty($loginData) && ...Online) || $differentIp, Probably Intended Meaning: ! empty($loginData) && (...Online || $differentIp)
Loading history...
106
                    session_regenerate_id();
107
                    Session::destroy();
108
                    header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=multiple_connection_not_allowed');
109
                    exit;
110
                } else {
111
                    // First time
112
                    Session::write('first_user_login', 1);
113
                    Session::write('current_ip', api_get_real_ip());
114
                }
115
            }
116
        }
117
    }
118
}
119
120
/**
121
 * This function handles the logout and is called whenever there is a $_GET['logout'].
122
 *
123
 * @param int  $user_id
124
 * @param bool $logout_redirect
125
 *
126
 * @author Fernando P. García <[email protected]>
127
 */
128
function online_logout($user_id = null, $logout_redirect = false)
129
{
130
    global $extAuthSource;
131
132
    // Database table definition
133
    $tbl_track_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
134
135
    if (empty($user_id)) {
136
        $user_id = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
137
    }
138
139
    // Changing global chat status to offline
140
    if (api_is_global_chat_enabled()) {
141
        $chat = new Chat();
142
        $chat->setUserStatus(0);
143
    }
144
145
    $chat = new Chat();
146
    $chat->close();
147
148
    // selecting the last login of the user
149
    $sql = "SELECT login_id, login_date
150
    		FROM $tbl_track_login
151
    		WHERE login_user_id = $user_id
152
    		ORDER BY login_date DESC
153
    		LIMIT 0,1";
154
    $q_last_connection = Database::query($sql);
155
    $i_id_last_connection = 0;
156
    if (Database::num_rows($q_last_connection) > 0) {
157
        $i_id_last_connection = Database::result($q_last_connection, 0, "login_id");
158
    }
159
160
    if (!isset($_SESSION['login_as']) && !empty($i_id_last_connection)) {
161
        $current_date = api_get_utc_datetime();
162
        $sql = "UPDATE $tbl_track_login SET logout_date='".$current_date."'
163
        		WHERE login_id='$i_id_last_connection'";
164
        Database::query($sql);
165
    }
166
    $logInfo = [
167
        'tool' => 'logout',
168
        'tool_id' => 0,
169
        'tool_id_detail' => 0,
170
    ];
171
    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

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

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

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

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

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

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

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