You are here

function event_block_upcoming in Event 5

Same name and namespace in other branches
  1. 5.2 event.module \event_block_upcoming()

Creates a block that contains upcoming events.

Parameters

$limit The number of events that can be displayed in the block.:

Return value

A string containing the fully themed block.

Related topics

1 call to event_block_upcoming()
event_block in ./event.module
Provides the blocks that this module is capable of displaying.

File

./event.module, line 1815

Code

function event_block_upcoming($limit = 6) {
  global $user;
  $time = time();

  // Lookup events currently taking place and upcoming events, up to $limit events.
  $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, n.type, n.status, n.changed, e.event_start, e.event_end FROM {node} n INNER JOIN {event} e ON n.nid = e.nid WHERE n.status = 1 AND (((e.event_start != e.event_end) AND (%d >= e.event_start AND %d < e.event_end)) OR ((e.event_start = e.event_end) AND (%d <= e.event_start + %d )) OR e.event_start >= %d) ORDER BY event_start LIMIT %d"), $time, $time, $time, 60 * 60 * 2, $time, $limit);
  while ($node = db_fetch_object($result)) {

    // Call the event_edit_upcoming hook in all modules. Note that modules can
    // prevent display of a node by setting its status to 0 here.
    foreach (module_implements('event_edit_upcoming') as $module) {
      $function = $module . '_event_edit_upcoming';
      $function($node);
    }
    if ($node->status) {
      if ($node->event_start >= $time) {
        $minutesleft = floor(($node->event_start - $time) / 60);
        if ($minutesleft >= 0 && $minutesleft < 60) {
          $timeleft = format_plural($minutesleft, '1 minute', '@count minutes');
        }
        else {
          if ($minutesleft >= 60 && $minutesleft < 24 * 60) {
            $timeleft = format_plural(floor($minutesleft / 60), '1 hour', '@count hours');
          }
          else {
            if ($minutesleft >= 24 * 60) {
              $days = floor($minutesleft / (24 * 60));

              // hours remainder
              $hours = $minutesleft % (24 * 60) / 60;

              // hours left in the day
              $hours_left = 24 - date('G', time());

              // see if the remainder of hours on the event date is greater than the hours left in today, if so increase the days by one so that the days remaining mimics the date rather than how many 24 hour periods there are between now and then.
              if ($hours > $hours_left) {
                $days++;
              }
              $timeleft = format_plural($days, '1 day', '@count days');
            }
          }
        }
      }
      else {
        $timeleft = t('Now');
      }
      $node->timeleft = $timeleft;
      $node->typename = node_get_types('name', $node);
      $items[] = theme('event_upcoming_item', $node);
    }
  }
  if (!count($items)) {
    $items[] = t('No upcoming events available');
  }
  $output = theme('event_upcoming_block', $items);
  $output .= theme('event_ical_link', 'event/ical');
  $output .= theme('event_more_link', 'event');
  return $output;
}