You are here

function _userpoints_regenerate_counts_uid in User Merge 7.2

Regenerates the points tables based off of the userpoints_txn table, and the $uid argument, so it doesn't do the whole table as userpoints_regenerate_counts() does.

1 call to _userpoints_regenerate_counts_uid()
userpoints_usermerge_merge_accounts in includes/userpoints.usermerge.inc
Implements hook_usermerge_merge_accounts() on behalf of userpoints.

File

includes/userpoints.usermerge.inc, line 105
Adds support for User Points. Supplemental include loaded via usermerge_load_includes().

Code

function _userpoints_regenerate_counts_uid($uid) {
  $userpoints_total_query = "SELECT uid, MAX(changed) AS last_updated,\n    SUM(points) AS points, SUM(points) AS max_points\n      FROM {userpoints_txn}\n      WHERE uid = :uid\n      GROUP BY uid";
  $results = db_query($userpoints_total_query, array(
    ':uid' => $uid,
  ))
    ->fetchAll();

  // Handle case where the user no longer has any transactions.
  if (0 == count($results)) {
    db_delete('userpoints_total')
      ->condition('uid', $uid)
      ->execute();
  }
  $userpoints_total_replace_query = "REPLACE INTO {userpoints_total} (uid, points, max_points, last_update)\n    VALUES (:uid, :points, :max_points, :last_update)";
  foreach ($results as $row) {
    $res = db_query($userpoints_total_replace_query, array(
      ':uid' => $row->uid,
      ':points' => $row->points,
      ':max_points' => $row->max_points,
      ':last_update' => $row->last_updated,
    ));
  }
  $userpoints_query = "SELECT uid, MAX(changed) AS last_updated,\n    SUM(points) AS points, SUM(points) AS max_points, tid\n      FROM userpoints_txn\n      WHERE uid = :uid\n      GROUP BY uid, tid";
  $results = db_query($userpoints_query, array(
    ':uid' => $uid,
  ))
    ->fetchAll();

  // Handle case where the user no longer has any transactions.
  if (0 == count($results)) {
    db_delete('userpoints')
      ->condition('uid', $uid)
      ->execute();
  }
  $userpoints_replace_query = "REPLACE INTO {userpoints} (uid, points, max_points, last_update, tid)\n    VALUES (:uid, :points, :max_points, :last_update, :tid)";
  foreach ($results as $row) {
    $res = db_query($userpoints_replace_query, array(
      ':uid' => $row->uid,
      ':points' => $row->points,
      ':max_points' => $row->max_points,
      ':last_update' => $row->last_updated,
      ':tid' => $row->tid,
    ));
  }
}