From 434e2c0bcc50808f5c4313793c2da48431afc3ea Mon Sep 17 00:00:00 2001
From: Roman Zimmermann <torotil@gmail.com>
Date: Sun, 3 Mar 2013 20:27:20 +0100
Subject: [PATCH] Use entity_type instead of node->type in votingapi.

---
 advpoll.install                               | 16 +++++++-
 advpoll.module                                |  6 +--
 advpoll_converter/advpoll_converter.admin.inc |  2 +-
 advpoll_ranking/advpoll_ranking.module        |  6 +--
 includes/advpoll.pages.inc                    |  2 +-
 includes/advpoll_voteapi.inc                  | 10 ++---
 views/advpoll.views.inc                       | 57 ---------------------------
 7 files changed, 28 insertions(+), 71 deletions(-)
 delete mode 100644 views/advpoll.views.inc

diff --git a/advpoll.install b/advpoll.install
index ff39390..1a720f8 100644
--- a/advpoll.install
+++ b/advpoll.install
@@ -105,7 +105,7 @@ function advpoll_uninstall() {
   if ($nid) {
     db_delete('votingapi_vote')
       ->condition('entity_id', $nid, 'IN')
-      ->condition('entity_type', 'advpoll')
+      ->condition('entity_type', 'node')
       ->execute();
 
     // Delete all the nodes at once
@@ -805,6 +805,20 @@ function _advpoll_installed_instances() {
   );
 }
 
+/**
+ * Remove node->type from entity_type columns.
+ */
+function advpoll_update_7300() {
+  db_update('votingapi_vote')
+    ->fields(array('entity_type' => 'node'))
+    ->condition('entity_type', 'advpoll')
+    ->execute();
+  db_update('votingapi_cache')
+    ->fields(array('entity_type' => 'node'))
+    ->condition('entity_type', 'advpoll')
+    ->execute();
+}
+
 /*
  * Implements hook_update_N()
  * 7000 removes Show votes option.
diff --git a/advpoll.module b/advpoll.module
index 5891260..74fe056 100644
--- a/advpoll.module
+++ b/advpoll.module
@@ -331,7 +331,7 @@ function advpoll_node_presave($node) {
     if (isset($nid)) {
       $criteria = array();
       $criteria['entity_id'] = $node->nid;
-      $criteria['entity_type'] = 'advpoll';
+      $criteria['entity_type'] = 'node';
       $results = votingapi_select_votes($criteria);
 
       // Scrub any votes that are orphaned when a choice is removed.
@@ -356,7 +356,7 @@ function advpoll_node_presave($node) {
 function advpoll_node_delete($node) {
   db_delete('votingapi_vote')
     ->condition('entity_id', $node->nid)
-    ->condition('entity_type', 'advpoll')
+    ->condition('entity_type', 'node')
     ->execute();
 }
 
@@ -736,7 +736,7 @@ function advpoll_form_submit($form, &$form_state) {
   if (count($selected) > 0 && count($selected) <= $data->max_choices) {
     foreach ($selected as $item) {
       $vote = array();
-      $vote['type'] = 'advpoll';
+      $vote['type'] = 'node';
       $vote['tag'] = $item;
       $vote['nid'] = $nid;
       $vote['value'] = 1;
diff --git a/advpoll_converter/advpoll_converter.admin.inc b/advpoll_converter/advpoll_converter.admin.inc
index 286f34a..dd20473 100644
--- a/advpoll_converter/advpoll_converter.admin.inc
+++ b/advpoll_converter/advpoll_converter.admin.inc
@@ -183,7 +183,7 @@ function _advpoll_converter_create_nodes($nids) {
     $votes = array();
     foreach ($poll_votes as $row) {
       $votes = array(
-        'entity_type' => 'advpoll',
+        'entity_type' => 'node',
         'entity_id' => $nid,
         'value' => 1,
         'tag' => $choice_by_id[$row->chid],
diff --git a/advpoll_ranking/advpoll_ranking.module b/advpoll_ranking/advpoll_ranking.module
index 6836a37..47aa3fb 100644
--- a/advpoll_ranking/advpoll_ranking.module
+++ b/advpoll_ranking/advpoll_ranking.module
@@ -461,7 +461,7 @@ function advpoll_ranking_process_votes($data, $nid, $votes) {
 
   foreach ($votes as $ranking) {
     $vote = array();
-    $vote['type'] = 'advpoll';
+    $vote['type'] = 'node';
     $vote['tag'] = $ranking['id'];
     $vote['nid'] = $nid;
     $vote['value'] = $points - ($ranking['rank'] - 1);
@@ -540,7 +540,7 @@ function advpoll_display_runoff_results($nid, $data, $page = 0) {
 
     $criteria = array();
     $criteria['entity_id'] = $nid;
-    $criteria['entity_type'] = 'advpoll';
+    $criteria['entity_type'] = 'node';
 
     $results = votingapi_select_votes($criteria);
 
@@ -837,7 +837,7 @@ function advpoll_ranking_cancel_vote_submit($form, &$form_state) {
 
     $criteria = array();
     $criteria['entity_id'] = $nid;
-    $criteria['entity_type'] = 'advpoll';
+    $criteria['entity_type'] = 'node';
     $criteria['uid'] = $user->uid;
 
     votingapi_delete_votes(votingapi_select_votes($criteria));
diff --git a/includes/advpoll.pages.inc b/includes/advpoll.pages.inc
index eb7a1ae..23bac9b 100644
--- a/includes/advpoll.pages.inc
+++ b/includes/advpoll.pages.inc
@@ -616,7 +616,7 @@ function advpoll_merge_writein_form_submit($form, &$form_state) {
   // transfer merge from votes to new vote object and update votingapi
   foreach ($merge_fromVotes as $vote) {
     $votes = array(
-      'entity_type' => 'advpoll',
+      'entity_type' => 'node',
       'entity_id' => $nid,
       'value' => $vote['value'],
       'tag' => $to_tag,
diff --git a/includes/advpoll_voteapi.inc b/includes/advpoll_voteapi.inc
index 39efb15..b74e521 100644
--- a/includes/advpoll_voteapi.inc
+++ b/includes/advpoll_voteapi.inc
@@ -22,7 +22,7 @@
 function advpoll_get_votes($nid, $behavior = 'approval') {
   $criteria = array();
   $criteria['entity_id'] = $nid;
-  $criteria['entity_type'] = 'advpoll';
+  $criteria['entity_type'] = 'node';
 
   $results = votingapi_select_votes($criteria);
 
@@ -233,7 +233,7 @@ function advpoll_user_eligibility($node) {
   if ($data->mode === 'normal') {
     $criteria = array();
     $criteria['entity_id'] = $node->nid;
-    $criteria['entity_type'] = $node->type;
+    $criteria['entity_type'] = 'node';
 
     if ($user->uid) {
       $criteria['uid'] = $user->uid;
@@ -279,11 +279,11 @@ function advpoll_check_electoral_list($uid, $nid) {
  * @param $vote
  * A keyed array that is used to determine the method in which the vote will be
  * tracked and what values will be passed to votingapi
- *   $vote['type'] The content type - either advpoll or advpoll_ranking
+ *   $vote['type'] The entity type (ie. node, comment, ...)
  *   $vote['tag'] = Tag corresponds to the index of the selected choice
  *   $vote['nid'] = The node ID of poll being voted on
  *   $vote['mode'] = normal, cookie, or unlimited
- *   $vote['duration'] = duration is minutes that the cookie will last if one is 
+ *   $vote['duration'] = duration is minutes that the cookie will last if one is
  *                       set.
  */
 function advpoll_add_votes($vote) {
@@ -394,7 +394,7 @@ function advpoll_get_user_votes($nid) {
   $votes = array();
   $criteria = array();
   $criteria['entity_id'] = $nid;
-  $criteria['entity_type'] = 'advpoll';
+  $criteria['entity_type'] = 'node';
 
   if ($user->uid) {
     $criteria['uid'] = $user->uid;
diff --git a/views/advpoll.views.inc b/views/advpoll.views.inc
deleted file mode 100644
index a62b775..0000000
--- a/views/advpoll.views.inc
+++ /dev/null
@@ -1,57 +0,0 @@
-<?php
-
-/*
- * Implementation of hook_views_data_alter
- */
-
-function advpoll_views_data_alter(&$views_data) {
-// Borrowed structure from votingapi poll since this poll relies upon its 
-// behavior to tie votes to node.
-  $default_relationships[] = array(
-    'description' => t('Advanced Poll'),
-    'entity_type' => 'advpoll',
-    'base_table' => 'node',
-    'entity_id_column' => 'nid',
-    'pseudo_vote' => 'votingapi_vote', // for legacy compatability w/RC1.
-    'pseudo_cache' => 'votingapi_cache', // for legacy compatability w/RC1.
-  );  
-  
-foreach ($default_relationships as $data) {
-    $pseudo = str_replace(array(' ','-','.'), '_', $data['entity_type'] . '_' . $data['entity_id_column']);
-    $pseudo_vote = empty($data['pseudo_vote']) ? 'vapi_'. $pseudo : $data['pseudo_vote'];
-    $pseudo_cache = empty($data['pseudo_cache']) ? 'vapic_'. $pseudo : $data['pseudo_cache'];
-    
-    $views_data[$data['base_table']][$pseudo_vote]['relationship'] = array(
-      'title' => 'Votes',
-      'help' => 'Votes cast by users on ' . $data['description'] . '.',
-      'base' => 'votingapi_vote',
-      'field' => 'entity_id',
-      'relationship field' => $data['entity_id_column'],
-      'handler' => 'votingapi_views_handler_relationship',
-      'extra' => array(
-        array(
-          'field' => 'entity_type',
-          'value' => $data['entity_type'],
-          'numeric' => FALSE
-        ),
-      ),
-    );
-
-    $views_data[$data['base_table']][$pseudo_cache]['relationship'] = array(
-      'title' => 'Vote results',
-      'help' => 'Aggregate results of votes cast on ' . $data['description'] . '.',
-      'base' => 'votingapi_cache',
-      'field' => 'entity_id',
-      'relationship field' => $data['entity_id_column'],
-      'handler' => 'votingapi_views_handler_relationship',
-      'extra' => array(
-        array(
-          'field' => 'entity_type',
-          'value' => $data['entity_type'],
-          'numeric' => FALSE
-        ),
-      ),
-    );
-  }  
-  
-}
\ No newline at end of file
-- 
1.7.12.4

