You are here

function _userpoints_transaction in User Points 5.3

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

File

./userpoints.module, line 576

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['moderate'])) {

      //if not passed then site default is used
      $params['status'] = variable_get(USERPOINTS_POINTS_MODERATION, 0);
    }
    else {
      if ($params['moderate'] == true) {
        $params['status'] = 1;
      }
      else {
        $params['status'] = 0;
      }
    }
    if (!isset($params['operation'])) {
      $params['operation'] = NULL;
    }
    if (!isset($params['description'])) {
      $params['description'] = NULL;
    }
    if (!isset($params['reference'])) {
      $params['reference'] = 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();
    }
    if (!isset($params['expired'])) {
      $params['expired'] = NULL;
    }
    if (!isset($params['parent_txn_id'])) {
      $params['parent_txn_id'] = NULL;
    }
    if (!isset($params['entity_id'])) {
      $params['entity_id'] = NULL;
    }
    if (!isset($params['entity_type'])) {
      $params['entity_type'] = NULL;
    }
  }

  // if txn_id
  if (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);
    foreach ($txn as $key => $value) {
      if (isset($params[$key])) {
        $arr[] = $key . ' = \'' . $params[$key] . '\'';
      }
      else {
        $params[$key] = $value;
      }
    }
    db_query('UPDATE {userpoints_txn} SET ' . implode(', ', $arr) . '
              WHERE txn_id = %d', $params['txn_id']);
    _userpoints_update_cache($params);
  }
  else {
    $ret = db_query("INSERT INTO {userpoints_txn}\n      (uid, points, time_stamp, status, operation, description, \n      reference, expirydate, expired, parent_txn_id, tid, entity_id, entity_type)\n      VALUES (%d, %d, %d, %d, '%s', '%s', '%s', %d, %d, %d, %d, %d, '%s')", $params['uid'], $params['points'], time(), $params['status'], $params['operation'], $params['description'], $params['reference'], $params['expirydate'], $params['expired'], $params['parent_txn_id'], $params['tid'], $params['entity_id'], $params['entity_type']);
    if ($params['status'] != true && $ret != false) {
      _userpoints_update_cache($params);
    }
  }
  return TRUE;
}