I am importing content from phpBB via the phpbb2drupal module. As part of this module, phpbb polls are migrated. In my current migration, there are 50+ polls.

When displaying polls via the poll.module, the standard pager query is set for 15 requests. As I have more than that migrated over, I should see the standard pager displayed.

I have traced this to the pager_query() call in the poll.module for the function poll_page(). This function sends a query to the via the pager_query() function (part of pager module). The pager (includes/pager.inc) attempts to run a query to obtain the record count of the query prepared in the poll module so as to determine the number of pager results to display.

As stated in the pager_query() function:

* Unfortunately, the rewrite rule does not always work as intended for queries
* that already have a "COUNT(*)" or a "GROUP BY" clause, and possibly for
* other complex queries. In those cases, you can optionally pass a query that
* will be used to count the records.
*
* For example, if you want to page the query "SELECT COUNT(*), TYPE FROM node
* GROUP BY TYPE", pager_query() would invoke the incorrect query "SELECT
* COUNT(*) FROM node GROUP BY TYPE". So instead, you should pass "SELECT
* COUNT(DISTINCT(TYPE)) FROM node" as the optional $count_query parameter.

The query passed to pager_query() inside the poll_page() function does in fact include a GROUP BY query. The result is that the query that pager_query() uses to attempt to get the total record count does not return the desired results.

pager_query() has an optional function parameter used to replace the standard record COUNT query in pager_query(). If this parameter is sent in the poll_page() function in poll.module, the pager will work as expected.

In poll.module, I made the following changes to poll_page(). The pager now displays as expected.

$count_query = "SELECT COUNT(*) FROM {node} n INNER JOIN {poll} p ON n.nid = p.nid WHERE n.status = 1 AND n.moderate = 0";
$count_query = db_rewrite_sql($count_query);
$result = pager_query($sql, 15, 0, $count_query);

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mindless’s picture

Thanks for the patch.. works for me.

beginner’s picture

Version: 4.7.0 » x.y.z
magico’s picture

Title: Pager not working for Poll module » Pager not working for poll.module
Status: Active » Needs work

@jaydub: can you verify if this is still a problem with HEAD?

Anyway there is some code that could be used to create a proper patch.

mindless’s picture

Version: x.y.z » 6.x-dev
Status: Needs work » Needs review
FileSize
1.07 KB

Just put the code from above into a patch against current HEAD rev for poll.module.
Not sure why the COUNT query includes n.moderate = 0 as $sql doesn't have this.

Dries’s picture

mindless: you're right. That moderate-test should no longer be there ... :)

mindless’s picture

FileSize
1.04 KB

Updated patch.

catch’s picture

Status: Needs review » Needs work

No longer applies.

Gábor Hojtsy’s picture

Version: 6.x-dev » 5.x-dev
Status: Needs work » Reviewed & tested by the community
FileSize
1.52 KB

The patch is not applicable to neither the latest 5.x, neither the latest 6.x code, as both have a counter query in place, so this is solved already. The only bug I noticed around here is that although the original query was run through db_rewrite_sql(), the counter query was not. I committed a fix to 6.x just like the one I attach for 5.x

Gábor Hojtsy’s picture

Title: Pager not working for poll.module » Poll page pager is not node access aware
Gábor Hojtsy’s picture

Assigned: Unassigned » Gábor Hojtsy
drumm’s picture

Status: Reviewed & tested by the community » Fixed
drumm’s picture

Committed to 5.x.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.