Browse Source

Poll results are displayed in the print page of the topic [Bug 4881]

Signed-off-by: emanuele <[email protected]>
emanuele 12 years ago
parent
commit
8897f2a98a
2 changed files with 194 additions and 10 deletions
  1. 165 2
      Sources/Printpage.php
  2. 29 8
      Themes/default/Printpage.template.php

+ 165 - 2
Sources/Printpage.php

@@ -29,7 +29,7 @@ if (!defined('SMF'))
 function PrintTopic()
 {
 	global $topic, $txt, $scripturl, $context, $user_info;
-	global $board_info, $smcFunc, $modSettings;
+	global $board_info, $smcFunc, $modSettings, $settings;
 
 	// Redirect to the boardindex if no valid topic id is provided.
 	if (empty($topic))
@@ -40,9 +40,10 @@ function PrintTopic()
 
 	// Get the topic starter information.
 	$request = $smcFunc['db_query']('', '
-		SELECT m.poster_time, IFNULL(mem.real_name, m.poster_name) AS poster_name
+		SELECT mem.id_member, m.poster_time, IFNULL(mem.real_name, m.poster_name) AS poster_name, t.id_poll
 		FROM {db_prefix}messages AS m
 			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
+			LEFT JOIN {db_prefix}topics as t ON (t.id_first_msg = m.id_msg)
 		WHERE m.id_topic = {int:current_topic}
 		ORDER BY m.id_msg
 		LIMIT 1',
@@ -56,6 +57,168 @@ function PrintTopic()
 	$row = $smcFunc['db_fetch_assoc']($request);
 	$smcFunc['db_free_result']($request);
 
+	if (!empty($row['id_poll']))
+	{
+		loadLanguage('Post');
+		// Get the question and if it's locked.
+		$request = $smcFunc['db_query']('', '
+			SELECT
+				p.question, p.voting_locked, p.hide_results, p.expire_time, p.max_votes, p.change_vote,
+				p.guest_vote, p.id_member, IFNULL(mem.real_name, p.poster_name) AS poster_name, p.num_guest_voters, p.reset_poll
+			FROM {db_prefix}polls AS p
+				LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = p.id_member)
+			WHERE p.id_poll = {int:id_poll}
+			LIMIT 1',
+			array(
+				'id_poll' => $row['id_poll'],
+			)
+		);
+		$pollinfo = $smcFunc['db_fetch_assoc']($request);
+		$smcFunc['db_free_result']($request);
+
+		$request = $smcFunc['db_query']('', '
+			SELECT COUNT(DISTINCT id_member) AS total
+			FROM {db_prefix}log_polls
+			WHERE id_poll = {int:id_poll}
+				AND id_member != {int:not_guest}',
+			array(
+				'id_poll' => $row['id_poll'],
+				'not_guest' => 0,
+			)
+		);
+		list ($pollinfo['total']) = $smcFunc['db_fetch_row']($request);
+		$smcFunc['db_free_result']($request);
+
+		// Total voters needs to include guest voters
+		$pollinfo['total'] += $pollinfo['num_guest_voters'];
+
+		// Get all the options, and calculate the total votes.
+		$request = $smcFunc['db_query']('', '
+			SELECT pc.id_choice, pc.label, pc.votes, IFNULL(lp.id_choice, -1) AS voted_this
+			FROM {db_prefix}poll_choices AS pc
+				LEFT JOIN {db_prefix}log_polls AS lp ON (lp.id_choice = pc.id_choice AND lp.id_poll = {int:id_poll} AND lp.id_member = {int:current_member} AND lp.id_member != {int:not_guest})
+			WHERE pc.id_poll = {int:id_poll}',
+			array(
+				'current_member' => $user_info['id'],
+				'id_poll' => $row['id_poll'],
+				'not_guest' => 0,
+			)
+		);
+		$pollOptions = array();
+		$realtotal = 0;
+		$pollinfo['has_voted'] = false;
+		while ($row = $smcFunc['db_fetch_assoc']($request))
+		{
+			censorText($row['label']);
+			$pollOptions[$row['id_choice']] = $row;
+			$realtotal += $row['votes'];
+			$pollinfo['has_voted'] |= $row['voted_this'] != -1;
+		}
+		$smcFunc['db_free_result']($request);
+
+		// If this is a guest we need to do our best to work out if they have voted, and what they voted for.
+		if ($user_info['is_guest'] && $pollinfo['guest_vote'] && allowedTo('poll_vote'))
+		{
+			if (!empty($_COOKIE['guest_poll_vote']) && preg_match('~^[0-9,;]+$~', $_COOKIE['guest_poll_vote']) && strpos($_COOKIE['guest_poll_vote'], ';' . $row['id_poll'] . ',') !== false)
+			{
+				// ;id,timestamp,[vote,vote...]; etc
+				$guestinfo = explode(';', $_COOKIE['guest_poll_vote']);
+				// Find the poll we're after.
+				foreach ($guestinfo as $i => $guestvoted)
+				{
+					$guestvoted = explode(',', $guestvoted);
+					if ($guestvoted[0] == $row['id_poll'])
+						break;
+				}
+				// Has the poll been reset since guest voted?
+				if ($pollinfo['reset_poll'] > $guestvoted[1])
+				{
+					// Remove the poll info from the cookie to allow guest to vote again
+					unset($guestinfo[$i]);
+					if (!empty($guestinfo))
+						$_COOKIE['guest_poll_vote'] = ';' . implode(';', $guestinfo);
+					else
+						unset($_COOKIE['guest_poll_vote']);
+				}
+				else
+				{
+					// What did they vote for?
+					unset($guestvoted[0], $guestvoted[1]);
+					foreach ($pollOptions as $choice => $details)
+					{
+						$pollOptions[$choice]['voted_this'] = in_array($choice, $guestvoted) ? 1 : -1;
+						$pollinfo['has_voted'] |= $pollOptions[$choice]['voted_this'] != -1;
+					}
+					unset($choice, $details, $guestvoted);
+				}
+				unset($guestinfo, $guestvoted, $i);
+			}
+		}
+
+		$context['user']['started'] = $user_info['id'] == $row['id_member'] && !$user_info['is_guest'];
+		// Set up the basic poll information.
+		$context['poll'] = array(
+			'id' => $row['id_poll'],
+			'image' => 'normal_' . (empty($pollinfo['voting_locked']) ? 'poll' : 'locked_poll'),
+			'question' => parse_bbc($pollinfo['question']),
+			'total_votes' => $pollinfo['total'],
+			'change_vote' => !empty($pollinfo['change_vote']),
+			'is_locked' => !empty($pollinfo['voting_locked']),
+			'options' => array(),
+			'lock' => allowedTo('poll_lock_any') || ($context['user']['started'] && allowedTo('poll_lock_own')),
+			'edit' => allowedTo('poll_edit_any') || ($context['user']['started'] && allowedTo('poll_edit_own')),
+			'allowed_warning' => $pollinfo['max_votes'] > 1 ? sprintf($txt['poll_options6'], min(count($pollOptions), $pollinfo['max_votes'])) : '',
+			'is_expired' => !empty($pollinfo['expire_time']) && $pollinfo['expire_time'] < time(),
+			'expire_time' => !empty($pollinfo['expire_time']) ? timeformat($pollinfo['expire_time']) : 0,
+			'has_voted' => !empty($pollinfo['has_voted']),
+			'starter' => array(
+				'id' => $pollinfo['id_member'],
+				'name' => $row['poster_name'],
+				'href' => $pollinfo['id_member'] == 0 ? '' : $scripturl . '?action=profile;u=' . $pollinfo['id_member'],
+				'link' => $pollinfo['id_member'] == 0 ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $pollinfo['id_member'] . '">' . $row['poster_name'] . '</a>'
+			)
+		);
+
+		// Make the lock and edit permissions defined above more directly accessible.
+		$context['allow_lock_poll'] = $context['poll']['lock'];
+		$context['allow_edit_poll'] = $context['poll']['edit'];
+
+		// You're allowed to view the results if:
+		// 1. you're just a super-nice-guy, or
+		// 2. anyone can see them (hide_results == 0), or
+		// 3. you can see them after you voted (hide_results == 1), or
+		// 4. you've waited long enough for the poll to expire. (whether hide_results is 1 or 2.)
+		$context['allow_poll_view'] = allowedTo('moderate_board') || $pollinfo['hide_results'] == 0 || ($pollinfo['hide_results'] == 1 && $context['poll']['has_voted']) || $context['poll']['is_expired'];
+
+		// 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 ($pollOptions as $i => $option)
+		{
+			// First calculate the percentage, and then the width of the bar...
+			$bar = round(($option['votes'] * 100) / $divisor, $precision);
+			$barWide = $bar == 0 ? 1 : floor(($bar * 8) / 3);
+
+			// Now add it to the poll's contextual theme data.
+			$context['poll']['options'][$i] = array(
+				'id' => 'options-' . $i,
+				'percent' => $bar,
+				'votes' => $option['votes'],
+				'voted_this' => $option['voted_this'] != -1,
+				'bar' => '<span style="white-space: nowrap;"><img src="' . $settings['images_url'] . '/poll_' . ($context['right_to_left'] ? 'right' : 'left') . '.png" alt="" /><img src="' . $settings['images_url'] . '/poll_middle.png" width="' . $barWide . '" height="12" alt="-" /><img src="' . $settings['images_url'] . '/poll_' . ($context['right_to_left'] ? 'left' : 'right') . '.png" alt="" /></span>',
+				// Note: IE < 8 requires us to set a width on the container, too.
+				'bar_ndt' => $bar > 0 ? '<div class="bar" style="width: ' . ($bar * 3.5 + 4) . 'px;"><div style="width: ' . $bar * 3.5 . 'px;"></div></div>' : '',
+				'bar_width' => $barWide,
+				'option' => parse_bbc($option['label']),
+				'vote_button' => '<input type="' . ($pollinfo['max_votes'] > 1 ? 'checkbox' : 'radio') . '" name="options[]" id="options-' . $i . '" value="' . $i . '" class="input_' . ($pollinfo['max_votes'] > 1 ? 'check' : 'radio') . '" />'
+			);
+		}
+	}
+
 	// Lets "output" all that info.
 	loadTemplate('Printpage');
 	$context['template_layers'] = array('print');

+ 29 - 8
Themes/default/Printpage.template.php

@@ -50,13 +50,13 @@ function template_print_above()
 				padding: 0;
 				list-style: none;
 			}
