You are here

function _avatar_selection_image_list in Avatar Selection 5.2

Same name and namespace in other branches
  1. 5 avatar_selection.module \_avatar_selection_image_list()
  2. 6 avatar_selection.module \_avatar_selection_image_list()
  3. 7 avatar_selection.module \_avatar_selection_image_list()

Get the list of avatars available to a certain user.

Parameters

$user: User object (optional).

$set_type: Set type, can be 'role' or 'og' (optional).

$set_id: The unique identifier of the set (optional).

$from: The offset.

$count: Number of avatars to return.

Return value

Return an array with the list of avatars for the current user, together with the number of avatars.

3 calls to _avatar_selection_image_list()
avatar_selection_edit_form in ./avatar_selection.module
Create the form structure for listing the avatars and managing them in the 'Manage Avatars' tab under the Avatar Selection administration page.
avatar_selection_form_alter in ./avatar_selection.module
Implementation of hook_form_alter().
avatar_selection_get_random_image in ./avatar_selection.module
Select a random avatar picture for a certain user.

File

./avatar_selection.module, line 299
The Avatar Selection module allows the user to pick an avatar image from a list already loaded by an administrative user, and to the administrator to disable uploading other avatar files by the user.

Code

function _avatar_selection_image_list($user = "", $set_type = "", $set_id = 0, $from = 0, $count = 0) {
  $avatars = array();
  $dir = file_create_path('avatar_selection');
  $url = file_create_url($dir);
  $total = 0;

  // If we're searching on a particular role.
  if ($set_type == 'role') {
    if ($set_id) {
      $total = db_result(db_query("SELECT count(*) FROM {avatar_selection} avs, {avatar_selection_roles} avsr WHERE avs.aid = avsr.aid AND avsr.rid = %d", $set_id));
      $result = db_query_range("SELECT DISTINCT avatar, name, weight FROM {avatar_selection} avs, {avatar_selection_roles} avsr WHERE avs.aid = avsr.aid AND avsr.rid = %d ORDER BY weight, name, avatar", $set_id, $from, $count);
    }
    else {
      $total = db_result(db_query("SELECT count(*) FROM {avatar_selection} avs LEFT JOIN {avatar_selection_roles} avsr ON avs.aid = avsr.aid WHERE avsr.rid IS NULL"));
      $result = db_query_range("SELECT DISTINCT avatar, name, weight FROM {avatar_selection} avs LEFT JOIN {avatar_selection_roles} avsr ON avs.aid = avsr.aid WHERE avsr.rid IS NULL ORDER BY weight, name, avatar", $from, $count);
    }
  }
  elseif ($set_type == 'og') {
    if ($set_id) {
      $total = db_result(db_query("SELECT count(*) FROM {avatar_selection} avs, {avatar_selection_og} og WHERE avs.aid = og.aid AND og.ogid = %d", $set_id));
      $result = db_query_range("SELECT DISTINCT avatar, name, weight FROM {avatar_selection} avs, {avatar_selection_og} og WHERE avs.aid = og.aid AND og.ogid = %d ORDER BY weight, name, avatar", $set_id, $from, $count);
    }
    else {
      $total = db_result(db_query("SELECT count(*) FROM {avatar_selection} avs LEFT JOIN {avatar_selection_og} og ON avs.aid = og.aid WHERE og.ogid IS NULL"));
      $result = db_query_range("SELECT DISTINCT avatar, name, weight FROM {avatar_selection} avs LEFT JOIN {avatar_selection_og} og ON avs.aid = og.aid WHERE og.ogid IS NULL ORDER BY weight, name, avatar", $from, $count);
    }
  }
  elseif (!empty($user)) {

    // Set up some variables.
    $user_roles = array();
    $user_roles_placeholders = '';
    if (is_array($user->roles) && !empty($user->roles)) {
      $user_roles = array_keys($user->roles);
      $user_roles_placeholders = implode(',', array_fill(0, count($user_roles), '%d'));
    }
    $user_og = array();
    $user_og_placeholders = '';
    if (module_exists("og")) {
      if (!empty($user->og_groups) && is_array($user->og_groups)) {
        $user_og = array_keys($user->og_groups);
        $user_og_placeholders = implode(',', array_fill(0, count($user_og), '%d'));
      }
    }

    // Distinct avatars are enabled.
    if (variable_get('avatar_selection_distinctive_avatars', FALSE)) {

      // Organic groups enabled.
      if (module_exists("og") && !empty($user_og)) {
        $total = db_result(db_query("SELECT count(distinct avs.aid) FROM {avatar_selection} avs LEFT JOIN {users} u ON u.picture = concat('%s/', avs.avatar) LEFT JOIN {avatar_selection_roles} avsr ON avs.aid = avsr.aid LEFT JOIN {avatar_selection_og} avso ON avs.aid = avso.aid WHERE u.picture IS NULL AND (avsr.rid IS NULL OR avsr.rid NOT IN (" . $user_roles_placeholders . ")) AND (avso.ogid IS NULL OR avso.ogid NOT IN (" . $user_og_placeholders . "))", $dir, $user_roles, $user_og));
        if ($count == 0) {
          $count = $total;
        }
        $result = db_query_range("SELECT DISTINCT avatar, avs.name, avs.weight FROM {avatar_selection} avs LEFT JOIN {users} u ON u.picture = concat('%s/', avs.avatar) LEFT JOIN {avatar_selection_roles} avsr ON avs.aid = avsr.aid LEFT JOIN {avatar_selection_og} avso ON avs.aid = avso.aid WHERE u.picture IS NULL AND (avsr.rid IS NULL OR avsr.rid NOT IN (" . $user_roles_placeholders . ")) AND (avso.ogid IS NULL OR avso.ogid NOT IN (" . $user_og_placeholders . ")) ORDER BY avs.weight, avs.name, avatar", $dir, $user_roles, $user_og, $from, $count);
      }
      else {
        $total = db_result(db_query("SELECT count(distinct avs.aid) FROM {avatar_selection} avs LEFT JOIN {users} u ON u.picture = concat('%s/', avs.avatar) LEFT JOIN {avatar_selection_roles} avsr ON avs.aid = avsr.aid WHERE u.picture IS NULL AND (avsr.rid IS NULL OR avsr.rid NOT IN (" . $user_roles_placeholders . "))", $dir, $user_roles));
        if ($count == 0) {
          $count = $total;
        }
        $result = db_query_range("SELECT DISTINCT avatar, avs.name, avs.weight FROM {avatar_selection} avs LEFT JOIN {users} u ON u.picture = concat('%s/', avs.avatar) LEFT JOIN {avatar_selection_roles} avsr ON avs.aid = avsr.aid WHERE u.picture IS NULL AND (avsr.rid IS NULL OR avsr.rid NOT IN (" . $user_roles_placeholders . ")) ORDER BY avs.weight, avs.name, avatar", $dir, $user_roles, $from, $count);
      }
    }
    elseif ($user->uid > 1 || in_array(1, array_keys($user->roles))) {

      // Organic groups enabled.
      if (module_exists("og") && !empty($user_og)) {
        $total = db_result(db_query("SELECT count(distinct avs.aid) FROM {avatar_selection} avs LEFT JOIN {avatar_selection_roles} avsr ON avs.aid = avsr.aid LEFT JOIN {avatar_selection_og} avso ON avs.aid = avso.aid WHERE (avsr.rid IS NULL OR avsr.rid IN (" . $user_roles_placeholders . ")) AND (avso.ogid IS NULL OR avso.ogid IN (" . $user_og_placeholders . "))", $user_roles, $user_og));
        if ($count == 0) {
          $count = $total;
        }
        $result = db_query_range("SELECT DISTINCT avatar, name, weight FROM {avatar_selection} avs LEFT JOIN {avatar_selection_roles} avsr ON avs.aid = avsr.aid LEFT JOIN {avatar_selection_og} avso ON avs.aid = avso.aid WHERE (avsr.rid IS NULL OR avsr.rid IN (" . $user_roles_placeholders . ")) AND (avso.ogid IS NULL OR avso.ogid IN (" . $user_og_placeholders . ")) ORDER BY weight, name, avatar", $user_roles, $user_og, $from, $count);
      }
      else {
        $total = db_result(db_query("SELECT count(distinct avs.aid) FROM {avatar_selection} avs LEFT JOIN {avatar_selection_roles} avsr ON avs.aid = avsr.aid WHERE avsr.rid IS NULL OR avsr.rid IN (" . $user_roles_placeholders . ")", $user_roles));
        if ($count == 0) {
          $count = $total;
        }
        $result = db_query_range("SELECT DISTINCT avatar, name, weight FROM {avatar_selection} avs LEFT JOIN {avatar_selection_roles} avsr ON avs.aid = avsr.aid WHERE avsr.rid IS NULL OR avsr.rid IN (" . $user_roles_placeholders . ") ORDER BY weight, name, avatar", $user_roles, $from, $count);
      }
    }
    else {
      $total = db_result(db_query("SELECT count(*) FROM {avatar_selection} avs"));
      if ($count == 0) {
        $count = $total;
      }
      $result = db_query_range("SELECT aid, avatar, name, weight FROM {avatar_selection} avs ORDER BY weight, name, avatar", $from, $count);
    }
  }
  while ($avatar = db_fetch_object($result)) {
    $avs_image = $avatar->avatar;
    $name = $avatar->name;
    $avatars[$avs_image] = theme('image', $url . '/' . $avs_image, $name, $name, NULL, FALSE);
  }
  $selects['avatars'] = $avatars;
  $selects['total'] = $total;
  return $selects;
}