You are here

function signup_cron in Signup 5

Same name and namespace in other branches
  1. 5.2 signup.module \signup_cron()
  2. 6.2 signup.module \signup_cron()
  3. 6 signup.module \signup_cron()
  4. 7 signup.module \signup_cron()

Implementation of hook_cron().

Related topics

File

./signup.module, line 50

Code

function signup_cron() {

  // Only run this function if the event module is enabled.
  if (module_exists('event')) {

    // We must manually include this here as the event module doesn't
    // include timezone support on all page requests.
    require_once drupal_get_path('module', 'event') . '/event_timezones.inc';

    // Get the current time, and pull all of the nodes for which the
    // current time + the reminder_days_before time is greater than
    // the event's start date.  These are the events for which a
    // reminder email needs to be sent.
    $curtime = time();
    $result = db_query("SELECT n.title, n.nid, e.event_start, e.timezone, s.reminder_email, s.forwarding_email FROM {signup} s\n      INNER JOIN {node} n ON n.nid = s.nid INNER JOIN {event} e ON e.nid = s.nid WHERE\n      s.send_reminder = 1 AND (%d + ((s.reminder_days_before) * 86400)) > e.event_start", $curtime);

    // Grab each event, construct the email header and subject, and query
    // the signup log to pull all users who are signed up for this event.
    $from = variable_get('site_mail', ini_get('sendmail_from'));
    while ($event = db_fetch_object($result)) {
      $subject = t('Event reminder: !event', array(
        '!event' => $event->title,
      ));
      $signups = db_query("SELECT u.name, u.mail, s_l.anon_mail, s_l.form_data FROM {signup_log} s_l INNER JOIN {users} u ON u.uid = s_l.uid WHERE s_l.nid = %d", $event->nid);

      // Get timezone offset.
      $offset = event_get_offset($event->timezone, $event->event_start);

      // Loop through the users, composing their customized message
      // and sending the email.
      while ($signup = db_fetch_object($signups)) {
        $mail_address = $signup->anon_mail ? $signup->anon_mail : $signup->mail;
        $signup_data = unserialize($signup->form_data);
        if (!empty($signup_data)) {
          $signup_info = theme('signup_email_token_custom_data', $signup_data);
        }
        $trans = array(
          '%event' => $event->title,
          '%time' => _event_date(variable_get('signup_date_string', 'D, M jS, g:i A'), $event->event_start, $offset),
          '%username' => $signup->name,
          '%useremail' => $mail_address,
          '%info' => $signup_info,
        );
        $message = strtr($event->reminder_email, $trans);
        drupal_mail('signup_reminder_mail', $mail_address, $subject, $message, $from);
        watchdog('signup', t('Reminder for %event sent to %useremail.', array(
          '%event' => $event->title,
          '%useremail' => $mail_address,
        )), WATCHDOG_NOTICE, l(t('view'), 'node/' . $event->nid));
      }

      // Reminders for this event are all sent, so mark it in the
      // database so they're not sent again.
      db_query("UPDATE {signup} SET send_reminder = 0 WHERE nid = %d", $event->nid);
    }

    // Calculate the closing time for the event, which is the current
    // time + the number of hours before the event start when closing is
    // preferred.  Query the database for all signup events which have a
    // start time less than this.
    $closing_time = $curtime + variable_get('signup_close_early', 1) * 3600;
    $result = db_query("SELECT s.nid FROM {signup} s INNER JOIN {event} e ON e.nid = s.nid WHERE s.completed = 0 AND e.event_start < %d", $closing_time);

    // Loop through the results, calling the event closing function.
    while ($signup = db_fetch_object($result)) {
      signup_close_signup($signup->nid, $cron = 'yes');
      $node = node_load($signup->nid);
      foreach (module_implements('signup_close') as $module) {
        $function = $module . '_signup_close';
        $function($node);
      }
      watchdog('signup', t('Signups closed for %event by cron.', array(
        '%event' => $node->title,
      )), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
    }
  }
}