From 91db86db5ce3f1b94949bedcc21953a376a13af3 Mon Sep 17 00:00:00 2001
From: Marco Villegas <marvil07@gmail.com>
Date: Sun, 21 Nov 2010 13:22:51 -0500
Subject: [PATCH] bug #962996 by catch, marvil07: Avoid unnecessary queries in vud_widget_proxy().

- Added a permissions check to confirm whether the user viewing the
  widget is allowed to vote or not before trying to look up their vote,
  this saves 90 queries when viewing 90 comments showing the widget.
- Only query positives and negatives for upanddown widget.
---
 WIDGETAPI.txt                   |    4 ++--
 vud.theme.inc                   |   21 +--------------------
 widgets/upanddown/upanddown.inc |   23 +++++++++++++++++++++++
 3 files changed, 26 insertions(+), 22 deletions(-)

diff --git WIDGETAPI.txt WIDGETAPI.txt
index fa30d11..7b2b359 100644
--- WIDGETAPI.txt
+++ WIDGETAPI.txt
@@ -172,8 +172,8 @@ number of votes.
     (signed).
 `$unsigned_points` :: Number of total vote points for the object
     (unsigned).
-`$up_points`	     :: Number of total positive vote points for the vote object.
-`$down_points`	   :: Number of total positive vote points for the vote object.
+`$up_points`	     :: Number of total positive vote points for the vote object. Only available for upanddown widget.
+`$down_points`	   :: Number of total positive vote points for the vote object. Only available for upanddown widget.
 `$vote_count`	     :: Number of total votes for the vote object.
 `$readonly`        :: Boolean that indicates if the actual user can vote
     on the object where the widget is displayed.
diff --git vud.theme.inc vud.theme.inc
index 8f26a9e..841768a 100644
--- vud.theme.inc
+++ vud.theme.inc
@@ -154,7 +154,7 @@ function vud_widget_proxy($content_id, $type, $tag, $widget_theme, $readonly=NUL
     'tag' => $variables['tag'],
   );
   $criteria = $base_criteria + $uid;
-  $user_vote = votingapi_select_single_vote_value($criteria);
+  $user_vote = user_access('use vote up/down') ? votingapi_select_single_vote_value($criteria) : 0;
 
   if ($user_vote > 0) {
     $variables['class_up'] = 'up-active';
@@ -195,25 +195,6 @@ function vud_widget_proxy($content_id, $type, $tag, $widget_theme, $readonly=NUL
 
   $variables['unsigned_points'] = $vote_result;
 
-  $criteria = array(
-    'content_type' => $type,
-    'content_id' => $content_id,
-    'value_type' => 'points',
-    'tag' => $tag,
-    'function' => 'positives'
-  );
-  $positives = (int)votingapi_select_single_result_value($criteria);
-  $variables['up_points'] = $positives;
-  $criteria = array(
-    'content_type' => $type,
-    'content_id' => $content_id,
-    'value_type' => 'points',
-    'tag' => $tag,
-    'function' => 'negatives'
-  );
-  $negatives = (int)votingapi_select_single_result_value($criteria);
-  $variables['down_points'] = $negatives;
-
   if ($vote_result > 0) {
     $variables['class'] = 'positive';
     $variables['points'] = '+'. $vote_result;
diff --git widgets/upanddown/upanddown.inc widgets/upanddown/upanddown.inc
index 82de1fa..a6b5d9d 100644
--- widgets/upanddown/upanddown.inc
+++ widgets/upanddown/upanddown.inc
@@ -13,5 +13,28 @@ function vud_upanddown_vud_widgets() {
   return array(
     'title' => t('Up and down'),
     'widget template' => 'widget',
+    'alter template variables' => 'vud_widget_upanddown_alter_template_vars',
   );
 }
+
+function vud_widget_upanddown_alter_template_vars($template_type, &$variables) {
+  $criteria = array(
+    'content_type' => $variables['type'],
+    'content_id' => $variables['content_id'],
+    'value_type' => 'points',
+    'tag' => $variables['tag'],
+    'function' => 'positives'
+  );
+  $positives = (int)votingapi_select_single_result_value($criteria);
+  $variables['up_points'] = $positives;
+
+  $criteria = array(
+    'content_type' => $variables['type'],
+    'content_id' => $variables['content_id'],
+    'value_type' => 'points',
+    'tag' => $variables['tag'],
+    'function' => 'negatives'
+  );
+  $negatives = (int)votingapi_select_single_result_value($criteria);
+  $variables['down_points'] = $negatives;
+}
-- 
1.7.2.3

