views:

121

answers:

1

While refactoring, I am looking for an optimised algorithm for a timetable (calendar) in PHP.

I have a list of events which have a list of timestamps. This must be presented in a tabular way. The current code works, but has a) quite some quircks and is b) very inflexible. I am looking to refactor this piece and looking for input on optimised ways.

Example here, notice the header of the table bearing the hours. And the times within that hour being grouped in that column. One event, having two timestamps in one hour can be ignored (may only happen in theory, as per the technical docs)

The table is built with Drupals theme_table(), which I can build up from any array, bearing arrays for each row. the Drupal part, however, is of little interest here :)

Current code (snippets):

<?php
//inside function that builds the content for the page linked to above:
$this_morning = _playdates_get_start_of_day_with($timestamp);
$this_night = _playdates_get_end_of_day_with($timestamp);
$nodes = _playdates_load_all_in_range($types, $this_morning, $this_night);
usort($nodes, '_playdates_cmp_titles');
//we now have a list of "nodes", being the events listed in the first column, which have
//  a list of playdates, the timestamps. $node->nid is a unique id for each event.  
foreach ($nodes as $node) {
  $times = $dates = array();
  if (is_array($node->starts_at)) {
    foreach ($node->starts_at as $starts_at) {
      $date = getdate($starts_at);
      $dates[$starts_at] = $date;
      if (!isset($active) && ($timestamp <= ($date[0] + $timezone))) {
        $active = _playdates_get_idx_hour($date);
      }
    }
    $times = _playdates_group_by_hour($dates);
    foreach ($times as $key => $value) {
      $header_times[$key] = $key;
    }
    $header = array_merge($header, $header_times);
    $entries[$node->nid] = $node;
    $entries[$node->nid]->times = $times;
  }
}

function _playdates_group_by_hour($timestamps, $playdate = NULL) {
  $indexes = array();
  foreach ($timestamps as $key => $value) {
    $indexes[_playdates_get_idx_hour($value)] = theme('playdates_format_time', $value, $playdate);
  }
  ksort($indexes);
  return $indexes;
}   

function _playdates_get_idx_hour($date) {
  return playdates_format_date($date[0], 'custom', 'H:00');
}

So, the pattern is: grab (and order) the list of events. Loop over that, for each event, loop over the timestamps and create an array with the key being the hour of the timestamp (group by hours).

Are there better patterns? Is there a more generic way to achieve this? Any keywords that I can search for (I have no idea how such a pattern would be called)?

example dataset The $nodes (unnessecary data removed) look like this:

array(15) {
  [1]=>
  object(stdClass)#48 (6) {
    ["title"]=> 
    string(19) "Cosa voglio di più"
    ["type"]=>
    string(4) "film"
    ["nid"]=>
    string(4) "2823"
    ["premiere"]=>
    object(stdClass)#49 (1) {
      ["starts_at"]=>
      string(10) "1286550000"
    }
    ["starts_at"]=>
    array(2) {
      [0]=>
      string(10) "1286550000"
      [1]=>
      string(10) "1286559000"
    }
    ["ends_at"]=>
    array(2) {
      [0]=>
      string(1) "0"
      [1]=>
      string(1) "0"
    }
  }
  [12]=>
  object(stdClass)#46 (6) {
    ["title"]=>
    string(5) "Tirza"
    ["type"]=>
    string(4) "film"
    ["nid"]=>
    string(4) "2813"
    ["premiere"]=>
    object(stdClass)#47 (1) {
      ["starts_at"]=>
      string(10) "1286550000"
    }
    ["starts_at"]=>
    array(3) {
      [0]=>
      string(10) "1286550000"
      [1]=>
      string(10) "1286558100"
      [2]=>
      string(10) "1286566200"
    }
    ["ends_at"]=>
    array(3) {
      [0]=>
      string(1) "0"
      [1]=>
      string(1) "0"
      [2]=>
      string(1) "0"
    }
  }
  [14]=>
  object(stdClass)#36 (6) {
    ["title"]=>
    string(47) "Vision - aus dem Leben der Hildegard von Bingen"
    ["type"]=>
    string(4) "film"
    ["nid"]=>
    string(4) "2783"
    ["premiere"]=>
    object(stdClass)#39 (1) {
      ["starts_at"]=>
      string(10) "1286541900"
    }
    ["starts_at"]=>
    array(1) {
      [0]=>
      string(10) "1286541900"
    }
    ["ends_at"]=>
    array(1) {
      [0]=>
      string(1) "0"
    }
  }
}