This CodeLet adds a tab on node page and display all the visits to the node by users.
<?php
/**
* @file
*
* Retrive and Display user visits of a node
*
* @author DrupalD
*/
/**
* Implemntation of hook_menu
*
* @return $item
* @author DrupalD
*/
function track_user_menu() {
$item = array();
$item['node/%node/node-track'] = array(
'type' => MENU_LOCAL_TASK,
'weight' => 0,
'title' => 'Who visited this node',
'title callback' => 't',
'page callback' => 'get_node_visitor',
'page arguments' => array(1),
'access callback' => 'validate_node_visitor',
);
return $item;
}
/**
* validate_node_visitor
*
* Validates the access to the user visits to the node
*
* @return unknown_type
* @author DrupalD
*/
function validate_node_visitor() {
global $user;
if (in_array('admin', $user->roles) || $user->uid == 1) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* get_node_visitor
*
* @param $node
* @return unknown_type
* @author DrupalD
*/
function get_node_visitor($node) {
if ($node->nid) {
$am__field_ttile = array(t("User"));
$r__result = db_query("SELECT DISTINCT uid FROM {accesslog}
WHERE PATH = '%s' AND uid <> 0", "node/". $node->nid);
while ($om__user = db_fetch_object($r__result)) {
$ss__username = db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $om__user->uid));
$row[] = array(l($ss__username, "user/". $om__user->uid));
unset($om__user);
}
return theme('table', $am__field_title, $row);
}
}
?>