ILIAS  release_8 Revision v8.24
class.ilOnlineTracking.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=0);
4/* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
5
14{
18 protected static ?int $last_access_time = null;
19
20 public static function getOnlineTime(int $a_user_id): int
21 {
22 global $DIC;
23
24 $ilDB = $DIC->database();
25 $res = $ilDB->query(
26 'SELECT online_time FROM ut_online WHERE usr_id = ' . $ilDB->quote(
27 $a_user_id,
28 'integer'
29 )
30 );
31 while ($row = $ilDB->fetchAssoc($res)) {
32 return (int) $row['online_time'];
33 }
34 return 0;
35 }
36
37 public static function addUser(int $a_user_id): bool
38 {
39 global $DIC;
40
41 $ilDB = $DIC->database();
42 $res = $ilDB->query(
43 'SELECT access_time FROM ut_online WHERE usr_id = ' . $ilDB->quote(
44 $a_user_id,
45 'integer'
46 )
47 );
48 if ($ilDB->numRows($res)) {
49 $row = $ilDB->fetchAssoc($res);
50 self::$last_access_time = (int) $row['access_time'];
51 return false;
52 }
53
54 $ilDB->manipulateF(
55 'INSERT INTO ut_online (usr_id, access_time) VALUES (%s, %s)',
56 array('integer', 'integer'),
57 array($a_user_id, time())
58 );
59 self::$last_access_time = time();
60 return true;
61 }
62
63 public static function updateAccess(ilObjUser $user): bool
64 {
65 global $DIC;
66
67 $ilDB = $DIC->database();
68 $ilSetting = $DIC->settings();
69
70 if (null === self::$last_access_time) {
71 $query = 'SELECT access_time FROM ut_online WHERE usr_id = ' . $ilDB->quote(
72 $user->getId(),
73 'integer'
74 );
75 $res = $ilDB->query($query);
76 if (!$ilDB->numRows($res)) {
77 return false;
78 }
79 $row = $ilDB->fetchAssoc($res);
80 self::$last_access_time = (int) $row['access_time'];
81 }
82
83 $time_span = (int) $ilSetting->get('tracking_time_span', '300');
84 if (($diff = time() - self::$last_access_time) <= $time_span) {
85 $ilDB->manipulateF(
86 'UPDATE ut_online SET online_time = online_time + %s, access_time = %s WHERE usr_id = %s',
87 array('integer', 'integer', 'integer'),
88 array($diff, time(), $user->getId())
89 );
90 } else {
91 $ilDB->manipulateF(
92 'UPDATE ut_online SET access_time = %s WHERE usr_id = %s',
93 array('integer', 'integer'),
94 array(time(), $user->getId())
95 );
96 }
97 return true;
98 }
99}
User class.
static addUser(int $a_user_id)
static updateAccess(ilObjUser $user)
static getOnlineTime(int $a_user_id)
static int $last_access_time
This static variable is used to prevent two database request (addUser and updateAccess) on login.
global $DIC
Definition: feed.php:28
$res
Definition: ltiservices.php:69
global $ilSetting
Definition: privfeed.php:17
$query