You are here

function userpoints_expire_transactions in User Points 7.2

Same name and namespace in other branches
  1. 5.3 userpoints.module \userpoints_expire_transactions()
  2. 6 userpoints.module \userpoints_expire_transactions()
  3. 7 userpoints.module \userpoints_expire_transactions()

Finds and expires expired points.

Finds all transactions with a expirydate < REQUEST_TIME and posts opposite transactions (sum of 0).

1 call to userpoints_expire_transactions()
userpoints_cron in ./userpoints.module
Implements hook_cron().

File

./userpoints.module, line 1142

Code

function userpoints_expire_transactions() {
  $sql = "SELECT txn_id\n          FROM {userpoints_txn}\n          WHERE status = 0 AND expired = 0\n          AND (expirydate < :expiry_date AND expirydate != 0)";
  $txn_ids = db_query($sql, array(
    ':expiry_date' => REQUEST_TIME,
  ))
    ->fetchCol();
  foreach (userpoints_transaction_load_multiple($txn_ids) as $transaction) {
    $time_stamp_formatted = format_date($transaction->time_stamp, 'custom', 'Y-m-d H:i');
    $arguments = array_merge(userpoints_translation(), array(
      '!operation' => $transaction->operation,
      '!description' => $transaction->description,
      '!txn_id' => $transaction->txn_id,
      '!date' => $time_stamp_formatted,
    ));
    $description = strtr(variable_get(USERPOINTS_EXPIRY_DESCRIPTION, NULL), $arguments);
    userpoints_grant_points('expiry', -$transaction->points, $transaction->type, $transaction->uid)
      ->setDescription($description)
      ->setParent($transaction->txn_id)
      ->setTid($transaction->tid)
      ->setExpiryDate(0)
      ->save();

    // Ok we've expired the entry, now the original transaction needs to be
    // marked as expired.
    $transaction
      ->setExpired(1)
      ->save();
  }
}