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
|
<?php
class RecalculateScore extends Migration {
function description() {
return 'Recalculates the score for all users that have their score published.';
}
function up() {
try {
$statement = DBManager::get()->prepare("
ALTER TABLE message ADD INDEX autor_id (autor_id)
");
$statement->execute();
} catch (PDOException $e) {}
$statement = DBManager::get()->prepare("
SELECT user_id FROM user_info WHERE score > 0
");
$statement->execute();
while ($user_id = $statement->fetch(PDO::FETCH_COLUMN, 0)) {
self::getScore($user_id);
}
}
function down() {
}
/**
* Retrieves a user's score by aggregating activities from database
* and writes the result to database and cache.
*
* @param string $user_id the user to calculate the score for
* @return int The given user's score.
*/
private static function getScore($user_id)
{
$user_id || $user_id = $GLOBALS['user']->id;
$cache = \Studip\Cache\Factory::getCache();
if ($cache->read("user_score_of_".$user_id)) {
return $cache->read("user_score_of_".$user_id);
}
//Behold! The all new mighty score algorithm!
//Step 1: Select all activities as mkdate-timestamps.
//Step 2: Group these activities to timeslots of halfhours
// with COUNT(*) as a weigh of the timeslot.
//Step 3: Calculate the measurement of the timeslot from the weigh of it.
// This makes the first activity count fully, the second
// almost half and so on. We use log_n to make huge amounts of
// activities to not count so much.
//Step 4: Calculate a single score for each timeslot depending on the
// measurement and the mkdate-timestamp. Use arctan as the function
// here so that older activities tend to zero.
//Step 5: Sum all scores from all timeslots together.
$sql = "SELECT ROUND(SUM((-atan(measurement / " . round(31556926 / 1800) . ") / PI() + 0.5) * 200)) AS score
FROM (
SELECT ((UNIX_TIMESTAMP() / 1800) - timeslot) / (LN(weigh) + 1) AS measurement
FROM (
SELECT (round(mkdate / 1800)) as timeslot, COUNT(*) AS weigh
FROM (" . self::createTimestampQuery() . ") AS mkdates
GROUP BY timeslot
) AS measurements
) AS dates";
$stmt = DBManager::get()->prepare($sql);
$stmt->execute([':user' => $user_id]);
$score = $stmt->fetchColumn();
$query = "UPDATE user_info SET score = ? WHERE user_id = ? AND score > 0";
$statement = DBManager::get()->prepare($query);
$statement->execute([$score, $user_id]);
$cache->write("user_score_of_".$user_id, $score, 60 * 5);
return $score;
}
private static function createTimestampQuery()
{
$statements = [];
foreach (self::getActivityTables() as $table) {
$statements[] = "SELECT "
. ($table['date_column'] ? : 'mkdate')
. " AS mkdate FROM "
. $table['table']
. " WHERE "
. ($table['user_id_column'] ? : 'user_id')
. " = :user "
. ($table['where'] ? (' AND ' . $table['where']) : '');
}
return join(' UNION ', $statements);
}
private static function getActivityTables()
{
$tables = [];
$tables[] = ['table' => "user_info"];
$tables[] = ['table' => "comments"];
$tables[] = ['table' => "dokumente"];
$tables[] = ['table' => "forum_entries"];
$tables[] = ['table' => "news"];
$tables[] = ['table' => "seminar_user"];
$tables[] = [
'table' => "blubber",
'where' => "context_type != 'private'"
];
$tables[] = [
'table' => "kategorien",
'user_id_column' => "range_id"
];
$tables[] = [
'table' => "message",
'user_id_column' => "autor_id"
];
$tables[] = [
'table' => "vote",
'user_id_column' => "range_id"
];
$tables[] = [
'table' => "voteanswers_user",
'date_column' => "votedate"
];
$tables[] = [
'table' => "vote_user",
'date_column' => "votedate"
];
$tables[] = [
'table' => "wiki",
'date_column' => "chdate"
];
foreach (PluginManager::getInstance()->getPlugins(ScorePlugin::class) as $plugin) {
foreach ((array) $plugin->getPluginActivityTables() as $table) {
if ($table['table']) {
$tables[] = $table;
}
}
}
return $tables;
}
}
|