? .svn
? dec-cache.patch
? decisions-alt-cache.patch
? po
? tests
? translations
? modes/.svn
? modes/ranking-dupe-hooks.patch
Index: decisions.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/decisions/decisions.install,v
retrieving revision 1.31
diff -u -p -r1.31 decisions.install
--- decisions.install	4 Feb 2010 17:32:54 -0000	1.31
+++ decisions.install	2 Mar 2010 02:20:26 -0000
@@ -49,6 +49,8 @@ function decisions_schema() {
       'vote_offset' => array('vote_offset'),
     ),
   );
+  $schema['cache_decisions'] = drupal_get_schema_unprocessed('system', 'cache');
+  $schema['cache_decisions']['description'] =  '';
   return $schema;
 }
 
@@ -129,6 +131,11 @@ function decisions_update_6000() {
   return $items;
 }
 
+function decisions_update_6001() {
+  $ret = array();
+  db_create_table($ret, 'cache_decisions', drupal_get_schema_unprocessed('system', 'cache'));
+  return $items;
+}
 /**
  * Implementation of hook_uninstall().
  */
Index: decisions.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/decisions/decisions.module,v
retrieving revision 1.236
diff -u -p -r1.236 decisions.module
--- decisions.module	23 Feb 2010 19:37:47 -0000	1.236
+++ decisions.module	2 Mar 2010 02:20:27 -0000
@@ -228,7 +228,15 @@ function decisions_admin() {
     '#description' => t('Some modules allow votes to be submitted with an asynchronous interface.'),
     '#default_value' => variable_get('decisions_ahah', 0),
   );
-  
+
+  $form['main']['decisions_voted_cache_fallback'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Use the VotingAPI as a fallback when checking if a user voted.'),
+    '#description' => t("Decisions sets a cache item when a user votes and can fallback to the VotingAPI when the cache item doesn't exist.
+    Turning this fallback off can improve performance but may result in users voting twice in rare circumstances."),
+    '#default_value' => variable_get('decisions_voted_cache_fallback', 1),
+  );
+
   return system_settings_form($form);
 }
 
@@ -1088,19 +1096,16 @@ function _decisions_electoral_list_reset
  * Load the votes and decision-specific data into the node object.
  */
 function decisions_load($node) {
+  global $user;
   $decision = db_fetch_object(db_query("SELECT * FROM {decisions} WHERE nid = %d", $node->nid));
   $result = db_query("SELECT vote_offset, label FROM {decisions_choices} WHERE nid = %d ORDER BY vote_offset", $node->nid);
   while ($choice = db_fetch_array($result)) {
-    $decision->choice[$choice['vote_offset']] = $choice;
+    $decision->choice[$choice['vote_offset']] = $choice;  
   }
   $decision->choices = count($decision->choice);
 
   // See if user has voted
-  $criteria = votingapi_current_user_identifier();
-  $criteria['content_type'] = 'decisions';
-  $criteria['content_id'] = $node->nid;
-  $decision->voted = count(votingapi_select_votes($criteria)) > 0;
-
+  $decision->voted = decisions_voted($node->nid, $user->uid);
   return $decision;
 }
 
@@ -1706,3 +1711,65 @@ function _decisions_randomize_options($c
   }
   return $randomized_choices;
 }
+
+/*
+ * Check whether the user voted on a Decisions node in a performant way.
+ * See decisions_votingapi_insert() for more information.
+ */
+function decisions_voted_cache($nid, $uid) {
+  $identifier = $uid > 0 ? $uid : ip_address();
+  $cache = cache_get('decisions-'. $nid . '-' . $identifier, 'cache_decisions');
+  if ($uid == 0) {
+    //Respect the VotingAPI anonymous window.
+    $in_window = time() <= $cache->created + variable_get('votingapi_anonymous_window', 3600);
+    if (!$in_window) {
+      return FALSE;
+    }
+  }
+  return empty($cache) ? FALSE : TRUE;
+}
+
+/*
+ * Check whether the given user voted on a particular node using the proper source of information.
+ */
+function decisions_voted($nid, $uid) {
+  $fallback = variable_get('decisions_voted_cache_fallback', 1);
+  $voted_cache = decisions_voted_cache($nid, $uid);
+  if ($voted_cache) {
+    return TRUE;
+  }
+  else {
+    if ($fallback) {
+      $votes = decisions_get_votes($nid, $uid);
+      return(!empty($votes));
+    }
+    else {
+      return FALSE;
+    }
+  }
+}
+/*
+ * Implementation of hook_votingapi_insert().
+ * Sets a simple cache item that we can use to check if a user voted.
+ * This can be more performant on sites that use a fast cache store, like Memcache.
+ */
+function decisions_votingapi_insert($votes) {
+  foreach ($votes as $vote) {
+    if (isset($vote['content_type']) && $vote['content_type'] == 'decisions' ) {
+      $identifier = $vote['uid'] > 0 ? $vote['uid'] : ip_address();
+    }
+    cache_set('decisions-'. $vote['content_id'] . '-' . $identifier, $vote['vote_id'], 'cache_decisions');
+  }
+}
+
+/*
+ * Implementation of hook_votingapi_delete().
+ */
+function decisions_votingapi_delete($votes) {
+  foreach ($votes as $vote) {
+    if (isset($vote['content_type']) && $vote['content_type'] == 'decisions' ) {
+      $identifier = $vote['uid'] > 0 ? $vote['uid'] : ip_address();
+      cache_clear_all('decisions-'. $vote['content_id'] . '-' . $identifier, 'cache_decisions');
+    }
+  }
+}
Index: modes/selection.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/decisions/modes/selection.module,v
retrieving revision 1.26
diff -u -p -r1.26 selection.module
--- modes/selection.module	26 Feb 2010 17:05:13 -0000	1.26
+++ modes/selection.module	2 Mar 2010 02:20:27 -0000
@@ -64,7 +64,7 @@ function selection_decisions_algorithms(
  * @return
  * Returns HTML for AHAH voting, or redirects to the destination for synchronous voting.
  */
-function selection_vote($nid = 0, $cid = -1) {
+function selection_vote($nid, $cid) {
   global $user;
   if (selection_check_token($_REQUEST['token'], $nid, $cid)) {
     $valid_cid = db_result(db_query("SELECT label FROM {decisions_choices} WHERE nid = %d AND vote_offset = %d", $nid, $cid));
@@ -73,12 +73,11 @@ function selection_vote($nid = 0, $cid =
         drupal_goto(drupal_get_destination());
       }
       else {
-        print '';
         exit();
       }
     }
-    $voted = decisions_get_votes($nid, $user->uid);
-    if (empty($voted)) {
+    $voted = decisions_voted($nid, $user->uid);
+    if (!$voted) {
       $vote = array(
         'value' => 1,
         'tag' => $cid,
@@ -96,7 +95,6 @@ function selection_vote($nid = 0, $cid =
         drupal_goto(drupal_get_destination());
       }
       else {
-        print '';
         exit();
       }
     }
@@ -118,7 +116,6 @@ function selection_vote($nid = 0, $cid =
     drupal_goto(drupal_get_destination());
   }
   else {
-    print '';
     exit();
   }
 }
@@ -231,8 +228,7 @@ function decisions_selection_voting_form
 }
 
 /**
- * Implementation of the decisions_view_results() hook for the selection module
- *
+ * Display the selection results.
  * TODO: implement (http://drupal.org/node/48249)
  */
 function selection_decisions_view_results($node, $teaser, $page, $this_vote = array()) {
