You are here

function registration_cron in Entity Registration 7.2

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

Implements hook_cron().

File

./registration.module, line 1203

Code

function registration_cron() {

  //@TODO: need to have a sensible batch limit, passed in as a limit param

  // Send reminder emails.
  // Grab all registrations that have reminders set for this day.
  $results = db_select('registration_entity', 're')
    ->fields('re')
    ->condition('send_reminder', 1)
    ->condition('reminder_date', date('Y-m-d G:i:s'), '<=')
    ->condition('reminder_date', date('Y-m-d G:i:s', strtotime('-2 days')), '>')
    ->range(0, 10)
    ->addTag('registration_cron_select')
    ->execute()
    ->fetchAllAssoc('entity_id');
  foreach ($results as $result) {
    $entity = entity_load_single($result->entity_type, $result->entity_id);
    $message = $result->reminder_template;
    $subject = t('Reminder for !label', array(
      '!label' => entity_label($result->entity_type, $entity),
    ));
    registration_send_broadcast($result->entity_type, $result->entity_id, $subject, $message);

    // Set reminder flag to off.
    db_update('registration_entity')
      ->fields(array(
      'send_reminder' => 0,
    ))
      ->condition('entity_id', $result->entity_id)
      ->condition('entity_type', $result->entity_type)
      ->execute();
  }

  // Remove any registrations from held state that qualify.
  $held_states = registration_get_held_states();
  if (!empty($held_states)) {
    $results = db_select('registration', 'r')
      ->fields('r')
      ->condition('state', $held_states, 'IN')
      ->execute()
      ->fetchAllAssoc('registration_id');
    $registrations = registration_load_multiple(array_keys($results));
    foreach ($registrations as $registration) {
      $registration_wrapper = entity_metadata_wrapper('registration', $registration);

      // Determine whether hold expire minimum time time has elapsed.
      // Setting for how long the hold lasts is stored on the registration type entity.
      $registration_type = registration_type_load($registration_wrapper
        ->getBundle());

      // Change registration state if hold has expired.
      // Expiration time is in hours - hence the multiplication.
      if ($registration_type->held_expire !== '0' && time() - $registration_type->held_expire * 60 * 60 > $registration->updated) {

        // Set registration state to what is configured for its registration type.
        $registration_wrapper->state
          ->set($registration_type->held_expire_state);
        $registration_wrapper
          ->save();
      }
    }
  }
}