1<?php
2/* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Class ilOnlineTracking
6 * @author  Stefan Meyer <meyer@leifos.com>
7 * @version $Id$
8 * @package ilias-core
9 *          Stores total online time of users
10 */
11class ilOnlineTracking
12{
13    /**
14     * This static variable is used to prevent two database request (addUser and updateAccess) on login
15     * @var int
16     * @static
17     */
18    protected static $last_access_time = null;
19
20    /**
21     * @static
22     * @param int $a_user_id
23     * @return int
24     */
25    public static function getOnlineTime($a_user_id)
26    {
27        /**
28         * @var $ilDB ilDB
29         */
30        global $DIC;
31
32        $ilDB = $DIC['ilDB'];
33
34        $res = $ilDB->query('SELECT online_time FROM ut_online WHERE usr_id = ' . $ilDB->quote($a_user_id, 'integer'));
35        while ($row = $ilDB->fetchAssoc($res)) {
36            return (int) $row['online_time'];
37        }
38
39        return 0;
40    }
41
42    /**
43     * Add access time
44     * @param int $a_user_id
45     * @return bool
46     * @static
47     */
48    public static function addUser($a_user_id)
49    {
50        /**
51         * @var $ilDB ilDB
52         */
53        global $DIC;
54
55        $ilDB = $DIC['ilDB'];
56
57        $res = $ilDB->query('SELECT access_time FROM ut_online WHERE usr_id = ' . $ilDB->quote($a_user_id, 'integer'));
58        if ($ilDB->numRows($res)) {
59            $row = $ilDB->fetchAssoc($res);
60            self::$last_access_time = (int) $row['access_time'];
61            return false;
62        }
63
64        $ilDB->manipulateF(
65            'INSERT INTO ut_online (usr_id, access_time) VALUES (%s, %s)',
66            array('integer', 'integer'),
67            array($a_user_id, time())
68        );
69        self::$last_access_time = time();
70
71        return true;
72    }
73
74    /**
75     * Update access time
76     * @param ilObjUser $user
77     * @return bool
78     * @static
79     */
80    public static function updateAccess(ilObjUser $user)
81    {
82        /**
83         * @var $ilDB      ilDB
84         * @var $ilSetting ilSetting
85         */
86        global $DIC;
87
88        $ilDB = $DIC['ilDB'];
89        $ilSetting = $DIC['ilSetting'];
90
91        if (null === self::$last_access_time) {
92            $query = 'SELECT access_time FROM ut_online WHERE usr_id = ' . $ilDB->quote($user->getId(), 'integer');
93            $res = $ilDB->query($query);
94            if (!$ilDB->numRows($res)) {
95                return false;
96            }
97            $row = $ilDB->fetchAssoc($res);
98            self::$last_access_time = $row['access_time'];
99        }
100
101        $time_span = (int) $ilSetting->get('tracking_time_span', 300);
102        if (($diff = time() - self::$last_access_time) <= $time_span) {
103            $ilDB->manipulateF(
104                'UPDATE ut_online SET online_time = online_time + %s, access_time = %s WHERE usr_id = %s',
105                array('integer', 'integer', 'integer'),
106                array($diff, time(), $user->getId())
107            );
108        } else {
109            $ilDB->manipulateF(
110                'UPDATE ut_online SET access_time = %s WHERE usr_id = %s',
111                array('integer', 'integer'),
112                array(time(), $user->getId())
113            );
114        }
115        return true;
116    }
117}
118