You are here

function agenda_debug in Agenda 6.2

Same name and namespace in other branches
  1. 6 agenda.admin.php \agenda_debug()
  2. 7.2 agenda.admin.php \agenda_debug()
  3. 7 agenda.admin.php \agenda_debug()

Provide a page to debug a calendar ID that is not working

1 string reference to 'agenda_debug'
agenda_menu in ./agenda.module
Implementation of hook_menu().

File

./agenda.admin.php, line 360
Administration interface for the agenda module

Code

function agenda_debug($bid) {
  $output = array();

  // Date check (http://drupal.org/node/545174)
  $output[] = t('Checking server time: %date', array(
    '%date' => gmdate('r'),
  ));
  $output[] = t('Checking real UTC time via NTP: %date', array(
    '%date' => gmdate('r', agenda_debug_ntp_time('0.pool.ntp.org')),
  ));
  $output[] = t('Ensure these values are approximately the same, otherwise your system is incorrectly configured and agenda will be unable to calculate dates properly.');

  // Find calendar sources
  $block = agenda_settings($bid);
  $debug_title = array(
    '#markup' => '<h2>' . t('Debugging %calendar block', array(
      '%calendar' => $block->title,
    )) . ' - ' . l('Edit', 'admin/settings/agenda/' . $bid . '/configure', array(
      'query' => drupal_get_destination(),
    )) . '</h2>',
  );
  $output[] = t('Reading calendar input:');
  $output[] = '<pre>' . htmlspecialchars($block->calendars) . '</pre>';
  $calendars = preg_split('@\\r\\n?|\\n@', $block->calendars);
  $calendars = array_map('trim', $calendars);
  $output[] = t('Found following calendars:');
  $output[] = sprintf('<pre>%s</pre>', print_r($calendars, TRUE));
  $googleid = $calendars[0];
  if (count($calendars) > 1) {
    $output[] = t('Multiple calendars found, debugging with the first calendar: %googleid', array(
      '%googleid' => $googleid,
    ));
  }
  $timeMin = date('Y-m-d', strtotime($block->start)) . 'T00:00:00' . date('P');
  $timeMax = date('Y-m-d', strtotime($block->end)) . 'T00:00:00' . date('P');
  $output[] = t('Using Agenda Block start value of %blockStart converted for Google API to %timeMin', array(
    '%blockStart' => $block->start,
    '%timeMin' => $timeMin,
  ));
  $output[] = t('Using Agenda Block end value of %blockEnd converted for Google API to %timeMax', array(
    '%blockEnd' => $block->end,
    '%timeMax' => $timeMax,
  ));
  $output[] = t('Maximum number of events to fetch: %maxEvents', array(
    '%maxEvents' => $block->maxevents,
  ));
  $output[] = t('Timezone: %timezone', array(
    '%timezone' => $block->timezone,
  ));
  $googlekey = !empty($block->googleapi_override) ? $block->googleapi_override : variable_get('agenda_googleapi', '');
  $output[] = t('Using Google API Key: %googlekey', array(
    '%googlekey' => $googlekey,
  ));

  // Load the calendar
  list($address, $token) = _agenda_parse_googleid($googleid);

  // Load the XML
  $calendar = _agenda_load_google($address, $token, $block);
  if (!$calendar) {
    $output[] = t('<strong>Warning</strong>: Failed');
    return theme('item_list', $output, NULL, 'ul', array(
      'id' => 'agenda-debug',
    ));
  }
  $output[] = t('Loaded Calendar successfully');

  // Find the events
  $calendar_events = $calendar
    ->getItems();
  $number_of_events = count($calendar_events);
  $output[] = t('Found @count events', array(
    '@count' => $number_of_events,
  ));
  if ($number_of_events === 0) {
    $output[] = t('<strong>Warning</strong>: Nothing to do with empty calendar, stopping!');
    $debug_log = theme('item_list', array(
      'items' => $output,
    ));
    return array(
      'title' => array(
        '#markup' => '<h2>' . t('Debugging %calendar block', array(
          '%calendar' => $block->title,
        )) . ' - ' . l('Edit', 'admin/settings/agenda/' . $bid . '/configure') . '</h2>',
      ),
      'debug_log' => array(
        '#markup' => $debug_log,
        '#prefix' => '<h3>Log</h3><div id="agenda-debug-log">',
        '#suffix' => '</div>',
      ),
      '#attached' => array(
        'css' => array(
          drupal_get_path('module', 'agenda') . '/agenda.css',
        ),
      ),
    );
  }

  // Parse the events
  $i = 0;
  $eventdata = array();
  foreach ($calendar_events as $event) {
    $thisevent = _agenda_parse_event($event, $block);
    if (!$thisevent) {
      $output[] = t('<strong>Warning</strong>: Failed to parse event!');
      continue;
    }
    $output[] = t('Successfully parsed event:');
    $output[] = '<pre>' . print_r($thisevent, TRUE) . '</pre>';
    $eventdata[] = $thisevent;
  }
  $output[] = t('Parsed @success/@total events successfully', array(
    '@success' => count($eventdata),
    '@total' => $number_of_events,
  ));
  $output[] = t('Begin filtering based on date: string provided (@old) which has timestamp (@timestamp) which has date (@date).', array(
    '@old' => $what_is_old = agenda_variable_get($bid, 'start', '-1 day'),
    '@timestamp' => $what_is_old_ts = strtotime($what_is_old),
    '@date' => format_date($what_is_old_ts),
  ));
  foreach ($eventdata as $key => $event) {
    if ($event['start timestamp'] < $what_is_old_ts) {
      $output[] = t('&raquo; Discarding %title because %start is older than %old', array(
        '%title' => $event['title'],
        '%start' => format_date($event['start timestamp'], 'small'),
        '%old' => format_date($what_is_old_ts, 'small'),
      ));
      unset($eventdata[$key]);
    }
  }
  $output[] = t('Finished. Started with @total events, will display @display events', array(
    '@total' => $number_of_events,
    '@display' => count($eventdata),
  ));

  // Now take all of our debug data and theme it up, starting with the logs
  $html = theme('item_list', $output, NULL, 'ul', array(
    'id' => 'agenda-debug',
  ));
  if (count($eventdata)) {
    $first = array_slice($eventdata, 0, 1);
    $html .= theme('table', array_keys($first[0]), $eventdata);
  }
  return $html;
}