You are here

function recently_read_get_read_items in Recently Read 7

Same name and namespace in other branches
  1. 6 recently_read.module \recently_read_get_read_items()
  2. 7.2 recently_read.module \recently_read_get_read_items()
1 call to recently_read_get_read_items()
recently_read_block_view in ./recently_read.module
Implements hook_block_view().

File

./recently_read.module, line 292
Recently read module file. Displays a history of recently read nodes by currently logged in user.

Code

function recently_read_get_read_items($node_types, $user_id, $limit = 0) {

  // normalize arguments
  if (!is_array($node_types)) {
    $node_types = array(
      $node_types,
    );
  }
  if ($limit == 0) {
    $limit = variable_get('recently_read_max_entries', 10) * count($node_types);
  }
  $items = array();

  // get history from _SESSION variable if anonymous
  if ($user_id == 0 && variable_get('recently_read_anonymous_enabled', FALSE)) {
    foreach ($node_types as $node_type) {
      $key = "recently_read-{$node_type}";
      if (isset($_SESSION[$key]) && is_array($_SESSION[$key])) {
        $items = $items + $_SESSION[$key];
      }
    }
    usort($items, '_recently_read_sort_fcn');
    $items = array_slice($items, 0, $limit);
  }

  // get history from database if authenticated
  if ($user_id > 0) {
    $query = db_select('node', 'n');
    $query
      ->innerJoin('recently_read_nodes', 'rr', 'n.nid = rr.nid');
    $items = $query
      ->fields('n', array(
      'nid',
      'title',
      'type',
    ))
      ->fields('rr', array(
      'timestamp',
    ))
      ->condition('rr.uid', $user_id)
      ->condition('n.status', 1)
      ->condition('n.type', $node_types, 'IN')
      ->orderBy('rr.timestamp', 'DESC')
      ->range(0, $limit)
      ->addTag('node_access')
      ->execute()
      ->fetchAll(PDO::FETCH_ASSOC);
  }
  return $items;
}