<?php
/*
Plugin Name: Democracy
Plugin URI: http://blog.jalenack.com/archives/democracy/
Description: Ajax polling plugin
Version: 1.2
Author: Andrew Sutherland
Author URI: http://blog.jalenack.com/
*/

// With hat tips from Denis de Bernhardy
// http://semiologic.com

// Released under the CC GPL 2.0:
// http://creativecommons.org/licenses/GPL/2.0/

// The current version of this plugin
$jal_dem_version "1.2";

// Required user level to access admin panel
$jal_dem_admin_level 8;

// When viewing results, order the answers by number of votes or by original order
// To order by votes, set this to TRUE
$jal_order_answers FALSE;

// HTML to go around the poll's question
// Depending on your theme, you may want '<h2>';
$jal_before_question '<strong id="poll-question">';

// Closing tags for the HTML around the question
$jal_after_question '</strong>';

// Graph percentages as a percent of the total votes or of the winner
// If it is false, then the winning vote will be 100% of the graph, and the other answers will be a percent of that,
$jal_graph_from_total TRUE;

// The length of time, in *seconds*, that cookies should remain. Default: 3 months
// Note that IP logging will still be in effect when you lower this number
// To take away IP stopping functionality, comment out the checkIP() calls at the bottom
$jal_cookietime 60*60*24*30*3;
// Change it in the js file as well



/*/\\//\\//\\//\\//\\//\\//\\//

    No edits needed below here

//\\//\\//\\//\\//\\//\\//\\/*/

function jal_edit_poll () {
    global 
$wpdb$table_prefix$user_level$jal_dem_admin_level;

    
// Security
    
get_currentuserinfo();
    if (
$user_level <  $jal_dem_admin_level) { die('不正な行為です!'); }

    
$poll_id = (int) $_POST['poll_id'];

    
// read which aids are in this poll
    
$edits explode(" "$_POST['editable']);
     
$question $wpdb->escape($_POST['question']);

    
// Allow users to edit poll?
    
$allowusers = (isset($_POST['allowNewAnswers'])) ? "1" "0";

    
// update the question
    
$wpdb->query("UPDATE {$table_prefix}democracyQ SET question='{$question}', allowusers = '{$allowusers}' WHERE id = {$poll_id}");

    foreach(
$_POST['answer'] as $aid => $answer) {
        
$aid = (int) $aid;

        if (empty(
$answer) && in_array($aid$edits))
           
$wpdb->query("DELETE FROM {$table_prefix}democracyA WHERE qid = {$poll_id} AND aid = {$aid}");

        if (!empty(
$answer) && in_array($aid$edits))
            
$wpdb->query("UPDATE {$table_prefix}democracyA SET answers='".$wpdb->escape($answer)."' WHERE qid = {$poll_id} AND aid = {$aid}");

        if (!empty(
$answer) && !in_array($aid$edits))
            
$wpdb->query("INSERT INTO {$table_prefix}democracyA (qid, answers, votes) VALUES ({$poll_id}, '{$answer}', 0)");

    }
}

function 
jal_activate_poll () {
    global 
$wpdb$table_prefix$user_level$jal_dem_admin_level;

    
// Security
    
get_currentuserinfo();
    if (
$user_level <  $jal_dem_admin_level) { die('不正な行為です!'); }

    
$id = (int) $_GET['poll_id'];

    
// Deactivate the old active poll
    
$wpdb->query("UPDATE {$table_prefix}democracyQ SET active='0' WHERE active = '1'");
    
// Activate the new poll
    
$wpdb->query("UPDATE {$table_prefix}democracyQ SET active='1' WHERE id = {$id}");
}


function 
jal_deactivate_poll () {
    global 
$wpdb$table_prefix$user_level$jal_dem_admin_level;

    
// Security
    
get_currentuserinfo();
    if (
$user_level <  $jal_dem_admin_level) { die('不正な行為です!'); }

    
// Deactivate the old active poll
    
$wpdb->query("UPDATE {$table_prefix}democracyQ SET active='0' WHERE active = '1'");
}


