Home > Web/Internet > Drupal Most Recent Poll Block Not Caching

Drupal Most Recent Poll Block Not Caching

I was working on my new Drupal site, Shop the Internet, yesterday and I tried to insert a poll into a sidebar block that would ask the users which online shopping site did they prefer: Amazon, Bestbuy, Newegg, Target, or Walmart.  To test the poll I click Amazon and viola, it posted my vote.  Success!  Or so I thought…I was logged in to my admin panel in Firefox so I tried it in Internet Explorer.  When I refreshed the page, it said it was voted on and was showing the results.  So obviously the poll must have been more advanced than I thought, using IP address over cookies.  So again I tried it on Google Chrome and same thing.  Okay everything is fine.  So I go back to work with my site.  However after half an hour when my cache data was set to reset I decided to retest it and so I opened Internet Explorer and went to Shop the Internet…but now the poll was available to be voted on.  Of course I instantly realized this was because the caching of the poll was wrong, it was caching the poll and saving it as the cached version so that anyone who viewed it after I had voted on it would see the results only for another half an hour.  So I started my research on Google of course.

I searched for a bit, but no results and I was wondering if it were my fault that the poll was being cached.  But eventually I came upon a Drupal forum post http://drupal.org/node/523676 which gave a patch which is transcribed below:

diff -urp –strip-trailing-cr ../drupal-6.x-dev/modules/poll/poll.install ./modules/poll/poll.install
— ../drupal-6.x-dev/modules/poll/poll.install 2009-01-06 16:46:37.000000000 +0100
+++ ./modules/poll/poll.install 2009-07-18 22:29:37.000000000 +0200
@@ -130,3 +130,21 @@ function poll_schema() {
return $schema;
}

