You are here

function recently_read_get_read_items in Recently Read 6

Same name and namespace in other branches
  1. 7 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 in ./recently_read.module
Implementation of hook_block().

File

./recently_read.module, line 280
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);
  }

  // get history from _SESSION variable if anonymous
  if ($user_id == 0 && variable_get('recently_read_anonymous_enabled', FALSE)) {
    $items = array();
    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) {

    // make a list of links to recently read nodes which are published
    $placeholders = db_placeholders($node_types, 'text');

    // prepare args for sql query
    $args = array(
      $user_id,
    );
    $args = array_merge($args, $node_types);
    $args[] = $limit;
    $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, n.type, rr.timestamp FROM {node} n\n      INNER JOIN {recently_read_nodes} rr\n      ON n.nid = rr.nid WHERE rr.uid = %d AND n.status = 1 AND n.type IN({$placeholders})\n      ORDER BY rr.timestamp DESC\n      LIMIT 0, %d"), $args);
    $items = array();
    while ($row = db_fetch_array($result)) {
      $items[] = $row;
    }
  }
  return $items;
}