| 67 |
$node_types = array($node_type); |
$node_types = array($node_type); |
| 68 |
} |
} |
| 69 |
|
|
| 70 |
if (count($node_types)) { |
if (!empty($node_types)) { |
| 71 |
global $user; |
global $user; |
| 72 |
$node_nid = _stumble_get_random_nid($node_types, $node_nid, $user->uid); |
$node_nid = _stumble_get_random_nid($node_types, $node_nid, $user->uid); |
| 73 |
} |
} |
| 74 |
|
|
| 75 |
// Redirect to the random node or the current node if no random nodes were |
// Redirect to the random node or the front page if no random nodes were |
| 76 |
// available. |
// available. |
| 77 |
drupal_goto($node_nid ? 'node/'. $node_nid : '<front>'); |
drupal_goto($node_nid ? 'node/'. $node_nid : '<front>'); |
| 78 |
} |
} |
| 92 |
* The random node's nid or FALSE if there is not an available random node. |
* The random node's nid or FALSE if there is not an available random node. |
| 93 |
*/ |
*/ |
| 94 |
function _stumble_get_random_nid($node_types, $nid, $uid = 0) { |
function _stumble_get_random_nid($node_types, $nid, $uid = 0) { |
|
$placeholders = db_placeholders($node_types, 'varchar'); |
|
| 95 |
$unread = $uid && stumble_var('stumble_prefer_unread'); |
$unread = $uid && stumble_var('stumble_prefer_unread'); |
| 96 |
|
|
| 97 |
|
$placeholders = db_placeholders($node_types, 'varchar'); |
| 98 |
|
$sql = "SELECT COUNT(n.nid) AS count, MAX(n.nid) AS max, MIN(n.nid) AS min FROM {node} n WHERE n.status = 1 AND n.nid NOT IN ($subquery_sql) AND n.type IN ($placeholders)"; |
| 99 |
|
|
| 100 |
$subquery_sql = $unread ? "SELECT h.nid FROM {history} h WHERE h.uid = %d" : $nid; |
$subquery_sql = $unread ? "SELECT h.nid FROM {history} h WHERE h.uid = %d" : $nid; |
| 101 |
$args = array_merge(array($unread ? $uid : $nid), $node_types); |
$args = array_merge(array($unread ? $uid : $nid), $node_types); |
| 102 |
$sql = "SELECT COUNT(n.nid) AS count, MAX(n.nid) AS max, MIN(n.nid) AS min FROM {node} n WHERE n.status = 1 AND n.nid NOT IN ($subquery_sql) AND n.type IN ($placeholders)"; |
|
| 103 |
$nodes = db_fetch_object(db_query(db_rewrite_sql($sql), $args)); |
$nodes = db_fetch_object(db_query(db_rewrite_sql($sql), $args)); |
| 104 |
if ($nodes && $nodes->count > 0) { |
if ($nodes && $nodes->count > 0) { |
| 105 |
array_unshift($args, $nodes->min, $nodes->max); |
array_unshift($args, $nodes->min, $nodes->max); |
| 108 |
return db_result(db_query_range(db_rewrite_sql($sql), $args, $offset, 1)); |
return db_result(db_query_range(db_rewrite_sql($sql), $args, $offset, 1)); |
| 109 |
} |
} |
| 110 |
elseif ($unread) { |
elseif ($unread) { |
| 111 |
|
// If a search for user's unread nodes didn't find any results, perform |
| 112 |
|
// another search for just a random node. |
| 113 |
return _stumble_get_random_nid($node_types, $nid); |
return _stumble_get_random_nid($node_types, $nid); |
| 114 |
} |
} |
| 115 |
return $nid; |
return FALSE; |
| 116 |
|
// Return the current's node id on failure |
| 117 |
|
//return $nid; |
| 118 |
} |
} |
| 119 |
|
|
| 120 |
/** |
/** |