Location: PHPKode > projects > Enigma > Enigma2/Enigma2_Install/Blocks/Block_polls.php
<?php
/*
*****************************************************************

 Block_polls.php
 
*****************************************************************
LSP: Lunabyte Systems Portal
Open-Source Project Inspired by Zef Hemel (hide@address.com)
*****************************************************************
Software Version:                  LSP 2.0 "Enigma 2"
Software by:                         Lunabyte Systems (http://www.lunabyte.net)
Copyright 2002-2005 by:       Lunabyte Systems (http://www.lunabyte.net)
Support, News, Updates at:    http://www.lunabyte.net
*****************************************************************
This program is free software; you may redistribute it and/or modify it
under the terms of the provided license as published by Lunabyte Systems.

This program is distributed in the hope that it is and will be useful,                  
but WITHOUT ANY WARRANTIES; without even any implied warranty of           
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                        

See the "LSP_license.txt" file for details of the LSP license.
The latest version can always be found at http://www.lunabyte.net.
*****************************************************************
*/

if (!defined('ENIGMA'))
	die('<b>Access Violation</b><br />Direct Access to this location is not allowed.');

// set this so the load Blocks functions knows there's a template file for this block
// But... we only set it if the current user can view the poll.  Why load the template if we don't need to... ;)
if (allowedTo('bpoll_view'))
	$blockTemplate = true;