-			dt.postheader
+			div.postheader, #poll_data
 			{
 				border: solid #000;
 				border-width: 1px 0;
 				padding: 4px 0;
 			}
-			dd.postbody
+			div.postbody
 			{
 				margin: 1em 0 2em 2em;
 			}
@@ -98,27 +98,48 @@ function template_print_above()
 				color: black;
 				background-color: black;
 			}
+			.voted
+			{
+				font-weight: bold;
+			}
 		</style>
 	</head>
 	<body>
 		<h1 id="title">', $context['forum_name_html_safe'], '</h1>
 		<h2 id="linktree">', $context['category_name'], ' => ', (!empty($context['parent_boards']) ? implode(' => ', $context['parent_boards']) . ' => ' : ''), $context['board_name'], ' => ', $txt['topic_started'], ': ', $context['poster_name'], ' ', $txt['search_on'], ' ', $context['post_time'], '</h2>
-		<dl id="posts">';
+		<div id="posts">';
 }
 
 function template_main()
 {
 	global $context, $settings, $options, $txt;
 
+	if (!empty($context['poll']))
+	{
+		echo '
+			<div id="poll_data">', $txt['poll'], '
+				<div class="question">', $txt['poll_question'], ': <strong>', $context['poll']['question'], '</strong>';
+
+		$options = 1;
+		foreach ($context['poll']['options'] as $option)
+			echo '
+					<div class="', $option['voted_this'] ? 'voted' : '', '">', $txt['option'], ' ', $options++, ': <strong>', $option['option'], '</strong>
+						', $context['allow_poll_view'] ? $txt['votes'] . ': ' . $option['votes'] . '' : '', '
+					</div>';
+
+		echo '
+			</div>';
+	}
+
 	foreach ($context['posts'] as $post)
 		echo '
-			<dt class="postheader">
+			<div class="postheader">
 				', $txt['title'], ': <strong>', $post['subject'], '</strong><br />
 				', $txt['post_by'], ': <strong>', $post['member'], '</strong> ', $txt['search_on'], ' <strong>', $post['time'], '</strong>
-			</dt>
-			<dd class="postbody">
+			</div>
+			<div class="postbody">
 				', $post['body'], '
-			</dd>';
+			</div>';
 }
 
 function template_print_below()
@@ -126,7 +147,7 @@ function template_print_below()
 	global $context, $settings, $options;
 
 	echo '
-		</dl>
+		</div>
 		<div id="footer" class="smalltext">
 			', theme_copyright(), '
 		</div>