function 
jal_delete_poll () {
    global 
$wpdb$table_prefix$user_level$jal_dem_admin_level;

    
// Security
    
get_currentuserinfo();
    if (
$user_level <  $jal_dem_admin_level) die('不正な行為です!');

     
$id = (int) $_GET['poll_id'];

    
// Delete the poll question and its answers
    
$wpdb->query("DELETE FROM {$table_prefix}democracyQ WHERE id = {$id}");
    
$wpdb->query("DELETE FROM {$table_prefix}democracyA WHERE qid = {$id}");
}


function 
jal_add_question () {
    global 
$wpdb$table_prefix$user_level$jal_dem_admin_level;

    
// Security
    
get_currentuserinfo();
    if (
$user_level <  $jal_dem_admin_level) die('不正な行為です!');

    
// deactive old poll
    
$wpdb->query("UPDATE {$table_prefix}democracyQ SET active='0' WHERE active='1'");

    
// Let users add their own answers?
    
$allow_users_to_add = (isset($_POST['allowNewAnswers'])) ? "1" "0";

    
// Add a new question and activate it    
    
$wpdb->query("INSERT INTO {$table_prefix}democracyQ (question, timestamp, allowusers, active) VALUES ('".$wpdb->escape($_POST['jal_dem_question'])."', '".time()."', '".$allow_users_to_add."', '1')");

    
// Get the id of that new question
    
$new_q $wpdb->get_var("SELECT id FROM {$table_prefix}democracyQ WHERE active = '1'");

    
// Add the questions
    
foreach($_POST['answer'] as $answer)
        if (!empty(
$answer))
            
$sql[] = "INSERT INTO {$table_prefix}democracyA (qid, answers, votes) VALUES ({$new_q}, '".$wpdb->escape($answer)."', 0);";

    foreach(
$sql as $query)
        
$wpdb->query($query);

}


function 
jal_dem_admin_head() { ?>
    <script type="text/javascript" src="<?php echo get_bloginfo('wpurl') . "/wp-content/plugins/democracy/admin.js"?>"></script>
<?php }

