You are here

function support_charts_build_pie in Support Ticketing System 7

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

Gather data and build a chart API structure.

Return value

array chart API structure.

1 call to support_charts_build_pie()
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 119
Support charting. @author Jeremy Andrews <jeremy@tag1consulting.com> @package Support

Code

function support_charts_build_pie($id, $type = 'global', $object = NULL) {
  $chart = array();
  $chart['#type'] = CHART_TYPE_PIE;
  $chart['#size'] = chart_size(600, 350);
  $args = array();
  switch ($id) {
    case 'tickets_open':
      $chart['#chart_id'] = 'tickets_open';
      if ($type == 'user') {
        $chart['#title'] = t('Tickets opened by !user', array(
          '!user' => check_plain($object->name),
        ));
        $args[':uid'] = $object->uid;
        $filter = "AND n.uid = :uid";
      }
      else {
        $chart['#title'] = t('Open tickets');
        $filter = '';
      }
      $result = db_query("SELECT COUNT(t.nid) AS count, s.state as label, s.isclosed FROM {support_ticket} t LEFT JOIN {support_states} s ON t.state = s.sid LEFT JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 {$filter} GROUP BY s.isclosed ORDER BY s.isclosed", $args);
      break;
    case 'tickets_per_state':
      $chart['#chart_id'] = 'tickets_per_state';
      if ($type == 'user') {
        $chart['#title'] = t('Open tickets by !user per state', array(
          '!user' => check_plain($object->name),
        ));
        $args[':uid'] = $object->uid;
        $filter = "AND n.uid = :uid";
      }
      else {
        if ($type == 'user_assigned') {
          $chart['#title'] = t('Open tickets assigned to !user per state', array(
            '!user' => check_plain($object->name),
          ));
          $args[':uid'] = $object->uid;
          $filter = "AND t.assigned = :uid";
        }
        else {
          $chart['#title'] = t('Open tickets per state');
          $filter = '';
        }
      }
      $result = db_query("SELECT COUNT(t.nid) AS count, s.state as label FROM {support_ticket} t LEFT JOIN {support_states} s ON t.state = s.sid LEFT JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND s.isclosed = 0 {$filter} GROUP BY s.state ORDER BY s.state", $args);
      break;
    case 'tickets_per_priority':
      $chart['#chart_id'] = 'tickets_per_priority';
      if ($type == 'user') {
        $chart['#title'] = t('Open tickets by !user per priority', array(
          '!user' => check_plain($object->name),
        ));
        $args[':uid'] = $object->uid;
        $filter = "AND n.uid = :uid";
      }
      else {
        if ($type == 'user_assigned') {
          $chart['#title'] = t('Open tickets assigned to !user per priority', array(
            '!user' => check_plain($object->name),
          ));
          $args[':uid'] = $object->uid;
          $filter = "AND t.assigned = :uid";
        }
        else {
          $chart['#title'] = t('Open tickets per priority');
          $filter = '';
        }
      }
      $result = db_query("SELECT COUNT(t.nid) AS count, p.priority as label FROM {support_ticket} t LEFT JOIN {support_priority} p ON t.priority = p.pid LEFT JOIN {support_states} s ON t.state = s.sid LEFT JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND s.isclosed = 0 {$filter} GROUP BY t.priority ORDER BY t.priority", $args);
      break;
    case 'tickets_per_client':
      $chart['#chart_id'] = 'tickets_per_client';
      if ($type == 'user') {
        $chart['#title'] = t('Open tickets by !user per client', array(
          '!user' => check_plain($object->name),
        ));
        $args[':uid'] = $object->uid;
        $filter = "AND n.uid = :uid";
      }
      else {
        if ($type == 'user_assigned') {
          $chart['#title'] = t('Open tickets assigned to !user per client', array(
            '!user' => check_plain($object->name),
          ));
          $args[':uid'] = $object->uid;
          $filter = "AND t.assigned = :uid";
        }
        else {
          $chart['#title'] = t('Open tickets per client');
          $filter = '';
        }
      }
      $result = db_query("SELECT COUNT(t.nid) AS count, c.name as label FROM {support_ticket} t LEFT JOIN {support_client} c ON t.client = c.clid LEFT JOIN {support_states} s ON t.state = s.sid LEFT JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND s.isclosed = 0 {$filter} GROUP BY t.client ORDER BY t.client", $args);
      break;
    case 'tickets_per_assigned':
      $chart['#chart_id'] = 'tickets_per_assigned';
      if ($type == 'user_assigned') {
        $chart['#title'] = t('Tickets assigned to !user', array(
          '!user' => check_plain($object->name),
        ));
        $result = db_query('SELECT COUNT(t.nid) AS count, s.state as label, s.isclosed FROM {support_ticket} t LEFT JOIN {users} u ON t.assigned = u.uid LEFT JOIN {support_states} s ON t.state = s.sid LEFT JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND t.assigned = :assigned GROUP BY s.isclosed ORDER BY s.isclosed', array(
          ':assigned' => $object->uid,
        ));
      }
      else {
        $chart['#title'] = t('Assigned open tickets');
        $result = db_query('SELECT COUNT(t.nid) AS count, u.name as label FROM {support_ticket} t LEFT JOIN {users} u ON t.assigned = u.uid LEFT JOIN {support_states} s ON t.state = s.sid LEFT JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND s.isclosed = 0 GROUP BY t.assigned ORDER BY t.assigned');
      }
      break;
  }
  foreach ($result as $data) {
    if ($id == 'tickets_open') {
      if ($data->isclosed) {
        $label = t('closed');
      }
      else {
        $label = t('open');
      }
    }
    else {
      if ($id == 'tickets_per_assigned') {
        if (empty($data->label)) {
          $label = t('Not assigned');
        }
        else {
          $label = $data->label;
        }
      }
      else {
        $label = isset($data->label) ? $data->label : t('unknown');
      }
    }
    $chart['#data'][] = $data->count;
    $chart['#labels'][] = "{$label}: {$data->count}";
    $chart['#data_colors'][] = chart_unique_color($label);
    $chart['#adjust_resolution'] = TRUE;
  }
  return theme('chart', array(
    'chart' => $chart,
  ));
}