New field to count the number of views of a node

This commit is contained in:
Manuel Cillero 2017-07-26 19:44:24 +02:00
parent fda0aca579
commit 673511a713
2 changed files with 13 additions and 4 deletions

View file

@ -40,6 +40,13 @@ function recently_read_schema() {
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'views' => array(
'description' => 'The number of times that the node has been read.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 1
)
),
'primary key' => array('nid', 'uid')

View file

@ -110,7 +110,7 @@ function recently_read_exit() {
drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);
// track history for authenticated user
if ((arg(0) == 'node') && is_numeric(arg(1)) && arg(2) == '') {
if ((arg(0) == 'node') && is_numeric(arg(1))) { // && arg(2) == '') {
$nid = arg(1);
// get node type
@ -124,16 +124,18 @@ function recently_read_exit() {
$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"',
$views = db_result(db_query(
'SELECT views 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) {
if (db_affected_rows()) {
$record->views = $views + 1;
drupal_write_record('recently_read_nodes', $record, array('nid', 'uid'));
}
// a node has not been viewed before, add new row
else {
$record->views = 1;
drupal_write_record('recently_read_nodes', $record);
}
}