forked from StartupAPI/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregatepoints.php
More file actions
60 lines (54 loc) · 1.55 KB
/
aggregatepoints.php
File metadata and controls
60 lines (54 loc) · 1.55 KB
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
<?php
// This script
require_once(dirname(__FILE__).'/config.php');
function aggregatePoints() {
$db = UserConfig::getDB();
if ($db->query('CREATE TEMPORARY TABLE activity_points (
activity_id int(2) UNSIGNED NOT NULL,
points int(4) UNSIGNED NOT NULL)') === TRUE)
{
$query = 'INSERT INTO activity_points VALUES';
$pairs = array();
foreach (UserConfig::$activities as $id => $activity) {
$pairs[] = "($id, ".$activity[1].')';
}
$query.=' '.implode(', ', $pairs);
if ($db->query($query) === TRUE)
{
if ($db->query('CREATE TEMPORARY TABLE user_activity_points
SELECT u.id AS user_id, SUM(p.points) AS points
FROM '.UserConfig::$mysql_prefix.'users u
INNER JOIN '.UserConfig::$mysql_prefix.'activity a ON u.id = a.user_id
INNER JOIN activity_points p ON a.activity_id = p.activity_id
GROUP BY u.id'))
{
if ($stmt = $db->prepare('UPDATE '.UserConfig::$mysql_prefix.'users u
INNER JOIN user_activity_points up ON u.id = up.user_id
SET u.points = up.points'))
{
if (!$stmt->execute())
{
throw new Exception("Can't execute statement: ".$stmt->error);
}
$stmt->close();
} else {
throw new Exception("Can't prepare statement: ".$db->error);
}
} else {
throw new Exception("Can't prepare statement: ".$db->error);
}
} else {
throw new Exception("Can't prepare statement: ".$db->error);
}
}
else
{
throw new Exception("Can't prepare statement: ".$db->error);
}
}
try
{
aggregatePoints();
} catch (Exception $e){
error_log(var_export($e, true));
}