* @author Slawkens * @copyright 2017 MyAAC * @version 0.4.1 * @link http://my-aac.org */ defined('MYAAC') or die('Direct access not allowed!'); $title = 'Polls'; /* Polls System By Averatec from pervera.pl & otland.net */ if(!tableExist('z_polls')) $db->query(' CREATE TABLE `z_polls` ( `id` int(11) NOT NULL auto_increment, `question` varchar(255) NOT NULL, `description` varchar(255) NOT NULL, `end` int(11) NOT NULL, `start` int(11) NOT NULL, `answers` int(11) NOT NULL, `votes_all` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;'); if(!tableExist('z_polls_answers')) $db->query(' CREATE TABLE `z_polls_answers` ( `poll_id` int(11) NOT NULL, `answer_id` int(11) NOT NULL, `answer` varchar(255) NOT NULL, `votes` int(11) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1;'); if(!fieldExist('vote', 'accounts')) $db->query('ALTER TABLE `accounts` ADD `vote` INT( 11 ) NOT NULL ;'); function getColorByPercent($percent) { if($percent < 15) return 'red'; else if($percent < 35) return 'orange'; else if($percent < 50) return 'yellow'; return ''; } $number_of_rows = 0; $showed = false; $link = "polls"; // your link to polls in index.php $dark = $config['darkborder']; $light = $config['lightborder']; $time = time(); $POLLS = $db->query('SELECT * FROM '.$db->tableName('z_polls').''); $level = 20; // need level to vote if(empty($_REQUEST['id']) and (!isset($_REQUEST['control']) || $_REQUEST['control'] != "true")) // list of polls { $active = $db->query('SELECT * FROM `z_polls` where `end` > '.$time.''); // active polls $closed = $db->query('SELECT * FROM `z_polls` where `end` < '.$time.' order by `end` desc'); // closed polls /* Active Polls */ echo ''; echo ''; $bgcolor = getStyle($number_of_rows++); $empty_active = false; foreach($active as $poll) { echo ' '; $empty_active = true; } if(!$empty_active) { echo ''; } echo "
Active Polls
TopicEnd
'.$poll['question'] . ' '.date("M j Y", $poll['end']).'
There are no active polls.


"; /* Closed Polls */ echo ''; echo ''; $bgcolor = getStyle($number_of_rows++); $empty_closed = false; foreach($closed as $poll) { echo ' '; $empty_closed = true; } if(!$empty_closed) { echo ''; } echo "
Closed Polls
TopicEnd
'.$poll['question'] . ' '.date("M j Y", $poll['end']).'
There are no closed polls.
"; $showed=true; } if(!$logged) { echo 'You are not logged in. Log in to vote in polls.

'; return; } /* Checking Account */ $allow = false; $account_players = $account_logged->getPlayers(); foreach($account_players as $player) { $player = $player->getLevel(); if($player >= $level) $allow=true; } if(!empty($_REQUEST['id']) and (!isset($_REQUEST['control']) || $_REQUEST['control'] != "true")) { foreach($POLLS as $POLL) { if($_REQUEST['id'] == $POLL['id']) { $ANSWERS = $db->query('SELECT * FROM '.$db->tableName('z_polls_answers').' where `poll_id` = '.addslashes(htmlspecialchars(trim($_REQUEST['id']))).' order by `answer_id`'); $votes_all = $POLL['votes_all']; if($votes_all == 0) { $i=1; foreach($ANSWERS as $answer) { $percent[$i] = 0; $i++; } } else { $i=1; foreach($ANSWERS as $answer) { $percent[$i] = round(((100*$answer['votes'])/$votes_all),2); $i++; } } ?>
'; } if($POLL['end'] > $time) // active poll { if(isset($_REQUEST['vote']) && $_REQUEST['vote'] == true and $allow == true) { if($account_logged->getCustomField('vote') < $_REQUEST['id'] and !empty($_POST['answer'])) { if(isset($_POST['continue'])) { $vote = addslashes(htmlspecialchars(trim($_REQUEST['id']))); $account_logged->setCustomField("vote", $vote); $UPDATE_poll = $db->query('UPDATE `z_polls` SET `votes_all` = `votes_all` + 1 where `id` = '.addslashes(htmlspecialchars(trim($_REQUEST['id']))).''); $UPDATE_answer = $db->query('UPDATE `z_polls_answers` SET `votes` = `votes` + 1 where `answer_id` = '.addslashes(htmlspecialchars($_POST['answer'])).' and`poll_id` = '.addslashes(htmlspecialchars(trim($_REQUEST['id']))).''); header('Location: ?subtopic='.$link.'&id='.$_REQUEST['id'].''); } } else { header('Location: ?subtopic='.$link.'&id='.$_REQUEST['id'].''); } } if($account_logged->getCustomField('vote') < $_REQUEST['id'] and $allow == true) { echo ''; echo ' '; $ANSWERS_input = $db->query('SELECT * FROM '.$db->tableName('z_polls_answers').' where `poll_id` = '.$_REQUEST['id'].' order by `answer_id`'); $i=1; foreach($ANSWERS_input as $answer) { if(is_int($i / 2)) { $bgcolor = $dark; } else { $bgcolor = $light; } echo ''; $i++; } echo '
Vote
'.$POLL['question'].'
' . $POLL['description'] . '
'.$answer['answer'].'


