You are here

function regcode_code_validate in Registration codes 8

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

Validates a regcode.

Parameters

string $regcode: The regcode alphanumeric code.

Return value

bool|int|object An error code, or the loaded regcode.

2 calls to regcode_code_validate()
regcode_code_consume in ./regcode.module
Consumes a regcode and attribute it to a user.
regcode_code_element_validate in ./regcode.module
Validates the content of the code-field on user registration.

File

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

Code

function regcode_code_validate($regcode) {

  // Load the code.
  $code = regcode_load_single(NULL, [
    '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 > \Drupal::time()
    ->getRequestTime()) {
    return REGCODE_VALIDITY_NOTAVAILABLE;
  }
  if (!empty($code->expires) && $code->expires < \Drupal::time()
    ->getRequestTime()) {
    return REGCODE_VALIDITY_EXPIRED;
  }
  return $code;
}