function 
jal_dem_admin_page() {
    global 
$wpdb$table_prefix;
    
$poll_id = (int) $_GET['poll_id'];

 if (isset(
$_GET['jal_dem_delete'])) { ?>
<div class="updated">
    <p>統計 #<?php echo $poll_id?> の削除を完了しました。</p>
</div>
<?php ?>

<?php if (isset($_POST['jal_dem_edit'])) { ?>
<div class="updated">
    <p>統計 #<?php echo $_POST['poll_id']; ?> 編集を保存しました。</p>
</div>
<?php ?>

<?php if (!empty($_GET['edit'])) { ?>

<div class="wrap">

    <h2>統計 #<?php echo $poll_id?> を編集</h2>

    <?php


    $latest 
$wpdb->get_var("SELECT id FROM {$table_prefix}democracyQ ORDER BY id DESC LIMIT 1");
    
$question $wpdb->get_row("SELECT question, allowusers FROM {$table_prefix}democracyQ WHERE id = {$poll_id}"ARRAY_A);
    
$results $wpdb->get_results("SELECT * FROM {$table_prefix}democracyA WHERE qid = {$poll_id} ORDER BY aid");

    
?>
    <p>回答を減らすなら、入力欄は空白のままにしてください。他から回答を移動することは、その統計を無効にします。<br />
    </p>
        <form action="edit.php?page=democracy" method="post" onsubmit="return jal_validate();">
    <strong>質問: <input id="question" type="text" name="question" value="<?php echo $question['question']; ?>" /></strong>

    <ol id="inputList"><?php

    $count 
1;
    
$loop "";
    foreach (
$results as $r) {

        
// Add to the list of answers in the hidden input element
        
$loop $loop ." "$r->aid;

        
$r->answers htmlspecialchars(stripslashes($r->answers));

     
?><li><input type="text" value="<?php echo $r->answers?>" name="answer[<?php echo $r->aid?>]" /></li><?php $count++; } 

        
?></ol>
    <p>
        <label for="allowNewAnswers"><input type="checkbox" <?php if ($question['allowusers'] == 1) echo 'checked="checked"'?> value="true" name="allowNewAnswers" id="allowNewAnswers" /> 閲覧者が回答を作ることを許可する</label><br /><br />
        <a href="javascript: addQuestion();" id="adder">入力欄を増やす</a>&nbsp;&nbsp;
        <a href="javascript: eatQuestion();" id="subtractor">入力欄を減らす</a>&nbsp;

        <input type="hidden" id="qnum" name="qnum" value="<?php echo $count?>" />
        <input type="hidden" name="jal_dem_edit" value="true" />
        <input type="hidden" name="editable" value="<?php echo trim($loop); ?>" />
        <input type="hidden" name="poll_id" value="<?php echo $poll_id?>" />
        <input type="submit" value="編集" />
    </p>
    </form>

   <?php } else { ?>

    <?php 
    
// get the current poll
    
$current $wpdb->get_var("SELECT id FROM {$table_prefix}democracyQ WHERE active = '1'");

    if (!
$current) { ?>
        <div class="updated">
            <p style="font-weight: bold">現在、有効な統計はありません。統計を有効にしなければ、何も表示しません。</p>
        </div>
    <?php ?>

<div class="wrap">

    <h2>統計 管理画面</h2>

    <table width="100%" border="0" cellspacing="3" cellpadding="3">
        <tr>
         <th scope="col">ID</th>
            <th scope="col">質問</th>
            <th scope="col">投票総数</th>
            <th scope="col">統計結果</th>
            <th scope="col">統計開始日</th>
            <th scope="col">現在の状態</th>
        </tr>
<?php 
   $winners 
= array();
    
$totalvotes = array();
    
$x $wpdb->get_results("SELECT * from {$table_prefix}democracyQ ORDER BY id");

    
$totalvotes $wpdb->get_results("
     SELECT SUM(votes) as total_votes, qid
     FROM {$table_prefix}democracyA
     GROUP BY qid
     ORDER BY qid"
     
ARRAY_A);
    
     
$winner_answers $wpdb->get_results("
      SELECT votes, answers, qid
      FROM {$table_prefix}democracyA
      GROUP BY qid, votes
      ORDER BY qid"
      
ARRAY_A);

    
// index by qid
    
foreach ($totalvotes as $winner)
        
$total_vote[$winner['qid']] = $winner['total_votes'];

    
// no need for the old query info
    
unset ($y);
    
    
// index by qid
    
foreach ($winner_answers as $winning_answer)
        
$winners[$winning_answer['qid']] = $winning_answer['answers'];

    
// no need for the old query info
    
unset ($y);

    
$alt true;
    if (
$x) {
    foreach (
$x as $r) {


     
$alt = ($alt) ? FALSE TRUE;
?>
        <tr<?php if ($current == $r->id) { echo ' class="active"'; } if ($alt == true && $current !== $r->id) { echo ' class="alternate"';  } ?>>
           <td style="text-align: center"><?php echo $r->id?></td>
           <td style="text-align: center"><?php echo stripslashes($r->question); ?></td>
           <td style="text-align: center"><?php if ($current == $r->id) { echo "So far, "; } echo $total_vote[$r->id]; ?></td>
           <td style="text-align: center"><?php echo stripslashes($winners[$r->id]); ?>
           <td style="text-align: center"><?php echo date(get_settings('date_format'), $r->timestamp); ?></td>
           <td style="text-align: center">
               <form action="" method="get">
                   <div>
                       <input type="hidden" name="page" value="democracy" />
                       <input type="hidden" name="poll_id" value="<?php echo $r->id?>" />
                       <?php if ($current !== $r->id) :  ?>
                           <input type="submit" value="有効化" name="jal_dem_activate" />
                       <?php else : ?>
                           <input type="submit" value="無効化" name="jal_dem_deactivate" />
                       <?php endif; ?>
                       <input type="submit" value="編集" name="edit">
                       <input type="submit" value="削除" onclick="return confirm('統計を削除しようとしています。本当に削除しますか?');" name="jal_dem_delete" class="delete" />
                   </div>
               </form>
           </td>
        </tr>
    <?php } } else { echo "<br />データベースには統計がありません。新しく作成してください。<br /><br />"; } ?>

    </table>
    <h2>新しい統計を作成する</h2>
    <p>この統計システムは完全な互換性を持つHTMLです。入力欄に書かれたとおりに表示します。回答が2つ以上思いつかなくても、空白の入力欄は出力されません。</p>
    <form action="edit.php?page=democracy" method="post" onsubmit="return jal_validate();">
    <div id="form_questions">

    <a href="javascript: addQuestion();" id="adder">入力欄を増やす</a><br />
    <a href="javascript: eatQuestion();" id="subtractor">入力欄を減らす</a><br />
    <br />
    <label for="question">
        <strong>質問:</strong>
    </label>

        <input type="text" name="jal_dem_question" value="" id="question" />
        <ol id="inputList"><?php
             
for($i 1$i 5$i++) { 
                echo 
'<li><input type="text" value="" name="answer[]" /></li>';
            } 
?></ol>

    <label for="allowNewAnswers"><input type="checkbox" value="true" name="allowNewAnswers" id="allowNewAnswers" /> 閲覧者が回答を作ることを許可する</label><br /><br />
    <input type="submit" value="新しい統計を作成" />
    </div>
    </form>
    <?php // end of Edit conditional ?>

</div>

<?php }


// Adds the Democracy Poll Plugin tab to the admin navigation
function jal_add_page() {
    global 
$jal_dem_admin_level;
    
add_management_page('Democracy''Democracy'$jal_dem_admin_level'democracy''jal_dem_admin_page');
}


// Add the javascript to the head of the page
function jal_add_js () {
    global 
$jal_dem_version;

    
//$jal_wp_url = (dirname($_SERVER['PHP_SELF']) == "/") ? "/" : dirname($_SERVER['PHP_SELF']) . "/";
    
$jal_wp_url get_bloginfo('wpurl') . "/";

    echo 
'
    <!-- Added By Democracy Plugin. Version '
.$jal_dem_version.' -->
    <script type="text/javascript" src="'
.$jal_wp_url.'wp-content/plugins/democracy/js.php"></script>
    <link rel="stylesheet" href="'
.$jal_wp_url.'wp-content/plugins/democracy/democracy.css" type="text/css" />
     '
;
}

// Run a check of visitors IP to make sure they haven't voted already.
// This process is usually silent, cookies take care of the design
function jal_checkIP ($poll_id 0) {
    global 
$wpdb$table_prefix;

    
$where = ($poll_id == 0) ? "active = '1'" "id = '".$poll_id."'";

    
$all_ips $wpdb->get_var("SELECT voters FROM {$table_prefix}democracyQ WHERE {$where}");

    
$results unserialize($all_ips);
    
// make sure there have been votes
    
if ($results)
        
//check the ip address, and quit if there's a match
        
if (in_array($_SERVER['REMOTE_ADDR'], $results))
            die(
"投票は1回だけしかできません");

        
// add the new IP address to the array
        
$results[] = $_SERVER['REMOTE_ADDR'];
        
$final $wpdb->escape(serialize($results));

        
$wpdb->query("UPDATE {$table_prefix}democracyQ SET voters = '{$final}' WHERE {$where}");
}


// Print the poll html
function jal_democracy($poll_id 0) {
 global 
$wpdb$table_prefix$jal_before_question$jal_after_question;

    
$where = ($poll_id == 0) ? "active = '1'" "id = '".$poll_id."'";
     
$poll_question $wpdb->get_row("SELECT id, question, voters, allowusers FROM {$table_prefix}democracyQ WHERE {$where}");

// Check if they've voted
 
if ($_GET['jal_no_js'] || isset($_COOKIE['demVoted_'.$poll_question->id]))
    
jal_SeeResults($poll_question->id);
 else {
        
$wpdb->hide_errors();
          
$poll_answers $wpdb->get_results("SELECT aid, answers, added_by FROM {$table_prefix}democracyA WHERE qid = {$poll_question->id} ORDER BY aid");
        
$wpdb->show_errors();

        if (empty(
$poll_question) || empty($poll_answers))
                echo 
"<!-- There are no active polls in the database, or there was an error in finding one -->"
          else {

        
// Check if there are already arguments in the URL
        
$x = (strstr($_SERVER['REQUEST_URI'], '?')) ? "&amp;" "?";

         
$latestaid $wpdb->get_var("SELECT aid FROM {$table_prefix}democracyA ORDER BY aid DESC LIMIT 1");

         
$total_votes $wpdb->get_var("SELECT SUM(votes) FROM {$table_prefix}democracyA WHERE qid = ".$poll_question->id); ?>
         
     <form action="<?php echo get_bloginfo('wpurl') . '/wp-content/plugins/democracy/democracy.php?jal_nojs=true'?>" method="post" id="democracyForm" onsubmit="return ReadVote();">
        <div id="democracy">
        <p><?php echo $jal_before_question stripslashes($poll_question->question) . $jal_after_question?>

        <ul>
          <?php foreach ( $poll_answers as $r) { ?>
            <li>
                <label for="choice_<?php echo $r->aid?>"><input type="radio" id="choice_<?php echo $r->aid?>" value="<?php echo $r->aid?>" name="poll_aid" /> <?php echo stripslashes($r->answers); ?><?php if ($r->added_by == "1") { echo '<sup title="Added by users">1</sup>'$user_added TRUE; }?></label>
            </li>
          <?php }
          if ( 
$poll_question->allowusers == 1) {
           
?>


            <?php /* No-JS users */ if (isset($_GET['jal_add_user_answer'])) { ?>

            <li>
            <input type="radio" name="poll_aid" id="jalAddAnswerRadio" value="newAnswer" checked="checked" />
            <input type="text" size="15" id="jalAddAnswerInput" name="poll_vote" value="" />

            </li>

            <?php } else { ?>
            <li>
                <a href="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']).$x?>jal_add_user_answer=true" id="jalAddAnswer">回答を作る</a>
                <input type="radio" name="poll_aid" id="jalAddAnswerRadio" value="<?php echo $latestaid 1?>" style="display: none" /> <input type="text" size="15" style="display: none" id="jalAddAnswerInput" />
            </li>

            <?php } } ?>
        </ul>
        <p><input type="hidden" id="poll_id" name="poll_id" value="<?php echo $poll_question->id?>" /><input type="submit" name="Vote" value="投票する" /></p>
        <p><?php if ($total_votes 0) { 
            
// For non-js users...JS users get this link changed onload
        
?>
            <a id="view-results" href="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']).$x?>jal_no_js=true&amp;poll_id=<?php echo $poll_question->id?>">結果発表</a>
            <?php if ($user_added) echo "<br /><small><sup>1</sup> = Added by a guest</small>"?>
        <?php } else { echo "まだ投票されてません"; } ?></p>
       </div>
    </form>

<?php } } 
}

// Installs the tables needed for polls
// Check the codex article for more info
// http://codex.wordpress.org/creating_tables_with_plugins
function jal_dem_install () {
   global 
$table_prefix$wpdb$user_level,  $jal_dem_admin_level;

   
$table_name $table_prefix "democracyQ";

   
get_currentuserinfo();
   if (
$user_level <  $jal_dem_admin_level) return;

   
$result mysql_list_tables(DB_NAME);
   
$tables = array();

   while (
$row mysql_fetch_row($result)) { $tables[] = $row[0]; }

    
$first_install = (in_array($table_name$tables)) ? FALSE TRUE;

   
// PRIMARY KEY has 2 spaces on purpose ... some weird dbDelta thing...

   
$qry "CREATE TABLE {$table_prefix}democracyA (
            aid int(10) unsigned NOT NULL auto_increment,
            qid int(10) NOT NULL default '0',
            answers varchar(200) NOT NULL default '',
            votes int(10) NOT NULL default '0',
            added_by enum('1','0') NOT NULL default '0',
            PRIMARY KEY  (aid)
           );

           CREATE TABLE {$table_prefix}democracyQ (
            id int(10) unsigned NOT NULL auto_increment,
            question varchar(200) NOT NULL default '',
            timestamp int(10) NOT NULL default '',
            voters text NULL default '',
            allowusers enum('0','1') NOT NULL default '0',
            active enum('0','1') NOT NULL default '0',
            PRIMARY KEY  (id)
           ); "
;

    require_once(
ABSPATH 'wp-admin/upgrade-functions.php');
    
dbDelta($qry);

    if (
$first_install == true) {

        
// Add In Poll Question/Answers
        
$sql[] = "INSERT INTO {$table_prefix}democracyQ VALUES (1, 'Rate my site', '".time()."', '', '0', '0');";
        
$sql[] = "INSERT INTO {$table_prefix}democracyA VALUES (1, 1, 'Best. Blog. Ever.', 0, 0);";
        
$sql[] = "INSERT INTO {$table_prefix}democracyA VALUES (2, 1, 'Could be better...', 0, 0);";
        
$sql[] = "INSERT INTO {$table_prefix}democracyA VALUES (3, 1, 'My grandma could make a better website!', 0, 0);";
        
$sql[] = "INSERT INTO {$table_prefix}democracyA VALUES (4, 1, 'Ooo look, a butterfly!', 0, 0);";
        
$sql[] = "INSERT INTO {$table_prefix}democracyA VALUES (5, 1, 'No Comment', 0, 0);";

        foreach(
$sql as $query) {
            
$wpdb->query($query);
        }

    }
}


// Prints the standings of a poll
function jal_SeeResults($poll_id 0$javascript FALSE) {
    global 
$wpdb$table_prefix$jal_order_answers$jal_before_question$jal_after_question$jal_graph_from_total;

   
$order_by = ($jal_order_answers) ? " ORDER BY votes DESC" " ORDER by aid ASC";

   
$where = ($poll_id == 0) ? "active = '1'" "id = '".$poll_id."'";
   
$poll_question $wpdb->get_row("SELECT id, question, voters FROM {$table_prefix}democracyQ WHERE ".$where);
   
$poll_answers $wpdb->get_results("SELECT aid, answers, votes, added_by FROM {$table_prefix}democracyA WHERE qid = ".$poll_question->id $order_by);

    if (!
$javascript)
        echo 
"<div id='democracy'>\n\n\t";
    else
        
header('Content-Type: text/html; charset='.get_option('blog_charset'));
?>

                                <?php echo $jal_before_question stripslashes($poll_question->question) . $jal_after_question?>

                                <ul>
    <?php 

    $output 
"";
    
$cookie $_COOKIE['demVoted_'.$poll_question->id];
    
$values = array();

     
// Search for the winner of the poll
    
foreach ($poll_answers as $row)
        
$values[] = $row->votes;

    
$winner      max($values);
    
$total_votes array_sum($values);

    
// Loop for the number of answers
    
foreach ($poll_answers as $r) {

        
// Percent of total votes
        
$percent round($r->votes / ($total_votes 0.0001) * 100);

        
// Percent to display in the graph, as set at the top of the file
        
$graph_percent = ($jal_graph_from_total) ? $percent round($r->votes / ($winner 0.0001) * 100);

        
// See which choice they voted for
        
$voted_for_this = ($cookie == $r->aid) ? TRUE FALSE;
        
$user_added = ($r->added_by == "1") ? '<sup title="Added by users">1</sup>' '';
        if (
$r->added_by == "1") {
                
$user_added '<sup title="Added by a guest">1</sup>';
                
$add_sup    TRUE;
        } else 
$user_added "";


        
// In the graphs, define which class/id to use.
                
$graph_hooks = ($voted_for_this) ? 'id="voted-for-this" class="dem-choice-border"' 'class="dem-choice-border"';

                
$output .="\n\t\t\t\t\t";

                
$output .= "<li>";

                
$output .= stripslashes($r->answers) . $user_added ": ";
                
$output .= "<strong>".$percent."%</strong>";
                
$output .= " (".$r->votes.")";

                
// Graph it
                
$output .= '<span '.$graph_hooks.'><span class="democracy-choice" style="width: '.$graph_percent.'%"></span></span>';

                
$output .= "</li>";

                echo 
$output;

                
// reset $output for the next loop
                
$output "";

        } 
?>

                                </ul>
                                <p>
                                        <em id="dem-total-votes">投票総数 : <?php echo $total_votes?></em>
         <?php /* if they are just looking at the results and haven't voted */
          
if (!$_COOKIE['demVoted_'.$poll_question->id]) { ?>
                                        <br />
                                        <a href="<?php echo htmlspecialchars($_SERVER['HTTP_REFERER']); ?>">投票する</a>
     <?php ?>
     <?php if ($add_sup) echo "<br /><small><sup>1</sup> = Added by a guest</small>"?>

                        </p>

                        <?php if (!$javascript) echo "</div>\n\n";
 }

/*
===========================
Archiving Functions
===========================
*/


function jal_democracy_archives ($show_active FALSE$before_title '<h3>',$after_title '</h3>') {
        global 
$wpdb$table_prefix$jal_graph_from_total;

        
$where = ($show_active) ? "" "WHERE active = '0'";

        
$poll_questions $wpdb->get_results("SELECT * FROM {$table_prefix}democracyQ {$where} ORDER BY id DESC"ARRAY_A);

        
$poll_answers   $wpdb->get_results("SELECT * FROM {$table_prefix}democracyA ORDER BY votes DESC"ARRAY_A);

        
$poll_q = array();
        
$poll_votes = array();

        
// index by poll question id. Much faster than querying for each question
        
foreach  ($poll_answers as $answer) {
             
// index answer arrays
             
$poll_q[$answer['qid']][] = $answer;
             
// index total votes
             
$poll_votes[$answer['qid']][] = $answer['votes'];
        }

        
// loop for all the poll questions
        
foreach ($poll_questions as $question) {

                
$total_votes array_sum($poll_votes[$question['id']]);
                
$winner max($poll_votes[$question['id']]);

                echo 
$before_title.stripslashes($question['question']).$after_title;
                echo 
"<br /><strong>統計開始日:</strong> ".date(get_settings('date_format'), $question['timestamp']);
                echo 
"<br /><strong>投票総数:</strong> {$total_votes}";

                echo 
"<ul>";

                foreach  (
$poll_q[$question['id']] as $answer) {
                        
// Percent of total votes
                        
$percent round($answer['votes'] / ($total_votes 0.0001) * 100);
                        
// Percent to display in the graph, as set at the top of the file
                        
$graph_percent = ($jal_graph_from_total) ? $percent round($answer['votes'] / ($winner 0.0001) * 100);

                        
// See which choice they voted for
                        
$voted_for_this = ($cookie == $answer['aid']) ? TRUE FALSE;

                        if (
$answer['added_by'] == "1") {
                                
$user_added '<sup title="Added by a guest">1</sup>';
                                
$add_sup    TRUE;
                        } else 
$user_added "";

                        
$output "<li>";

                        
$output .= stripslashes($answer['answers']). $user_added ": ";
                        
$output .= "<strong>".$percent."%</strong>";
                        
$output .= " ({$answer['votes']})";

                        
// Graph it
                        
$output .= '<span class="dem-choice-border"><span class="democracy-choice" style="width: '.$graph_percent.'%"></span></span>';

                        
$output .= "</li>";
                        echo 
$output;
                }
                echo 
"</ul>";
        }

        if (
$add_sup) echo "<br /><small><sup>1</sup> = Added by a guest</small>";
}



/*
===========================
Now, the functions are done
Let's run them....
===========================
*/


// for users with js turned off
if (isset($_GET['jal_nojs'])) {
    include (
"../../../wp-config.php");