+/**
+ * @defgroup updates-6.x-extra Extra poll updates for 6.x
+ * @{
+ */
+
+/**
+ * Fix cache mode for “Most recent poll” block.
+ */
+function poll_update_6000() {
+ $ret = array();
+ $ret[] = update_sql(“UPDATE {blocks} SET cache = 2 WHERE module = ‘poll’ AND delta = ’0′”);
+ return $ret;
+}
+
+/**
+ * @} End of “defgroup updates-6.x-extra”
+ * The next series of updates should start at 7000.
+ */
diff -urp –strip-trailing-cr ../drupal-6.x-dev/modules/poll/poll.module ./modules/poll/poll.module
— ../drupal-6.x-dev/modules/poll/poll.module 2008-12-18 16:46:20.000000000 +0100
+++ ./modules/poll/poll.module 2009-07-18 21:45:53.000000000 +0200
@@ -131,6 +131,7 @@ function poll_block($op = ‘list’, $delta
if (user_access(‘access content’)) {
if ($op == ‘list’) {
$blocks[0]['info'] = t(‘Most recent poll’);
+ $blocks[0]['cache'] = BLOCK_CACHE_PER_USER;
return $blocks;
}
else if ($op == ‘view’) {
@@ -776,6 +777,8 @@ function poll_cancel($form, &$form_state

// Subtract from the votes.
db_query(“UPDATE {poll_choices} SET chvotes = chvotes – 1 WHERE nid = %d AND chorder = %d”, $node->nid, $node->vote);
+
+ cache_clear_all();
}

/**

If this code looks strange to you you are not alone.  I was confused too.  I have patched files in linux before, but my webhost, Ixwebhosting, does not have access to ssh or the terminal in general so I was stuck.  This was an obvious bug and I could not fix it.  All I could find were links to how to convert a patch file to an exe on Windows, but first I have a linux server and also how would I have executed that anyway?  So I decided to do it the manual way, open the files and manually edit it.  Make sure you back up all original files before editing!

  1. First open  ./modules/poll/poll.install and go to line 130
    Find

    function poll_schema() {
       return $schema;
     }

    and add after it

    function poll_update_6000() {
      $ret = array();
      $ret[] = update_sql("UPDATE {blocks} SET cache = 2 WHERE module = 'poll' AND delta = '0'");
      return $ret;
    }
  2. Save and exit.  Next open  ./modules/poll/poll.module and go to line 131
    Find:

    if (user_access('access content')) {
         if ($op == 'list') {
           $blocks[0]['info'] = t('Most recent poll');
           return $blocks;
         }
         else if ($op == 'view') {

    and replace with:

    if (user_access('access content')) {
         if ($op == 'list') {
           $blocks[0]['info'] = t('Most recent poll');
          $blocks[0]['cache'] = BLOCK_CACHE_PER_USER;
           return $blocks;
         }
         else if ($op == 'view') {
  3. In the same file go to line 777
    Find:

    function poll_cancel($form, &$form_state) {
      $node = node_load($form['#nid']);
      global $user;
      if ($user->uid) {
        db_query('DELETE FROM {poll_votes} WHERE nid = %d and uid = %d', $node->nid, $user->uid);
      }
      else {
        db_query("DELETE FROM {poll_votes} WHERE nid = %d and hostname = '%s'", $node->nid, ip_address());
      }
      // Subtract from the votes.
      db_query("UPDATE {poll_choices} SET chvotes = chvotes - 1 WHERE nid = %d AND chorder = %d", $node->nid, $node->vote);
    }

    and replace with:

    function poll_cancel($form, &$form_state
    
       // Subtract from the votes.
       db_query("UPDATE {poll_choices} SET chvotes = chvotes - 1 WHERE nid = %d AND chorder = %d", $node->nid, $node->vote);
       cache_clear_all();
     }
  4. Save this file and upload.  Now the Poll block should work well.
ibed below:
  1. December 4th, 2011 at 22:04 | #1

    I love your blog.. very nice colors & theme. Did you make this website yourself or did you hire someone to do it for you? Plz answer back as I’m looking to construct my own blog and would like to know where u got this from. thanks

  2. December 4th, 2011 at 22:54 | #2

    There are a few helpful things soon enough in this particular article but I don?? comprehend if I see all of them core to heart. There is some validity however I’ll bring hold belief unless I look at it further. Good article , appreciate it and we wish more! Added to FeedBurner as well

  3. December 5th, 2011 at 04:05 | #3

    Thanks for sharing Pear’s Fun Facts: Bananarama | Annoying Orange with us keep update bro love your article about Pear’s Fun Facts: Bananarama | Annoying Orange .

  4. December 5th, 2011 at 04:35 | #4

    My husband and i got more than happy Raymond managed to conclude his homework while using the precious recommendations he grabbed from your web pages. It’s not at all simplistic just to happen to be giving out tips and hints that many other folks have been trying to sell. And we discover we have you to be grateful to because of that. The type of explanations you’ve made, the easy website navigation, the friendships you can make it possible to engender – it is mostly sensational, and it is leading our son and us consider that this concept is fun, and that’s pretty indispensable. Many thanks for all the pieces!

  5. December 5th, 2011 at 08:36 | #5

    Gift baskets this holidays are something that everyone should be considering during Xmas day and in this short article I’m going to provide you with a handful of main reasons why It really is extremely crucial that you too think about buying gift baskets to your family members.

  6. December 5th, 2011 at 11:53 | #6

    I’m curious to find out what blog platform you happen to be utilizing? I’m having some minor security problems with my latest site and I’d like to find something more secure. Do you have any recommendations?

  7. December 5th, 2011 at 13:55 | #7

    Hey good blog!!! Man .. Beautiful .. Incredible .. I am going to bookmark your website and take the feeds also great article thanks Hi. I needed to thank you for that fantastic information you’ve posted on your site. I am going to definitelycome to look it over once again and have subscribedto your Rss. Have a fantastic day.

  8. December 5th, 2011 at 14:42 | #8

    What youre declaring is completely accurate. I am aware that everyone need to say precisely the same point, but I just believe that you put it within a way that everyone can comprehend. I also enjoy the photos you put in here. They fit so well with what youre wanting to say. Im certain youll reach so many men and women with what youve obtained to say.

  9. December 5th, 2011 at 14:54 | #9

    I like this web blog very much so much good info.

  10. January 11th, 2012 at 08:10 | #10

    Magnificent goods from you, man. I have remember your stuff previous to and you’re simply too great. I actually like what you have bought right here, really like what you’re saying and the best way wherein you are saying it. You are making it enjoyable and you continue to take care of to stay it smart. I cant wait to read far more from you. This is actually a tremendous website.

Comment pages
1 4 5 6 153
  1. October 19th, 2011 at 08:12 | #1
  2. November 17th, 2011 at 11:49 | #2
  3. November 30th, 2011 at 01:16 | #3
  4. December 1st, 2011 at 11:17 | #4
  5. December 1st, 2011 at 16:23 | #5
  6. December 2nd, 2011 at 02:56 | #6
  7. December 2nd, 2011 at 05:38 | #7
  8. December 3rd, 2011 at 06:31 | #8
  9. December 3rd, 2011 at 12:37 | #9
  10. December 3rd, 2011 at 14:48 | #10
  11. December 4th, 2011 at 05:27 | #11
  12. December 4th, 2011 at 07:48 | #12
  13. December 5th, 2011 at 03:04 | #13
  14. December 5th, 2011 at 10:30 | #14
  15. December 5th, 2011 at 11:25 | #15