You are here

function user_stats_post_count_update in User Stats 6

Same name and namespace in other branches
  1. 5 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

$uid: Unique id of the user who's record should be updated.

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

5 calls to user_stats_post_count_update()
user_stats_comment in ./user_stats.module
Implementation of hook_comment().
user_stats_cron in ./user_stats.module
Implementation of hook_cron().
user_stats_get_stats in ./user_stats.module
Returns user stats.
user_stats_nodeapi in ./user_stats.module
Implementation of hook_nodeapi().
user_stats_post_count_reset_action in ./user_stats.module
Implementation of a Drupal action. Resets a user's post count.

File

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

Code

function user_stats_post_count_update($op, $uid) {
  if (!is_numeric($uid)) {
    return;
  }
  switch ($op) {
    case 'increment':
      if (user_stats_isset('post_count', $uid)) {
        db_query("UPDATE {user_stats_values} SET value = value + 1\n          WHERE name = 'post_count' AND uid = %d", $uid);

        // Flush internal cache.
        user_stats_cache_set('reset', $uid);
      }
      else {
        user_stats_post_count_update('reset', $uid);
      }
      break;
    case 'decrement':
      if (user_stats_isset('post_count', $uid)) {
        db_query("UPDATE {user_stats_values} SET value = value - 1\n          WHERE name = 'post_count' AND uid = %d", $uid);

        // Flush internal cache.
        user_stats_cache_set('reset', $uid);
      }
      else {
        user_stats_post_count_update('reset', $uid);
      }
      break;
    case 'reset':
      $total_count = 0;
      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, $uid));
        $total_count += $node_count;
      }
      if (variable_get('user_stats_count_comments', TRUE)) {
        $sql = "SELECT COUNT(*) FROM {comments} c\n          INNER JOIN {node} n ON c.nid = n.nid\n          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, $uid));
        $total_count += $comments_count;
      }
      db_query("DELETE FROM {user_stats_values}\n        WHERE name = 'post_count' AND uid = %d", $uid);
      db_query("INSERT INTO {user_stats_values} (name, uid, value)\n        VALUES ('post_count', %d, %d)", $uid, $total_count);

      // Prime the cache, this will be used by module_invoke_all() below.
      user_stats_cache_set('post_count', $uid, $total_count);
      break;
  }

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

  // Allow modules to react to a statistic change.
  module_invoke_all('user_stats', 'post_count', $op, $uid, user_stats_get_stats('post_count', $uid));
}