You are here

function user_stats_post_count_update in User Stats 7

Same name and namespace in other branches
  1. 5 user_stats.module \user_stats_post_count_update()
  2. 6 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'

9 calls to user_stats_post_count_update()
user_stats_comment_delete in ./user_stats.module
Implements hook_comment_delete().
user_stats_comment_insert in ./user_stats.module
Implements hook_comment_insert().
user_stats_comment_update in ./user_stats.module
Implements hook_comment_update().
user_stats_cron in ./user_stats.module
Implements hook_cron().
user_stats_get_stats in ./user_stats.module
Returns user stats.

... See full list

File

./user_stats.module, line 793
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)) {

        //@TODO: Previous query tried to update and add in one query,

        // that wasn't working.
        $count = user_stats_get_stats('post_count', $uid) + 1;
        db_update('user_stats_values')
          ->fields(array(
          'value' => $count,
        ))
          ->condition('name', 'post_count')
          ->condition('uid', $uid)
          ->execute();

        // 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)) {

        //@TODO: Same issue as 'increment'.  Previous query tried to update

        // and add in one query... that wasn't working
        $count = user_stats_get_stats('post_count', $uid) - 1;
        db_update('user_stats_values')
          ->fields(array(
          'value' => $count,
        ))
          ->condition('name', 'post_count')
          ->condition('uid', $uid)
          ->execute();

        // 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 = :uid 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_query($sql, array(
          ':uid' => $uid,
        ))
          ->fetchField();
        $total_count += $node_count;
      }
      if (variable_get('user_stats_count_comments', TRUE)) {

        // COMMENT_PUBLISHED is now 1 in D7, and COMMENT_UNPUBLISHED is 0
        $sql = "SELECT COUNT(*) FROM {comment} c \n          INNER JOIN {node} n ON c.nid = n.nid\n          WHERE c.uid = :uid AND c.status = 1 AND n.status = 1";
        if (!empty($post_count_content_types)) {
          $where = ' AND n.type IN (' . $content_types . ')';
          $sql .= $where;
        }
        $comments_count = db_query($sql, array(
          ':uid' => $uid,
        ))
          ->fetchField();
        $total_count += $comments_count;
      }
      db_delete('user_stats_values')
        ->condition('name', 'post_count')
        ->condition('uid', $uid)
        ->execute();
      $id = db_insert('user_stats_values')
        ->fields(array(
        'name' => 'post_count',
        'uid' => $uid,
        'value' => $total_count,
      ))
        ->execute();

      // 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));
}