You are here

function _userpoints_update_cache in User Points 6

Same name and namespace in other branches
  1. 5.3 userpoints.module \_userpoints_update_cache()
  2. 7 userpoints.module \_userpoints_update_cache()
1 call to _userpoints_update_cache()
_userpoints_transaction in ./userpoints.module

File

./userpoints.module, line 809

Code

function _userpoints_update_cache(&$params) {
  if ($params['status'] != 0 || $params['expired'] == 1) {

    //Only update the cache for fully approved non-expired points
    return FALSE;
  }
  if (!isset($params['tid'])) {
    $params['tid'] = NULL;
  }

  // Calculate the current points based upon the tid
  $current_points = (int) $params['points'] + userpoints_get_current_points($params['uid'], $params['tid']);

  //Grab the user's maximum points to preserve it
  $max_points = db_result(db_query('SELECT max_points FROM {userpoints} WHERE uid = %d AND tid = %d', $params['uid'], $params['tid']));
  if ($params['points'] > 0) {

    //points are greater than zero, update their max_points
    $max_points = (int) $params['points'] + (int) $max_points;
  }

  // insert or update the userpoints caching table with the user's current points
  if (_userpoints_user_exists($params['uid'], $params['tid'])) {
    db_query("UPDATE {userpoints}\n              SET points = %d, max_points = %d, last_update = %d\n              WHERE uid = %d AND tid = %d", $current_points, $max_points, time(), $params['uid'], $params['tid']);
  }
  else {
    $result = db_query("INSERT INTO {userpoints}\n      (uid, points, max_points, last_update, tid)\n      VALUES (%d, %d, %d, %d, %d )", $params['uid'], $current_points, $max_points, time(), $params['tid']);
  }
  unset($params);
}