You are here

function support_charts_build_graph in Support Ticketing System 7

Same name and namespace in other branches
  1. 6 support_charts/support_charts.module \support_charts_build_graph()

Gather data and build a chart API structure.

Return value

array chart API structure.

1 call to support_charts_build_graph()
support_charts_display in support_charts/support_charts.module
Display charts and content in context to the current page.

File

support_charts/support_charts.module, line 255
Support charting. @author Jeremy Andrews <jeremy@tag1consulting.com> @package Support

Code

function support_charts_build_graph($id, $type = 'global', $object = NULL) {
  $chart = array();
  $chart['#type'] = CHART_TYPE_LINE;
  $chart['#size'] = chart_size(620, 250);
  $chart['#grid_lines'] = chart_grid_lines(25, 9.5, 1, 3);
  $chart['#adjust_resolution'] = TRUE;
  $now = isset($_GET['year']) && isset($_GET['month']) ? mktime(0, 0, 0, $_GET['month'], 30, $_GET['year']) : time();
  $args = array();
  switch ($id) {
    case 'tickets_created':
      if ($type == 'user') {
        $chart['#title'] = chart_title(t('Tickets created by !user in !date', array(
          '!date' => date('F Y', $now),
          '!user' => check_plain($object->name),
        )));
        $args[':uid'] = $object->uid;
        $filter = "AND n.uid = :uid";
      }
      else {
        $chart['#title'] = chart_title(t('Tickets created in !date', array(
          '!date' => date('F Y', $now),
        )));
        $filter = '';
      }
      $chart['#chart_id'] = $id;
      $args[':end'] = $now;
      $args[':begin'] = mktime(0, 0, 0, date('m', $now), 1, date('Y', $now));
      $result = db_query("SELECT n.created AS timestamp FROM {node} n WHERE n.type = 'support_ticket' AND n.created < :end AND n.created > :begin {$filter} ORDER BY n.created", $args);
      break;
    case 'tickets_updated':
      if ($type == 'user') {
        $chart['#title'] = chart_title(t('Tickets updated by !user in !date', array(
          '!date' => date('F Y', $now),
          '!user' => check_plain($object->name),
        )));
        $args[':uid'] = $object->uid;
        $filter = "AND n.uid = :uid";
      }
      else {
        $chart['#title'] = chart_title(t('Tickets updated in !date', array(
          '!date' => date('F Y', $now),
        )));
        $filter = '';
      }
      $chart['#chart_id'] = $id;
      $args[':end'] = $now;
      $args[':begin'] = mktime(0, 0, 0, date('m', $now), 1, date('Y', $now));
      $result = db_query("SELECT GREATEST(n.changed, l.last_comment_timestamp) AS timestamp FROM {node} n LEFT JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE n.type = 'support_ticket' AND (n.changed < :end OR l.last_comment_timestamp < :end) AND (n.changed > :begin OR l.last_comment_timestamp > :begin) {$filter} ORDER BY timestamp", $args);
      break;
    case 'tickets_closed':
      if ($type == 'user') {
        $chart['#title'] = chart_title(t('Tickets closed by !user in !date', array(
          '!date' => date('F Y', $now),
          '!user' => check_plain($object->name),
        )));
        $args[':uid'] = $object->uid;
        $filter = "AND n.uid = :uid";
      }
      else {
        $chart['#title'] = chart_title(t('Tickets closed in !date', array(
          '!date' => date('F Y', $now),
        )));
        $filter = '';
      }
      $chart['#chart_id'] = $id;
      $args[':end'] = $now;
      $args[':begin'] = mktime(0, 0, 0, date('m', $now), 1, date('Y', $now));
      $result = db_query("SELECT GREATEST(n.changed, l.last_comment_timestamp) AS timestamp FROM {node} n LEFT JOIN {node_comment_statistics} l ON n.nid = l.nid LEFT JOIN {support_ticket} t ON n.nid = t.nid LEFT JOIN {support_states} s ON t.state = s.sid WHERE s.isclosed = 1 AND n.type = 'support_ticket' AND (n.changed < :end OR l.last_comment_timestamp < :end) AND (n.changed > :begin OR l.last_comment_timestamp > :begin) {$filter} ORDER BY timestamp", $args);
      break;
  }
  $max = array();
  $counts = array();
  $types = array();
  foreach ($result as $data) {
    $day = ltrim(date('d', $data->timestamp), '0');
    $types[$type] = $type;
    if (!isset($counts[$day][$type])) {
      $counts[$day][$type] = 0;
    }
    $counts[$day][$type]++;
    if (!isset($max[$type])) {
      $max[$type] = 0;
    }
    $max[$type]++;
  }

  // Generate data and labels
  if (count($counts) && count($types)) {
    for ($i = 0; $i <= date('d', $now); $i++) {
      $chart['#labels'][] = $i;
      foreach ($types as $type) {
        if (!empty($counts[$i][$type])) {
          $chart['#data'][$type][] = $counts[$i][$type];
        }
        else {
          $chart['#data'][$type][] = '0';
        }
      }
    }
  }

  // Data colors, legends, line styles, and labels
  if (count($types)) {
    foreach ($types as $type) {
      $chart['#data_colors'][] = chart_unique_color($type);
      $chart['#legends'][] = $type;
      $chart['#line_styles'][] = chart_line_style(2);
    }
  }
  $max = count($max) ? max($max) : 0;
  $chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_range_label(0, $max);
  return theme('chart', array(
    'chart' => $chart,
  ));
}