You are here

function support_overview_summary in Support Ticketing System 6

Same name and namespace in other branches
  1. 7 support_overview/support_overview.module \support_overview_summary()

Display support ticket overview, highlighting ticket queues with tickets older than a configurable age limit.

1 string reference to 'support_overview_summary'
support_overview_menu in support_overview/support_overview.module
Implementation of hook_menu().

File

support_overview/support_overview.module, line 72
support_overview.module

Code

function support_overview_summary() {
  drupal_add_css(drupal_get_path('module', 'support_overview') . '/support-overview.css');

  // Determine which clients to display
  $enabled_clients = variable_get('support_overview_clients', array());
  if (!is_array($enabled_clients) || empty($enabled_clients)) {
    $enabled_clients = _support_available_clients();
  }
  $all_clients = _support_available_clients();
  $clients = array();
  foreach ($enabled_clients as $clid) {

    // Don't display clients user doesn't have permission to see
    if (isset($all_clients[$clid])) {
      $clients[$clid] = support_client_load($clid);
    }
  }
  $enabled_states = variable_get('support_overview_states', array());
  if (!is_array($enabled_states) || empty($enabled_states)) {
    $states = _support_states();
  }
  else {
    $states = array();
    foreach ($enabled_states as $sid) {
      if ($sid) {
        $states[$sid] = _support_state($sid);
      }
      else {
        $states[$sid] = t('all');
      }
    }
  }
  $output = '<div class="support-overview">';
  $header = array(
    'tickets',
    'all',
  );
  $enabled_priorities = variable_get('support_overview_priorities', array());
  foreach ($enabled_priorities as $pid) {
    $header[] = _support_priorities($pid);
  }
  foreach ($clients as $client) {
    $rows = array();
    $current_row = 0;

    // Break down overview by ticket state
    foreach ($states as $sid => $state) {
      $rows[$current_row][] = l($state, "support/{$client->path}/{$state}");
      $total_sql = 'SELECT COUNT(t.nid) FROM {support_ticket} t WHERE t.client = %d';
      $total_args = array(
        $client->clid,
      );
      $latest_sql = $oldest_sql = 'SELECT GREATEST(n.changed, l.last_comment_timestamp) AS last_update, n.nid FROM {node} n LEFT JOIN {support_ticket} t ON n.nid = t.nid INNER JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE t.client = %d';
      $latest_args = $oldest_args = array(
        $client->clid,
      );
      if ($sid) {
        $total_sql .= ' AND t.state = %d';
        $total_args[] = $sid;
        $latest_sql .= ' AND t.state = %d';
        $latest_args[] = $sid;
        $oldest_sql .= ' AND t.state = %d';
        $oldest_args[] = $sid;
      }
      $latest_sql .= ' ORDER BY last_update DESC';
      $oldest_sql .= ' ORDER BY last_update ASC';
      $total = db_result(db_query($total_sql, $total_args));
      $latest = db_fetch_object(db_query_range($latest_sql, $latest_args, 0, 1));
      $oldest = db_fetch_object(db_query_range($oldest_sql, $oldest_args, 0, 1));
      $latest_alert = _support_overview_alert($latest->last_update);
      $oldest_alert = _support_overview_alert($oldest->last_update);
      $alerts = "{$latest_alert} {$oldest_alert}";
      $node = node_load(array(
        'nid' => $latest->nid,
      ));
      $latest_update = $latest->last_update ? t('most recent: !time, "!title"', array(
        '!time' => format_interval(time() - $latest->last_update),
        '!title' => l($node->title, "node/{$node->nid}"),
      )) : '';
      if ($latest->last_update != $oldest->last_update) {
        $node = node_load(array(
          'nid' => $oldest->nid,
        ));
        $oldest_update = $oldest->last_update ? t('oldest: !time, "!title"', array(
          '!time' => format_interval(time() - $oldest->last_update),
          '!title' => l($node->title, "node/{$node->nid}"),
        )) : '';
      }
      else {
        $oldest_update = '';
      }
      $rows[$current_row][] = t('!total !latest !oldest', array(
        '!total' => "<span class='support-overview-total {$state}'>{$total}</span>",
        '!latest' => "<div class='support-overview-latest {$state}{$latest_alert}'>{$latest_update}</div>",
        '!oldest' => "<div class='support-overview-oldest {$state}{$oldest_alert}'>{$oldest_update}</div>",
      ));
      foreach ($enabled_priorities as $pid) {
        if ($pid && $sid) {
          $total = db_result(db_query('SELECT COUNT(nid) FROM {support_ticket} WHERE client = %d AND state = %d AND priority = %d', $client->clid, $sid, $pid));
        }
        else {
          if ($pid) {
            $total = db_result(db_query('SELECT COUNT(nid) FROM {support_ticket} WHERE client = %d AND priority = %d', $client->clid, $pid));
          }
          else {
            $total = db_result(db_query('SELECT COUNT(nid) FROM {support_ticket} WHERE client = %d AND state = %d', $client->clid, $sid));
          }
        }
        if ($total) {
          $priority = _support_priorities($pid);
        }
        else {
          $priority = 'not-' . _support_priorities($pid);
        }
        $rows[$current_row][] = "<div class='" . check_plain($priority) . "'>{$total}</div>";
      }
      $current_row++;
    }

    // Break down overview by who the ticket is assigned to
    $args = array();
    $query = 'SELECT DISTINCT(assigned) AS assigned FROM {support_ticket} WHERE client = %d';
    $args[] = $client->clid;
    if (!isset($enabled_states[0])) {
      $state_append = ' AND state IN(';
      $state_append .= implode(', ', $enabled_states);
      $state_append .= ')';
      $query .= $state_append;
    }
    $query .= ' ORDER BY assigned ASC';
    $result = db_query($query, $args);
    while ($assigned = db_fetch_object($result)) {
      $account = user_load(array(
        'uid' => $assigned->assigned,
      ));
      $total_sql = 'SELECT COUNT(nid) FROM {support_ticket} WHERE client = %d AND assigned = %d';
      $latest_sql = $oldest_sql = 'SELECT GREATEST(n.changed, l.last_comment_timestamp) AS last_update, n.nid FROM {node} n LEFT JOIN {support_ticket} t ON n.nid = t.nid INNER JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE t.client = %d AND assigned = %d';
      if (!empty($state_append)) {
        $total_sql .= $state_append;
        $latest_sql .= $state_append;
        $oldest_sql .= $state_append;
      }
      $total_args = $latest_args = $oldest_args = array(
        $client->clid,
        $assigned->assigned,
      );
      $latest_sql .= ' ORDER BY last_update DESC';
      $oldest_sql .= ' ORDER BY last_update ASC';
      $total = db_result(db_query($total_sql, $total_args));
      $latest = db_fetch_object(db_query_range($latest_sql, $latest_args, 0, 1));
      $oldest = db_fetch_object(db_query_range($oldest_sql, $oldest_args, 0, 1));
      $latest_alert = _support_overview_alert($latest->last_update);
      $oldest_alert = _support_overview_alert($oldest->last_update);
      $alerts = "{$latest_alert} {$oldest_alert}";
      $node = node_load(array(
        'nid' => $latest->nid,
      ));
      $latest_update = $latest->last_update ? t('most recent: !time, "!title"', array(
        '!time' => format_interval(time() - $latest->last_update),
        '!title' => l($node->title, "node/{$node->nid}"),
      )) : '';
      if ($latest->last_update != $oldest->last_update) {
        $node = node_load(array(
          'nid' => $oldest->nid,
        ));
        $oldest_update = $oldest->last_update ? t('oldest: !time, "!title"', array(
          '!time' => format_interval(time() - $oldest->last_update),
          '!title' => l($node->title, "node/{$node->nid}"),
        )) : '';
      }
      else {
        $oldest_update = '';
      }
      if (empty($account->name)) {
        $rows[$current_row][] = "<div class='support-overview-name {$alerts}'>" . t('not assigned') . '</div>';
      }
      else {
        $rows[$current_row][] = "<div class='support-overview-name {$alerts}'>" . l($account->name, "support/{$account->uid}/assigned") . '</div>';
      }
      $rows[$current_row][] = t('!total !latest !oldest', array(
        '!total' => "<span class='support-overview-total {$state}'>{$total}</span>",
        '!latest' => "<div class='support-overview-latest {$state}{$latest_alert}'>{$latest_update}</div>",
        '!oldest' => "<div class='support-overview-oldest {$state}{$oldest_alert}'>{$oldest_update}</div>",
      ));
      foreach ($enabled_priorities as $pid) {
        $query = 'SELECT COUNT(nid) FROM {support_ticket} WHERE client = %d AND assigned = %d';
        $args = array(
          $client->clid,
          $assigned->assigned,
        );
        if ($pid) {
          $query .= ' AND priority = %d';
          $args[] = $pid;
        }
        $query .= $state_append;
        $total = db_result(db_query($query, $args));
        if ($total) {
          $priority = _support_priorities($pid);
        }
        else {
          $priority = 'not-' . _support_priorities($pid);
        }
        $rows[$current_row][] = "<div class='" . check_plain($priority) . "'>{$total}</div>";
      }
      $current_row++;
    }
    $output .= "<a href='" . url("support/{$client->path}") . "'><h3 class='support-overview-client-name {$alerts}'>" . check_plain($client->name) . '</h3></a>';
    $output .= theme('table', $header, $rows);
  }
  $output .= '</div>';
  return $output;
}