You are here

function invite_stats_display_user_rank in Invite 6.2

Same name and namespace in other branches
  1. 5.2 invite_stats.module \invite_stats_display_user_rank()
  2. 5 invite_stats.module \invite_stats_display_user_rank()
  3. 7.2 modules/invite_stats/invite_stats.module \invite_stats_display_user_rank()

Render the displayed user's rank block.

1 call to invite_stats_display_user_rank()
invite_stats_block in ./invite_stats.module
Implementation of hook_block().

File

./invite_stats.module, line 129
Displays some statistics about sent invitations.

Code

function invite_stats_display_user_rank($uid) {
  $inviters = array();
  $max = variable_get('invite_num_ranks', 5);

  // Get user's invite count.
  $user_invite_stats_count = db_result(db_query("SELECT COUNT(*) FROM {invite} WHERE uid = %d AND joined <> 0", $uid));

  // Calculate user's rank.
  $result = db_query("SELECT DISTINCT COUNT(uid) FROM {invite} WHERE joined <> 0 GROUP BY uid HAVING COUNT(uid) > %d", $user_invite_stats_count);
  $rank = 1;
  while ($row = db_fetch_array($result)) {
    $rank++;
  }

  // Fetch users with more invites.
  $i = 0;
  $prev_count = 0;
  $result = db_query_range("SELECT uid, COUNT(uid) AS count FROM {invite} WHERE joined <> 0 GROUP BY uid HAVING COUNT(uid) > %d ORDER BY count ASC", $user_invite_stats_count, 0, $max - 1);
  while ($row = db_fetch_object($result)) {
    if ($row->count != $prev_count) {
      $prev_count = $row->count;
      $rank--;

      // Decrement initial rank
    }
    array_unshift($inviters, $row);
  }

  // Add current user, now we have invite_num_ranks users at max.
  $inviters[] = (object) array(
    'uid' => $uid,
    'count' => $user_invite_stats_count,
  );
  $user_idx = count($inviters);

  // Add users with equal or less invites.
  $i = 0;
  $prev_count = $user_invite_stats_count;
  $result = db_query_range("SELECT uid, COUNT(uid) AS count FROM {invite} WHERE joined <> 0 GROUP BY uid HAVING COUNT(uid) <= %d AND uid <> %d ORDER BY count DESC", $user_invite_stats_count, $uid, 0, $max - 1);
  while ($row = db_fetch_object($result)) {
    $inviters[] = $row;
  }

  // Slice result.
  $start = max($user_idx - 1 - floor($max / 2), 0);
  if (count($inviters) - $user_idx < floor($max / 2)) {
    $start -= max(count($inviters) - $user_idx, floor($max / 2));
    $start = max($start, 0);
  }
  $inviters = array_slice($inviters, $start, $max);
  return theme('invite_stats_ranking', $inviters, $rank);
}