ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilOnlineTracking.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=0);
28 {
32  protected static ?int $last_access_time = null;
33 
34  public static function getOnlineTime(int $a_user_id): int
35  {
36  global $DIC;
37 
38  $ilDB = $DIC->database();
39  $res = $ilDB->query(
40  'SELECT online_time FROM ut_online WHERE usr_id = ' . $ilDB->quote(
41  $a_user_id,
42  'integer'
43  )
44  );
45  while ($row = $ilDB->fetchAssoc($res)) {
46  return (int) $row['online_time'];
47  }
48  return 0;
49  }
50 
51  public static function addUser(int $a_user_id): bool
52  {
53  global $DIC;
54 
55  $ilDB = $DIC->database();
56  $res = $ilDB->query(
57  'SELECT access_time FROM ut_online WHERE usr_id = ' . $ilDB->quote(
58  $a_user_id,
59  'integer'
60  )
61  );
62  if ($ilDB->numRows($res)) {
63  $row = $ilDB->fetchAssoc($res);
64  self::$last_access_time = (int) $row['access_time'];
65  return false;
66  }
67 
68  $ilDB->manipulateF(
69  'INSERT INTO ut_online (usr_id, access_time) VALUES (%s, %s)',
70  array('integer', 'integer'),
71  array($a_user_id, time())
72  );
73  self::$last_access_time = time();
74  return true;
75  }
76 
77  public static function updateAccess(ilObjUser $user): bool
78  {
79  global $DIC;
80 
81  $ilDB = $DIC->database();
82  $ilSetting = $DIC->settings();
83 
84  if (null === self::$last_access_time) {
85  $query = 'SELECT access_time FROM ut_online WHERE usr_id = ' . $ilDB->quote(
86  $user->getId(),
87  'integer'
88  );
89  $res = $ilDB->query($query);
90  if (!$ilDB->numRows($res)) {
91  return false;
92  }
93  $row = $ilDB->fetchAssoc($res);
94  self::$last_access_time = (int) $row['access_time'];
95  }
96 
97  $time_span = (int) $ilSetting->get('tracking_time_span', '300');
98  if (($diff = time() - self::$last_access_time) <= $time_span) {
99  $ilDB->manipulateF(
100  'UPDATE ut_online SET online_time = online_time + %s, access_time = %s WHERE usr_id = %s',
101  array('integer', 'integer', 'integer'),
102  array($diff, time(), $user->getId())
103  );
104  } else {
105  $ilDB->manipulateF(
106  'UPDATE ut_online SET access_time = %s WHERE usr_id = %s',
107  array('integer', 'integer'),
108  array(time(), $user->getId())
109  );
110  }
111  return true;
112  }
113 }
$res
Definition: ltiservices.php:66
static updateAccess(ilObjUser $user)
static int $last_access_time
This static variable is used to prevent two database request (addUser and updateAccess) on login...
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static addUser(int $a_user_id)
static getOnlineTime(int $a_user_id)
global $DIC
Definition: shib_login.php:22
global $ilSetting
Definition: privfeed.php:31