Permalink
Browse files

Moved activity point aggregation code into User class

  • Loading branch information...
1 parent 0653648 commit 6355e87f91becdd2c2d3e27ed78a1ffbf12e5c62 @sergeychernyshev sergeychernyshev committed May 7, 2016
Showing with 57 additions and 60 deletions.
  1. +1 −60 aggregatepoints.php
  2. +56 −0 classes/User.php
View
@@ -11,68 +11,9 @@
*/
require_once(__DIR__.'/global.php');
-/**
- * Aggregates activity points for users, can be ran as cron job
- * on a daily basis or more often if needed.
- *
- * @package StartupAPI
- * @subpackage Analytics
- *
- * @throws DBException
- */
-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 u_users u
- INNER JOIN u_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 u_users u
- INNER JOIN user_activity_points up ON u.id = up.user_id
- SET u.points = up.points'))
- {
- if (!$stmt->execute())
- {
- throw new DBExecuteStmtException($db, $stmt);
- }
-
- $stmt->close();
- } else {
- throw new DBException($db);
- }
-
- } else {
- throw new DBException($db);
- }
- } else {
- throw new DBException($db);
- }
- }
- else
- {
- throw new DBException($db);
- }
-}
-
try
{
- aggregatePoints();
+ User::aggregateActivityPoints();
} catch (Exception $e){
error_log(var_export($e, true));
}
View
@@ -1204,6 +1204,62 @@ public static function getDailyActivityPoints(User $user = null) {
}
/**
+ * Aggregates activity points for users, should be ran within cron job
+ * on a daily basis or more often if needed.
+ *
+ * @throws DBException
+ */
+ public static function aggregateActivityPoints() {
+ $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 u_users u
+ INNER JOIN u_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 u_users u
+ INNER JOIN user_activity_points up ON u.id = up.user_id
+ SET u.points = up.points'))
+ {
+ if (!$stmt->execute())
+ {
+ throw new DBExecuteStmtException($db, $stmt);
+ }
+
+ $stmt->close();
+ } else {
+ throw new DBException($db);
+ }
+
+ } else {
+ throw new DBException($db);
+ }
+ } else {
+ throw new DBException($db);
+ }
+ }
+ else
+ {
+ throw new DBException($db);
+ }
+ }
+
+ /**
* Returns a number of users registered for each date
*
* @return array Array of registration numbers for each date

0 comments on commit 6355e87

Please sign in to comment.