You are here

function regcode_code_consume in Registration codes 7

Same name and namespace in other branches
  1. 8 regcode.module \regcode_code_consume()
  2. 7.2 regcode.module \regcode_code_consume()

Consume a regcode and attribute it to a user.

Parameters

string $regcode: The registration code.

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

Return value

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

3 calls to regcode_code_consume()
regcode_user_insert in ./regcode.module
Implements hook_user_insert().
regcode_voucher_profiletab_form_submit in regcode_voucher/regcode_voucher.module
Submit handler for regcode_voucher_profiletab_form().
regcode_voucher_user_update in regcode_voucher/regcode_voucher.module
Implements hook_user_update().

File

./regcode.module, line 461
Main functionality and hooks of regcode module.

Code

function regcode_code_consume($regcode, $account_id) {
  $code = regcode_code_validate($regcode);

  // Check the code validated, otherwise return the error code.
  if (!is_object($code)) {
    return $code;
  }
  $code->uses++;

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

  // Use the code.
  db_update('regcode')
    ->fields(array(
    'uses' => $code->uses,
    'lastused' => REQUEST_TIME,
    'uid' => $account_id,
    'is_active' => $active,
  ))
    ->condition('rid', $code->rid)
    ->execute();

  // Trigger the regcode_used hook.
  $account = user_load($account_id);
  $account->regcode = $code;
  foreach (module_implements('regcode_used') as $module) {
    $hook = $module . '_regcode_used';
    $hook($code, $account);
  }

  // Notify rules module.
  if (is_object($code) && module_exists('rules')) {
    rules_invoke_event('regcode_used', $account, $code);
  }
  return $code;
}