You are here

function birthdays_get_birthdays in Birthdays 6

Same name and namespace in other branches
  1. 5 birthdays.module \birthdays_get_birthdays()

Get the next X birthdays

Integer stating the amount of birthdays to retrieve.

Return value

array An array containing user objects meeting the criteria

1 call to birthdays_get_birthdays()
birthdays_block in ./birthdays.module
Implementation of hook_block().

File

./birthdays.module, line 532
The Birthdays module allows users to add their birthday to their profile. It lists birthdays on a seperate page and in different blocks. Users can receive an e-mail on their birthday automatically, and the administrator can receive daily reminders of…

Code

function birthdays_get_birthdays($amount) {
  $birthdays = array();

  // Current logged in user
  global $user;

  // Get user time zone
  // Needed to determine what day 'today' is in the timezone of the user/website.
  if (variable_get('configurable_timezones', 1) && $user->uid && drupal_strlen($user->timezone)) {
    $timezone = $user->timezone;
  }
  else {
    $timezone = variable_get('date_default_timezone', 0);
  }

  // $amount should be larger or equal to 1
  if ($amount < 1) {
    $amount = 1;
  }

  // MySQL prior to 4.1.1 has no option to use UTC, while drupal uses UTC.
  // This is compensated by subtracting the machines timezone from the Drupal timezone.
  // I believe the assumption is that the HTTP-server has the same timezone as the MySQL server.
  $timezone -= date('Z');

  /* Query:
      - Select all active users that have their birthday today or in the future (stops at 31-12)
      - return at most $amount users
      - Don't show blocked users
      - Users that haven't logged in yet are also not shown (Drupal prohibits accessing their profile, thus showing
        a link to the profile is unwanted). This is a anti spammers method.
    */
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $result = db_query_range("SELECT {dob}.uid FROM {dob}, {users} WHERE {users}.uid = {dob}.uid AND {users}.status <> 0 AND {users}.access <> 0\n        AND DATE_FORMAT({dob}.birthday,'%%c%%d') - DATE_FORMAT(ADDDATE(NOW(),INTERVAL %d SECOND),'%%c%%d') >= 0\n        ORDER BY MONTH({dob}.birthday), DAYOFMONTH({dob}.birthday), YEAR({dob}.birthday), {users}.name", $timezone, 0, $amount);
      break;
    case 'pgsql':
      $result = db_query_range("SELECT {dob}.uid FROM {dob}, {users} WHERE {users}.uid = {dob}.uid AND {users}.status <> 0 AND {users}.access <> 0\n        AND cast(to_char({dob}.birthday,'FMMMDD') as integer) - cast(to_char(current_timestamp + INTERVAL '%d seconds','FMMMDD') as integer) >= 0\n        ORDER BY date_part('month', {dob}.birthday), date_part('day', {dob}.birthday), date_part('year', {dob}.birthday), {users}.name", $timezone, 0, $amount);
      break;
  }
  $count_rows = 0;
  while ($account = db_fetch_object($result)) {
    $birthdays[] = $account->uid;
    $count_rows++;
  }

  // If less than $amount results returned, look for more after 31-12
  // return at most the difference between the number already found and
  if ($count_rows < $amount) {
    switch ($GLOBALS['db_type']) {
      case 'mysql':
      case 'mysqli':
        $result = db_query_range("SELECT {dob}.uid FROM {dob}, {users} WHERE {users}.uid = {dob}.uid AND {users}.status <> 0 AND {users}.access <> 0\n          AND DATE_FORMAT({dob}.birthday,'%%c%%d') - DATE_FORMAT(ADDDATE(NOW(),INTERVAL %d SECOND),'%%c%%d') < 0\n          ORDER BY MONTH({dob}.birthday), DAYOFMONTH({dob}.birthday), YEAR({dob}.birthday), {users}.name", $timezone, 0, $amount - $count_rows);
        break;
      case 'pgsql':
        $result = db_query_range("SELECT {dob}.uid FROM {dob}, {users} WHERE {users}.uid = {dob}.uid AND {users}.status <> 0 AND {users}.access <> 0\n          AND cast(to_char({dob}.birthday,'FMMMDD') as integer) - cast(to_char(current_timestamp + INTERVAL '%d seconds','FMMMDD') as integer) < 0\n          ORDER BY date_part('month', {dob}.birthday), date_part('day', {dob}.birthday), date_part('year', {dob}.birthday), {users}.name", $timezone, 0, $amount - $count_rows);
        break;
    }
    while ($account = db_fetch_object($result)) {
      $birthdays[] = $account->uid;
    }
  }

  // Return array of uids that have their birthday
  return $birthdays;
}