As fago explained, a userode gives opportunity to use normal node features for users. For site's consistency I currently implement a guestbook/shoutbox feature based on comments on usernodes (coming up simple usernode_guestbook.module). Usernode comments are shown on the usual profile page. This works fine. My problem: Even if comments has been viewed, this is not recognized by the usernode and all comments are seen as "new". I would like to add some kind of usernode-"touch" to the user-hook to simulate viewing the original usernode. Does anybody knows a common Drupal-way for that? Or are there more sophisticated Drupal commands to read a comment and "touch" the belonging node?

Comments

marcor’s picture

Assigned: Unassigned » marcor
Status: Active » Fixed

OK, the answer can be found in node module where a node is rendered:

/**
 * Generate a page displaying a single node, along with its comments.
 */
function node_show($node, $cid) {
  $output = node_view($node, FALSE, TRUE);

  if (function_exists('comment_render') && $node->comment) {
    $output .= comment_render($node, $cid);
  }

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

  return $output;
}

The function node_tag_new($nid) "touches" (in the meaning of a unix touch command) a certain node as being read. What excectly happens can be read in the definition:

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

So even a usernode can be set as viewed by the current user, if the a profile page is viewed. See a shortened example of usernode_guestbook.module here:

/**
 * Implementation of hook_user().
 */
function usernode_guestbook_user($type, &$edit, &$user, $category = NULL) {
  switch ($type) {
    case 'view':
      $items[t('Guestbook')]['entries'] = array(
        'value' => usernode_guestbook_usernode($user),
      );
      return $items;
  }
}

/**
 * Render the usernode with its comments to be displayed additionally in a page
 */
function usernode_guestbook_usernode($account) {
  $node = usernode_get_node($account);
  $output = comment_form_box(array('nid' => $node->nid), NULL);
  $output .= comment_render($node);
  // Update the history table, stating that this user viewed the usernode.
  node_tag_new($node->nid);
  return $output;
}
Anonymous’s picture

Status: Fixed » Closed (fixed)