You are here

function user_stats_get_stats in User Stats 5

Same name and namespace in other branches
  1. 6 user_stats.module \user_stats_get_stats()
  2. 7 user_stats.module \user_stats_get_stats()

Returns user stats.

Parameters

$type: The statistic to return. Possible values are:

  • "ip_address"
  • "join_date"
  • "login_count"
  • "login_days"
  • "post_count"
  • "post_days"
  • "reg_days"
  • "online"
  • "profile"

$uid: The user id who's stats should be retrieved.

Return value

The statistic requested. Every statistic except join_date, online and IP address is a numeric. Join date is a string, whilst online is a boolean and IP Address a string. Note: if $type = "post_days" and the user hasn't posted any content (of the counted types) then 'n/a' is returned.

2 calls to user_stats_get_stats()
user_stats_preprocess_author_pane in ./user_stats.module
Implementation of hook_preprocess_author_pane(). A preprocess function for the author pane used by Advanced Forum and Advanced Profile Kit.
user_stats_token_values in ./user_stats.module
Implementation of hook_token_values().

File

./user_stats.module, line 220
User Stats provides commonly requested user statistics for themers. These are:

Code

function user_stats_get_stats($type, $uid, $options = array()) {
  $user = _user_stats_user_cache($uid);

  // IP address is really a bit of feature creep. But it's so useful in stopping spammers!
  if (user_access('View IP addresses') && $type == 'ip_address') {
    return $user->user_ip_address;
  }
  if (user_access('View statistics')) {
    switch ($type) {
      case 'join_date':
        return $user->created;
      case 'login_count':
        if (!variable_get('user_stats_count_logins', TRUE)) {
          return 'n/a';
        }
        else {
          if (isset($user->user_login_count)) {
            return $user->user_login_count;
          }
          else {
            return 0;
          }
        }
      case 'login_days':
        return floor((time() - $user->access) / 86400);
      case 'post_count':
        if (!variable_get('user_stats_count_posts', TRUE) && !variable_get('user_stats_count_comments', TRUE)) {
          return 'n/a';
        }

        // If the post count for this user hasn't been set then update it.
        $post_count_profile_field = variable_get('user_stats_post_count_profile_field', 'user_post_count');
        if (!isset($user->{$post_count_profile_field})) {
          user_stats_post_count_update($user, 'reset');
        }
        return $user->{$post_count_profile_field};
      case 'post_days':
        $last_post = _user_stats_last_post($user);
        if ($last_post !== FALSE) {
          return floor((time() - $last_post) / 86400);
        }
        else {
          return 'n/a';
        }
      case 'reg_days':
        return floor((time() - $user->created) / 86400);
      case 'online':
        return round((time() - $user->access) / 60) < 15 ? TRUE : FALSE;
      case 'profile':
        if (!empty($options)) {
          if (isset($user->{$options}['field']) && !is_array($user->{$options}['field'])) {
            return $user->{$options}['field'];
          }
          break;
        }
        break;
    }
  }
}