You are here

ad_report.module in Advertisement 6.3

Provides comprehensive charts and reports about advertising statistics.

Copyright (c) 2007-2009. Jeremy Andrews <jeremy@tag1consulting.com>.

File

report/ad_report.module
View source
<?php

/**
 * @file
 * Provides comprehensive charts and reports about advertising statistics.
 *
 * Copyright (c) 2007-2009.
 *   Jeremy Andrews <jeremy@tag1consulting.com>.
 */

/**
 * Implementation of hook_menu().
 */
function ad_report_menu() {
  $items = array();
  $items['admin/content/ad/report'] = array(
    'title' => t('Reports'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ad_report_admin',
    ),
    'access arguments' => array(
      'generate administrative reports',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  );
  $items['admin/content/ad/report/display'] = array(
    'page callback' => 'ad_report_admin_display',
    'access arguments' => array(
      'generate administrative reports',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/content/ad/report/csv'] = array(
    'page callback' => 'ad_report_admin_ad_table',
    'page arguments' => array(
      '0',
      '0',
      array(),
      TRUE,
    ),
    'access arguments' => array(
      'generate administrative reports',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['node/%node/report'] = array(
    'title' => t('Reports'),
    'page callback' => 'ad_report_bargraph_handler',
    'page arguments' => array(
      1,
    ),
    'type' => MENU_LOCAL_TASK,
    'access callback' => 'ad_report_bargraph_access',
    'access arguments' => array(
      1,
    ),
  );
  $items['node/%node/report/%/%/%'] = array(
    'title' => t('Reports'),
    'page callback' => 'ad_report_bargraph_handler',
    'page arguments' => array(
      1,
      3,
      4,
      5,
    ),
    'type' => MENU_LOCAL_TASK,
    'access callback' => 'ad_report_bargraph_access',
    'access arguments' => array(
      1,
    ),
  );
  return $items;
}

/**
 * Drupal hook_perm implementation.
 */
function ad_report_perm() {
  return array(
    t('generate administrative reports'),
  );
}

/**
 * Menu system callback, determine if current user can generate reports.
 */
function ad_report_bargraph_access($node) {
  if (isset($node->adtype)) {
    return ad_permission($node->nid, 'generate reports');
  }
}

/**
 *
 */
function ad_report_bargraph_handler($node, $start = 0, $end = 0, $data = '') {
  $data = explode('|', $data);
  $graph = array();
  foreach ($data as $value) {
    if ($value) {
      $graph[$value] = $value;
    }
  }
  return ad_report_bargraph($node, "node/{$node->nid}/report", $start, $end, $graph);
}

/**
 * Ad module hook_adapi.
 */
function ad_report_adapi($op, $node = NULL) {
  switch ($op) {
    case 'permissions':
      return array(
        'generate reports' => TRUE,
      );
  }
}

/**
 *
 */
function ad_report_admin() {
  $form = array();
  $start = isset($_SESSION['ad_report_start']) ? strtotime($_SESSION['ad_report_start']) : _ad_report_first_day_of_month();
  $end = isset($_SESSION['ad_report_end']) ? strtotime($_SESSION['ad_report_end']) : _ad_report_last_day_of_month();
  $group = isset($_SESSION['ad_report_group']) ? $_SESSION['ad_report_group'] : array(
    'all',
  );
  $form['dates'] = array(
    '#type' => 'fieldset',
    '#title' => t('Report dates'),
    '#prefix' => '<div class="container-inline">',
    '#suffix' => '</div>',
  );
  $form['dates']['start'] = array(
    '#type' => 'textfield',
    '#title' => t('Start'),
    '#size' => 24,
    '#maxlength' => 64,
    '#default_value' => _ad_report_format_date_human($start),
    // display pop up calendar if jstools jscalendar module enabled
    '#attributes' => array(
      'class' => 'jscalendar',
    ),
    '#jscalendar_ifFormat' => '%Y-%m-%d %H:%M',
    '#jscalendar_timeFormat' => '24',
  );
  $form['dates']['space1'] = array(
    '#value' => '&nbsp;&nbsp;',
  );
  $form['dates']['end'] = array(
    '#type' => 'textfield',
    '#title' => t('End'),
    '#size' => 24,
    '#maxlength' => 64,
    '#default_value' => _ad_report_format_date_human($end),
    // display pop up calendar if jstools jscalendar module enabled
    '#attributes' => array(
      'class' => 'jscalendar',
    ),
    '#jscalendar_ifFormat' => '%Y-%m-%d %H:%M',
  );
  $form['dates']['space2'] = array(
    '#value' => '&nbsp;&nbsp;&nbsp;',
  );

  // groups
  $groups = ad_groups_list();
  $form['groups'] = array(
    '#type' => 'fieldset',
    '#title' => t('Groups'),
  );
  $options = array();
  $options['all'] = t('- All -');
  $options = $options + $groups;
  $form['groups']['group'] = array(
    '#type' => 'select',
    '#title' => t('Ad groups'),
    '#options' => $options,
    '#multiple' => TRUE,
    '#required' => TRUE,
    '#default_value' => $group,
  );

  // submit
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Generate report'),
    '#weight' => 10,
  );
  $form['reset'] = array(
    '#type' => 'submit',
    '#value' => t('Reset report'),
    '#weight' => 10,
  );
  return $form;
}

/**
 * Sanity check the date range.
 */
function ad_report_admin_validate($form, $form_state) {
  if ($form_state['clicked_button']['#value'] == t('Reset report')) {
    unset($_SESSION['ad_report_start']);
    unset($_SESSION['ad_report_end']);
    unset($_SESSION['ad_report_group']);
  }
  else {
    $start = isset($form_state['values']['start']) ? strtotime($form_state['values']['start']) : 0;
    $end = isset($form_state['values']['start']) ? strtotime($form_state['values']['end']) : 0;
    if (!$start) {
      form_set_error('start', t('You must enter a valid start date.'));
    }
    else {
      if ($start >= time() - 3600) {
        form_set_error('start', t('The report must start at least one hour before the current time.'));
      }
      else {
        if ($start >= $end) {
          form_set_error('start', t('The report must start before it ends.'));
        }
      }
    }
    if (!$end) {
      form_set_error('end', t('You must enter a valid end date.'));
    }
  }
}

/**
 * Redirect to a path to generate the requested report.
 */
function ad_report_admin_submit($form, $form_state) {
  if ($form_state['clicked_button']['#value'] == t('Generate report')) {
    $start = date('YmdHi', strtotime($form_state['values']['start']));
    $end = date('YmdHi', strtotime($form_state['values']['end']));
    $group = $form_state['values']['group'];
    $_SESSION['ad_report_start'] = $start;
    $_SESSION['ad_report_end'] = $end;
    $_SESSION['ad_report_group'] = $group;
    drupal_goto('admin/content/ad/report/display');
  }
}

/**
 * Display the administrative report.
 */
function ad_report_admin_display() {
  $start = isset($_SESSION['ad_report_start']) ? $_SESSION['ad_report_start'] : 0;
  $end = isset($_SESSION['ad_report_end']) ? $_SESSION['ad_report_end'] : 0;
  $group = isset($_SESSION['ad_report_group']) ? $_SESSION['ad_report_group'] : array();
  if (!$start && !$end) {
    drupal_goto('admin/content/ad/report');
  }
  $ads = array();
  $output .= ad_report_admin_ad_table(strtotime($start), strtotime($end), $group, FALSE, $ads);
  $output .= '<div>' . l(t('Modify report'), 'admin/content/ad/report') . '</div>';
  $start_time = strtotime($start);
  $end_time = strtotime($end);
  $elapse = $end_time - $start_time;
  if (count($ads) && $start && $end && $elapse > 0 && $start > 0 && $end > 0) {
    if ($elapse > 86400 * 100) {

      // months
      $segment = 2678400;
    }
    else {
      if ($elapse > 86400 * 5) {

        // days
        $segment = 86400;
      }
      else {

        // hours
        $segment = 3600;
      }
    }
    $max = 0;
    $elements = round($elapse / $segment, 0);
    $space = _ad_report_size($elements);
    $s = $space;
    for ($i = $start_time; $i < $end_time; $i += $segment) {
      $start_date = _ad_report_format_date_db($i);
      $end_date = _ad_report_format_date_db($i + $segment);
      foreach ($ads as $aid) {
        $ad = node_load($aid);
        $imp = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d AND date <= %d", $aid, $start_date, $end_date));
        if ($imp > $max) {
          $max = $imp;
        }
        $title = isset($ad->title) ? $ad->title : "ad {$aid}";
        $impressions[$title][] = $imp ? $imp : '0';
      }
      if ($s++ >= $space) {
        if ($segment == 3600) {
          $labels[] = date('M j ga', $i);
        }
        else {
          $labels[] = date('M j', $i);
        }
        $s = 0;
      }
      else {
        $labels[] = '';
      }
    }
    $values = array();
    $i = 0;
    foreach ($ads as $aid) {
      if (++$i > 5) {

        // if we include too many ads, the URL is too long for the browser
        drupal_set_message(t('Only plotting the first five ads on the graph.'));
        break;
      }
      $ad = node_load($aid);
      $title = isset($ad->title) ? $ad->title : "ad {$aid}";
      $values[$title] = $impressions[$title];
    }
    if (!empty($values)) {
      $values['#max'] = $max;
      $values['#labels'] = $labels;
      $values['#title'] = 'ad impressions';
    }
    $output .= ad_report_generate_bargraph($values);
  }
  return $output;
}

/**
 * Generate a table reporting on the selected advertisements.
 * Returns an array of ad NIDs in $ads.
 */
function ad_report_admin_ad_table($start = 0, $end = 0, $group = array(), $csv = FALSE, &$ads) {
  if (!$start) {
    $start = isset($_SESSION['ad_report_start']) ? strtotime($_SESSION['ad_report_start']) : 0;
  }
  if (!$end) {
    $end = isset($_SESSION['ad_report_end']) ? strtotime($_SESSION['ad_report_end']) : 0;
  }
  if (!$csv) {
    drupal_set_message(t('Report dates: !start to !end', array(
      '!start' => isset($_SESSION['ad_report_start']) ? format_date($start, 'small') : 'now',
      '!end' => isset($_SESSION['ad_report_end']) ? format_date($end, 'small') : 'now',
    )));
  }
  if (empty($group)) {
    $group = isset($_SESSION['ad_report_group']) ? $_SESSION['ad_report_group'] : array();
  }

  // prepare dates
  $start = _ad_report_format_date_db($start);
  $end = _ad_report_format_date_db($end);

  // prepare groups
  $groups = ad_groups_list();
  $all = FALSE;
  $none = FALSE;
  if (is_array($group)) {
    if (in_array('all', $group)) {
      $all = TRUE;
    }
    if (!$all) {
      if (sizeof($group) == sizeof($groups)) {
        $all = TRUE;
      }
    }
    if (in_array('0', $group)) {
      unset($group[0]);
      $none = TRUE;
    }
  }
  if (!$csv) {
    $list = array();
    if ($all) {
      $list[] = t('all');
    }
    else {
      if ($none) {
        $list[] = t('none');
      }
      else {
        foreach ($group as $gid) {
          if (isset($groups[$gid])) {
            $list[] = $groups[$gid];
          }
        }
      }
    }
    drupal_set_message(t('Ad groups: !groups', array(
      '!groups' => implode(', ', $list),
    )));
  }
  $select = 'SELECT DISTINCT(aid) as nid FROM {ad_statistics} a';
  if ($all) {
    $where = array(
      "a.action = 'view'",
      'a.date >= %d',
      'a.date <= %d',
      'a.aid > 0',
    );
    $join = array();
    $args = array(
      $start,
      $end,
    );
  }
  else {
    if ($none) {
      if (sizeof($group)) {
        $where = array(
          '(t.tid IN (%s) OR ISNULL(t.tid))',
          "a.action = 'view'",
          'a.date >= %d',
          'a.date <= %d',
        );
        $join = array(
          'LEFT JOIN {term_node} t ON a.aid = t.nid',
        );
        $args = array(
          implode(',', $group),
          $start,
          $end,
        );
      }
      else {
        $where = array(
          'ISNULL(t.tid)',
          "a.action = 'view'",
          'a.date >= %d',
          'a.date <= %d',
        );
        $join = array(
          'LEFT JOIN {term_node} t ON a.aid = t.nid',
        );
        $args = array(
          $start,
          $end,
        );
      }
    }
    else {
      $where = array(
        't.tid IN (%s)',
        "a.action = 'view'",
        'a.date >= %d',
        'a.date <= %d',
      );
      $join = array(
        'LEFT JOIN {term_node} t ON a.aid = t.nid',
      );
      $args = array(
        implode(',', $group),
        $start,
        $end,
      );
    }
  }
  $return = module_invoke_all('adreport', $join, $where, $args, $select);
  foreach ($return as $type => $value) {
    switch ($type) {
      case 'join':
        if (is_array($value)) {
          foreach ($value as $option) {
            $join[] = $option;
          }
        }
        break;
      case 'where':
        if (is_array($value)) {
          foreach ($value as $option) {
            $where[] = $option;
          }
        }
        break;
      case 'args':
        if (is_array($value)) {
          foreach ($value as $option) {
            $args[] = $option;
          }
        }
        break;
    }
  }

  // Build the query.
  $query = $select . ' ' . implode(' ', $join) . ' WHERE ' . implode(' AND ', $where);
  $result = db_query($query, $args);
  $ads = array();
  while ($ad = db_fetch_object($result)) {
    if ($ad->nid) {
      $ads[$ad->nid] = $ad->nid;
    }
  }
  if ($csv) {
    header('Content-type: application/octet-stream');
    header("Content-Disposition: attachment; filename=report-{$start}-{$end}.csv");
    echo "ad id, title, first impression, latest impression, impressions, clicks, click-thru\n";
  }
  else {
    $output = '<div class="describe">' . t('There !count found.', array(
      '!count' => format_plural(sizeof($ads), 'was <strong>1</strong> ad', 'were <strong>@count</strong> ads'),
    )) . '</div>';
    $headers = array(
      t('Advertisement'),
      t('Active dates'),
      t('Impressions'),
      t('Clicks'),
      t('Click-thru'),
    );

    // get counts for each ad
    $rows = array();
  }
  $total_views = $total_clicks = 0;
  foreach ($ads as $nid) {
    $ad = node_load($nid);
    if ($ad->nid) {
      $views = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d AND date <= %d", $nid, $start, $end));
      $first = _ad_report_get_date_from_path((int) db_result(db_query("SELECT MIN(date) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d AND date <= %d", $nid, $start, $end)));
      $first = format_date($first, 'small');
      $last = _ad_report_get_date_from_path((int) db_result(db_query("SELECT MAX(date) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d AND date <= %d", $nid, $start, $end)));
      $last = format_date($last, 'small');
      $clicks = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d AND date <= %d", $nid, $start, $end));
      if ($views) {
        $clickthru = number_format(100 * $clicks / $views, 2) . '%';
      }
      else {
        $clickthru = '0%';
      }
      if ($views || $clicks) {
        if ($csv) {
          echo "{$ad->nid}, {$ad->title}, {$first}, {$last}, {$views}, {$clicks}, {$clickthru}\n";
        }
        else {
          $row = array();
          $row[] = l($ad->title, "node/{$ad->nid}");
          $row[] = "first impression: {$first}<br />latest impression: {$last}";
          $row[] = number_format($views);
          $row[] = number_format($clicks);
          $row[] = $clickthru;
          $rows[] = $row;
          $total_views += $views;
          $total_clicks += $clicks;
        }
      }
    }
  }
  if ($csv) {
    return 0;
  }
  if ($total_views || $total_clicks) {
    $row = array();
    $row[] = '<strong>' . t('Total') . '</strong>';
    $row[] = '';
    $row[] = '<strong>' . number_format($total_views) . '</strong>';
    $row[] = '<strong>' . number_format($total_clicks) . '</strong>';
    if ($total_views) {
      $row[] = '<strong>' . number_format(100 * $total_clicks / $total_views, 2) . '%' . '</strong>';
    }
    else {
      $row[] = '<strong>' . '0%' . '</strong>';
    }
    $rows[] = $row;
  }
  $output .= theme('table', $headers, $rows);
  $output .= l(t('Download CSV'), 'admin/content/ad/report/csv');
  return $output;
}

/**
 * Returns a timestamp for the first hour of the first day of the month.
 */
function _ad_report_first_day_of_month($time = NULL) {
  if ($time === NULL) {
    $time = time();
  }
  return strtotime(date('Ym010000', $time));
}

/**
 * Returns a timestamp for the last hour of the last day of the month.
 */
function _ad_report_last_day_of_month($time = NULL) {
  if ($time === NULL) {
    $time = time();
  }
  $month = date('m', $time);
  $year = date('Y', $time);
  $day = date('d', mktime(0, 0, 0, $month + 1, 0, $year));
  return strtotime("{$year}{$month}{$day}2359");
}

/**
 * Return a form for selecting a date range for generating a report.
 */
function ad_report_range_form($form_state, $node, $url = NULL, $start = NULL, $end = NULL, $data = array()) {
  $form = array();
  $start = $start ? $start : _ad_report_first_day_of_month();
  $end = $end ? $end : _ad_report_last_day_of_month();
  $form['data'] = array(
    '#type' => 'fieldset',
    '#title' => t('Report data'),
  );
  $form['data']['impressions'] = array(
    '#type' => 'checkbox',
    '#title' => t('Impressions'),
    '#default_value' => empty($data) || isset($data['i']) ? TRUE : FALSE,
  );
  $form['data']['clicks'] = array(
    '#type' => 'checkbox',
    '#title' => t('Clicks'),
    '#default_value' => isset($data['c']) ? TRUE : FALSE,
  );
  $form['report'] = array(
    '#type' => 'fieldset',
    '#title' => t('Report dates'),
    '#prefix' => '<div class="container-inline">',
    '#suffix' => '</div>',
  );
  $form['report']['nid'] = array(
    '#value' => $node->nid,
    '#type' => 'hidden',
  );
  $form['report']['url'] = array(
    '#value' => $url,
    '#type' => 'hidden',
  );
  if (isset($data['goto'])) {
    $form['report']['goto'] = array(
      '#value' => $data['goto'],
      '#type' => 'hidden',
    );
  }
  $form['report']['start'] = array(
    '#type' => 'textfield',
    '#title' => t('Start'),
    '#size' => 24,
    '#maxlength' => 64,
    '#default_value' => _ad_report_format_date_human($start),
    // display pop up calendar if jstools jscalendar module enabled
    '#attributes' => array(
      'class' => 'jscalendar',
    ),
    '#jscalendar_ifFormat' => '%Y-%m-%d %H:%M',
    '#jscalendar_timeFormat' => '24',
  );
  $form['report']['space1'] = array(
    '#value' => '&nbsp;&nbsp;',
  );
  $form['report']['end'] = array(
    '#type' => 'textfield',
    '#title' => t('End'),
    '#size' => 24,
    '#maxlength' => 64,
    '#default_value' => _ad_report_format_date_human($end),
    // display pop up calendar if jstools jscalendar module enabled
    '#attributes' => array(
      'class' => 'jscalendar',
    ),
    '#jscalendar_ifFormat' => '%Y-%m-%d %H:%M',
  );
  $form['report']['space2'] = array(
    '#value' => '&nbsp;&nbsp;&nbsp;',
  );
  $form['report']['generate'] = array(
    '#type' => 'submit',
    '#value' => t('Generate report'),
  );
  return $form;
}

/**
 * Validate the form range.
 */
function ad_report_range_form_validate($form, $form_state) {
  $start = isset($form_state['values']['start']) ? strtotime($form_state['values']['start']) : 0;
  $end = isset($form_state['values']['start']) ? strtotime($form_state['values']['end']) : 0;
  if (!$start) {
    form_set_error('start', t('You must enter a valid start date.'));
  }
  else {
    if ($start >= time() - 3600) {
      form_set_error('start', t('The report must start at least one hour before the current time.'));
    }
    else {
      if ($start >= $end) {
        form_set_error('start', t('The report must start before it ends.'));
      }
    }
  }
  if (!$end) {
    form_set_error('end', t('You must enter a valid end date.'));
  }
}

/**
 * Redirect to URL for displaying report.
 */
function ad_report_range_form_submit($form, $form_state) {
  $start = date('YmdHi', strtotime($form_state['values']['start']));
  $end = date('YmdHi', strtotime($form_state['values']['end']));
  $nid = $form_state['values']['nid'];
  if (isset($form_state['values']['impressions']) && $form_state['values']['impressions']) {
    $data[] = 'i';
  }
  if (isset($form_state['values']['clicks']) && $form_state['values']['clicks']) {
    $data[] = 'c';
  }
  if (!empty($data)) {
    $data = implode('|', $data);
  }
  if (isset($form_state['values']['goto'])) {
    drupal_goto($form_state['values']['goto'] . "/{$start}/{$end}/{$data}");
  }
  else {
    drupal_goto("node/{$nid}/report/{$start}/{$end}/{$data}");
  }
}

/**
 * Helper function to extract date from URL.
 */
function _ad_report_get_date_from_path($path) {
  if (isset($path) && $path) {
    $year = substr($path, 0, 4);
    $month = substr($path, 4, 2);
    $day = substr($path, 6, 2);
    $hour = substr($path, 8, 2);
    if (strlen($path) == 12) {
      $minute = substr($path, 10, 2);
    }
    else {
      $minute = 0;
    }
    $date = strtotime("{$month}/{$day}/{$year} {$hour}:{$minute}");
    if ($date > 0) {
      return $date;
    }
    drupal_set_message(t('Invalid date specified in range.'), 'error');
  }
}

/**
 * Helper function to format date.
 */
function _ad_report_format_date_human($date) {
  return date('Y-m-d H:i', $date);
}

/**
 * Helper function to format date.
 */
function _ad_report_format_date_db($date) {
  return date('YmdH', $date);
}

/**
 * Page to display ad with bargraph.
 */
function ad_report_bargraph($node, $url, $start = 0, $end = 0, $data = array()) {
  $output = '';
  $start_time = strtotime($start);
  $end_time = strtotime($end);
  $elapse = $end_time - $start_time;
  if ($start && $end && $elapse > 0 && $start > 0 && $end > 0) {
    if ($elapse > 86400 * 100) {

      // months
      $segment = 2678400;
    }
    else {
      if ($elapse > 86400 * 5) {

        // days
        $segment = 86400;
      }
      else {

        // hours
        $segment = 3600;
      }
    }
    $max = 0;
    $elements = round($elapse / $segment, 0);
    $space = _ad_report_size($elements);
    $s = $space;
    for ($i = $start_time; $i < $end_time; $i += $segment) {
      $start_date = _ad_report_format_date_db($i);
      $end_date = _ad_report_format_date_db($i + $segment);
      if ($data['i']) {
        $imp = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d AND date <= %d", $node->nid, $start_date, $end_date));
        if ($imp > $max) {
          $max = $imp;
        }
        $impressions[] = $imp ? $imp : '0';
      }
      if ($data['c']) {
        $cli = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d AND date <= %d", $node->nid, $start_date, $end_date));
        if ($cli > $max) {
          $max = $cli;
        }
        $clicks[] = $cli ? $cli : '0';
      }
      if ($s++ >= $space) {
        if ($segment == 3600) {
          $labels[] = date('M j ga', $i);
        }
        else {
          $labels[] = date('M j', $i);
        }
        $s = 0;
      }
      else {
        $labels[] = '';
      }
    }
    $values = array();
    if (!empty($impressions)) {
      $values['impressions'] = $impressions;
    }
    if (!empty($clicks)) {
      $values['clicks'] = $clicks;
    }
    if (!empty($values)) {
      $values['#max'] = $max;
      $values['#labels'] = $labels;
      $values['#title'] = $node->title;
      $values['#node'] = $node;
    }
    $output .= ad_report_generate_bargraph($values);
  }
  $start_date = _ad_report_get_date_from_path($start);
  $end_date = _ad_report_get_date_from_path($end);
  $output .= drupal_get_form('ad_report_range_form', $node, $url, $start_date, $end_date, $data);
  return $output;
}

/**
 * Page that utilizes the Google chart api to generate a bargraph.
 */
function ad_report_generate_bargraph($data) {
  $chart = array(
    '#chart_id' => 'ad',
    '#type' => CHART_TYPE_LINE,
    '#size' => chart_size(600, 400),
    // TODO: Calculate these based on the # of days
    '#grid_lines' => chart_grid_lines(10, 9.5, 1, 3),
    '#adjust_resolution' => TRUE,
  );
  foreach ($data as $legend => $values) {
    switch ($legend) {
      case '#max':
        $max = $values;
        break;
      case '#labels':
        $chart['#labels'] = $values;
        break;
      case '#title':
        $chart['#title'] = $values;
        break;
      case '#node':

        // we don't use this, but we pass it in case someone altering the
        // table needs data from the node object.
        break;
      default:
        $chart['#legends'][] = $legend;
        $chart['#data_colors'][] = chart_unique_color($legend);
        $chart['#line_styles'][] = chart_line_style(2);
        $chart['#data'][$legend] = $values;
    }
  }
  $chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_range_label(0, $max);
  return chart_render($chart);
}
function _ad_report_size($elements) {
  if ($elements > 200) {
    $space = 80;
  }
  else {
    if ($elements > 80) {
      $space = 18;
    }
    else {
      if ($elements > 60) {
        $space = 12;
      }
      else {
        if ($elements > 48) {
          $space = 8;
        }
        else {
          if ($elements > 35) {
            $space = 6;
          }
          else {
            if ($elements > 20) {
              $space = 3;
            }
            else {
              if ($elements > 12) {
                $space = 2;
              }
              else {
                if ($elements > 8) {
                  $space = 1;
                }
                else {
                  $space = 0;
                }
              }
            }
          }
        }
      }
    }
  }
  return $space;
}

Functions

Namesort descending Description
ad_report_adapi Ad module hook_adapi.
ad_report_admin
ad_report_admin_ad_table Generate a table reporting on the selected advertisements. Returns an array of ad NIDs in $ads.
ad_report_admin_display Display the administrative report.
ad_report_admin_submit Redirect to a path to generate the requested report.
ad_report_admin_validate Sanity check the date range.
ad_report_bargraph Page to display ad with bargraph.
ad_report_bargraph_access Menu system callback, determine if current user can generate reports.
ad_report_bargraph_handler
ad_report_generate_bargraph Page that utilizes the Google chart api to generate a bargraph.
ad_report_menu Implementation of hook_menu().
ad_report_perm Drupal hook_perm implementation.
ad_report_range_form Return a form for selecting a date range for generating a report.
ad_report_range_form_submit Redirect to URL for displaying report.
ad_report_range_form_validate Validate the form range.
_ad_report_first_day_of_month Returns a timestamp for the first hour of the first day of the month.
_ad_report_format_date_db Helper function to format date.
_ad_report_format_date_human Helper function to format date.
_ad_report_get_date_from_path Helper function to extract date from URL.
_ad_report_last_day_of_month Returns a timestamp for the last hour of the last day of the month.
_ad_report_size