From 1d8bbcaf9aaf7e28d69db7f6311fb86a711fd095 Mon Sep 17 00:00:00 2001
From: Marco Villegas <marvil07@gmail.com>
Date: Fri, 29 Oct 2010 02:22:25 -0500
Subject: [PATCH] bug #890404 by Agileware, marvil07: Avoid array_merge_recursive() on hook_votingapi_results_alter() function.

New aggregation functions for votingapi cache table were assuming there
will be only one tag, that's now fixed.

In the onthe side, we are now avoiding use array_merge_recursive() and
instead, add the values directly to the $cache array
fix aggregation functions for positives/negatives so they calculate all tags results instead of only one tag result

This patch also move the logic again to the
hook_votingapi_results_alter() function.
---
 vud.module |   57 +++++++++++++++++++++------------------------------------
 1 files changed, 21 insertions(+), 36 deletions(-)

diff --git vud.module vud.module
index 36bb666..de116fb 100644
--- vud.module
+++ vud.module
@@ -212,10 +212,29 @@ function vud_ctools_plugin_directory($module, $plugin) {
 
 /**
  * Implementation of votingapi hook_votingapi_results_alter().
+ *
+ * Add positive/negative aggregations for VotingAPI cache points.
  */
 function vud_votingapi_results_alter(&$cache, $content_type, $content_id) {
-  $vud_cache = _vud_get_standard_results($content_type, $content_id);
-  $cache = array_merge_recursive($cache, $vud_cache);
+  // positive points
+  $sql  = "SELECT SUM(v.value) as value_positives, v.tag ";
+  $sql .= "FROM {votingapi_vote} v ";
+  $sql .= "WHERE v.content_type = '%s' AND v.content_id = %d AND v.value_type = 'points' AND v.value > 0 ";
+  $sql .= "GROUP BY v.value_type, v.tag";
+  $result = db_query($sql, $content_type, $content_id);
+  while ($record = db_fetch_object($result)) {
+    $cache[$record->tag]['points']['positives'] = $record->value_positives;
+  }
+
+  // negative points
+  $sql  = "SELECT SUM(v.value) as value_negatives, v.tag ";
+  $sql .= "FROM {votingapi_vote} v ";
+  $sql .= "WHERE v.content_type = '%s' AND v.content_id = %d AND v.value_type = 'points' AND v.value < 0 ";
+  $sql .= "GROUP BY v.value_type, v.tag";
+  $result = db_query($sql, $content_type, $content_id);
+  while ($record = db_fetch_object($result)) {
+    $cache[$record->tag]['points']['negatives'] = $record->value_negatives;
+  }
 }
 
 /**
@@ -233,37 +252,3 @@ function vud_votingapi_metadata_alter(&$data) {
     'module' => 'vud',
   );
 }
-
-/**
- * Calculate positive/negative results for VotingAPI cache.
- *
- * We provide 0 if not votes found.
- */
-function _vud_get_standard_results($content_type, $content_id) {
-  $cache = array();
-  $tag = variable_get('vud_tag', 'vote');
-
-  $sql  = "SELECT SUM(v.value) as value_positives ";
-  $sql .= "FROM {votingapi_vote} v ";
-  $sql .= "WHERE v.content_type = '%s' AND v.content_id = %d AND v.value_type = 'points' AND v.value > 0 ";
-  $sql .= "GROUP BY v.value_type, v.tag";
-  $value_positives = db_result(db_query($sql, $content_type, $content_id));
-
-  if ($value_positives === FALSE) {
-    $value_positives = 0;
-  }
-  $cache[$tag]['points']['positives'] = $value_positives;
-
-  $sql  = "SELECT SUM(v.value) as value_negatives ";
-  $sql .= "FROM {votingapi_vote} v ";
-  $sql .= "WHERE v.content_type = '%s' AND v.content_id = %d AND v.value_type = 'points' AND v.value < 0 ";
-  $sql .= "GROUP BY v.value_type, v.tag";
-  $value_negatives = db_result(db_query($sql, $content_type, $content_id));
-
-  if ($value_negatives === FALSE) {
-    $value_negatives = 0;
-  }
-  $cache[$tag]['points']['negatives'] = $value_negatives;
-
-  return $cache;
-}
-- 
1.7.1

