function _userpoints_update_cache in User Points 7
Same name and namespace in other branches
- 5.3 userpoints.module \_userpoints_update_cache()
- 6 userpoints.module \_userpoints_update_cache()
Update the caching table.
Parameters
$params: Array with the transaction params.
$txn: The original transaction, if this is an update.
2 calls to _userpoints_update_cache()
- MigrateDestinationUserpoints::rollback in ./
userpoints.migrate.inc - Delete the provided userpoint transaction.
- _userpoints_transaction in ./
userpoints.module - Adds the points to the txn table.
File
- ./
userpoints.module, line 1070
Code
function _userpoints_update_cache($txn, $old_txn = NULL) {
// Store eventual updates in this array.
$updates = array();
$totals = array();
if (!$old_txn) {
// For new transactions, only update the cache for fully approved non-expired
// points.
if ($txn['status'] == USERPOINTS_TXN_STATUS_APPROVED && $txn['expired'] != 1) {
// Calculate the current points based upon the tid.
$updates['points'] = $txn['points'] + userpoints_get_current_points($txn['uid'], $txn['tid']);
$totals['points'] = $txn['points'] + userpoints_get_current_points($txn['uid'], 'all');
}
}
else {
// For existing transactions, it is a bit more complex.
// Expired transactions that were expired before can be ignored.
if ($txn['expired'] == 1 && $old_txn['expired'] == 1) {
return;
}
if ($old_txn['tid'] != $txn['tid']) {
// If the category has changed, remove the points of the old transaction
// from the old category.
$remove_points = userpoints_get_current_points($txn['uid'], $old_txn['tid']) - $old_txn['points'];
db_merge('userpoints')
->key(array(
'uid' => $txn['uid'],
'tid' => (int) $old_txn['tid'],
))
->fields(array(
'points' => $remove_points,
))
->execute();
// Subtract the points from the total.
$totals['points'] = userpoints_get_current_points($txn['uid'], 'all') - $old_txn['points'];
if ($txn['status'] == USERPOINTS_TXN_STATUS_APPROVED) {
// Make sure to add the points so that they are added to the new category.
$updates['points'] = userpoints_get_current_points($txn['uid'], $txn['tid']) + $txn['points'];
// Add them to the totals.
$totals['points'] += $txn['points'];
}
}
else {
if ($old_txn['status'] == USERPOINTS_TXN_STATUS_APPROVED && $txn['status'] != USERPOINTS_TXN_STATUS_APPROVED) {
// If the transaction goes from approved to not approved, subtract the
// points to the total.
$updates['points'] = userpoints_get_current_points($txn['uid'], $txn['tid']) - $old_txn['points'];
$totals['points'] = userpoints_get_current_points($txn['uid'], 'all') - $old_txn['points'];
}
else {
if ($txn['points'] != $old_txn['points'] && $old_txn['status'] == USERPOINTS_TXN_STATUS_APPROVED && $txn['status'] == USERPOINTS_TXN_STATUS_APPROVED) {
// If the category did not change but the points and the transaction
// was and still is approved, update the points difference.
$updates['points'] = userpoints_get_current_points($txn['uid'], $txn['tid']) + ($txn['points'] - $old_txn['points']);
$totals['points'] = userpoints_get_current_points($txn['uid'], 'all') + ($txn['points'] - $old_txn['points']);
}
elseif ($old_txn['status'] != USERPOINTS_TXN_STATUS_APPROVED && $txn['status'] == USERPOINTS_TXN_STATUS_APPROVED) {
// Calculate the current points based upon the tid.
$updates['points'] = userpoints_get_current_points($txn['uid'], $txn['tid']) + $txn['points'];
$totals['points'] = userpoints_get_current_points($txn['uid'], 'all') + $txn['points'];
}
}
}
}
if (!empty($updates)) {
$max_points = userpoints_get_max_points($txn['uid'], $txn['tid']);
// If the new points are higher then the maximum, update it.
if ($updates['points'] > $max_points) {
$updates['max_points'] = $updates['points'];
}
$updates['last_update'] = REQUEST_TIME;
// Insert or update the userpoints caching table with the user's current
// points.
db_merge('userpoints')
->key(array(
'uid' => $txn['uid'],
'tid' => (int) $txn['tid'],
))
->fields($updates)
->execute();
}
// Update totals if necessary.
if (!empty($totals)) {
// Update the total max points if necessary.
$max_points_total = userpoints_get_max_points($txn['uid'], 'all');
if ($totals['points'] > $max_points_total) {
$totals['max_points'] = $totals['points'];
}
$totals['last_update'] = REQUEST_TIME;
// Insert or update the userpoints total caching table with the user's current
// points.
db_merge('userpoints_total')
->key(array(
'uid' => $txn['uid'],
))
->fields($totals)
->execute();
}
}