<?php

# MODxBB * PrevPollsBB * by N!Ce (A.S.) September, 2010
# Version 0.9.4
# Based on fetchpoll by Noxwizard of phpBB
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::



define('IN_PHPBB', true);
global $user, $auth, $db, $phpEx, $modx;
$phpbb_root_path = './forum/';


// configuration and initialisation
$forum_ids = array(5,6); // forum ID to pull polls from
$num_posts = 5; // total number of polls to display
$per_page = 1; // number of polls per page
$page_start = request_var('start','1'); // request pagination value

// get the desired forums the user can read from
$can_read_forum = $auth->acl_getf('f_read');
$forums_auth_ary = array_keys($can_read_forum);
$authed_ary = array_intersect($forum_ids, $forums_auth_ary);
unset($can_read_forum);
unset($forums_auth_ary);

// get the topic IDs
$sql = 'SELECT topic_id FROM ' . TOPICS_TABLE . "
WHERE poll_title <> ''
AND " . $db->sql_in_set('forum_id', $authed_ary) . '
ORDER BY topic_last_post_time DESC';

$result = $db->sql_query_limit($sql, $num_posts);
$rows = $db->sql_fetchrowset($result);
$db->sql_freeresult($result);

if (!$rows) {
	echo '<br /><br /><br /><h2 style="text-align:center;">No polls found</h2>';
	return;
}

// calculate pagination and content per page
$pollcount = (count($rows)) ? count($rows) : 1;
if ($per_page > $pollcount) {
	$per_page = $pollcount;
}
$rows0 = array();
$rows0 = array_slice($rows, round($page_start - 1), $per_page);
$topics = array();

// output the according pagination label
if (round($page_start) == $pollcount || round($page_start) == round($page_start + $per_page - 1)) {
	echo  '<br /><div style="text-align:center;">(Showing Poll ' . round($page_start) . ' of ' . $pollcount . ')</div><br />'; 
} elseif (round($page_start + $per_page - 1) > $pollcount) {
	echo '<br /><div style="text-align:center;">(Showing Poll ' . round($page_start) . ' - ' . $pollcount . ' of ' . $pollcount . ')</div><br />'; 
} else {
	echo '<br /><div style="text-align:center;">(Showing Poll ' . round($page_start) . ' - ' . round($page_start + $per_page - 1) . ' of ' . $pollcount . ')</div><br />'; 
}

foreach($rows0 as $row)
{
	$topics[] = $row['topic_id'];
}

// get the poll information -  needs two queries to effectively limit it
$sql = 'SELECT t.poll_title, t.topic_id, p.poll_option_text, p.poll_option_total
FROM ' . TOPICS_TABLE . ' t, ' . POLL_OPTIONS_TABLE . " p
WHERE t.poll_title <> ''
AND p.topic_id = t.topic_id
AND " . $db->sql_in_set('t.topic_id', $topics) . '
ORDER BY t.topic_last_post_time DESC';

$result = $db->sql_query($sql);
$row = $db->sql_fetchrowset($result);
$db->sql_freeresult($result);

$polls = array();
$row = array_reverse($row);
foreach($row as $entry)
{
	$polls[$entry['topic_id']]['title'] = $entry['poll_title'];
	$polls[$entry['topic_id']]['poll_total'] = 0;
	$polls[$entry['topic_id']][] = $entry['poll_option_text'];
	$polls[$entry['topic_id']]['totals'][] = $entry['poll_option_total'];
}

// calculate the total count
foreach($polls as $topic_id => $poll)
{
	$poll_total = 0;
	$count = 0;
	foreach($poll['totals'] as $total)
	{
		$poll_total += $total;
		$count++;
	}
	$polls[$topic_id]['poll_total'] = $poll_total;
	$polls[$topic_id]['poll_option_count'] = $count;
}

// output the votes
foreach($polls as $topic_id => $poll)
{
	$post_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 't=' . $topic_id);

	// strip bbcodes from title
	strip_bbcode($poll['title']);

	echo '<div class="sitepoll"><h2 style="margin:4px auto; text-align:center;">' . $poll['title'] . '</h2><table>';

	for ($i = 0; $i < $poll['poll_option_count']; $i++)
	{
		strip_bbcode($poll[$i]);
		echo '<tr class="pollrow" style="border:1px dashed lightgrey;">
		<td class="polloptions" style="padding-left:3px">' . $poll[$i] . '</td>
		<td class="pollshare"><div style="color:white; width:' . round(($poll['totals'][$i] / max($poll['totals']))*100) . '%;">' . round((($poll['totals'][$i] / $poll['poll_total'])*100), 2) . '%</div></td>
		<td align="right" style="width:90px; padding-right:21px;"> [' . $poll['totals'][$i] . ' votes]</td>
		</tr>';
	}
	echo '<tr><td colspan="3" style="padding:3px 0; text-align:center;"><a href="' . $post_url . '">Discuss this poll...</a></td></tr></table></div><br />';
}

// output pagination
if ($pollcount > $per_page) {
	echo '<div class="pagination">';
	for ($i = 1; $i <= ceil($pollcount / $per_page); $i++) {
		if (round(($page_start + $per_page - 1) / $per_page) == $i) {
			echo '<strong>' . $i . '</strong>';
		} else {
			echo '<a href="' . $modx->config['site_url'] . 'index.php?id=' . $modx->documentIdentifier . '&start=' . round($per_page * ($i - 1) + 1) . '">' . $i . '</a>';
		}
	}
	echo '</div>';
}

?>