You are here

function regcode_use in Registration codes 6.2

Validate the use of a code, or use the code and attribute it to a user.

This is a raw API method for low level usage. It does not call the regcode_used hook, or interact with the user account or the rules module.

See the regcode_use_helper function.

Parameters

$code string The registration code:

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

Return value

An error code, or TRUE if the code was assigned successfully.

2 calls to regcode_use()
regcode_user in ./regcode.module
Implementation of hook_user().
regcode_use_helper in ./regcode.module
Validate the use of a code, or use the code and attribute it to a user and call the hooks.

File

./regcode.module, line 401

Code

function regcode_use($regcode, $account_id = FALSE) {

  // Load the code
  $code = regcode_load(array(
    'code' => trim($regcode),
  ));

  // Check validity
  if ($code === FALSE) {
    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) && $code->begins > time()) {
    return REGCODE_VALIDITY_NOTAVAILABLE;
  }
  if (!empty($code->expires) && $code->expires < time()) {
    return REGCODE_VALIDITY_EXPIRED;
  }

  // Use the code if an account has been provided
  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=%d, uid=%d, is_active=%d WHERE rid=%d", time(), $account_id, $active, $code->rid);
  }
  return $code;
}