You are here

function user_stats_nodeapi in User Stats 5

Same name and namespace in other branches
  1. 6 user_stats.module \user_stats_nodeapi()

Implementation of hook_nodeapi().

Increments post count on insert or publish. Decrements post count on delete or unpublish. Checks and updates users without post counts on view.

Parameters

&$node: The node the action is being performed on.

$op: The operation being performed. We are interested in insert, delete, update and view.

File

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

Code

function user_stats_nodeapi(&$node, $op) {

  // We stop before any work is done if the $op isn't one of the ones we need
  if (!in_array($op, array(
    'insert',
    'delete',
    'update',
  ))) {
    return;
  }
  $post_count_content_types = variable_get('user_stats_included_content_types', array());
  if ((empty($post_count_content_types) || in_array($node->type, $post_count_content_types)) && variable_get('user_stats_count_posts', TRUE)) {
    switch ($op) {
      case 'insert':
        $user_node = user_stats_user_load($node->uid);
        if ($node->status) {
          user_stats_post_count_update($user_node, 'increment');
        }
        break;
      case 'delete':
        $user_node = user_stats_user_load($node->uid);
        user_stats_post_count_update($user_node, 'decrement');
        break;
      case 'update':
        $user_node = user_stats_user_load($node->uid);

        // Can't think of any other way of doing this than resetting the user...
        user_stats_post_count_update($user_node, 'reset');
        break;
    }
  }

  // Do IP Address update.
  switch ($op) {
    case 'insert':
      global $user;

      // User IP addresses are only interesting if they are posting the content.
      if ($node->uid == $user->uid) {
        user_stats_ip_address_update($user, $_SERVER['REMOTE_ADDR']);
      }
      break;
    case 'update':
      global $user;
      if ($node->uid == $user->uid) {
        user_stats_ip_address_update($user, $_SERVER['REMOTE_ADDR']);
      }
      break;
  }
}