You are here

function _userpoints_transaction in User Points 6

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

File

./userpoints.module, line 723

Code

function _userpoints_transaction(&$params) {

  //Check, again, for a properly formed array
  if (!is_array($params)) {
    return FALSE;
  }
  $time = time();
  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'] = variable_get(USERPOINTS_CATEGORY_DEFAULT_TID, NULL);
    }
    elseif ($params['tid'] == 0) {

      //tid with 0 are uncategorized and are set to NULL

      //this is a backwards compatibilty 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'] > $time) {
      $params['time_stamp'] = $time;
    }
  }

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

    //A transaction ID was passed in so we'll update the transaction
    $result = db_query("SELECT txn_id, uid, approver_uid, points,\n      time_stamp, status, operation, description, reference, expirydate, expired,\n      parent_txn_id, tid, entity_id, entity_type\n      FROM {userpoints_txn}\n      WHERE txn_id = %d", $params['txn_id']);
    $txn = db_fetch_array($result);

    //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);
    }
  }
  else {

    //Create new transaction record
    $ret = drupal_write_record('userpoints_txn', $params);

    //don't cache entry if it's pending
    if ($params['status'] != TRUE && $ret != FALSE) {
      _userpoints_update_cache($params);
    }
  }
  return TRUE;
}