You are here

function regcode_get_code in Registration codes 6

Same name and namespace in other branches
  1. 5.3 regcode_api.inc.php \regcode_get_code()

Retrieve a particular registration code identified by given code id, or assign it to a given user

Parameters

$code: The code identifiert string

$account_id: Optional user id to assign the given code to

Return value

The data array of the code to retrieve, or a boolean result (TRUE=successfully assigned to given uid) if assign_to_uid has been given.

1 call to regcode_get_code()
regcode_user in ./regcode.module
Implementation of hook_user().

File

./regcode.api.php, line 45
A generic set of functions for interacting with and creating regcodes

Code

function regcode_get_code($code, $account_id = FALSE) {

  // Look up and retrieve code record from DB
  $code = db_fetch_array(db_query("SELECT * FROM {regcode} WHERE code = '%s'", $code));

  // Check validity
  if (!is_array($code)) {
    return REGCODE_VALIDITY_NOTEXISTING;
  }
  if ($code['uses'] >= $code['maxuses'] && $code['maxuses'] !== '0') {
    return REGCODE_VALIDITY_TAKEN;
  }
  if (!$code['is_active']) {
    return REGCODE_VALIDITY_NOTAVAILABLE;
  }
  if (!empty($code['begins']) && strtotime($code['begins']) > time()) {
    return REGCODE_VALIDITY_NOTAVAILABLE;
  }
  if (!empty($code['expires']) && strtotime($code['expires']) < time()) {
    return REGCODE_VALIDITY_EXPIRED;
  }
  if ($account_id) {

    // Mark the code inactive if it's used up
    $active = 1;
    if ($code['maxuses'] != 0 && $code['uses'] + 1 >= $code['maxuses']) {
      $active = 0;
    }

    // Use the code
    db_query("UPDATE {regcode} SET uses=uses+1, lastused=NOW(), uid=%d, is_active=%d WHERE code='%s'", $account_id, $active, $code['code']);
  }
  return $code;
}