3

So as far as I've seen, the history table holds timestamps when users viewed nodes.

Which Drupal function insert the data in that database table? The most logical place for me would be in statistics_exit(), where the accesslog table is being populated, but it's not there. The only place I've seen something similar is in node_tag_new(), but I am not sure if that's the right one.

Can anyone shed some light into this?

  • Do you have any reason to believe that function is not the one you are looking for? Did you check which hooks call that function, and when those hooks are invoked? – kiamlaluno Jan 20 '14 at 16:37
  • Mainly the name of the function. It doesn't sound right, at least doesn't seem to do what it says. – Eduard Luca Jan 20 '14 at 19:37
6

It is node_tag_new(), the function that is part of the Node module (line 299 of that file in Drupal 7).

/**
 * Updates the 'last viewed' timestamp of the specified node for current user.
 *
 * @param $node
 *   A node object.
 */
function node_tag_new($node) {
  global $user;
  if ($user->uid) {
    db_merge('history')
      ->key(array(
        'uid' => $user->uid,
        'nid' => $node->nid,
      ))
      ->fields(array('timestamp' => REQUEST_TIME))
      ->execute();
   }
}

Here's the same function in Drupal 6.

/**
 * Update the 'last viewed' timestamp of the specified node for current user.
 */
function node_tag_new($nid) {
  global $user;

  if ($user->uid) {
    if (node_last_viewed($nid)) {
      db_query('UPDATE {history} SET timestamp = %d WHERE uid = %d AND nid = %d', time(), $user->uid, $nid);
    }
    else {
      @db_query('INSERT INTO {history} (uid, nid, timestamp) VALUES (%d, %d, %d)', $user->uid, $nid, time());
    }
  }
}

The function is called by node_show(), which uses the following code.

// Update the history table, stating that this user viewed this node.
node_tag_new($node);

As said from that comment, the purpose of the function call is updating the history table to record the fact the current user viewed the node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.