You are here

function user_stats_post_count_update in User Stats 5

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

Manage the post count of a given user.

Parameters

$user: Reference to user object.

$op: Whether the user post count should be incremented, decremented, or reset. The default is to increment. Possible values are: 'increment' (default) 'decrement' 'reset'

7 calls to user_stats_post_count_update()
user_stats_action_post_count_decrement in ./user_stats.module
hook_action_info() callback
user_stats_action_post_count_increment in ./user_stats.module
hook_action_info() callback
user_stats_action_post_count_reset in ./user_stats.module
hook_action_info() callback
user_stats_comment in ./user_stats.module
Implementation of hook_comment().
user_stats_cron in ./user_stats.module
Implementation of hook_cron().

... See full list

File

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

Code

function user_stats_post_count_update(&$user, $op = 'increment') {

  // The lock stops infinite recursions, the action will still happen, but the
  // corresponding event (the reason for an infinite loop) will not be fired.
  static $locked;

  // Shared SQL for increment and decrement
  $sql = "UPDATE {profile_values} SET value=%d\n    WHERE fid = (SELECT fid FROM {profile_fields} WHERE name = '%s')\n    AND uid=%d";
  $post_count_profile_field = variable_get('user_stats_post_count_profile_field', 'user_post_count');
  switch ($op) {
    case 'increment':
      if (!isset($user->{$post_count_profile_field})) {
        $user = user_stats_post_count_update($user, 'reset');
      }
      else {
        $user->{$post_count_profile_field}++;
        db_query($sql, $user->{$post_count_profile_field}, $post_count_profile_field, $user->uid);

        // Flush user cache.
        _user_stats_user_cache($user->uid, TRUE);

        // Flush token cache.
        if (module_exists('token')) {
          token_get_values('user', NULL, TRUE);
        }

        // We use $locked to stop infinite loops.
        if (!$locked && module_exists('workflow_ng')) {
          $locked = TRUE;
          workflow_ng_invoke_event('user_stats_post_count_increment', $user);
          $locked = FALSE;
        }
      }
      break;
    case 'decrement':
      if (!isset($user->{$post_count_profile_field})) {
        $user = user_stats_post_count_update($user, 'reset');
      }
      else {
        $user->{$post_count_profile_field}--;
        db_query($sql, max($user->{$post_count_profile_field}, 0), $post_count_profile_field, $user->uid);

        // Flush user cache.
        _user_stats_user_cache($user->uid, TRUE);

        // Flush token cache.
        if (module_exists('token')) {
          token_get_values('user', NULL, TRUE);
        }
        if (!$locked && module_exists('workflow_ng')) {
          $locked = TRUE;
          workflow_ng_invoke_event('user_stats_post_count_decrement', $user);
          $locked = FALSE;
        }
      }
      break;
    case 'reset':
      static $fid;
      $total_count = 0;
      if (!isset($fid)) {
        $sql = "SELECT fid FROM {profile_fields} WHERE name='%s'";
        $fid = db_result(db_query($sql, $post_count_profile_field));
      }
      if (variable_get('user_stats_count_posts', TRUE)) {
        $sql = "SELECT COUNT(*) FROM {node} WHERE uid=%d AND status=1";
        $post_count_content_types = variable_get('user_stats_included_content_types', array());
        if (!empty($post_count_content_types)) {
          $content_types = "'" . implode("','", $post_count_content_types) . "'";
          $where = ' AND type IN (' . $content_types . ')';
          $sql .= $where;
        }
        $node_count = db_result(db_query($sql, $user->uid));
        $total_count += $node_count;
      }
      if (variable_get('user_stats_count_comments', TRUE)) {
        $sql = "SELECT COUNT(*) FROM {comments} c INNER JOIN {node} n ON c.nid=n.nid WHERE c.uid=%d AND c.status=0 AND n.status=1";
        if (!empty($post_count_content_types)) {
          $where = ' AND n.type IN (' . $content_types . ')';
          $sql .= $where;
        }
        $comments_count = db_result(db_query($sql, $user->uid));
        $total_count += $comments_count;
      }
      $sql = "DELETE FROM {profile_values}\n        WHERE fid=%d AND uid=%d";
      db_query($sql, $fid, $user->uid);
      $sql = "INSERT INTO {profile_values} (fid, uid, value)\n        VALUES ( %d, %d, %d )";
      db_query($sql, $fid, $user->uid, $total_count);
      $user->user_post_count = $total_count;

      // Flush user cache.
      _user_stats_user_cache($user->uid, TRUE);

      // Flush token cache
      if (module_exists('token')) {
        token_get_values('user', NULL, TRUE);
      }
      if (!$locked && module_exists('workflow_ng')) {
        $locked = TRUE;
        workflow_ng_invoke_event('user_stats_post_count_reset', $user);
        $locked = FALSE;
      }
      break;
  }
}