You are here

function _userpoints_transaction in User Points 7

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

Adds the points to the txn table.

1 call to _userpoints_transaction()
userpoints_userpointsapi in ./userpoints.module
Save userpoints changes and call hooks.

File

./userpoints.module, line 984

Code

function _userpoints_transaction(&$params) {

  // Check, again, for a properly formed array.
  if (!is_array($params)) {
    return FALSE;
  }
  if (!isset($params['txn_id'])) {

    // If a txn_id is preset we UPDATE the record instead of adding one
    // the standard checks don't apply.
    if (!is_numeric($params['points'])) {
      return FALSE;
    }
    if (!isset($params['uid'])) {
      global $user;
      $params['uid'] = $user->uid;

      // There must be a UID, anonymous does not receive points.
      if (!$params['uid'] > 0) {
        return FALSE;
      }
    }
    if (isset($params['expirydate']) && !is_numeric($params['expirydate'])) {
      return FALSE;
    }

    // Check if parameters are set.
    $params_null_check = array(
      'operation',
      'description',
      'reference',
      'expired',
      'parent_txn_id',
      'entity_id',
      'entity_type',
    );
    foreach ($params_null_check as $param_null_check) {
      if (!isset($params[$param_null_check])) {
        $params[$param_null_check] = NULL;
      }
    }
    if (!isset($params['tid']) || !is_numeric($params['tid'])) {
      $params['tid'] = userpoints_get_default_tid();
    }
    elseif ($params['tid'] == 0) {

      // Tid with 0 are uncategorized and are set to NULL
      // this is a backwards compatibility issue.
      $params['tid'] = NULL;
    }
    if (!isset($params['expirydate'])) {
      $params['expirydate'] = userpoints_get_default_expiry_date();
    }

    // Use current time for time_stamp if configured to always use the default,
    // not set, not a positive integer or in the future.
    if (variable_get(USERPOINTS_TRANSACTION_TIMESTAMP, 1) || !isset($params['time_stamp']) || $params['time_stamp'] <= 0 || $params['time_stamp'] > REQUEST_TIME) {
      $params['time_stamp'] = REQUEST_TIME;
    }
  }

  // Always force changed timestamp to current REQUEST_TIME for transaction tracking.
  $params['changed'] = REQUEST_TIME;
  if (!empty($params['txn_id']) && $params['txn_id'] > 0) {

    // A transaction ID was passed in so we'll update the transaction.
    $txn = (array) userpoints_transaction_load($params['txn_id']);
    if (!$txn) {
      return FALSE;
    }

    // Don't superseed existing keys, just complete missing keys.
    $params += $txn;

    // Update existing transaction record for key txn_id.
    $ret = drupal_write_record('userpoints_txn', $params, array(
      'txn_id',
    ));

    // Only update if the record has been successfully updated.
    if ($ret != FALSE) {
      _userpoints_update_cache($params, $txn);
    }
  }
  else {

    // Create new transaction record.
    $ret = drupal_write_record('userpoints_txn', $params);
    if ($ret != FALSE) {
      _userpoints_update_cache($params);
    }
  }
  return TRUE;
}