You are here

function ad_report_generate_bargraph in Advertisement 5.2

Same name and namespace in other branches
  1. 5 report/ad_report.module \ad_report_generate_bargraph()
  2. 6.3 report/ad_report.module \ad_report_generate_bargraph()
  3. 6 report/ad_report.module \ad_report_generate_bargraph()
  4. 6.2 report/ad_report.module \ad_report_generate_bargraph()
  5. 7 report/ad_report.module \ad_report_generate_bargraph()

Page that utilizes gd to generate a bargraph.

1 string reference to 'ad_report_generate_bargraph'
ad_report_menu in report/ad_report.module
Implementation of hook_menu().

File

report/ad_report.module, line 617
Provides comprehensive charts and reports about advertising statistics.

Code

function ad_report_generate_bargraph($id, $type, $start, $end) {
  header("Content-type: image/png");

  // be sure we've been passed in valid parameters
  $elapse = $end - $start;
  if ($elapse <= 0 || $start <= 0 || $end <= 0) {
    return NULL;
  }
  $increments = (int) ($elapse / 3600);

  // image size
  $image_width = 700;
  $image_height = 360;

  // graph size
  $graph_width = 600;
  $graph_height = 250;
  $graph_x_offset = 8;
  $graph_y_offset = 8;
  $graph_y = 8;

  // calculate slices to extract from database
  $width = $graph_width / $increments;
  $number = $increments;
  $factor = 1;
  if ($width < 1) {
    $factor = 1 / $width;
  }
  $number = $number / $factor;
  $width = $width * $factor;
  $slice = $elapse / $number;

  // retrieve views and clicks from the database
  $views = array();
  $clicks = array();
  $max_views = 0;
  $max_clicks = 0;
  $key = 0;
  for ($i = $start; $i < $end; $i += $slice) {
    $start_date = _ad_report_format_date_db($i);
    $end_date = _ad_report_format_date_db($i + $slice);
    switch ($type) {
      case 'node':
        $views[] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d AND date <= %d", $id, $start_date, $end_date));
        $clicks[] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d AND date <= %d", $id, $start_date, $end_date));
        break;
      case 'user':
        $views[] = (int) db_result(db_query("SELECT SUM(a.count) FROM {ad_statistics} a LEFT JOIN {node} n ON a.aid = n.nid WHERE n.uid = %d AND n.type = 'ad' AND a.action = 'view' AND a.date >= %d AND a.date <= %d", $id, $start_date, $end_date));
        $clicks[] = (int) db_result(db_query("SELECT SUM(a.count) FROM {ad_statistics} a LEFT JOIN {node} n ON a.aid = n.nid WHERE n.uid = %d AND n.type = 'ad' AND a.action = 'click' AND a.date >= %d AND a.date <= %d", $id, $start_date, $end_date));
        break;
      case 'admin':
        $group = $_SESSION['ad_report_group'];
        $all = FALSE;
        $none = FALSE;
        if (is_array($group)) {
          if (in_array('all', $group)) {
            $all = TRUE;
          }
          if (!$all) {
            $groups = ad_groups_list();
            if (sizeof($group) == sizeof($groups)) {
              $all = TRUE;
            }
          }
          if (in_array('0', $group)) {
            unset($group[0]);
            $none = TRUE;
          }
        }
        if ($all) {
          $views[] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE action = 'view' AND date >= %d AND date <= %d", $start_date, $end_date));
          $clicks[] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE action = 'click' AND date >= %d AND date <= %d", $start_date, $end_date));
        }
        else {
          if ($none) {
            if (sizeof($group)) {
              $views[] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} a LEFT JOIN {term_node} t ON a.aid = t.tid WHERE (t.tid IN (%s) OR ISNULL(t.tid)) AND action = 'view' AND date >= %d AND date <= %d", implode(',', $group), $start_date, $end_date));
              $clicks[] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} a LEFT JOIN {term_node} t ON a.aid = t.tid WHERE (t.tid IN (%s) OR ISNULL(t.tid)) AND action = 'click' AND date >= %d AND date <= %d", implode(',', $group), $start_date, $end_date));
            }
            else {
              $views[] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} a LEFT JOIN {term_node} t ON a.aid = t.tid WHERE ISNULL(t.tid) AND action = 'view' AND date >= %d AND date <= %d", $start_date, $end_date));
              $clicks[] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} a LEFT JOIN {term_node} t ON a.aid = t.tid WHERE ISNULL(t.tid) AND action = 'click' AND date >= %d AND date <= %d", $start_date, $end_date));
            }
          }
          else {
            $views[] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} a LEFT JOIN {term_node} t ON a.aid = t.tid WHERE tid IN (%s) AND action = 'view' AND date >= %d AND date <= %d", implode(',', $group), $start_date, $end_date));
            $clicks[] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} a LEFT JOIN {term_node} t ON a.aid = t.tid WHERE t.tid IN (%s) AND action = 'click' AND date >= %d AND date <= %d", implode(',', $group), $start_date, $end_date));
          }
        }
        break;
      default:
        $function = "ad_report_views_{$type}";
        if (function_exists("{$function}")) {
          $views[] = $function($id, $day_start, $day_end);
        }
        $function = "ad_report_clicks_{$type}";
        if (function_exists("{$function}")) {
          $clicks[] = $function($id, $day_start, $day_end);
        }
        break;
    }
    $max_views = $views[$key] > $max_views ? $views[$key] : $max_views;
    $max_clicks = $clicks[$key] > $max_clicks ? $clicks[$key] : $max_clicks;
    $key++;
  }

  // create graph
  $graph = imagecreate($image_width, $image_height);

  // configure colors to use in chart
  $color = array(
    'white' => imagecolorallocate($graph, 255, 255, 255),
    'black' => imagecolorallocate($graph, 0, 0, 0),
    'grey' => imagecolorallocate($graph, 192, 192, 192),
    'blue' => imagecolorallocate($graph, 0, 0, 255),
    'orange' => imagecolorallocate($graph, 220, 210, 60),
    'red' => imagecolorallocate($graph, 255, 0, 0),
  );

  // determine how big the spacers should be
  $max = $max_views > $max_clicks ? $max_views : $max_clicks;
  $y_map = ceil($max / $graph_y / $graph_y) * $graph_y;
  $y_total = $y_map * $graph_y;
  if ($y_total) {

    // plot views and clicks on graph
    foreach ($views as $key => $value) {
      $view_height = $graph_height / $y_total * $value;
      if ($view_height) {
        imagefilledrectangle($graph, $graph_x_offset + $key * $width, $graph_y_offset + $graph_height - $view_height, $graph_x_offset + ($key + 1) * $width - 1, $graph_y_offset + $graph_height - 1, $color['blue']);
      }
      $click_height = $graph_height / $y_total * $clicks[$key];
      if ($click_height) {
        imagefilledrectangle($graph, $graph_x_offset + $key * $width, $graph_y_offset + $graph_height - $click_height, $graph_x_offset + ($key + 1) * $width - 1, $graph_y_offset + $graph_height - 1, $color['red']);
      }
    }
  }

  // add scale to y
  if ($y_map) {
    $graph_y_width = $graph_height / $graph_y;
    for ($i = 1; $i <= $graph_y; $i++) {
      $text = number_format($i * $y_map);
      $len = strlen($text);
      $x_offset = $graph_width + 14;
      $y_pos = $graph_height - $i * $graph_y_width;

      //imagestring($graph, 1, $x_offset, $graph_y_offset + $y_pos - 3, $text, $color['black']);
      imagestring($graph, 2, $x_offset, $graph_y_offset + $y_pos - 7, $text, $color['black']);
    }
  }

  // add scale to x
  $graph_x = _ad_report_select_x($number, 8, 0);
  $offset = $elapse / $graph_x;
  $graph_x_width = $graph_width / $graph_x;
  $x_offset = $graph_x_width / 2;
  for ($i = 1; $i <= $graph_x; $i++) {
    $text = date('M d, Y H', $start + $offset * $i - $offset / 2);
    $len = strlen($text);
    $x_pos = $graph_x_offset - $x_offset + $i * $graph_x_width - 7;
    $y_pos = $graph_height + $graph_y_offset + $len * 6 + 3;
    imagestringup($graph, 2, $x_pos, $y_pos, $text, $color['black']);

    //$x_pos = $graph_x_offset - $x_offset + $i * $graph_x_width - 4;

    //$y_pos = $graph_height + $graph_y_offset + ($len * 5) + 3;

    //imagestringup($graph, 1, $x_pos, $y_pos, $text, $color['black']);
  }

  // draw a grid
  $style = array(
    $color['grey'],
    IMG_COLOR_TRANSPARENT,
    IMG_COLOR_TRANSPARENT,
  );
  imagesetstyle($graph, $style);
  for ($i = 1; $i <= $graph_x; $i++) {
    imageline($graph, $graph_x_offset + $i * $graph_x_width - $graph_x_width / 2, $graph_y_offset, $graph_x_offset + $i * $graph_x_width - $graph_x_width / 2, $graph_y_offset + $graph_height - 1, IMG_COLOR_STYLED);
  }
  for ($i = 1; $i < $graph_y; $i++) {
    imageline($graph, $graph_x_offset, $graph_y_offset + $i * $graph_y_width, $graph_x_offset + $graph_width, $graph_y_offset + $i * $graph_y_width, IMG_COLOR_STYLED);
  }

  // left, right, top, and bottom borders, respectively
  imageline($graph, $graph_x_offset, $graph_y_offset, $graph_x_offset, $graph_y_offset + $graph_height, $color['grey']);
  imageline($graph, $graph_x_offset + $graph_width - 1, $graph_y_offset, $graph_x_offset + $graph_width - 1, $graph_y_offset + $graph_height, $color['grey']);
  imageline($graph, $graph_x_offset, $graph_y_offset, $graph_x_offset + $graph_width - 1, $graph_y_offset, $color['grey']);
  imageline($graph, $graph_x_offset, $graph_y_offset + $graph_height, $graph_x_offset + $graph_width - 1, $graph_y_offset + $graph_height, $color['grey']);

  // display the graph
  imagepng($graph);
  imagedestroy($graph);
}