Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/history/history.module @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 /** | |
4 * @file | |
5 * Records which users have read which content. | |
6 * | |
7 * @todo | |
8 * - Generic helper for _forum_user_last_visit() + history_read(). | |
9 * - Generic helper for node_mark(). | |
10 */ | |
11 | |
12 use Drupal\Core\Entity\EntityInterface; | |
13 use Drupal\Core\Entity\Display\EntityViewDisplayInterface; | |
14 use Drupal\Core\Routing\RouteMatchInterface; | |
15 | |
16 /** | |
17 * Entities changed before this time are always shown as read. | |
18 * | |
19 * Entities changed within this time may be marked as new, updated, or read, | |
20 * depending on their state for the current user. Defaults to 30 days ago. | |
21 */ | |
22 define('HISTORY_READ_LIMIT', REQUEST_TIME - 30 * 24 * 60 * 60); | |
23 | |
24 /** | |
25 * Implements hook_help(). | |
26 */ | |
27 function history_help($route_name, RouteMatchInterface $route_match) { | |
28 switch ($route_name) { | |
29 case 'help.page.history': | |
30 $output = '<h3>' . t('About') . '</h3>'; | |
31 $output .= '<p>' . t('The History module keeps track of which content a user has read. It marks content as <em>new</em> or <em>updated</em> depending on the last time the user viewed it. History records that are older than one month are removed during cron, which means that content older than one month is always considered <em>read</em>. The History module does not have a user interface but it provides a filter to <a href=":views-help">Views</a> to show new or updated content. For more information, see the <a href=":url">online documentation for the History module</a>.', [':views-help' => (\Drupal::moduleHandler()->moduleExists('views')) ? \Drupal::url('help.page', ['name' => 'views']) : '#', ':url' => 'https://www.drupal.org/documentation/modules/history']) . '</p>'; | |
32 return $output; | |
33 } | |
34 } | |
35 | |
36 /** | |
37 * Retrieves the timestamp for the current user's last view of a specified node. | |
38 * | |
39 * @param int $nid | |
40 * A node ID. | |
41 * | |
42 * @return int | |
43 * If a node has been previously viewed by the user, the timestamp in seconds | |
44 * of when the last view occurred; otherwise, zero. | |
45 */ | |
46 function history_read($nid) { | |
47 $history = history_read_multiple([$nid]); | |
48 return $history[$nid]; | |
49 } | |
50 | |
51 /** | |
52 * Retrieves the last viewed timestamp for each of the passed node IDs. | |
53 * | |
54 * @param array $nids | |
55 * An array of node IDs. | |
56 * | |
57 * @return array | |
58 * Array of timestamps keyed by node ID. If a node has been previously viewed | |
59 * by the user, the timestamp in seconds of when the last view occurred; | |
60 * otherwise, zero. | |
61 */ | |
62 function history_read_multiple($nids) { | |
63 $history = &drupal_static(__FUNCTION__, []); | |
64 | |
65 $return = []; | |
66 | |
67 $nodes_to_read = []; | |
68 foreach ($nids as $nid) { | |
69 if (isset($history[$nid])) { | |
70 $return[$nid] = $history[$nid]; | |
71 } | |
72 else { | |
73 // Initialize value if current user has not viewed the node. | |
74 $nodes_to_read[$nid] = 0; | |
75 } | |
76 } | |
77 | |
78 if (empty($nodes_to_read)) { | |
79 return $return; | |
80 } | |
81 | |
82 $result = db_query('SELECT nid, timestamp FROM {history} WHERE uid = :uid AND nid IN ( :nids[] )', [ | |
83 ':uid' => \Drupal::currentUser()->id(), | |
84 ':nids[]' => array_keys($nodes_to_read), | |
85 ]); | |
86 foreach ($result as $row) { | |
87 $nodes_to_read[$row->nid] = (int) $row->timestamp; | |
88 } | |
89 $history += $nodes_to_read; | |
90 | |
91 return $return + $nodes_to_read; | |
92 } | |
93 | |
94 /** | |
95 * Updates 'last viewed' timestamp of the specified entity for the current user. | |
96 * | |
97 * @param $nid | |
98 * The node ID that has been read. | |
99 * @param $account | |
100 * (optional) The user account to update the history for. Defaults to the | |
101 * current user. | |
102 */ | |
103 function history_write($nid, $account = NULL) { | |
104 | |
105 if (!isset($account)) { | |
106 $account = \Drupal::currentUser(); | |
107 } | |
108 | |
109 if ($account->isAuthenticated()) { | |
110 db_merge('history') | |
111 ->keys([ | |
112 'uid' => $account->id(), | |
113 'nid' => $nid, | |
114 ]) | |
115 ->fields(['timestamp' => REQUEST_TIME]) | |
116 ->execute(); | |
117 // Update static cache. | |
118 $history = &drupal_static('history_read_multiple', []); | |
119 $history[$nid] = REQUEST_TIME; | |
120 } | |
121 } | |
122 | |
123 /** | |
124 * Implements hook_cron(). | |
125 */ | |
126 function history_cron() { | |
127 db_delete('history') | |
128 ->condition('timestamp', HISTORY_READ_LIMIT, '<') | |
129 ->execute(); | |
130 } | |
131 | |
132 /** | |
133 * Implements hook_ENTITY_TYPE_view_alter() for node entities. | |
134 */ | |
135 function history_node_view_alter(array &$build, EntityInterface $node, EntityViewDisplayInterface $display) { | |
136 // Update the history table, stating that this user viewed this node. | |
137 if ($display->getOriginalMode() === 'full') { | |
138 $build['#cache']['contexts'][] = 'user.roles:authenticated'; | |
139 if (\Drupal::currentUser()->isAuthenticated()) { | |
140 // When the window's "load" event is triggered, mark the node as read. | |
141 // This still allows for Drupal behaviors (which are triggered on the | |
142 // "DOMContentReady" event) to add "new" and "updated" indicators. | |
143 $build['#attached']['library'][] = 'history/mark-as-read'; | |
144 $build['#attached']['drupalSettings']['history']['nodesToMarkAsRead'][$node->id()] = TRUE; | |
145 } | |
146 } | |
147 | |
148 } | |
149 | |
150 /** | |
151 * Implements hook_ENTITY_TYPE_delete() for node entities. | |
152 */ | |
153 function history_node_delete(EntityInterface $node) { | |
154 db_delete('history') | |
155 ->condition('nid', $node->id()) | |
156 ->execute(); | |
157 } | |
158 | |
159 /** | |
160 * Implements hook_user_cancel(). | |
161 */ | |
162 function history_user_cancel($edit, $account, $method) { | |
163 switch ($method) { | |
164 case 'user_cancel_reassign': | |
165 db_delete('history') | |
166 ->condition('uid', $account->id()) | |
167 ->execute(); | |
168 break; | |
169 } | |
170 } | |
171 | |
172 /** | |
173 * Implements hook_ENTITY_TYPE_delete() for user entities. | |
174 */ | |
175 function history_user_delete($account) { | |
176 db_delete('history') | |
177 ->condition('uid', $account->id()) | |
178 ->execute(); | |
179 } | |
180 | |
181 /** | |
182 * #lazy_builder callback; attaches the last read timestamp for a node. | |
183 * | |
184 * @param int $node_id | |
185 * The node ID for which to attach the last read timestamp. | |
186 * | |
187 * @return array | |
188 * A renderable array containing the last read timestamp. | |
189 */ | |
190 function history_attach_timestamp($node_id) { | |
191 $element = []; | |
192 $element['#attached']['drupalSettings']['history']['lastReadTimestamps'][$node_id] = (int) history_read($node_id); | |
193 return $element; | |
194 } |