You are here

function registration_status in Entity Registration 7.2

Same name and namespace in other branches
  1. 8.2 registration.module \registration_status()
  2. 8 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

8 calls to registration_status()
RegistrationAPITestCase::testHookStatus in tests/registration.test
Test hook_registration_status().
RegistrationStandardTestCase::testHostRegistrationStatus in tests/registration.test
Check internal status modifiers.
RegistrationWaitlistTestCase::testHostRegistrationWaitlistStatus in modules/registration_waitlist/registration_waitlist.test
Check internal status modifiers for the wait list.
registration_field_formatter_view in includes/registration.field.inc
Implements hook_field_formatter_view().
registration_form_validate in includes/registration.forms.inc
Validation callback for registration_form().

... See full list

File

./registration.module, line 1283

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);
  registration_interpret_settings($settings, $entity_type, $entity);
  $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(REGISTRATION_INSUFFICIENT_SPACES_MESSAGE);
    }

    // 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,
    'spaces' => $spaces,
    'registration_id' => $registration_id,
    'errors' => &$errors,
  );
  drupal_alter('registration_status', $status, $context);
  $checked[$entity_type][$entity_id] = array(
    'status' => $status,
    'errors' => $errors,
  );
  return $status;
}