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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
<?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);
if (!$constraints) {
return 0;
}
$query = "SELECT COUNT(*)
FROM forum_entries
WHERE lft >= :lft
AND rgt <= :rgt
AND user_id != :user_id
AND (user_id != '' OR author != '')
AND seminar_id = :seminar_id
AND topic_id != seminar_id
AND chdate > :lastvisit";
$stmt = DBManager::get()->prepare($query);
$stmt->bindValue(':user_id', $GLOBALS['user']->id);
$stmt->bindValue(':lft', $constraints['lft']);
$stmt->bindValue(':rgt', $constraints['rgt']);
$stmt->bindValue(':seminar_id', $constraints['seminar_id']);
$stmt->bindValue(':lastvisit', $visitdate);
$stmt->execute();
return $stmt->fetchColumn();
}
public static function getCounts(array $topic_ids, string $user_id, int $plugin_id)
{
if (count($topic_ids) === 0) {
return false;
}
$entries = DBManager::get()->fetchAll(
"SELECT forum_entries.seminar_id, COUNT(*) as count
FROM forum_entries
LEFT JOIN object_user_visits AS ouv
ON ouv.object_id = forum_entries.seminar_id
AND ouv.user_id = :user_id
AND ouv.plugin_id = :plugin_id
WHERE forum_entries.user_id != :user_id
AND forum_entries.seminar_id IN (:seminar_ids)
AND forum_entries.topic_id != forum_entries.seminar_id
AND forum_entries.chdate > IF(
IF(ouv.visitdate > :threshold, ouv.visitdate, :threshold) < :forum_visit_max,
:forum_visit_max,
IF(ouv.visitdate > :threshold, ouv.visitdate, :threshold)
)
GROUP BY forum_entries.seminar_id",
[
':user_id' => $user_id,
':seminar_ids' => $topic_ids,
':plugin_id' => $plugin_id,
':forum_visit_max' => time() - ForumVisit::LAST_VISIT_MAX,
':threshold' => object_get_visit_threshold()
]
);
$counts = array_fill_keys($topic_ids, 0);
foreach ($entries as $entry) {
$counts[$entry['seminar_id']] = $entry['count'];
}
return $counts;
}
/**
* 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;
}
}
|