aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/ForumVisit.php
blob: 05b4b5a5ac319b1e1c5dbbab03ab3e0941360b39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
 * ForumVisit - Functions for visit-dates for threads
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * @author      Till Glöggler <tgloeggl@uos.de>
 * @license     http://www.gnu.org/licenses/gpl-3.0.html GPL version 3
 * @category    Stud.IP
 */

class ForumVisit {

    /**
     * This is the maximum number of seconds that unread entries are
     * marked as new.
     */
    const LAST_VISIT_MAX = 7776000; // 90 days

    /**
     * return number of new entries since last visit up to 3 month ago
     *
     * @param string $parent_id the seminar_id for the entries
     * @param string $visitdate count all entries newer than this timestamp
     *
     * @return int the number of entries
     */
    static function getCount($parent_id, $visitdate)
    {
        if ($visitdate < time() - ForumVisit::LAST_VISIT_MAX) {
            $visitdate = time() - ForumVisit::LAST_VISIT_MAX;
        }

        $constraints = ForumEntry::getConstraints($parent_id);

        $stmt = DBManager::get()->prepare("SELECT COUNT(*) FROM forum_entries
            WHERE lft >= :lft AND rgt <= :rgt AND user_id != :user_id
                AND seminar_id = :seminar_id
                AND topic_id != seminar_id
                AND chdate > :lastvisit");

        $stmt->bindParam(':user_id', $GLOBALS['user']->id);
        $stmt->bindParam(':lft', $constraints['lft']);
        $stmt->bindParam(':rgt', $constraints['rgt']);
        $stmt->bindParam(':seminar_id', $constraints['seminar_id']);
        $stmt->bindParam(':lastvisit', $visitdate);

        $stmt->execute();

        return $stmt->fetchColumn();
    }

    /**
     * Set the seminar denoted by the passed id as visited by the currently
     * logged in user
     *
     * @param string $seminar_id
     */
    static function setVisit($seminar_id) {
        $type = get_object_type($seminar_id, words('fak inst sem'));
        if ($type === 'fak') {
            $type = 'inst';
        }
        if (self::getVisit($seminar_id) < object_get_visit($seminar_id, $type, false, false)) {
            self::setVisitdates($seminar_id);
        }
    }

    /**
     * Stores the visitdate in last_visitdate and sets the current time for as new visitdate
     *
     * @param string $seminar_id the seminar that has been entered
     */
    static function setVisitdates($seminar_id) {
        $stmt = DBManager::get()->prepare('SELECT visitdate FROM forum_visits
            WHERE user_id = ? AND seminar_id = ?');
        $stmt->execute([$GLOBALS['user']->id, $seminar_id]);
        $visitdate = $stmt->fetchColumn();

        $stmt = DBManager::get()->prepare("REPLACE INTO forum_visits
            (user_id, seminar_id, visitdate, last_visitdate)
            VALUES (?, ?, UNIX_TIMESTAMP(), ?)");
        $stmt->execute([$GLOBALS['user']->id, $seminar_id, $visitdate]);

    }


    /**
     * returns visitdate and last_visitdate for the passed seminar and the
     * currently logged in user
     *
     * @staticvar array $visit
     *
     * @param string $seminar_id the seminar to fetch the visitdates for
     * @return mixed an array containing visitdate and last_visitdate
     */
    private static function getVisitDates($seminar_id)
    {
        static $visit = [];

        // no costly checking for root or nobody necessary
        if ($GLOBALS['perm']->have_perm('root') || $GLOBALS['user']->id == 'nobody') {
            $tstamp = mktime(23, 59, 00, date('m'), 31, date('y'));
            return ['visit' => $tstamp, 'last_visitdate' => $tstamp];
        }

        if (!isset($visit[$seminar_id])) {
            $visit[$seminar_id] = [];
        }
        if (!isset($visit[$seminar_id][$GLOBALS['user']->id])) {
            $stmt = DBManager::get()->prepare("SELECT visitdate, last_visitdate FROM forum_visits
                WHERE seminar_id = ? AND user_id = ?");
            $stmt->execute([$seminar_id, $GLOBALS['user']->id]);
            $visit[$seminar_id][$GLOBALS['user']->id] = $stmt->fetch(PDO::FETCH_ASSOC);

            // no entry for this seminar yet present
            if (!$visit[$seminar_id][$GLOBALS['user']->id]) {
                // set visitdate to current time
                $visit[$seminar_id][$GLOBALS['user']->id] = [
                    'visit'      => time() - ForumVisit::LAST_VISIT_MAX,
                    'last_visitdate' => time() - ForumVisit::LAST_VISIT_MAX
                ];
            }

            // prevent visit-dates from being older than LAST_VISIT_MAX allows
            foreach ($visit[$seminar_id][$GLOBALS['user']->id] as $type => $date) {
                if ($date < time() - ForumVisit::LAST_VISIT_MAX) {
                    $visit[$seminar_id][$GLOBALS['user']->id][$type] = time() - ForumVisit::LAST_VISIT_MAX;
                }
            }
        }

        return $visit[$seminar_id][$GLOBALS['user']->id];
    }

    /**
     * return the last_visitdate for the passed seminar and currently logged in user
     *
     * @param string $seminar_id the seminar to get the last_visitdate for
     * @return int a timestamp
     */
    static function getLastVisit($seminar_id)
    {
        $visit = self::getVisitDates($seminar_id);
        return $visit['last_visitdate'];
    }

    /**
     * return the visitdate for the passed seminar and currently logged in user
     *
     * @param string $seminar_id the seminar to get the visitdate for
     * @return int a timestamp
     */
    static function getVisit($seminar_id)
    {
        $visit = self::getVisitDates($seminar_id);
        return $visit['visitdate'] ?? 0;
    }
}