1

I'm using polls (from core) to make some enquetes. Is there a way to use views to display only the polls that a user has not voted yet?

I didn't find anything on google. The only solutions where for Drupal 6. And I'm using drupal 7.

Thanks in advance =)

2
  • Have you actually tried? Can you, for example, create a view that simply shows all the pools?
    – Mołot
    Jul 10 '14 at 8:06
  • Yes. But it shows the polls as full nodes. It does not give any field that I could test if the user has voted or not. The only filter that I see is if the poll is open or closed. I've used views with relationships before, and with the poll module I am really lost. =P
    – nimbfire
    Jul 10 '14 at 21:19
0

First I tried to use the hook_views_pre_execute.

<?php    
function hook_views_pre_execute(&$view) {
    $uid = $GLOBALS['user']->uid;

    //gets one the poll and return it
        $query = db_select('node', 'n');
        $query->fields('n');

        $subquery = db_select('poll_vote', '');
        $subquery->fields('poll_vote', array('nid'));
        $subquery->condition('poll_vote.uid', $uid);

        $query->condition('n.nid', $subquery, 'NOT IN');
        $query->condition('type', 'poll');
        $query->condition('status', 1);
        $query->range(0, 1);

    $view->build_info['query'] = $query;
    $view->build_info['count_query'] = $query;
}

But when I tried to use the pager, it had too many bugs. It felt like a hack. So in the end I used:

<?php
/**
 * Returns a page with one not voted poll
 * @return [array] [drupal page array]
 */
function one_poll_page(){

    $page[] = "";
    $uid = $GLOBALS['user']->uid;

    //gets one the poll and return it
        $query = db_select('node', 'n');
        $query->fields('n');

        $subquery = db_select('poll_vote', '');
        $subquery->fields('poll_vote', array('nid'));
        $subquery->condition('poll_vote.uid', $uid);

        $query->condition('n.nid', $subquery, 'NOT IN');
        $query->condition('type', 'poll');
        $query->condition('status', 1);
        $query->range(0, 1);

        $result = $query->execute();

    foreach ($result as $record) {
      //saves the rendered poll node to the array
      $node = node_load($record->nid);
      $view = node_view($node, 'full');
      $rendered = drupal_render($view);
      $page['#markup'] .= $rendered;
    }

    return $page;
}

Because I can then control exactly what the page does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.