Code Duplication    Length = 102-110 lines in 2 locations

main/inc/lib/online.inc.php 1 location

@@ 220-321 (lines=102) @@
217
 * @param bool $friends
218
 * @return  array|bool For each line, a list of user IDs and login dates, or FALSE on error or empty results
219
 */
220
function who_is_online($from, $number_of_items, $column = null, $direction = null, $time_limit = null, $friends = false)
221
{
222
    // Time limit in seconds?
223
    if (empty($time_limit)) {
224
        $time_limit = api_get_setting('time_limit_whosonline');
225
    } else {
226
        $time_limit = intval($time_limit);
227
    }
228
229
    $from = intval($from);
230
    $number_of_items = intval($number_of_items);
231
232
    if (empty($column)) {
233
        $column = 'picture_uri';
234
        if ($friends) {
235
            $column = 'login_date';
236
        }
237
    }
238
239
    if (empty($direction)) {
240
        $direction = 'DESC';
241
    } else {
242
        if (!in_array(strtolower($direction), array('asc', 'desc'))) {
243
            $direction = 'DESC';
244
        }
245
    }
246
247
    $online_time = time() - $time_limit * 60;
248
    $current_date = api_get_utc_datetime($online_time);
249
    $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
250
    $friend_user_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
251
    $table_user	= Database::get_main_table(TABLE_MAIN_USER);
252
253
    if ($friends) {
254
        // 	who friends from social network is online
255
        $query = "SELECT DISTINCT login_user_id, login_date
256
                  FROM $track_online_table INNER JOIN $friend_user_table
257
                  ON (friend_user_id = login_user_id)
258
                  WHERE
259
                    login_date >= '".$current_date."' AND
260
                    friend_user_id <> '".api_get_user_id()."' AND
261
                    relation_type='".USER_RELATION_TYPE_FRIEND."' AND
262
                    user_id = '".api_get_user_id()."'
263
                  ORDER BY $column $direction
264
                  LIMIT $from, $number_of_items";
265
    } else {
266
        $query = "SELECT DISTINCT login_user_id, login_date
267
                    FROM ".$track_online_table ." e
268
                    INNER JOIN ".$table_user ." u ON (u.id = e.login_user_id)
269
                  WHERE u.status != ".ANONYMOUS." AND login_date >= '".$current_date."'
270
                  ORDER BY $column $direction
271
                  LIMIT $from, $number_of_items";
272
    }
273
274
    if (api_get_multiple_access_url()) {
275
        $access_url_id = api_get_current_access_url_id();
276
        if ($access_url_id != -1) {
277
            if ($friends) {
278
                // 	friends from social network is online
279
                $query = "SELECT distinct login_user_id, login_date
280
                            FROM $track_online_table track INNER JOIN $friend_user_table
281
                            ON (friend_user_id = login_user_id)
282
                            WHERE   track.access_url_id =  $access_url_id AND
283
                                    login_date >= '".$current_date."' AND
284
                                    friend_user_id <> '".api_get_user_id()."' AND
285
                                    relation_type='".USER_RELATION_TYPE_FRIEND."'
286
                            ORDER BY $column $direction
287
                            LIMIT $from, $number_of_items";
288
            } else {
289
                // all users online
290
                $query = "SELECT login_user_id, login_date
291
                          FROM ".$track_online_table ." track
292
                          INNER JOIN ".$table_user ." u
293
                          ON (u.id=track.login_user_id)
294
                          WHERE u.status != ".ANONYMOUS." AND track.access_url_id =  $access_url_id AND
295
                                login_date >= '".$current_date."'
296
                          ORDER BY $column $direction
297
                          LIMIT $from, $number_of_items";
298
            }
299
        }
300
    }
301
302
	//This query will show all registered users. Only for dev purposes.
303
	/*$query = "SELECT DISTINCT u.id as login_user_id, login_date
304
	        FROM $track_online_table e, $table_user u
305
            GROUP by u.id
306
            ORDER BY $column $direction
307
            LIMIT $from, $number_of_items";*/
308
309
    $result = Database::query($query);
310
    if ($result) {
311
        $users_online = array();
312
        while (list($login_user_id, $login_date) = Database::fetch_row($result)) {
313
            $users_online[] = $login_user_id;
314
        }
315
316
        return $users_online;
317
    } else {
318
319
        return false;
320
    }
321
}
322
323
function who_is_online_count($time_limit = null, $friends = false)
324
{

main/inc/lib/usermanager.lib.php 1 location

@@ 5641-5750 (lines=110) @@
5638
     * Gives a list of people online now (and in the last $valid minutes)
5639
     * @return  array       For each line, a list of user IDs and login dates, or FALSE on error or empty results
5640
     */
5641
    public static function whoIsOnline(
5642
        $from,
5643
        $number_of_items,
5644
        $column = null,
5645
        $direction = null,
5646
        $time_limit = null,
5647
        $friends = false
5648
    ) {
5649
        // Time limit in seconds?
5650
        if (empty($time_limit)) {
5651
            $time_limit = api_get_setting('display.time_limit_whosonline');
5652
        } else {
5653
            $time_limit = intval($time_limit);
5654
        }
5655
5656
        $from = intval($from);
5657
        $number_of_items = intval($number_of_items);
5658
5659
        if (empty($column)) {
5660
            $column = 'picture_uri';
5661
            if ($friends) {
5662
                $column = 'login_date';
5663
            }
5664
        }
5665
5666
        if (empty($direction)) {
5667
            $direction = 'DESC';
5668
        } else {
5669
            if (!in_array(strtolower($direction), array('asc', 'desc'))) {
5670
                $direction = 'DESC';
5671
            }
5672
        }
5673
5674
        $online_time = time() - $time_limit * 60;
5675
        $current_date = api_get_utc_datetime($online_time);
5676
        $track_online_table = Database::get_main_table(
5677
            TABLE_STATISTIC_TRACK_E_ONLINE
5678
        );
5679
        $friend_user_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
5680
        $table_user = Database::get_main_table(TABLE_MAIN_USER);
5681
5682
        if ($friends) {
5683
            // 	who friends from social network is online
5684
            $query = "SELECT DISTINCT login_user_id, login_date
5685
				  FROM $track_online_table INNER JOIN $friend_user_table
5686
				  ON (friend_user_id = login_user_id)
5687
				  WHERE
5688
				    login_date >= '".$current_date."' AND
5689
                    friend_user_id <> '".api_get_user_id()."' AND
5690
                    relation_type='".USER_RELATION_TYPE_FRIEND."' AND
5691
                    user_id = '".api_get_user_id()."'
5692
                  ORDER BY $column $direction
5693
                  LIMIT $from, $number_of_items";
5694
        } else {
5695
            $query = "SELECT DISTINCT login_user_id, login_date
5696
                    FROM ".$track_online_table." e
5697
		            INNER JOIN ".$table_user." u ON (u.id = e.login_user_id)
5698
                  WHERE u.status != ".ANONYMOUS." AND login_date >= '".$current_date."'
5699
                  ORDER BY $column $direction
5700
                  LIMIT $from, $number_of_items";
5701
        }
5702
5703
        if (api_get_multiple_access_url()) {
5704
            $access_url_id = api_get_current_access_url_id();
5705
            if ($access_url_id != -1) {
5706
                if ($friends) {
5707
                    // 	friends from social network is online
5708
                    $query = "SELECT distinct login_user_id, login_date
5709
							FROM $track_online_table track INNER JOIN $friend_user_table
5710
							ON (friend_user_id = login_user_id)
5711
							WHERE   track.access_url_id =  $access_url_id AND
5712
                                    login_date >= '".$current_date."' AND
5713
                                    friend_user_id <> '".api_get_user_id()."' AND
5714
                                    relation_type='".USER_RELATION_TYPE_FRIEND."'
5715
                            ORDER BY $column $direction
5716
                            LIMIT $from, $number_of_items";
5717
                } else {
5718
                    // all users online
5719
                    $query = "SELECT login_user_id, login_date
5720
						  FROM ".$track_online_table." track
5721
                          INNER JOIN ".$table_user." u
5722
                          ON (u.id=track.login_user_id)
5723
						  WHERE u.status != ".ANONYMOUS." AND track.access_url_id =  $access_url_id AND
5724
                                login_date >= '".$current_date."'
5725
                          ORDER BY $column $direction
5726
                          LIMIT $from, $number_of_items";
5727
                }
5728
            }
5729
        }
5730
5731
        //This query will show all registered users. Only for dev purposes.
5732
        /*$query = "SELECT DISTINCT u.id as login_user_id, login_date FROM ".$track_online_table ."  e , $table_user u
5733
                GROUP by u.id
5734
                ORDER BY $column $direction
5735
                LIMIT $from, $number_of_items";*/
5736
5737
        $result = Database::query($query);
5738
        if ($result) {
5739
            $users_online = array();
5740
            while (list($login_user_id, $login_date) = Database::fetch_row(
5741
                $result
5742
            )) {
5743
                $users_online[] = $login_user_id;
5744
            }
5745
5746
            return $users_online;
5747
        } else {
5748
            return false;
5749
        }
5750
    }
5751
5752
    /**
5753
     * @param int $user_id