Example #1
0
function show_poll_results($poll_id)
{
    // Validate parameters
    if (!is_valid_poll_id($poll_id)) {
        vote_die('ERROR: An invalid poll ID was submitted.');
    }
    // Get poll object
    global $VALID_POLLS;
    $poll = $VALID_POLLS[$poll_id];
    // Get vote summary
    $summarylist = vote_summary_list($poll_id);
    if ($summarylist === FALSE) {
        // If the summary list is missing, attempt to regenerate it
        if (regenerate_vote_summary($poll_id)) {
            $summarylist = vote_summary_list($poll_id);
        }
    }
    if ($summarylist !== FALSE) {
        $totalVotes = $summarylist[0];
        $largest_count = find_largest_vote_value_count($summarylist);
    } else {
        $totalVotes = 0;
        $largest_count = 0;
    }
    // Show question
    echo "\t<p class=\"question\">\r\n";
    echo "\t\t" . htmlspecialchars($poll->question) . "\r\n";
    echo "\t</p>\r\n";
    // Start table for results
    echo "<table class=\"pollTable\">\r\n";
    // Iterate through poll values
    $barNumber = 1;
    global $MAX_POLL_BAR_WIDTH;
    global $SHOW_COUNTS;
    foreach ($poll->values as $value_id => $description) {
        // Find vote count for this value
        $summary_row = find_vote_value_summary($value_id, $summarylist);
        if ($summary_row === FALSE) {
            $count = 0;
        } else {
            $count = $summary_row[1];
        }
        $percentage = $totalVotes > 0 ? $count / $totalVotes : 0;
        $bar_percentage = $largest_count > 0 ? $count / $largest_count : 0;
        echo "\t<tr>";
        echo "<td class=\"pollDescriptionCell\">";
        if (isset($poll->urls[$value_id]) && !empty($poll->urls[$value_id])) {
            echo "<a href=\"" . htmlspecialchars($poll->urls[$value_id]) . "\" target=\"_blank\" rel=\"nofollow\">" . htmlspecialchars($description) . "</a>";
        } else {
            echo htmlspecialchars($description);
        }
        echo "</td>";
        echo "<td class=\"pollBarCell\">";
        if ($count > 0) {
            echo "<div class=\"pollBar\" id=\"pollBar" . $barNumber++ . "\" style=\"width:" . round($MAX_POLL_BAR_WIDTH * $bar_percentage, 0) . "px;\"></div>";
        }
        echo "</td>";
        echo "<td class=\"pollCountCell\">" . ($SHOW_COUNTS === TRUE ? $count . " (" : "") . ($count > 0 ? round($percentage * 100, 2) : 0) . "%" . ($SHOW_COUNTS === TRUE ? ")" : "") . "</td>";
        echo "</tr>\r\n";
    }
    echo "</table>";
    if ($SHOW_COUNTS === TRUE) {
        global $NUMBER_OF_VOTES_STRING;
        $numberOfVotesString = sprintf($NUMBER_OF_VOTES_STRING, $totalVotes);
        echo "<p>" . htmlspecialchars($numberOfVotesString) . "</p>";
    }
}
Example #2
0
function show_poll_results($poll_id)
{
    // Validate parameters
    if (!is_valid_poll_id($poll_id)) {
        die('ERROR: An invalid poll ID was submitted.');
    }
    // Get poll object
    global $VALID_POLLS;
    $poll = $VALID_POLLS[$poll_id];
    // Get vote summary
    $summarylist = vote_summary_list($poll_id);
    $largest_count = find_largest_vote_value_count($summarylist);
    $totalVotes = $summarylist[0];
    // Start table for results
    echo "<table class=\"pollTable\">\r\n";
    // Iterate through poll values
    $barNumber = 1;
    global $MAX_POLL_BAR_WIDTH;
    foreach ($poll->values as $value_id => $description) {
        // Find vote count for this value
        $summary_row = find_vote_value_summary($value_id, $summarylist);
        if ($summary_row === FALSE) {
            $count = 0;
        } else {
            $count = $summary_row[1];
        }
        $percentage = $totalVotes > 0 ? $count / $totalVotes : 0;
        $bar_percentage = $largest_count > 0 ? $count / $largest_count : 0;
        echo "\t<tr>";
        echo "<td class=\"pollDescriptionCell\">" . htmlspecialchars($description) . "</td>";
        if ($count > 0) {
            echo "<td class=\"pollBarCell\"><div class=\"pollBar\" id=\"pollBar" . $barNumber++ . "\" style=\"width:" . round($MAX_POLL_BAR_WIDTH * $bar_percentage, 0) . "px;\"></div></td>";
        }
        echo "<td class=\"pollCountCell\">" . $count . " (" . ($count > 0 ? round($percentage * 100, 2) : 0) . "%)</td>";
        echo "</tr>\r\n";
    }
    echo "</table>";
    global $NUMBER_OF_VOTES_STRING;
    $numberOfVotesString = sprintf($NUMBER_OF_VOTES_STRING, $totalVotes);
    echo "<p>" . htmlspecialchars($numberOfVotesString) . "</p>";
}