You are here

function nodeviewcount_count_node_view_ajax in Node view count 7.3

Same name and namespace in other branches
  1. 7 includes/pages/nodeviewcount.pages.inc \nodeviewcount_count_node_view_ajax()
  2. 7.2 includes/pages/nodeviewcount.pages.inc \nodeviewcount_count_node_view_ajax()

Page callback: Add node view ajax.

Parameters

object $node: The node for counting.

object $user: The user for counting.

int $timestamp: When the node was viewed.

string $uip: The user ID for viewed.

string $token: Token generated to verify the URL.

1 string reference to 'nodeviewcount_count_node_view_ajax'
nodeviewcount_menu in ./nodeviewcount.module
Implements hook_menu().

File

includes/pages/nodeviewcount.pages.inc, line 22
Contains ajax callback.

Code

function nodeviewcount_count_node_view_ajax($node, $user, $timestamp, $uip, $token) {
  $is_js_way = variable_get('nodeviewcount_way_counting', NODEVIEWCOUNT_PHP_WAY_COUNT_VIEWS) == NODEVIEWCOUNT_JS_WAY_COUNT_VIEWS;

  // Process the request only if the JS counting method is the selected.
  if ($is_js_way) {
    $token_data = array(
      $node->nid,
      $user->uid,
      $timestamp,
      $uip,
    );
    $recreated_token = _nodeviewcount_create_token($token_data);
    $is_valid_token = $token == $recreated_token;

    // Check the request can be trusted through token validation.
    if ($is_valid_token) {
      $sessionTimeLimit = _nodeviewcount_get_session_time_limit($node->nid, TRUE);
      $sessionTimeLimitTs = $sessionTimeLimit === FALSE ? 0 : $sessionTimeLimit
        ->getTimestamp();

      // Check the time limit condition is met and the view can be counted.
      if ($timestamp >= $sessionTimeLimitTs) {
        if ($node && $user) {
          $_SESSION['nodeviewcount_views_limit_js'][$node->nid] = $_SESSION['nodeviewcount_views_limit'][$node->nid];
          nodeviewcount_insert_node_view($node->nid, $user->uid, $uip);
        }
      }
    }
  }
}