Combining the code in the built-in most recent poll question block with modifications and the code from the handbook on showing a block based on block types leads to the ability to add a block that shows the "next" poll question below the current question.

This allows someone to immediately answer the next poll question without needing to load another page. It's especially useful when you have a set of related questions.

AFAIK, the code below should work with 5.x, but I've only actually tested it with the 4.7 version of the advanced poll module, since that's the site I needed it for.

Steps to implement:

  1. Go to Admin -> Block -> Add Block
  2. Description -> "Next Question", Title -> "Next Question"
  3. Body ->
    <?php
    $output = '';
      if (arg(0) == 'node' && is_numeric(arg(1))) {
        $nid = arg(1);
            $result = db_query('SELECT nid FROM {advpoll} WHERE active=1 and nid < '.$nid.' ORDER BY nid DESC LIMIT 1');
            // Check that there is an active poll
            if (db_num_rows($result) > 0) {
            $poll = db_fetch_object($result);
        $node = node_load($poll->nid);
        $output .= '<h2>'.$node->title.':</h2>';
                    advpoll_view($node, false, true, false);
        $output .= $node->body;
            }
            else {
                    $output .= t('No more Poll questions.');
            }
    }
      print $output;
    ?>
    
  4. Input Format -> PHP Code and Save Block
  5. Back on the block page, "Configure" the block you just created.
  6. Under page specific visibility settings choose "Show if the following PHP code returns TRUE (PHP-mode, experts only)." and put the following in the box:
    <?php
      // Only show if $match is true
      $match = false;
    
      // Which node types
      $types = array('advpoll-binary', 'advpoll-ranking');
    
      // Match current node type with array of types
      if (arg(0) == 'node' && is_numeric(arg(1))) {
        $nid = arg(1);
        $node = node_load(array('nid' => $nid));
        $type = $node->type;  
        $match |= in_array($type, $types);
      }
    
      return $match;
    ?>
    
  7. Save Block
  8. Enable the block, set the weight to 10 and the placement to "content" or whatever your theme calls the main content area

Now each poll question, when viewed in page view, will show the question with the next higher nid just below it as the "Next Question". It should be possible to only show the next question block if the user has answered the previous question already and is thus looking at the results, but I'll leave that as an exercise for the future.

Comments

Fayna’s picture

I'm interested in the feature to only allow this block to show up if the user has voted on the poll. How would one go about doing this?

Thank you for posting your code! :-)