You are here

function userpoints_update_7004 in User Points 7.2

Same name and namespace in other branches
  1. 7 userpoints.install \userpoints_update_7004()

Filling the {userpoints_total} table.

File

./userpoints.install, line 385
Install time hook userpoints module.

Code

function userpoints_update_7004(&$sandbox) {

  // First run, initialize sandbox and check if we are ready to run.
  if (!isset($sandbox['current_uid'])) {
    $sandbox['current_uid'] = 0;

    // Assume that the uid's are distributed more or less equally over the
    // whole data set. This allows us to calculate the approximate progress.
    $sandbox['max'] = db_query('SELECT MAX(uid) FROM {userpoints}')
      ->fetchField();
  }

  // Fetch the next 10 thread_ids.
  $result = db_query_range('SELECT uid, SUM(points) AS points, SUM(max_points) AS max_points, MAX(last_update) AS last_update FROM {userpoints} WHERE uid > :uid GROUP BY uid ORDER BY uid ASC', 0, 10, array(
    ':uid' => $sandbox['current_uid'],
  ), array(
    'fetch' => PDO::FETCH_ASSOC,
  ));
  $insert = db_insert('userpoints_total')
    ->fields(array(
    'uid',
    'points',
    'max_points',
    'last_update',
  ));
  $last_uid = 0;
  foreach ($result as $row) {
    $insert
      ->values($row);
    $last_uid = $row['uid'];
  }
  $insert
    ->execute();
  $sandbox['current_uid'] = $last_uid;

  // Set #finished based on sandbox.
  $sandbox['#finished'] = empty($sandbox['max']) || $last_uid == 0 ? 1 : $sandbox['current_uid'] / $sandbox['max'];
}