history.module 3.9 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Records which users have read which content.
6
7
8
9
10
11
 *
 * @todo
 * - Generic helper for _forum_user_last_visit() + history_read().
 * - Generic helper for node_mark().
 */

12
use Drupal\Core\Entity\EntityInterface;
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

/**
 * Entities changed before this time are always shown as read.
 *
 * Entities changed within this time may be marked as new, updated, or read,
 * depending on their state for the current user. Defaults to 30 days ago.
 */
define('HISTORY_READ_LIMIT', REQUEST_TIME - 30 * 24 * 60 * 60);

/**
 * Retrieves the timestamp for the current user's last view of a specified node.
 *
 * @param int $nid
 *   A node ID.
 *
 * @return int
 *   If a node has been previously viewed by the user, the timestamp in seconds
 *   of when the last view occurred; otherwise, zero.
 */
function history_read($nid) {
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  $history = history_read_multiple(array($nid));
  return $history[$nid];
}

/**
 * Retrieves the last viewed timestamp for each of the passed node IDs.
 *
 * @param array $nids
 *   An array of node IDs.
 *
 * @return array
 *   Array of timestamps keyed by node ID. If a node has been previously viewed
 *   by the user, the timestamp in seconds of when the last view occurred;
 *   otherwise, zero.
 */
function history_read_multiple($nids) {
49
50
  $history = &drupal_static(__FUNCTION__, array());

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  $return = array();

  $nodes_to_read = array();
  foreach ($nids as $nid) {
    if (isset($history[$nid])) {
      $return[$nid] = $history[$nid];
    }
    else {
      // Initialize value if current user has not viewed the node.
      $nodes_to_read[$nid] = 0;
    }
  }

  if (empty($nodes_to_read)) {
    return $return;
66
67
  }

68
69
70
71
72
73
74
75
76
77
  $result = db_query('SELECT nid, timestamp FROM {history} WHERE uid = :uid AND nid IN(:nids)', array(
    ':uid' => \Drupal::currentUser()->id(),
    ':nids' => array_keys($nodes_to_read),
  ));
  foreach ($result as $row) {
    $nodes_to_read[$row->nid] = (int) $row->timestamp;
  }
  $history += $nodes_to_read;

  return $return + $nodes_to_read;
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
}

/**
 * Updates 'last viewed' timestamp of the specified entity for the current user.
 *
 * @param $nid
 *   The node ID that has been read.
 * @param $account
 *   (optional) The user account to update the history for. Defaults to the
 *   current user.
 */
function history_write($nid, $account = NULL) {
  global $user;

  if (!isset($account)) {
    $account = $user;
  }

96
  if ($account->isAuthenticated()) {
97
98
    db_merge('history')
      ->key(array(
99
        'uid' => $account->id(),
100
101
102
103
        'nid' => $nid,
      ))
      ->fields(array('timestamp' => REQUEST_TIME))
      ->execute();
104
105
106
107
    // Update static cache.
    $history = &drupal_static('history_read_multiple', array());
    $history[$nid] = REQUEST_TIME;
  }
108
109
110
111
112
113
114
115
116
117
118
119
120
121
}

/**
 * Implements hook_cron().
 */
function history_cron() {
  db_delete('history')
    ->condition('timestamp', HISTORY_READ_LIMIT, '<')
    ->execute();
}

/**
 * Implements hook_node_delete().
 */
122
function history_node_delete(EntityInterface $node) {
123
  db_delete('history')
124
    ->condition('nid', $node->id())
125
126
127
128
129
130
131
132
133
134
    ->execute();
}

/**
 * Implements hook_user_cancel().
 */
function history_user_cancel($edit, $account, $method) {
  switch ($method) {
    case 'user_cancel_reassign':
      db_delete('history')
135
        ->condition('uid', $account->id())
136
137
138
139
140
141
142
143
144
145
        ->execute();
      break;
  }
}

/**
 * Implements hook_user_delete().
 */
function history_user_delete($account) {
  db_delete('history')
146
    ->condition('uid', $account->id())
147
148
    ->execute();
}
149
150
151
152
153
154
155

/**
 * Implements hook_library_info().
 */
function history_library_info() {
  $libraries['drupal.history'] = array(
    'title' => 'History',
156
    'version' => \Drupal::VERSION,
157
158
159
160
161
162
163
164
165
166
167
168
169
    'js' => array(
      drupal_get_path('module', 'history') . '/js/history.js' => array(),
    ),
    'dependencies' => array(
      array('system', 'jquery'),
      array('system', 'drupalSettings'),
      array('system', 'drupal'),
      array('system', 'drupal.ajax'),
    ),
  );

  return $libraries;
}