Index: smackdown.module =================================================================== --- smackdown.module (revision 1178) +++ smackdown.module (working copy) @@ -320,6 +320,8 @@ * Implementation of hook_load(). */ function smackdown_load($node) { + global $user; + $additions = db_fetch_object(db_query('SELECT s.*, n1.title as node_ref_1_title, n2.title as node_ref_2_title FROM {smackdown} s INNER JOIN {node} n1 ON s.node_ref_1 = n1.nid INNER JOIN {node} n2 ON s.node_ref_2 = n2.nid WHERE s.vid = %d', $node->vid)); $additions = smackdown_results($additions); return $additions; @@ -332,7 +334,10 @@ ); // we need to find out what the previous node voted on was - $user_log = (count($_SESSION['smackdown_user_log']) > 1) ? array_reverse($_SESSION['smackdown_user_log']) : $_SESSION['smackdown_user_log']; + + $user_votes = smackdown_get_user_votes($user->uid); + $user_log = (count($user_votes) > 1) ? + array_reverse($user_votes) : $user_votes; if ($user_log) { $prev_node = node_load($user_log[0]); smackdown_results($prev_node); @@ -623,9 +628,6 @@ votingapi_recalculate_results('node', $nid); } - // Set user session var to include this $nid - $_SESSION['smackdown_user_log'][] = $nid; - // go to the next smackdown drupal_goto(smackdown_next($content_type)); } @@ -668,10 +670,11 @@ * url of the next smackdown node to show */ function smackdown_next($content_type) { + global $user; $smackdown_diff = array(); // get a list of all smackdowns the user has already voted upon - $smackdown_user_log = $_SESSION['smackdown_user_log']; + $smackdown_user_log = smackdown_get_user_votes($user->uid); // get a list of all created smackdown nids $nids = smackdown_get_smackdowns($content_type); @@ -826,4 +829,43 @@ return $res; } } -} \ No newline at end of file +} + +/** + * Get a user's previous votes. + * + * If the given user has previously voted on any smackdown nodes, + * return a list of the as an array. + * + * @param $uid + * The user ID of the user that interests us. + * @return + * The array of previously voted on smackdown node IDs. + */ +function smackdown_get_user_votes($uid) { + + /* + * In Drupal 5, the Voting API doesn't provide a useful function for + * returning all of the votes of a particular user. Therefore, we need + * to go around the API and dig into the DB directly. This won't be + * necessary in Drupal 6 because the Drupal 6 version of the Voting API + * provides a much more general way of obtaining such data through the two + * functions votingapi_select_votes() and votingapi_select_results(). It + * appears as though the latter grabs data from the cache while the former + * does not. We can use one of these in the Drupal 6 version of Smackdown, + * and do away with the following code. + */ + + // Execute the query. + $result = db_query(" + SELECT content_id FROM {votingapi_vote} + WHERE (content_type = 'node') AND (tag = 'smackdown') AND (uid = %d) + ", $uid); + + // Marshal the results into an array and then return it. + $votes = array(); + while ($db_object = db_fetch_object($result)) { + $votes[] = $db_object->content_id; + } + return $votes; +}