You are here

support_overview.module in Support Ticketing System 6

Same filename and directory in other branches
  1. 7 support_overview/support_overview.module

File

support_overview/support_overview.module
View source
<?php

/**
 * @file support_overview.module
 */

/**
 * Implementation of hook_perm().
 */
function support_overview_perm() {
  return array(
    'view client overview',
    'view user overview',
    'administer support overview',
  );
}

/**
 * Implementation of hook_menu().
 */
function support_overview_menu() {
  $items = array();
  $items['support/overview/user'] = array(
    'title' => 'User ticket overview',
    'page callback' => 'support_overview_page_users',
    'access arguments' => array(
      'view user overview',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['support/overview/user/all'] = array(
    'title' => 'User ticket overview',
    'page callback' => 'support_overview_page_users',
    'access arguments' => array(
      'view user overview',
    ),
    'page arguments' => array(
      TRUE,
    ),
    'type' => MENU_CALLBACK,
  );
  $items['support/overview/user/%user'] = array(
    'page callback' => 'support_overview_page_user',
    'page arguments' => array(
      3,
    ),
    'access callback' => 'support_access_user_tickets',
    'access arguments' => array(
      3,
    ),
    'type' => MENU_CALLBACK,
  );
  $items['support/overview/user/%user/all'] = array(
    'page callback' => 'support_overview_page_user',
    'page arguments' => array(
      3,
      TRUE,
    ),
    'access callback' => 'support_access_user_tickets',
    'access arguments' => array(
      3,
    ),
    'type' => MENU_CALLBACK,
  );
  $items['support/overview/client'] = array(
    'title' => 'Client ticket overview',
    'description' => 'Support client overview',
    'page callback' => 'support_overview_summary',
    'access arguments' => array(
      'view client overview',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/support/overview'] = array(
    'title' => 'Overview settings',
    'description' => 'Support client overview',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'support_overview_summary_settings',
    ),
    'access arguments' => array(
      'administer support overview',
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 2,
  );
  return $items;
}

/**
 * Display support ticket overview, highlighting ticket queues with tickets
 * older than a configurable age limit.
 */
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;
}

/**
 * Configure what to display on the summary page.
 */
function support_overview_summary_settings() {
  drupal_set_title(t('Support overview settings'));
  $form = array();
  $clients = _support_available_clients();
  $form['clients'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#title' => t('Include selected client(s)'),
    '#options' => $clients,
    '#size' => count($clients) > 10 ? 10 : count($clients),
    '#description' => t('If no clients are selected, all clients will be included on the overview page.  Otherwise, only the selected clients will be included on the overview page.'),
    '#default_value' => variable_get('support_overview_clients', array()),
  );
  $states = _support_states();
  $states[0] = t('all');
  $form['states'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#title' => t('List selected state(s)'),
    '#options' => $states,
    '#size' => count($states) > 10 ? 10 : count($states),
    '#description' => t('If no states are selected, all states will be broken out on the overview page.  Otherwise, only the selected states will be broken out on the overview page.'),
    '#default_value' => variable_get('support_overview_states', array()),
  );
  $priorities = _support_priorities();
  $form['priorities'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#title' => t('Break out selected priorities'),
    '#options' => $priorities,
    '#size' => count($priorities) > 10 ? 10 : count($priorities),
    '#description' => t('Select one or more priorities to display an extra column showing tickets of this priority.  If no priorities are selected, tickets won\'t be displayed by priority.'),
    '#default_value' => variable_get('support_overview_priorities', array()),
  );
  $form['alerts'] = array(
    '#type' => 'fieldset',
    '#title' => t('Alerts'),
    '#collapsible' => TRUE,
  );
  $options = array(
    '0' => t('Never'),
  ) + drupal_map_assoc(array(
    86400,
    86400 * 2,
    86400 * 3,
    86400 * 4,
    86400 * 5,
    86400 * 6,
    86400 * 7,
    86400 * 14,
    86400 * 21,
    86400 * 28,
  ), 'format_interval');
  $form['alerts']['alert1'] = array(
    '#type' => 'select',
    '#title' => 'Alert level 1',
    '#options' => $options,
    '#default_value' => variable_get('support_overview_alert1', 0),
    '#description' => t('Tickets that haven\'t been updated for more than the specified time will be marked with a "alert1" CSS class.'),
  );
  $form['alerts']['alert2'] = array(
    '#type' => 'select',
    '#title' => 'Alert level 2',
    '#options' => $options,
    '#default_value' => variable_get('support_overview_alert2', 0),
    '#description' => t('Tickets that haven\'t been updated for more than the specified time will be marked with a "alert2" CSS class.'),
  );
  $form['alerts']['alert3'] = array(
    '#type' => 'select',
    '#title' => 'Alert level 3',
    '#options' => $options,
    '#default_value' => variable_get('support_overview_alert3', 0),
    '#description' => t('Tickets that haven\'t been updated for more than the specified time will be marked with a "alert3" CSS class.'),
  );
  $form['user'] = array(
    '#type' => 'fieldset',
    '#title' => 'User overview settings',
    '#collapsible' => TRUE,
  );
  $form['user']['roles'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#title' => 'Display user from selected role(s)',
    '#options' => user_roles(),
    '#default_value' => variable_get('support_overview_roles', 0),
    '#description' => t('Optionally select one or more roles to limit users displayed on user overview pages.'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}

/**
 * Validate overview settings.
 */
function support_overview_summary_settings_validate($form, &$form_state) {
  if (!empty($form_state['values']['alert2']) && $form_state['values']['alert1'] > $form_state['values']['alert2']) {
    form_set_error('alert1', t('Alert 1 must be a smaller value than alert 2.'));
  }
  if (!empty($form_state['values']['alert3']) && $form_state['values']['alert1'] > $form_state['values']['alert3']) {
    form_set_error('alert1', t('Alert 1 must be a smaller value than alert 3.'));
  }
  if (!empty($form_state['values']['alert3']) && $form_state['values']['alert2'] > $form_state['values']['alert3']) {
    form_set_error('alert2', t('Alert 2 must be a smaller value than alert 3.'));
  }
}

/**
 * Save overview settings.
 */
function support_overview_summary_settings_submit($form, &$form_state) {
  variable_set('support_overview_clients', $form_state['values']['clients']);
  variable_set('support_overview_states', $form_state['values']['states']);
  variable_set('support_overview_priorities', $form_state['values']['priorities']);
  variable_set('support_overview_alert1', $form_state['values']['alert1']);
  variable_set('support_overview_alert2', $form_state['values']['alert2']);
  variable_set('support_overview_alert3', $form_state['values']['alert3']);
  variable_set('support_overview_roles', $form_state['values']['roles']);
  drupal_set_message('Overview settings updated.');
}

/**
 * Add CSS classes to highlight clients and users with old tickts.
 */
function _support_overview_alert($time) {
  if (!empty($time)) {
    $alert3 = variable_get('support_overview_alert3', 0);
    $alert2 = variable_get('support_overview_alert2', 0);
    $alert1 = variable_get('support_overview_alert1', 0);

    // Level 3 alert
    if ($alert3 && time() - $time > $alert3) {
      return ' alert3';
    }

    // Level 2 alert
    if ($alert2 && time() - $time > $alert2) {
      return ' alert2';
    }

    // Level 1 alert
    if ($alert1 && time() - $time > $alert1) {
      return ' alert1';
    }
  }
}

/**
 * Implementation of hook_user().
 */
function support_overview_user($type, &$edit, &$account) {
  global $user;
  if (variable_get('support_display_user_links', TRUE)) {
    if ($type == 'view' && (user_access('create tickets', $account) && $user->uid == $account->uid || user_access('administer support'))) {
      $items = array(
        l(t('Overview'), "support/overview/user/{$account->uid}", array(
          'attributes' => array(
            'title' => t("View overview of @username's tickets.", array(
              '@username' => check_plain($account->name),
            )),
          ),
        )),
      );
      $account->content['summary']['support']['#value'] .= theme('item_list', $items);
    }
  }
}
function support_overview_page_user($account, $all = FALSE, $page = TRUE) {
  global $user;
  if ($page) {
    drupal_add_css(drupal_get_path('module', 'support_overview') . '/support-overview.css');
    $output = '<div class="support-overview">';
    if ($user->uid == $account->uid) {
      drupal_set_title(t('My ticket overview'));
    }
    else {
      drupal_set_title(t("@username's ticket overview", array(
        '@username' => $account->name,
      )));
    }
    $breadcrumb = array();
    $breadcrumb[] = l(t('Home'), NULL);
    $breadcrumb[] = l($account->name, "user/{$account->uid}");
    drupal_set_breadcrumb($breadcrumb);
  }
  $enabled_clients = variable_get('support_overview_clients', array());
  $all_clients = _support_available_clients();
  if ($all || !is_array($enabled_clients) || empty($enabled_clients)) {
    $enabled_clients = array();
    foreach ($all_clients as $clid => $client) {
      $enabled_clients[] = $clid;
    }
    $all = TRUE;
  }
  $header = array(
    t('Client'),
    t('Tickets'),
  );
  $rows = array();
  $current_row = 0;
  foreach ($enabled_clients as $clid) {
    if (isset($all_clients[$clid])) {
      $total_sql = 'SELECT COUNT(t.nid) FROM {support_ticket} t WHERE t.client = %d AND t.assigned = %d';
      $total_args = array(
        $clid,
        $account->uid,
      );
      $enabled_states = variable_get('support_overview_states', array());
      if (!isset($enabled_states[0])) {
        $state_append = ' AND state IN(';
        $state_append .= implode(', ', $enabled_states);
        $state_append .= ')';
        $total_sql .= $state_append;
      }
      $count = db_result(db_query($total_sql, $total_args));
      if ($count) {
        $client = support_client_load($clid);
        $rows[$current_row][] = '<a href="' . url("support/{$client->path}") . '" class="support-overview-title">' . check_plain($client->name) . '</a>';
        $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)) {
          $latest_sql .= $state_append;
          $oldest_sql .= $state_append;
        }
        $args = array(
          $clid,
          $account->uid,
        );
        $latest_sql .= ' ORDER BY last_update DESC';
        $latest = db_fetch_object(db_query_range($latest_sql, $args, 0, 1));
        $latest_alert = _support_overview_alert($latest->last_update);
        $node = node_load(array(
          'nid' => $latest->nid,
        ));
        $latest_update = t('most recent: !time ago, "!title"', array(
          '!time' => format_interval(time() - $latest->last_update),
          '!title' => l($node->title, "node/{$latest->nid}"),
        ));
        if ($count > 1) {
          $oldest_sql .= ' ORDER BY last_update ASC';
          $oldest = db_fetch_object(db_query_range($oldest_sql, $args, 0, 1));
          $oldest_alert = _support_overview_alert($oldest->last_update);
          $node = node_load(array(
            'nid' => $oldest->nid,
          ));
          $oldest_update = t('oldest: !time ago, "!title"', array(
            '!time' => format_interval(time() - $oldest->last_update),
            '!title' => l($node->title, "node/{$oldest->nid}"),
          ));
        }
        else {
          $oldest_update = '';
        }
        $rows[$current_row][] = t('!total !latest !oldest', array(
          '!total' => "<span class='support-overview-total'>{$count}</span>",
          '!latest' => "<div class='support-overview-latest {$latest_alert}'>{$latest_link} {$latest_update}</div>",
          '!oldest' => "<div class='support-overview-oldest {$oldest_alert}'>{$oldest_link} {$oldest_update}</div>",
        ));
      }
      $current_row++;
    }
  }
  $output .= theme('table', $header, $rows);
  if ($page) {
    $output .= '<div class="support-overview-link">' . l($all ? t('selected clients') : t('all clients'), $all ? "support/overview/user/{$account->uid}" : "support/overview/user/{$account->uid}/all") . '</div>';
    $output .= '</div>';
  }
  return $output;
}
function support_overview_page_users($all = FALSE) {
  drupal_add_css(drupal_get_path('module', 'support_overview') . '/support-overview.css');
  $output = '<div class="support-overview">';
  $enabled_clients = variable_get('support_overview_clients', array());
  $all_clients = _support_available_clients();
  if ($all || !is_array($enabled_clients) || empty($enabled_clients)) {
    $enabled_clients = array();
    foreach ($all_clients as $clid => $client) {
      $enabled_clients[] = $clid;
    }
    $all = TRUE;
  }
  $args = array(
    implode(',', $enabled_clients),
  );
  $enabled_roles = variable_get('support_overview_roles', array());
  if (!empty($enabled_roles)) {
    $sql = 'SELECT DISTINCT(t.assigned) FROM {support_ticket} t LEFT JOIN {users} u ON t.assigned = u.uid LEFT JOIN {users_roles} r ON u.uid = r.uid WHERE t.client IN (%s) AND t.assigned != 0 AND r.rid IN (%s)';
    $args[] = implode(',', $enabled_roles);
  }
  else {
    $sql = 'SELECT DISTINCT(t.assigned) FROM {support_ticket} t LEFT JOIN {users_roles} u ON t.assigned = u.uid WHERE t.client IN (%s) AND t.assigned != 0';
  }
  $enabled_states = variable_get('support_overview_states', array());
  if (!isset($enabled_states[0])) {
    $state_append = ' AND state IN(';
    $state_append .= implode(', ', $enabled_states);
    $state_append .= ')';
    $sql .= $state_append;
  }
  $sql .= ' ORDER BY u.name ASC';
  $result = db_query($sql, $args);
  while ($uid = db_result($result)) {
    $account = user_load(array(
      'uid' => $uid,
    ));
    $output .= "<a href='" . url("support/overview/user/{$uid}") . "'><h3>" . check_plain($account->name) . '</h3></a>';
    $output .= support_overview_page_user($account, $all, FALSE);
  }
  $output .= '<div class="support-overview-link">' . l($all ? t('selected clients') : t('all clients'), $all ? "support/overview/user" : "support/overview/user/all") . '</div>';
  $output .= '</div>';
  return $output;
}

Functions

Namesort descending Description
support_overview_menu Implementation of hook_menu().
support_overview_page_user
support_overview_page_users
support_overview_perm Implementation of hook_perm().
support_overview_summary Display support ticket overview, highlighting ticket queues with tickets older than a configurable age limit.
support_overview_summary_settings Configure what to display on the summary page.
support_overview_summary_settings_submit Save overview settings.
support_overview_summary_settings_validate Validate overview settings.
support_overview_user Implementation of hook_user().
_support_overview_alert Add CSS classes to highlight clients and users with old tickts.