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