You are here

function _sms_user_sleep_active in SMS Framework 7

Same name and namespace in other branches
  1. 5 modules/sms_user/sms_user.module \_sms_user_sleep_active()
  2. 6.2 modules/sms_user/sms_user.module \_sms_user_sleep_active()
  3. 6 modules/sms_user/sms_user.module \_sms_user_sleep_active()

Returns the user's sleep status.

Parameters

object $account: The user account object.

Return value

bool TRUE if its currently the user's sleep time.

1 call to _sms_user_sleep_active()
sms_user_sms_send in modules/sms_user/sms_user.module
Implements hook_sms_send().

File

modules/sms_user/sms_user.module, line 169
Provides integration between the SMS Framework and Drupal users.

Code

function _sms_user_sleep_active($account) {

  // If the user has a timezone set in his account get the time in that timezone.
  if (variable_get('configurable_timezones', 1) && !empty($account->timezone_name)) {
    $timezone = new DateTimeZone($account->timezone);
  }
  else {
    $default = variable_get('date_default_timezone', '');
    $timezonestring = empty($default) ? 'UTC' : $default;
    $timezone = new DateTimeZone($timezonestring);
  }
  $date = new DateTime();
  $date
    ->setTimezone($timezone);
  $current_hour = date('G');

  // User has individual settings, which will always override globals.
  if (!empty($account->sms_user['sleep_enabled'])) {
    $start = $account->sms_user['sleep_start_time'];
    $end = $account->sms_user['sleep_end_time'];
  }
  else {
    $start = variable_get('sms_user_sleep_start_time', 0);
    $end = variable_get('sms_user_sleep_end_time', 0);
  }

  // If values are not set, send the message.
  if ($start == 0 && $end == 0) {
    return FALSE;
  }

  // Do 2 checks: one is where start < end
  // two is where end < start (like from 22 to 7).
  if ($start < $end) {
    if ($start <= $current_hour && $end > $current_hour) {
      return TRUE;
    }
  }
  if ($end < $start) {

    // Current time is between start and midnight.
    if ($start <= $current_hour) {
      return TRUE;
    }

    // Current time is between midnight and end.
    if ($current_hour < $end) {
      return TRUE;
    }
  }
  return FALSE;
}