/
UserActivityPointHandler.class.php
286 lines (254 loc) · 9.32 KB
/
UserActivityPointHandler.class.php
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
namespace wcf\system\user\activity\point;
use wcf\data\object\type\ObjectType;
use wcf\data\object\type\ObjectTypeCache;
use wcf\data\user\UserProfileAction;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\exception\InvalidObjectTypeException;
use wcf\system\exception\SystemException;
use wcf\system\SingletonFactory;
use wcf\system\user\group\assignment\UserGroupAssignmentHandler;
use wcf\system\WCF;
/**
* Handles the user activity point events
*
* @author Tim Duesterhus, Alexander Ebert, Matthias Schmidt
* @copyright 2001-2019 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*/
class UserActivityPointHandler extends SingletonFactory
{
/**
* list of user activity point object types
* @var ObjectType[]
*/
protected $objectTypes = [];
/**
* maps the user activity point object type ids to their object type names
* @var string[]
*/
protected $objectTypeNames = [];
/**
* @inheritDoc
*/
protected function init()
{
$this->objectTypes = ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.user.activityPointEvent');
foreach ($this->objectTypes as $objectType) {
$this->objectTypeNames[$objectType->objectTypeID] = $objectType->objectType;
}
}
/**
* Adds a new user activity point event.
*
* @param string $objectType
* @param int $objectID
* @param int $userID
* @param mixed[] $additionalData
* @throws InvalidObjectTypeException
* @throws SystemException
*/
public function fireEvent($objectType, $objectID, $userID = null, array $additionalData = [])
{
$objectTypeObj = $this->getObjectTypeByName($objectType);
if ($objectTypeObj === null) {
throw new InvalidObjectTypeException($objectType, 'com.woltlab.wcf.user.activityPointEvent');
}
if ($userID === null) {
$userID = WCF::getUser()->userID;
}
if (!$userID) {
throw new SystemException("Cannot fire user activity point events for guests");
}
// update user_activity_point
$sql = "INSERT INTO wcf" . WCF_N . "_user_activity_point
(userID, objectTypeID, activityPoints, items)
VALUES (?, ?, ?, 1)
ON DUPLICATE KEY UPDATE activityPoints = activityPoints + VALUES(activityPoints),
items = items + 1";
$statement = WCF::getDB()->prepareStatement($sql);
$statement->execute([
$userID,
$objectTypeObj->objectTypeID,
$objectTypeObj->points,
]);
$sql = "UPDATE wcf" . WCF_N . "_user
SET activityPoints = activityPoints + ?
WHERE userID = ?";
$statement = WCF::getDB()->prepareStatement($sql);
$statement->execute([
$objectTypeObj->points,
$userID,
]);
// update user ranks
$this->updateUserRanks([$userID]);
// check if the user will be automatically added to new user groups
// because of the new activity points
UserGroupAssignmentHandler::getInstance()->checkUsers([$userID]);
}
/**
* Bulk import for user activity point events.
*
* structure of $itemsToUser:
* array(
* userID => countOfItems
* )
*
* @param string $objectType
* @param int[] $itemsToUser
* @param bool $updateUsers
* @throws InvalidObjectTypeException
*/
public function fireEvents($objectType, array $itemsToUser, $updateUsers = true)
{
$objectTypeObj = $this->getObjectTypeByName($objectType);
if ($objectTypeObj === null) {
throw new InvalidObjectTypeException($objectType, 'com.woltlab.wcf.user.activityPointEvent');
}
if (empty($itemsToUser)) {
return;
}
// update user_activity_point
$values = '';
$parameters = $userIDs = [];
foreach ($itemsToUser as $userID => $items) {
if (!empty($values)) {
$values .= ',';
}
$values .= '(?, ?, ?, ?)';
$parameters[] = $userID;
$parameters[] = $objectTypeObj->objectTypeID;
$parameters[] = $items * $objectTypeObj->points;
$parameters[] = $items;
$userIDs[] = $userID;
}
$sql = "INSERT INTO wcf" . WCF_N . "_user_activity_point
(userID, objectTypeID, activityPoints, items)
VALUES " . $values . "
ON DUPLICATE KEY UPDATE activityPoints = activityPoints + VALUES(activityPoints),
items = items + VALUES(items)";
$statement = WCF::getDB()->prepareStatement($sql);
$statement->execute($parameters);
// update activity points for given user ids
if ($updateUsers) {
$this->updateUsers($userIDs);
// check if one of the user will be automatically added
// to new user groups because of the new activity points
UserGroupAssignmentHandler::getInstance()->checkUsers($userIDs);
}
}
/**
* Removes activity point events.
*
* @param string $objectType
* @param int[] $userToItems
* @throws InvalidObjectTypeException
*/
public function removeEvents($objectType, array $userToItems)
{
if (empty($userToItems)) {
return;
}
// get and validate object type
$objectTypeObj = $this->getObjectTypeByName($objectType);
if ($objectTypeObj === null) {
throw new InvalidObjectTypeException($objectType, 'com.woltlab.wcf.user.activityPointEvent');
}
// remove activity points
$sql = "UPDATE wcf" . WCF_N . "_user_activity_point
SET activityPoints = activityPoints - ?,
items = items - ?
WHERE objectTypeID = ?
AND userID = ?";
$statement = WCF::getDB()->prepareStatement($sql);
foreach ($userToItems as $userID => $items) {
$statement->execute([
$items * $objectTypeObj->points,
$items,
$objectTypeObj->objectTypeID,
$userID,
]);
}
// update total activity points per user
$userIDs = \array_keys($userToItems);
$this->updateUsers($userIDs);
}
/**
* Updates total activity points and ranks for given user ids.
*
* @param int[] $userIDs
*/
public function updateUsers(array $userIDs)
{
$userIDs = \array_unique($userIDs);
$conditions = new PreparedStatementConditionBuilder();
$conditions->add("userID IN (?)", [$userIDs]);
$sql = "UPDATE wcf" . WCF_N . "_user user_table
SET activityPoints = COALESCE((
SELECT SUM(activityPoints) AS activityPoints
FROM wcf" . WCF_N . "_user_activity_point
WHERE userID = user_table.userID
GROUP BY userID
), 0)
" . $conditions;
$statement = WCF::getDB()->prepareStatement($sql);
$statement->execute($conditions->getParameters());
// update user ranks
$this->updateUserRanks($userIDs);
}
/**
* Resets activity points and items for a given object type.
*
* @param string $objectType
* @throws InvalidObjectTypeException
*/
public function reset($objectType)
{
// get and validate object type
$objectTypeObj = $this->getObjectTypeByName($objectType);
if ($objectTypeObj === null) {
throw new InvalidObjectTypeException($objectType, 'com.woltlab.wcf.user.activityPointEvent');
}
$sql = "UPDATE wcf" . WCF_N . "_user_activity_point
SET activityPoints = 0,
items = 0
WHERE objectTypeID = ?";
$statement = WCF::getDB()->prepareStatement($sql);
$statement->execute([$objectTypeObj->objectTypeID]);
}
/**
* Returns the user activity point event object type with the given id or
* null if no such object type exists.
*
* @param int $objectTypeID
* @return ObjectType|null
*/
public function getObjectType($objectTypeID)
{
if (isset($this->objectTypeNames[$objectTypeID])) {
return $this->getObjectTypeByName($this->objectTypeNames[$objectTypeID]);
}
return null;
}
/**
* Returns the user activity point event object type with the given name
* or null if no such object type exists.
*
* @param string $objectType
* @return ObjectType|null
*/
public function getObjectTypeByName($objectType)
{
return $this->objectTypes[$objectType] ?? null;
}
/**
* Updates the user ranks for the given users.
*
* @param int[] $userIDs
*/
protected function updateUserRanks(array $userIDs)
{
$action = new UserProfileAction($userIDs, 'updateUserRank');
$action->executeAction();
}
}