'; } elseif($account_logged->getCustomField('vote') >= $_REQUEST['id']) { $result[] = '
You have already voted.
'; echo ''; echo ''; $ANSWERS_show = $db->query('SELECT * FROM '.$db->tableName('z_polls_answers').' where `poll_id` = '.$_REQUEST['id'].' order by `answer_id`'); $i=1; foreach($ANSWERS_show as $answer) { if(is_int($i / 2)) { $bgcolor = $dark; } else { $bgcolor = $light; } $x=0; echo ''; $i++; } echo '
Results
'.$POLL['question'].'
' . $POLL['description'] . '
'.$answer['answer'].' ' . slaw_getPercentBar($percent[$i]) . ' ' . $answer['votes'] . '(' . $percent[$i] . '%)
'; } $result[] = '
All players with a character of at least level ' . $level . ' may vote.
'; foreach($result as $error) { echo $error; } echo '
The poll started at '.date("M j Y", $POLL['start']).'
'; echo 'The poll will end at '.date("M j Y", $POLL['end']).'
'; echo '
Total votes '.$POLL['votes_all'].'
'; } else // closed poll { echo ''; echo ''; $ANSWERS_show = $db->query('SELECT * FROM '.$db->tableName('z_polls_answers').' where `poll_id` = '.$_REQUEST['id'].' order by `answer_id`'); $i=1; foreach($ANSWERS_show as $answer) { if(is_int($i / 2)) { $bgcolor = $dark; } else { $bgcolor = $light; } echo ''; $i++; } echo '
Results
'.$POLL['question'].'
'.$answer['answer'].' ' . slaw_getPercentBar($percent[$i]) . ' ' . $answer['votes'] . '(' . $percent[$i] . '%)


'; echo '
The poll started at '.date("M j Y", $POLL['start']).'
'; echo 'The poll ended at '.date("M j Y", $POLL['end']).'
'; echo '
Total votes '.$POLL['votes_all'].'
'; } $showed=true; echo '
Go to list of polls'; } } } if(admin() && (!isset($_REQUEST['control']) || $_REQUEST['control'] != "true")) { echo '
Panel Control

'; } /* Control Panel - Only Add Poll Function */ if(admin() && isset($_REQUEST['control']) && $_REQUEST['control'] == "true") { $show = false; if(isset($_POST['submit'])) { $_SESSION['answers'] = $_POST['answers']; echo '
Adding Poll

Question
Description
Time to end, in days
'; for( $x = 1; $x <= $_SESSION['answers']; $x++ ) { echo ' Answer no. '.$x.'
'; } echo '


'; $show=true; } if(isset($_POST['finish'])) { $id = $db->query('SELECT MAX(id) FROM `z_polls`')->fetch(); $id_next = $id[0] + 1; for( $x = 1; $x <= $_SESSION['answers']; $x++ ) { $INSERT_answer = $db->query('INSERT INTO `z_polls_answers` (`poll_id`,`answer_id`,`answer`) VALUES ('.$db->quote($id_next).','.$db->quote($x).','.$db->quote($_POST[$x]).')'); } $end = $time+24*60*60*$_POST['end']; $INSERT_poll = $db->query('INSERT INTO `z_polls` (`id`,`question`, `description`,`end`,`answers`,`start`) VALUES ('.$db->quote($id_next).','.$db->quote($_POST['question']).','.$db->quote($_POST['description']).','.$db->quote($end).','.$db->quote($_SESSION['answers']).','.$db->quote($time).')'); } $POLLS_check = $db->query('SELECT MAX(end) FROM '.$db->tableName('z_polls').''); foreach($POLLS_check as $checked) { if($checked[0] > $time) $check=true; else $check=false; } if(!$show) { if(!$check) { echo '
Adding Poll

Number of Answers


'; } else { echo '
Cannot be two and more active polls.

'; } } $showed=true; echo '
Go to list of polls'; } if(!$showed) { echo 'This poll doesn\'t exist.
'; echo '
Go to list of polls'; } ?>