function recently_read_exit in Recently Read 6
Same name and namespace in other branches
- 7 recently_read.module \recently_read_exit()
- 7.2 recently_read.module \recently_read_exit()
File
- ./
recently_read.module, line 107 - Recently read module file. Displays a history of recently read nodes by currently logged in user.
Code
function recently_read_exit() {
global $user;
drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);
// track history for authenticated user
if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == '') {
$nid = arg(1);
// get node type
$type = db_result(db_query('SELECT type FROM {node} WHERE nid = %d', $nid));
// track history for authenticated user
if (recently_read_is_enabled($type) && $user->uid) {
$record = new stdClass();
$record->nid = $nid;
$record->type = $type;
$record->uid = $user->uid;
$record->timestamp = time();
$count = db_result(db_query('SELECT COUNT(*) FROM {recently_read_nodes} WHERE nid = %d AND uid = %d AND type = "%s"', $nid, $user->uid, $type));
// a node has been viewed before, make an update
if ($count > 0) {
drupal_write_record('recently_read_nodes', $record, array(
'nid',
'uid',
));
}
else {
drupal_write_record('recently_read_nodes', $record);
}
}
// track history for anonymous user
if (recently_read_is_enabled($type) && !$user->uid && variable_get('recently_read_anonymous_enabled', FALSE)) {
$key = "recently_read-{$type}";
if (!isset($_SESSION[$key])) {
$_SESSION[$key] = array();
}
// remove previous entry, if present
unset($_SESSION[$key][$nid]);
// add new entry at the beginning of array
$title = db_result(db_query('SELECT title FROM {node} WHERE nid = %d', $nid));
$entry = array(
'nid' => $nid,
'title' => $title,
'type' => $type,
'timestamp' => time(),
);
$_SESSION[$key] = array(
$nid => $entry,
) + $_SESSION[$key];
while (count($_SESSION[$key]) > variable_get('recently_read_max_entries', 10)) {
array_pop($_SESSION[$key]);
}
}
// remove old entries
$nids = array();
$result = db_query("SELECT nid FROM {recently_read_nodes} WHERE uid = %d and type = '%s' ORDER BY timestamp DESC LIMIT %d,1000", $user->uid, $type, variable_get('recently_read_max_entries', 10) + 1);
while ($nid = db_result($result)) {
$nids[] = $nid;
}
if (count($nids)) {
$placeholders = implode(',', array_fill(0, count($nids), "%d"));
db_query("DELETE FROM {recently_read_nodes} WHERE uid = %d AND nid IN({$placeholders})", array_merge((array) $user->uid, $nids));
}
}
}