You are here

function _support_assigned in Support Ticketing System 7

Same name and namespace in other branches
  1. 6 support.module \_support_assigned()

Get list of available users to assign ticket to.

5 calls to _support_assigned()
support_mailcmd_support_fetch_message_alter in support_mailcmd/support_mailcmd.module
Implementation of hook_support_fetch_message_alter().
support_page_form in ./support.module
Display tickets
_support_comment_insert_update2 in ./support.module
_support_status_form_attach in ./support.module
Generate form for adding update to ticket. Enhances comment_form adding a ticket status bar.
_support_subscribe_form_attach in ./support.module
Provide option to subscribe/unsubscribe from ticket notification emails.

File

./support.module, line 2746
support.module

Code

function _support_assigned($assigned, $node, $limit = 9999) {
  $node = clone $node;
  global $user;
  static $available = array();
  $counter = 0;
  if (!$limit) {
    return FALSE;
  }
  if (!isset($node->nid)) {
    $node->nid = 0;
  }
  if (!isset($available["{$assigned}-{$node->nid}"])) {
    if ($assigned && $assigned != $user->uid) {

      // TODO Convert "user_load" to "user_load_multiple" if "$assigned" is other than a uid.
      // To return a single user object, wrap "user_load_multiple" with "array_shift" or equivalent.
      // Example: array_shift(user_load_multiple(array(), $assigned))
      $account = user_load($assigned);
      $available["{$assigned}-{$node->nid}"][$account->uid] = $account->name;
      $counter++;
    }

    // can always re-assign ticket to self
    $available["{$assigned}-{$node->nid}"][$user->uid] = $user->name;
    $counter++;
    if (isset($node->client) && is_numeric($node->client) && (user_access('administer support') || user_access('can assign tickets to any user'))) {
      $roles = array();
      $client = db_query('SELECT name FROM {support_client} WHERE clid = :clid', array(
        ':clid' => $node->client,
      ))
        ->fetchField();

      // retrieve all roles giving permission to access current tickets
      $result = db_query('SELECT rid FROM {role_permission} WHERE module = :module AND permission = :access OR permission = :admin', array(
        ':module' => 'support',
        ':access' => 'access ' . $client . ' tickets',
        ':admin' => 'administer support',
      ));
      foreach ($result as $role) {
        $roles[$role->rid] = $role->rid;
      }

      // retrieve all users in appropriate roles
      $accounts = array();
      $all = FALSE;
      foreach ($roles as $rid) {
        if ($rid == DRUPAL_AUTHENTICATED_RID) {
          $all = TRUE;
          $result = db_select('users', 'u')
            ->fields('u', array(
            'uid',
          ))
            ->condition('u.status', 1, '=')
            ->range(0, $limit)
            ->execute();
        }
        else {
          $query = db_select('users_roles', 'r');
          $query
            ->join('users', 'u', 'r.uid = u.uid');
          $result = $query
            ->fields('r', array(
            'uid',
          ))
            ->condition('r.rid', $rid, '=')
            ->condition('u.status', 1, '=')
            ->range(0, $limit)
            ->execute();
        }
        foreach ($result as $account) {
          $accounts[$account->uid] = $account->uid;
          $counter++;
          if (!$limit || $counter > $limit) {
            return FALSE;
          }
        }

        // we've already retrieved all active users, no need to search
        // additional roles
        if ($all) {
          break;
        }
      }

      // load users and allow them to be assigned
      foreach ($accounts as $uid) {
        $account = user_load($uid);
        $available["{$assigned}-{$node->nid}"][$account->uid] = $account->name;
      }

      // Filter uid1 if support_filter_uid1 enabled; however, don't filter if the ticket is already
      // assigned to uid1, or current user is uid1.
      if (variable_get('support_filter_uid1', FALSE) && $user->uid != 1 && $assigned != 1) {
        unset($available["{$assigned}-{$node->nid}"][1]);
      }
    }

    // sort by name
    asort($available["{$assigned}-{$node->nid}"]);

    // can only unassign tickets if assigned to self, or have admin permissions
    // (always put this at the top of the array)
    if (!$assigned || $assigned == $user->uid || user_access('can assign tickets to any user') || user_access('administer support')) {
      $available["{$assigned}-{$node->nid}"] = array(
        0 => ' - ' . t('not assigned') . ' -',
      ) + $available["{$assigned}-{$node->nid}"];
    }
  }
  return $available["{$assigned}-{$node->nid}"];
}