/** * Show the most recently posted poll. * * @param bool $topPollInstead = false * @param string $output_method = string */ function ssi_recentPoll($topPollInstead = false, $output_method = 'echo') { global $txt, $settings, $boardurl, $user_info, $context, $modSettings; $boardsAllowed = array_intersect(boardsAllowedTo('poll_view'), boardsAllowedTo('poll_vote')); if (empty($boardsAllowed)) { return array(); } $db = database(); $request = $db->query('', ' SELECT p.id_poll, p.question, t.id_topic, p.max_votes, p.guest_vote, p.hide_results, p.expire_time FROM {db_prefix}polls AS p INNER JOIN {db_prefix}topics AS t ON (t.id_poll = p.id_poll' . ($modSettings['postmod_active'] ? ' AND t.approved = {int:is_approved}' : '') . ') INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)' . ($topPollInstead ? ' INNER JOIN {db_prefix}poll_choices AS pc ON (pc.id_poll = p.id_poll)' : '') . ' LEFT JOIN {db_prefix}log_polls AS lp ON (lp.id_poll = p.id_poll AND lp.id_member > {int:no_member} AND lp.id_member = {int:current_member}) WHERE p.voting_locked = {int:voting_opened} AND (p.expire_time = {int:no_expiration} OR {int:current_time} < p.expire_time) AND ' . ($user_info['is_guest'] ? 'p.guest_vote = {int:guest_vote_allowed}' : 'lp.id_choice IS NULL') . ' AND {query_wanna_see_board}' . (!in_array(0, $boardsAllowed) ? ' AND b.id_board IN ({array_int:boards_allowed_list})' : '') . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? ' AND b.id_board != {int:recycle_enable}' : '') . ' ORDER BY ' . ($topPollInstead ? 'pc.votes' : 'p.id_poll') . ' DESC LIMIT 1', array('current_member' => $user_info['id'], 'boards_allowed_list' => $boardsAllowed, 'is_approved' => 1, 'guest_vote_allowed' => 1, 'no_member' => 0, 'voting_opened' => 0, 'no_expiration' => 0, 'current_time' => time(), 'recycle_enable' => $modSettings['recycle_board'])); $row = $db->fetch_assoc($request); $db->free_result($request); // This user has voted on all the polls. if ($row == false) { return array(); } // If this is a guest who's voted we'll through ourselves to show poll to show the results. if ($user_info['is_guest'] && (!$row['guest_vote'] || isset($_COOKIE['guest_poll_vote']) && in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote'])))) { return ssi_showPoll($row['id_topic'], $output_method); } $request = $db->query('', ' SELECT COUNT(DISTINCT id_member) FROM {db_prefix}log_polls WHERE id_poll = {int:current_poll}', array('current_poll' => $row['id_poll'])); list($total) = $db->fetch_row($request); $db->free_result($request); $request = $db->query('', ' SELECT id_choice, label, votes FROM {db_prefix}poll_choices WHERE id_poll = {int:current_poll}', array('current_poll' => $row['id_poll'])); $options = array(); while ($rowChoice = $db->fetch_assoc($request)) { censorText($rowChoice['label']); $options[$rowChoice['id_choice']] = array($rowChoice['label'], $rowChoice['votes']); } $db->free_result($request); // Can they view it? $is_expired = !empty($row['expire_time']) && $row['expire_time'] < time(); $allow_view_results = allowedTo('moderate_board') || $row['hide_results'] == 0 || $is_expired; $return = array('id' => $row['id_poll'], 'image' => 'poll', 'question' => $row['question'], 'total_votes' => $total, 'is_locked' => false, 'topic' => $row['id_topic'], 'allow_view_results' => $allow_view_results, 'options' => array()); // Calculate the percentages and bar lengths... $divisor = $return['total_votes'] == 0 ? 1 : $return['total_votes']; foreach ($options as $i => $option) { $bar = floor($option[1] * 100 / $divisor); $barWide = $bar == 0 ? 1 : floor($bar * 5 / 3); $return['options'][$i] = array('id' => 'options-' . ($topPollInstead ? 'top-' : 'recent-') . $i, 'percent' => $bar, 'votes' => $option[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" style="width:' . $barWide . 'px; height:12px;" alt="-" /><img src="' . $settings['images_url'] . '/poll_' . ($context['right_to_left'] ? 'left' : 'right') . '.png" alt="" /></span>', 'option' => parse_bbc($option[0]), 'vote_button' => '<input type="' . ($row['max_votes'] > 1 ? 'checkbox' : 'radio') . '" name="options[]" id="options-' . ($topPollInstead ? 'top-' : 'recent-') . $i . '" value="' . $i . '" class="input_' . ($row['max_votes'] > 1 ? 'check' : 'radio') . '" />'); } $return['allowed_warning'] = $row['max_votes'] > 1 ? sprintf($txt['poll_options6'], min(count($options), $row['max_votes'])) : ''; if ($output_method != 'echo') { return $return; } if ($allow_view_results) { echo ' <form class="ssi_poll" action="', $boardurl, '/SSI.php?ssi_function=pollVote" method="post" accept-charset="UTF-8"> <strong>', $return['question'], '</strong><br /> ', !empty($return['allowed_warning']) ? $return['allowed_warning'] . '<br />' : ''; foreach ($return['options'] as $option) { echo ' <label for="', $option['id'], '">', $option['vote_button'], ' ', $option['option'], '</label><br />'; } echo ' <input type="submit" value="', $txt['poll_vote'], '" class="button_submit" /> <input type="hidden" name="poll" value="', $return['id'], '" /> <input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '" /> </form>'; } else { echo $txt['poll_cannot_see']; } }
/** * Short description * * Long description * * @param * @return */ protected function show_poll() { try { $this->loadSSI(); } catch (Exception $e) { throw new \Exception($e->getMessage()); } if ('echo' == $this->output_method) { ob_start(); ssi_showPoll($this->topic, $this->output_method); $this->data = ob_get_contents(); ob_end_clean(); } else { $this->data = ssi_showPoll($this->topic, $this->output_method); } }
function sp_showPoll($parameters, $id, $return_parameters = false) { global $smcFunc, $context, $scripturl, $modSettings, $boardurl, $txt; $block_parameters = array('topic' => 'int', 'type' => 'select'); if ($return_parameters) { return $block_parameters; } $topic = !empty($parameters['topic']) ? $parameters['topic'] : null; $type = !empty($parameters['type']) ? (int) $parameters['type'] : 0; $boardsAllowed = boardsAllowedTo('poll_view'); if (empty($boardsAllowed)) { loadLanguage('Errors'); echo ' ', $txt['cannot_poll_view']; return; } if (!empty($type)) { $request = $smcFunc['db_query']('', ' SELECT t.id_topic FROM {db_prefix}polls AS p INNER JOIN {db_prefix}topics AS t ON (t.id_poll = p.id_poll' . ($modSettings['postmod_active'] ? ' AND t.approved = {int:is_approved}' : '') . ') INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board) WHERE {query_wanna_see_board} AND p.voting_locked = {int:not_locked}' . (!in_array(0, $boardsAllowed) ? ' AND b.id_board IN ({array_int:boards_allowed_list})' : '') . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? ' AND b.id_board != {int:recycle_enable}' : '') . ' ORDER BY {raw:type} LIMIT 1', array('boards_allowed_list' => $boardsAllowed, 'not_locked' => 0, 'is_approved' => 1, 'recycle_enable' => $modSettings['recycle_board'], 'type' => $type == 1 ? 'p.id_poll DESC' : 'RAND()')); list($topic) = $smcFunc['db_fetch_row']($request); $smcFunc['db_free_result']($request); } if (empty($topic) || $topic < 0) { loadLanguage('Errors'); echo ' ', $txt['topic_doesnt_exist']; return; } $poll = ssi_showPoll($topic, 'array'); if ($poll['allow_vote']) { echo ' <form action="', $boardurl, '/SSI.php?ssi_function=pollVote" method="post" accept-charset="', $context['character_set'], '"> <ul class="sp_list"> <li><strong>', $poll['question'], '</strong></li> <li>', $poll['allowed_warning'], '</li>'; foreach ($poll['options'] as $option) { echo ' <li><label for="', $option['id'], '">', $option['vote_button'], ' ', $option['option'], '</label></li>'; } echo ' <li class="sp_center"><input type="submit" value="', $txt['poll_vote'], '" class="button_submit" /></li> <li class="sp_center"><a href="', $scripturl, '?topic=', $poll['topic'], '.0">', $txt['sp-pollViewTopic'], '</a></li> </ul> <input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '" /> <input type="hidden" name="poll" value="', $poll['id'], '" /> </form>'; } elseif ($poll['allow_view_results']) { echo ' <ul class="sp_list"> <li><strong>', $poll['question'], '</strong></li>'; foreach ($poll['options'] as $option) { echo ' <li>', sp_embed_image('dot'), ' ', $option['option'], '</li> <li class="sp_list_indent"><strong>', $option['votes'], '</strong> (', $option['percent'], '%)</li>'; } echo ' <li><strong>', $txt['poll_total_voters'], ': ', $poll['total_votes'], '</strong></li> <li class="sp_center"><a href="', $scripturl, '?topic=', $poll['topic'], '.0">', $txt['sp-pollViewTopic'], '</a></li> </ul>'; } else { echo ' ', $txt['poll_cannot_see']; } }
flush(); ?> <hr /> <h3>Top Poster Function: <?php ssi_topPoster(); ?></h3> <?php ssi_topPoster(); flush(); ?> <hr /> <h3>Topic's Poll Function: <?php ssi_showPoll($topic); ?></h3> <?php ssi_showPoll(); flush(); ?> <hr /> <h3>Latest Member Function: <?php ssi_latestMember(); ?></h3> <?php ssi_latestMember(); flush(); ?> <hr /> <h3>Board Stats: <?php ssi_boardStats(); ?></h3> <?php