Hi,

I am displaying a poll on my website via a block. When the current user has not voted it shows the poll vote form, however I want to display both the current poll result and the poll vote form.

I'm sure this is possible but have no idea how, any pointers would be helpful.

Comments

IbnDrupal’s picture

I figured this out and am putting the answer here for others. Answer is for D6.

1. You will need to install the chart API module for this to work

2. In your template.php file paste the following function, change mytheme to your theme's name:

function mytheme_preprocess_poll_vote(&$variables) {
  $choices = $variables['form']['#node']->choice;

  $chart = array(
    '#chart_id' => 'poll_chart',
    '#type' => CHART_TYPE_PIE_3D,
  );

  $total_votes = 0;
  foreach ($choices as $c) { //total votes
     $total_votes += $c['chvotes'];
  }
  foreach ($choices as $c) { //make labels
     $chart['#data'][$c['chtext']] = $c['chvotes'];
     $percentage = round($c['chvotes'] * 100 / max($total_votes, 1));
     $chart['#legends'][] = $c['chtext'] . " (" . $percentage . "%)";
  }
  $variables['chart'] = chart_render($chart);
}

3. Copy the template file for the voting form to your theme directory, it can be found at /modules/poll/poll-vote.tpl.php
4. Insert the following lines into the poll-vote.tpl.php file wherever you want the chart printed

<div class="chart">
  <?php print $chart; ?>
</div>

Hope that helps!

3g0n’s picture

Hey,

thank you for the chart, but can you help me to display it in the result page?

Thanks,
3g0n

IbnDrupal’s picture

OK carrying on from before, put this in your template.php file (don't forget to change mytheme to your theme name)

function mytheme_preprocess_poll_results(&$variables) {
  //theme the poll results block

  $html = $variables['results'];
  $pattern = "@class=\"text\"\>(.*)\</div\>@";
  preg_match_all($pattern, $html, $choices); //extract choices
  $pattern = "@style=\"width: (.*)%;\"@";
  preg_match_all($pattern, $html, $votes); //extract percentages

  $chart = array(
    '#chart_id' => 'poll_chart',
    '#type' => CHART_TYPE_PIE_3D,
  );
// NEED TO FILL IN DATA TO MAKE CHART
  for ($c=0; $c < count($choices[1]); $c++) { //make labels and values
     $chart['#data'][$choices[1][$c]] = $votes[1][$c]; //number of votes
     $chart['#legends'][] = $choices[1][$c] . " (" . $votes[1][$c] . "%)"; // labels
  }
  $variables['results'] = chart_render($chart); //render graph
}

copy the poll-results-block.tpl.php to your template folder and make it something like

<div class="poll">
  <h3 class="title"><?php print $title ?></h3>
  <?php print $results ?>
</div>

And now your results block should contain the graph.

saiprasad kandavalli’s picture

Hi IbnDrupal, I got a chart. but I want to cancel my vote below the graph.
any suggestions.....

arbel’s picture

if you want to display multiple poll in the same page, you should change the chart id to :

'#chart_id' => 'poll_chart' . $variables[nid],

onewayticket’s picture

Thanks arbel but I've tried to add the code to allow multiple polls and it doesn't appear to show the each individual google chart. Strangely if I have two polls in the same page, the only chart displayed is the first one and the other charts show the same images URL as the first, so I need to make a distinction somewhere in the code to allow two or more charts to be rendered in one page. I do have a chart in a block as well. Could this be causing the problem?

Is there another way around this?

saiprasad kandavalli’s picture

Hi , i am doing this by using node_load function

miaoulafrite’s picture

hi i'm interested by the method of saiprasad_saiprasad
it does not require any additional module but uses hook_load($node)

if you can describe the way you did it here it would be fine

thanks ;)

saiprasad kandavalli’s picture

$am__node = node_load($nodeid); echo node_view($am__node, "FALSE");

Vishal Jain’s picture

Hi,

I have tried the method suggested by you but I did not work. I am working in Drupal 7 and novice to the system. Will this technique work on Drupal 7 if not could you tell me the way to make it work?

Thanks a ton.

Regards
Vishal Jain

saiprasad kandavalli’s picture

hi vishal, passing variables changed in drupal 7 and drupal 8.

check below:

4.6 – 6 node_view($node, $teaser = FALSE, $page = FALSE, $links = TRUE)
7 – 8 node_view($node, $view_mode = 'full', $langcode = NULL)

scoutex’s picture

Thanks @IbnDrupal, for a great tip

Dalvtor’s picture

Can anyone help me to do this in Drupal 7, please?

I've followed step by step but it doesn't work.

Thanks.