function Block_polls()
{
	global $db_prefix, $context, $scripturl, $modSettings, $settings, $txt, $ID_MEMBER, $user_info, $bpollinfo;

	// polls is a module, so we need to load the language file here instead of letting Blocks do it.
	loadMLanguage('Polls');

	// find out what the poll settings are and get the poll id accordingly
	if(empty($modSettings['polls_display']) || !allowedTo('bpoll_view'))
	{  // if the settings haven't been adjusted, display a "No polls" message.
		$context['no_poll'] = (int) true;
		return;
	}
	elseif ($modSettings['polls_display'] == -1)
	{  // if a random poll, pick an id at random
		$request = db_query("
			SELECT ID_POLL
			FROM {$db_prefix}polls
			ORDER BY RAND()
			LIMIT 1", __FILE__, __LINE__);
		$ID_POLL = mysql_num_rows($request) > 0 ? mysql_result($request,0,"ID_POLL") : '';
		mysql_free_result($request);
	}
	elseif ($modSettings['polls_display'] == -2)
	{  // if latest poll, get the highest id number
		$request = db_query("
			SELECT MAX(ID_POLL)
			FROM {$db_prefix}polls
			LIMIT 1", __FILE__, __LINE__);
		$ID_POLL = mysql_num_rows($request) > 0 ? mysql_result($request,0,"max(ID_POLL)") : '';
		mysql_free_result($request);
	}
	elseif ($modSettings['polls_display'] != 0)
	{  // was a specific poll picked for display?
		$ID_POLL = $modSettings['polls_display'];
	}

	// make sure there is an ID set to run the query on, if not exit and display the no poll message.
	if (empty($ID_POLL))
	{
		$context['no_poll'] = (int) true;
		return;
	}

	// Get the question and if it's locked.
	$request = db_query("
		SELECT
			p.ID_POLL, p.question, p.votingLocked, p.hideResults, p.expireTime, p.maxVotes, p.changeVote,
			p.ID_MEMBER, IFNULL(mem.realName, p.posterName) AS posterName,
			COUNT(DISTINCT lp.ID_MEMBER) AS total, COUNT(lp2.ID_MEMBER) AS has_voted,
			t.ID_TOPIC, t.ID_BOARD, b.memberGroups
		FROM ({$db_prefix}polls AS p)
			LEFT JOIN {$db_prefix}log_polls AS lp ON (lp.ID_POLL = p.ID_POLL)
			LEFT JOIN {$db_prefix}log_polls AS lp2 ON (lp2.ID_POLL = p.ID_POLL AND lp2.ID_MEMBER = $ID_MEMBER)
			LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = p.ID_MEMBER)
			LEFT JOIN {$db_prefix}topics AS t ON (t.ID_POLL = p.ID_POLL)
			LEFT JOIN {$db_prefix}boards AS b ON (b.ID_BOARD = t.ID_BOARD)
		WHERE p.ID_POLL = $ID_POLL
		GROUP BY p.ID_POLL
		LIMIT 1", __FILE__, __LINE__);
	$bpollinfo = mysql_fetch_assoc($request);
	mysql_free_result($request);

	// if poll is attached to a forum topic, make sure they can view the topic, if not, they can't view the poll
	if (!empty($bpollinfo['ID_TOPIC']))
	{
		$boardGroups = $bpollinfo['memberGroups'] == '' ? array() : explode(',', $bpollinfo['memberGroups']);
		if (count(array_intersect($user_info['groups'], $boardGroups)) == 0 && !$user_info['is_admin'])
		{  // sorry, can't view this one
		$context['no_poll'] = (int) true;
			return;
		}
	}
	// is this poll just a block poll?  Then check if the user can view those type of polls
	if (empty($bpollinfo['ID_TOPIC']) && !allowedTo('bpoll_view'))
	{  // sorry, can't view this one
		$context['no_poll'] = (int) true;
		return;
	}

	// Get all the options, and calculate the total votes.
	$request = db_query("
		SELECT ID_CHOICE, label, votes
		FROM {$db_prefix}poll_choices
		WHERE ID_POLL = $bpollinfo[ID_POLL]", __FILE__, __LINE__);
	$bpollOptions = array();
	$realtotal = 0;
	while ($row = mysql_fetch_assoc($request))
	{
		$bpollOptions[$row['ID_CHOICE']] = array($row['label'], $row['votes']);
		$realtotal += $row['votes'];
	}
	mysql_free_result($request);

	// Set up the basic poll information.
	$context['bpoll'] = array(
		'id' => $ID_POLL,
		'image' => 'normal_' . (empty($bpollinfo['votingLocked']) ? 'poll' : 'locked_poll'),
		'question' => doUBBC($bpollinfo['question']),
		'total_votes' => $bpollinfo['total'],
		'change_vote' => !empty($bpollinfo['changeVote']),
		'is_locked' => !empty($bpollinfo['votingLocked']),
		'options' => array(),
		'lock' => allowedTo('bpoll_lock'), // the following for future?  !empty($pollTopic['ID_BOARD']) ? allowedTo('poll_lock_any', $pollTopic['ID_BOARD']) || ($context['user']['started'] && allowedTo('poll_lock_own', $pollTopic['ID_BOARD'])) : allowedTo('poll_lock_any') || ($context['user']['started'] && allowedTo('poll_lock_own')),
		'edit' => allowedTo('poll_edit_any'), // the following for future?  !empty($pollTopic['ID_BOARD']) ? allowedTo('poll_edit_any', $pollTopic['ID_BOARD']) || ($context['user']['started'] && allowedTo('poll_edit_own', $pollTopic['ID_BOARD'])) : allowedTo('poll_edit_any') || ($context['user']['started'] && allowedTo('poll_edit_own')),
		'allowed_warning' => $bpollinfo['maxVotes'] > 1 ? sprintf($txt['poll_options6'], $bpollinfo['maxVotes']) : '',
		'is_expired' => !empty($bpollinfo['expireTime']) && $bpollinfo['expireTime'] < time(),
		'expire_time' => !empty($bpollinfo['expireTime']) ? timeformat($bpollinfo['expireTime']) : 0,
		'has_voted' => !empty($bpollinfo['has_voted']),
		'starter' => array(
			'id' => $bpollinfo['ID_MEMBER'],
			'name' => $row['posterName'],
			'href' => $bpollinfo['ID_MEMBER'] == 0 ? '' : $scripturl . '?action=profile;u=' . $bpollinfo['ID_MEMBER'],
			'link' => $bpollinfo['ID_MEMBER'] == 0 ? $row['posterName'] : '<a href="' . $scripturl . '?action=profile;u=' . $bpollinfo['ID_MEMBER'] . '">' . $row['posterName'] . '</a>'
		)
	);

	// You're allowed to vote if:
	// 1. the poll did not expire, and
	// 2. you're not a guest... and
	// 3. you're not trying to view the results, and
	// 4. the poll is not locked, and
	// 5. you have the proper permissions, and
	$allow_vote = !empty($pollTopic['ID_BOARD']) ? allowedTo('poll_vote', $pollTopic['ID_BOARD']) : allowedTo('bpoll_vote');
	$context['allow_vote'] = !$context['bpoll']['is_expired'] && !$user_info['is_guest'] && !isset($_REQUEST['viewResults']) && empty($bpollinfo['votingLocked']) && !empty($allow_vote);

	// You're allowed to view the results if:  (used for a view results link)
	// 1. You're just a super-nice-guy, or
	// 2. Anyone can see them (hideResults == 0), or
	// 3. You can see them after you voted (hideResults == 1), or
	// 4. You've waited long enough for the poll to expire. (whether hideResults is 1 or 2.)
	$allow_moderate = !empty($pollTopic['ID_BOARD']) ? allowedTo('moderate_board', $pollTopic['ID_BOARD']) : allowedTo('moderate_board');
	$context['allow_poll_view'] = !empty($allow_moderate) || $bpollinfo['hideResults'] == 0 || ($bpollinfo['hideResults'] == 1 && $context['bpoll']['has_voted']) || $context['bpoll']['is_expired'];

	// You're allowed to change your vote if:
	// 1. the poll did not expire, and
	// 2. you're not a guest... and
	// 3. the poll is not locked, and
	// 4. you have the proper permissions, and
	// 5. you have already voted.
	// 6. the poll creator has said you can!
	$context['allow_change_vote'] = !$context['bpoll']['is_expired'] && !$user_info['is_guest'] && empty($bpollinfo['votingLocked']) && !empty($allow_vote) && $context['bpoll']['has_voted'] && $context['bpoll']['change_vote'];

	// Calculate the percentages and bar lengths...
	$divisor = $realtotal == 0 ? 1 : $realtotal;

	// Determine if a decimal point is needed in order for the options to add to 100%.
	$precision = $realtotal == 100 ? 0 : 1;

	// Now look through each option, and...
	foreach ($bpollOptions as $i => $option)
	{
		// First calculate the percentage, and then the width of the bar...
		$bar = round(($option[1] * 100) / $divisor, $precision);
		$barWide = $bar == 0 ? 1 : floor(($bar * 2) / 4);

		// Now add it to the poll's contextual theme data.
		$context['bpoll']['options'][$i] = array(
			'percent' => $bar,
			'votes' => $option[1],
			'bar' => '<span style="white-space: nowrap;"><img src="' . $settings['images_url'] . '/poll_left.gif" alt="" /><img src="' . $settings['images_url'] . '/poll_middle.gif" width="' . $barWide . '" height="12" alt="-" /><img src="' . $settings['images_url'] . '/poll_right.gif" alt="" /></span>',
			'option' => doUBBC($option[0]),
			'vote_button' => '<input type="' . ($bpollinfo['maxVotes'] > 1 ? 'checkbox' : 'radio') . '" name="options[]" value="' . $i . '" class="check" />'
		);
	}

	// setup up the URL Location to return to after performing an action.
	$context['bpoll']['qstr'] = $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];

}
?>
Return current item: Enigma