mtWeb > Download > Poll system in PHP
<?php
/* $Id: poll.php,v 1.17 2002/09/28 20:25:07 shaggy Exp $ */
/*
Copyright (c) 2001, 2002 by Martin Tsachev. All rights reserved.
mailto:shaggy@members.evolt.org
http://martin.f2o.org
Redistribution and use in source and binary forms,
with or without modification, are permitted provided
that the conditions available at
http://www.opensource.org/licenses/bsd-license.html
are met.
*/
class Poll {
var $polls = 0; // count of active polls
var $newPolls = 0; // count of new polls
// current poll variables
var $poll = 1; // the current poll
var $title = ''; // poll title
var $votes = 0; // votes vount
var $member = 0; // author of the poll
var $created; // date created
var $last; // last vote
var $db; // PEAR::DB pointer
var $page; // pointer to the current page object
var $date; // current date quoted
function Poll(&$db, &$page) {
$this->db = $db;
$this->page = $page;
}
// initialization functions
function _count() {
$sql = "SELECT active, count(*) AS count FROM poll GROUP BY active";
$result = $this->db->getAll($sql);
foreach ($result as $row) {
if ($row->active == 'y') {
$this->polls = $row->count;
} else {
$this->newPolls = $row->count;
}
}
}
function _initPoll() {
$sql = "SELECT page, author, published, last FROM poll, page " .
"WHERE poll.id = $this->poll AND poll.page = page.id";
$result = $this->db->getRow($sql);
$page = $result->page;
$this->member = $result->author;
$this->created = format_date($result->published);
if ($result->last) {
$this->last = format_date($result->last);
} else {
$this->last = '';
}
$sql = "SELECT SUM(count) FROM poll_data WHERE poll = $this->poll";
$this->votes = $this->db->getOne($sql);
$this->title = $this->page->getTitle($page);
}
// interface
function validPoll($requested) {
$sql = "SELECT id FROM poll WHERE id = $requested AND active='y'";
$result = $this->db->query($sql);
if ($result->numRows() == 1) {
return $requested;
}
$poll = mt_rand(0, $this->polls - 1); // PHP 4.2.0+ mt_srand() is not necessary
$sql = "SELECT id FROM poll WHERE active='y'";
$sql = $this->db->modifyLimitQuery($sql, $poll, 1);
$poll = $this->db->getOne($sql);
return $poll;
}
function setPoll($requested) {
$this->_count(); // set counts
$this->poll = $this->validPoll( (int) $requested);
$this->_initPoll();
}
function getMaxVote() {
$sql = "SELECT count(*) FROM poll_data WHERE poll = " . $this->poll;
$result = $this->db->getOne($sql);
return $result;
}
function addVote($vote) {
$sql = "UPDATE poll SET last = CURRENT_DATE WHERE id = $this->poll";
$this->db->query($sql);
$sql = "UPDATE poll_data SET count=count+1 WHERE poll=$this->poll AND vote=$vote";
$this->db->query($sql);
return ($this->db->affectedRows() == 1);
}
function addPoll($title, $options) {
$poll = $this->db->nextId('poll_id');
$sql = "INSERT INTO page " .
"(parent, comments, uri, source, author, published, title) " .
"VALUES (24, 'form', '/poll/$poll', 'p', $_SESSION[uid], CURRENT_DATE, $title)";
$this->db->query($sql);
$sql = "SELECT id FROM page WHERE uri = '/poll/$poll'";
$page = $this->db->getOne($sql);
$sql = "INSERT INTO poll (id, page) VALUES ($poll, $page)";
$this->db->query($sql);
foreach ($options as $key => $value) {
$sql = "INSERT INTO poll_data (poll, vote, opt) VALUES ($poll, " . ++$key . ", $value) ";
$this->db->query($sql);
}
}
function &getResults($order = 'vote') {
$sql = "SELECT vote, count, opt FROM poll_data WHERE poll = $this->poll ORDER BY $order";
$result = $this->db->getAll($sql);
return $result;
}
function &getAllPolls() {
$sql = "SELECT poll.id, SUM(poll_data.count) AS count, title " .
"FROM poll, poll_data, page " .
"WHERE (poll.page = page.id) AND (poll.id = poll_data.poll) AND (poll.active = 'y') " .
"GROUP BY poll.id"; //, title"; // title is required to be grouped/aggr. (ANSI SQL)
$result = $this->db->getAll($sql);
return $result;
}
function pollInputs() {
?>
<form method="post" action="/poll/"><table summary="poll inputs">
<tr><td class="heading"><?php echo $this->title?></td></tr>
<?php
$results = $this->getResults();
$checked = 'checked="checked" ';
foreach ($results as $key => $row) {
$input ='<input type="radio" id="vote' . $key . '" name="vote" value="' . $row->vote . '" ' . "$checked/>";
echo "<tr><td>$input" . ' <label for="vote' . $key .'">' . "$row->opt</label></td></tr>";
$checked = '';
}
?>
<tr><td>Total votes: <?php echo $this->votes?></td></tr>
<tr><td>Last vote: <?php echo $this->last?></td></tr>
<tr><td><input type="submit" value="Vote" />
<a href="<?php echo "/poll/$this->poll"?>" title="View the results">Results</a></td></tr>
</table>
<div><input type="hidden" id="poll" name="poll" value="<?php echo $this->poll?>" /></div>
</form>
<?php
}
// end of class
}
?>