You are here

function registration_status in Entity Registration 8

Same name and namespace in other branches
  1. 8.2 registration.module \registration_status()
  2. 7.2 registration.module \registration_status()
  3. 7 registration.module \registration_status()

Check if new registrations are permitted for a host entity.

Modules may implement hook_registration_status_alter() to alter the status at runtime.

Parameters

string $entity_type: The host entity type.

int $entity_id: The host entity ID.

bool $reset: (optional) Whether to force checking status in case registration_status may have been called previously for this host entity.

int $spaces: (optional) The number of spaces to check that there is room for.

int $registration_id: (optional) A registration id to exclude from the has room check.

array $errors: (optional) An array of error message strings.

Return value

bool

6 calls to registration_status()
RegistrationWaitlistTestCase::testHostRegistrationWaitlistStatus in modules/registration_waitlist/registration_waitlist.test
Check internal status modifiers for the wait list.
registration_form_validate in includes/registration.forms.inc
Validation callback for registration_form().
registration_handler_field_entity_registration_status::render in src/registration_handler_field_entity_registration_status.php
registration_handler_field_entity_registration_status::render in modules/registration_views/includes/registration_handler_field_entity_registration_status.inc
registration_register_page in ./registration.module
Page callback: Add a new registration to a host entity.

... See full list

File

./registration.module, line 1204

Code

function registration_status($entity_type, $entity_id, $reset = FALSE, $spaces = 1, $registration_id = NULL, &$errors = array()) {
  $checked =& drupal_static(__FUNCTION__, array());
  if (!$reset && isset($checked[$entity_type][$entity_id])) {
    if (!is_array($checked[$entity_type][$entity_id]['errors'])) {
      $checked[$entity_type][$entity_id]['errors'] = array();
    }
    $errors = is_array($errors) ? array_merge($errors, $checked[$entity_type][$entity_id]['errors']) : $checked[$entity_type][$entity_id]['errors'];
    return $checked[$entity_type][$entity_id]['status'];
  }
  $entity = entity_load_single($entity_type, $entity_id);
  $registration_type = registration_get_entity_registration_type($entity_type, $entity);

  // The host entity does not have registrations enabled.
  if (!$registration_type) {
    return FALSE;
  }
  $settings = registration_entity_settings($entity_type, $entity_id, $reset);
  $status = $settings['status'];
  $open = isset($settings['open']) ? strtotime($settings['open']) : NULL;
  $close = isset($settings['close']) ? strtotime($settings['close']) : NULL;
  $now = REQUEST_TIME;

  // only explore other settings if main status is enabled
  if ($status) {

    // check max allowed spaces per registration
    if (isset($settings['settings']['maximum_spaces']) && $settings['settings']['maximum_spaces'] && $spaces > $settings['settings']['maximum_spaces']) {
      $status = FALSE;
      $errors[] = t('You may not register for more than @count spaces.', array(
        '@count' => $settings['settings']['maximum_spaces'],
      ));
    }

    // check capacity
    if (!registration_has_room($entity_type, $entity_id, $spaces, $registration_id, $reset)) {
      $status = FALSE;
      $errors[] = t('Insufficient spaces remaining.');
    }

    // check open date range
    if (isset($open) && $now < $open) {
      $status = FALSE;
      $errors[] = t('Registration is not yet open.');
    }

    // check close date range
    if (isset($close) && $now >= $close) {
      $status = FALSE;
      $errors[] = t('Registration is closed.');
    }
  }
  else {
    $errors[] = t('Registration is disabled.');
  }

  // Allow other mods to override status.
  $context = array(
    'entity_type' => $entity_type,
    'entity_id' => $entity_id,
    'errors' => &$errors,
  );
  \Drupal::moduleHandler()
    ->alter('registration_status', $status, $context);
  $checked[$entity_type][$entity_id] = array(
    'status' => $status,
    'errors' => $errors,
  );
  return $status;
}