Offtopic
This is a poll script I've wrote some time ago for my web site... It contains both admin and user part. If it's helpful for anyone, feel free to use it
Code: Select all
<?php
class Poll {
public $question;
public $id;
public $answers = array();
public $voted = false;
public $votes = array();
public function __construct($id, $data = false)
{
$this->id = $id > 0 ? $id : 0;
if ( $this->id && is_object( $data ) == false )
{
$sql = 'SELECT * FROM itl_polls WHERE id = '.intval( $id );
$data = DB::Instance()->query_first( $sql );
}
if ( is_object( $data ) )
{
$this->question = $data->question;
$this->answers = $this->_loadAnswers();
$this->votes = $this->_loadVotes();
$this->voted = $this->_checkVote();
} else {
$this->id = 0;
}
}
private function _loadAnswers()
{
$sql = 'SELECT * FROM itl_answers WHERE poll_id='.$this->id;
$res = DB::Instance()->query( $sql );
$count = DB::Instance()->num_rows($res);
$answers = array();
for ($i = 0; $i < $count; $i++ )
{
$row = DB::Instance()->get_row($res);
$answers[ $row->id ] = $row->answer;
}
return $answers;
}
private function _loadVotes()
{
$sql = 'SELECT answer_id, count(*) as count FROM itl_votes WHERE poll_id = '.$this->id.' GROUP BY answer_id';
$res = DB::Instance()->query( $sql );
$count = DB::Instance()->num_rows($res);
$votes = array();
for ($i = 0; $i < $count; $i++ )
{
$row = DB::Instance()->get_row($res);
$votes[ $row->answer_id ] = $row->count;
}
return $votes;
}
private function _checkVote()
{
// check if user have voted for this poll
// 1st: check via cookie
if (is_array($_COOKIE['polls']) == false )
{
$_COOKIE['polls'] = array($_COOKIE['polls']);
}
if ( in_array( $this->id, $_COOKIE['polls'] ) )
{
$this->voted = true;
return true;
}
// 2nd: check via member_id
if ( isset($_SESSION['user']) )
{
$id = $_SESSION['user']->getUserID();
$sql = 'SELECT * FROM itl_votes WHERE user_id = '.$id.' AND answer_id IN ('.implode( ',', array_keys( $this->answers ) ).')';
$res = DB::Instance()->query( $sql );
if (DB::Instance()->num_rows($res) > 0)
{
$this->voted = true;
return true;
}
}
return false;
}
public function Vote($answer_id, $member_id, $ip)
{
if ( $this->voted )
{
return false;
}
if ( in_array($answer_id, array_keys($this->answers) ) == false )
{
return false;
}
$date = date("Y-m-d");
$sql = 'INSERT INTO itl_votes (answer_id, user_id, ip, date, poll_id) VALUES';
$sql .= sprintf( ' (%d, %d, "%s", "%s", %d) ', $answer_id, $member_id, $ip, $date, $this->id );
$res = DB::Instance()->query( $sql );
setcookie("polls[]", $this->id, time() + (60*60*24*365) );
$this->voted = true;
return true;
}
public function Update($data)
{
// TODO: Transaction
$DB = DB::Instance();
if ($this->id)
{
$sql = sprintf('UPDATE itl_polls SET question = "%s" WHERE id = %d', $DB->escape_string( $data['question'] ), $this->id );
$res = $DB->query( $sql );
// asnwers to remove
$deletedAnswers = array_diff( array_keys($this->answers), array_keys($data['answers']) );
$sql = 'DELETE FROM itl_answers WHERE id IN ('.implode(',', $deletedAnswers).')';
$result = $DB->query( $sql );
foreach ( $data['answers'] as $id => $answer )
{
if ($id > 0)
{
$sql = sprintf('UPDATE itl_answers SET answer = "%s" WHERE id = %d', $DB->escape_string( $answer ), intval( $id ) );
} else {
$sql = sprintf('INSERT INTO itl_answers(poll_id, answer) VALUES(%d, "%s")', intval($this->id), $DB->escape_string( $answer ) );
}
$result = $DB->query( $sql );
}
return true;
}
$sql = sprintf('INSERT INTO itl_polls(question) VALUES ("%s")', $DB->escape_string( $data['question'] ) );
$result = $DB->query( $sql );
$id = $DB->insert_id();
foreach ( $data['answers'] as $a_id => $answer )
{
$sql = sprintf('INSERT INTO itl_answers(poll_id, answer) VALUES(%d, "%s")', $id, $DB->escape_string($answer) );
$result = $DB->query( $sql );
}
return true;
}
public function Delete()
{
// TODO: Transaction
$sql = 'DELETE FROM itl_votes WHERE answer_id IN ('.implode( ',', array_keys( $this->answers ) ).')';
$result = DB::Instance()->query( $sql );
$sql = 'DELETE FROM itl_answers WHERE poll_id = '.$this->id;
$result = DB::Instance()->query( $sql );
$sql = 'DELETE FROM itl_polls WHERE id = ' . $this->id;
$result = DB::Instance()->query( $sql );
return true;
}
static public function GetPolls()
{
$sql = 'SELECT * FROM itl_polls';
$res = DB::Instance()->query( $sql );
$count = DB::Instance()->num_rows($res);
$polls = array();
for ($i = 0; $i < $count; $i++)
{
$row = DB::Instance()->get_row($res);
$polls[] = new Poll($row->id, $row);
}
return $polls;
}
static public function GetCurrentPoll()
{
$sql = 'SELECT * FROM itl_polls ORDER BY id DESC LIMIT 1';
$res = DB::Instance()->query( $sql );
$row = DB::Instance()->get_row($res);
$poll = new Poll($row->id, $row);
return $poll;
}
}
?>