cvs: pearweb /cron election_results.php /include pear-voter.php /public_html election-info.php /sql election.sql /templates
election-results.tpl.php election-showresults.tpl.php election-vote.tpl.php

From: Date: Mon, 06 Nov 2006 05:54:53 +0000
Subject: cvs: pearweb /cron election_results.php /include pear-voter.php /public_html election-info.php /sql election.sql /templates
election-results.tpl.php election-showresults.tpl.php election-vote.tpl.php
Groups: php.pear.cvs php.pear.core 
Request: Send a blank email to pear-core+get-5155@lists.php.net to get a copy of this message
cellog Mon Nov 6 05:54:53 2006 UTC Added files: /pearweb/templates election-results.tpl.php election-showresults.tpl.php /pearweb/cron election_results.php /pearweb/include pear-voter.php Modified files: /pearweb/public_html election-info.php /pearweb/sql election.sql /pearweb/templates election-vote.tpl.php Log: next iteration of voting interface add missing pear-voter.php add support for election results, fix SQL to support multiple winners todo: recognize winners and highlight them in the election results pane todo: election creation admin interface

http://cvs.php.net/viewvc.cgi/pearweb/public_html/election-info.php?r1=1.1&r2=1.2&diff_format=u Index: pearweb/public_html/election-info.php diff -u pearweb/public_html/election-info.php:1.1 pearweb/public_html/election-info.php:1.2 --- pearweb/public_html/election-info.php:1.1 Sun Nov 5 22:38:18 2006 +++ pearweb/public_html/election-info.php Mon Nov 6 05:54:53 2006 @@ -113,4 +113,8 @@ require dirname(dirname(__FILE__)) . '/templates/election-dovote.tpl.php'; } } +if (isset($_GET['results'])) { + $info = $voter->electionInfo($_GET['election']); + require dirname(dirname(__FILE__)) . '/templates/election-showresults.tpl.php'; +} ?> \ No newline at end of file http://cvs.php.net/viewvc.cgi/pearweb/sql/election.sql?r1=1.1&r2=1.2&diff_format=u Index: pearweb/sql/election.sql diff -u pearweb/sql/election.sql:1.1 pearweb/sql/election.sql:1.2 --- pearweb/sql/election.sql:1.1 Sun Nov 5 22:38:20 2006 +++ pearweb/sql/election.sql Mon Nov 6 05:54:53 2006 @@ -33,7 +33,6 @@ voteend DATE NOT NULL, creator VARCHAR(20) NOT NULL, createdate DATETIME NOT NULL, - winner TINYINT, minimum_choices TINYINT DEFAULT '1' NOT NULL, maximum_choicse TINYINT DEFAULT '1' NOT NULL, PRIMARY KEY (id) @@ -51,4 +50,11 @@ election_id INT NOT NULL, handle VARCHAR(20) NOT NULL, PRIMARY KEY (election_id, handle) +); + +CREATE TABLE election_results ( + election_id INT NOT NULL, + choice TINYINT NOT NULL, + votepercent FLOAT NOT NULL, + PRIMARY KEY (election_id, choice) ); \ No newline at end of file http://cvs.php.net/viewvc.cgi/pearweb/templates/election-vote.tpl.php?r1=1.1&r2=1.2&diff_format=u Index: pearweb/templates/election-vote.tpl.php diff -u pearweb/templates/election-vote.tpl.php:1.1 pearweb/templates/election-vote.tpl.php:1.2 --- pearweb/templates/election-vote.tpl.php:1.1 Sun Nov 5 22:38:20 2006 +++ pearweb/templates/election-vote.tpl.php Mon Nov 6 05:54:53 2006 @@ -39,6 +39,7 @@ ?> </table> <?php endif; // if (count($currentelections)) +require dirname(dirname(__FILE__)) . '/templates/election-results.tpl.php'; if (count($allelections)): if ($retrieval && isset($info)): ?> <h3>Your vote information:</h3> @@ -78,5 +79,4 @@ <input type="text" size="17" name="salt" /> <input type="submit" value="Retrieve Vote" /> </form> -<?php endif; // if (count($allelections)) -require dirname(__FILE__) . '/election-results.tpl.php'; \ No newline at end of file +<?php endif; // if (count($allelections)) \ No newline at end of file http://cvs.php.net/viewvc.cgi/pearweb/templates/election-results.tpl.php?view=markup&rev=1.1 Index: pearweb/templates/election-results.tpl.php +++ pearweb/templates/election-results.tpl.php <?php if (count($completedelections)): ?> <h2>Election Results for Completed Elections:</h2> <table> <tr> <th class="form-label_top">Election Issue</th> <th class="form-label_top">Election dates</th> <th class="form-label_top">Did you vote?</th> </tr> <?php foreach ($completedelections as $election): $class = 'vote-inactive'; if ($election['voted'] == 'yes') { $class = 'vote-complete'; } ?> <tr> <td class="<?php echo $class; ?>"><a href="/election-info.php?election=<?php echo $election['id']; ?>&results=1"><?php echo htmlspecialchars($election['purpose']); ?></a></td> <td class="<?php echo $class; ?>"><?php echo $election['votestart'] . ' until ' . $election['voteend']; ?></td> <td class="<?php echo $class; ?>"><?php echo $election['voted']; ?></td> </tr> <?php endforeach; // foreach ($completedelections as $election): ?> </table> <?php endif; // if (count($completedelections)): ?> http://cvs.php.net/viewvc.cgi/pearweb/templates/election-showresults.tpl.php?view=markup&rev=1.1 Index: pearweb/templates/election-showresults.tpl.php +++ pearweb/templates/election-showresults.tpl.php <?php response_header('Results'); ?> <h2>Election Results for <?php echo $info['purpose'] ?>:</h2> <table> <tr> <th class="form-label_left">Election Issue</td> <td class="form-input"><?php echo htmlspecialchars($info['detail']) ?></td> </tr> <tr> <th class="form-label_left">Election dates</th> <td class="form-input"><?php echo $info['votestart'] . ' until ' . $info['voteend']; ?></td> </tr> <tr> <th class="form-label_left">Results</th> <td class="form-input"> <table><?php foreach ($info['results'] as $result) { echo '<tr><td>' . number_format($result['votepercent'] * 100, 2) . '%</td><td><a href="' . $result['summary_link'] . '">' . htmlspecialchars($result['summary']) . '</a></td></tr>'; } ?> </table> </td> </tr> </table> http://cvs.php.net/viewvc.cgi/pearweb/cron/election_results.php?view=markup&rev=1.1 Index: pearweb/cron/election_results.php +++ pearweb/cron/election_results.php <?php /** * Get common settings. */ require_once dirname(__FILE__) . '/../include/pear-config.php'; // Get the database class. require_once 'DB.php'; $options = array( 'persistent' => false, 'portability' => DB_PORTABILITY_ALL, ); $dbh =& DB::connect(PEAR_DATABASE_DSN, $options); if (PEAR::isError($dbh)) { die ("Failed to connect: $dsn\n"); } $dbh->setFetchMode(DB_FETCHMODE_ASSOC); $toProcess = $dbh->getAll(' SELECT * FROM elections e LEFT JOIN election_results r on e.id = r.election_id WHERE r.election_id IS NULL AND e.voteend < NOW() '); if (count($toProcess)) { foreach ($toProcess as $election) { $totalabstain = $dbh->getOne(' SELECT COUNT(*) FROM election_votes_abstain WHERE election_id=? ', array($election['id'])); if ($election['maximum_choices'] == 1) { $totalvotes = $dbh->getOne(' SELECT COUNT(*) FROM election_votes_single ') + $totalabstain; $results = $dbh->getAll(' SELECT COUNT(*) as total, vote FROM election_votes_single WHERE election_id=? GROUP BY vote ', array($election['id']), DB_FETCHMODE_ASSOC); } else { $totalvotes = $dbh->getOne(' SELECT COUNT(*) FROM election_votes_multiple ') + $totalabstain; $results = $dbh->getAll(' SELECT COUNT(*) as total, vote FROM election_votes_multiple WHERE election_id=? GROUP BY vote ', array($election['id']), DB_FETCHMODE_ASSOC); } foreach ($results as $vote) { $dbh->query(' INSERT INTO election_results (election_id, choice, votepercent) VALUES(?,?,?) ', array($election['id'], $vote['vote'], $vote['total'] / $totalvotes)); } } } http://cvs.php.net/viewvc.cgi/pearweb/include/pear-voter.php?view=markup&rev=1.1 Index: pearweb/include/pear-voter.php +++ pearweb/include/pear-voter.php <?php class PEAR_Voter { var $dbh; var $user = false; var $voteSalt; function PEAR_Voter() { $this->dbh = &$GLOBALS['dbh']; $this->user = isset($GLOBALS['auth_user']) ? $GLOBALS['auth_user']->handle : false; } function listCurrentElections() { if ($this->user) { $all = $this->dbh->getAll(' SELECT IF(e.votestart > NOW(),"no","yes") as active, e.purpose, e.votestart, e.voteend, e.id FROM elections e WHERE e.votestart > NOW() OR (e.votestart < NOW() AND e.voteend >= NOW()) ORDER BY e.votestart ', array(), DB_FETCHMODE_ASSOC); foreach ($all as $i => $election) { $vote = $this->dbh->getOne('SELECT COUNT(*) FROM election_handle_votes WHERE election_id=? AND handle=?', array($election['id'], $this->user)); $all[$i]['voted'] = $vote ? 'yes' : 'no'; } } else { $all = $this->dbh->getAll(' SELECT IF(e.votestart > NOW(),"no","yes") as active, "no" as voted, e.purpose, e.votestart, e.voteend, e.id FROM elections e WHERE e.votestart > NOW() OR (e.votestart < NOW() AND e.voteend >= NOW()) ORDER BY e.votestart ', array(), DB_FETCHMODE_ASSOC); } if (!is_array($all)) { return array(); } return $all; } function listCompletedElections() { if ($this->user) { $all = $this->dbh->getAll(' SELECT e.purpose, e.votestart, e.voteend, e.id FROM elections e, election_results r WHERE e.id = r.election_id AND e.voteend < NOW() GROUP BY e.id ORDER BY e.voteend DESC ', array(), DB_FETCHMODE_ASSOC); foreach ($all as $i => $election) { $results = $this->dbh->getAll(' SELECT * FROM election_results WHERE election_id=? ORDER BY votepercent DESC ', array($election['id']), DB_FETCHMODE_ASSOC); $vote = $this->dbh->getOne('SELECT COUNT(*) FROM election_handle_votes WHERE election_id=? AND handle=?', array($election['id'], $this->user)); $all[$i]['voted'] = $vote ? 'yes' : 'no'; $all[$i]['results'] = $results; } } else { $all = $this->dbh->getAll(' SELECT "no" as voted, e.purpose, e.votestart, e.voteend, e.id, c.summary as winner, c.summary_link as winnerlink e.id FROM elections e, election_choices c, election_results r WHERE e.id = r.election_id AND c.election_id = e.id AND e.voteend < NOW() ORDER BY e.voteend DESC ', array(), DB_FETCHMODE_ASSOC); foreach ($all as $i => $election) { $results = $this->dbh->getAll(' SELECT * FROM election_results WHERE election_id=? ORDER BY votepercent DESC ', array($election['id']), DB_FETCHMODE_ASSOC); $all[$i]['results'] = $results; } } if (!is_array($all)) { return array(); } return $all; } function listAllElections() { if (!$this->user) { return array(); } $all = $this->dbh->getAll(' SELECT e.purpose, e.id FROM elections e, election_handle_votes v WHERE v.election_id = e.id AND v.handle=? ORDER BY e.voteend DESC ', array($this->user), DB_FETCHMODE_ASSOC); if (!is_array($all)) { return array(); } return $all; } function electionExists($id) { return $this->dbh->getOne('SELECT COUNT(id) FROM elections WHERE id=?', array($id)); } function electionInfo($id) { $info = $this->dbh->getAll(' SELECT * FROM elections WHERE id = ? ', array($id), DB_FETCHMODE_ASSOC); if (!is_array($info)) { return false; } $info = $info[0]; $choices = $this->dbh->getAll(' SELECT * FROM election_choices WHERE election_id = ? ORDER BY choice ', array($id), DB_FETCHMODE_ASSOC); $info['choices'] = $choices; $info['results'] = $this->dbh->getAll(' SELECT e.votepercent, c.summary, c.summary_link FROM election_results e, election_choices c WHERE e.election_id = ? AND c.election_id = e.election_id AND c.choice = e.choice ORDER BY e.votepercent DESC ', array($id), DB_FETCHMODE_ASSOC); return $info; } function hasVoted($id) { return $this->dbh->getOne(' SELECT COUNT(*) FROM election_handle_votes WHERE election_id=? AND handle=?', array($id, $this->user)); } function getVoteSalt() { if (!isset($this->voteSalt)) { $this->voteSalt = date('YmdHis') . mt_rand(1, 999); } return $this->voteSalt; } function vote($id, $votes) { if (!$this->user) { return false; } if (!$this->electionExists($id)) { return false; } if ($this->hasVoted($id)) { return false; } if (!is_array($votes)) { return false; } $info = $this->electionInfo($id); if ($info['maximum_choices'] > 1) { if (count($votes) > $info['maximum_choices'] || count($votes) < $info['minimum_choices']) { return false; } $table = 'election_votes_multiple'; } else { if (count($votes) != 1) { return false; } $table = 'election_votes_single'; } $vote_hash = md5($this->user . $this->getVoteSalt()); PEAR::pushErrorHandling(PEAR_ERROR_RETURN); foreach ($votes as $vote) { $err = $this->dbh->query(' INSERT INTO ' . $table . ' (election_id, vote, vote_hash) VALUES(?,?,?)', array($id, $vote, $vote_hash)); if (PEAR::isError($err)) { // "rollback" the query $this->dbh->query('DELETE FROM ' . $table . ' WHERE election_id=? AND vote_hash=?', array($id, $vote_hash)); PEAR::popErrorHandling(); return false; } } $err = $this->dbh->query(' INSERT INTO election_handle_votes (election_id, handle) VALUES (?,?)', array($id, $this->user)); if (PEAR::isError($err)) { // "rollback" the query $this->dbh->query('DELETE FROM ' . $table . ' WHERE election_id=? AND vote_hash=?', array($id, $vote_hash)); $this->dbh->query('DELETE FROM election_handle_votes WHERE election_id=? AND handle=?', array($id, $this->user)); PEAR::popErrorHandling(); return false; } PEAR::popErrorHandling(); return true; } function abstain($id) { if (!$this->user) { return false; } if (!$this->electionExists($id)) { return false; } if ($this->hasVoted($id)) { return false; } $vote_hash = md5($this->user . $this->getVoteSalt()); PEAR::pushErrorHandling(PEAR_ERROR_RETURN); $err = $this->dbh->query(' INSERT INTO election_votes_abstain (election_id, vote_hash) VALUES (?,?)', array($id, $vote_hash)); if (PEAR::isError($err)) { PEAR::popErrorHandling(); return false; } $err = $this->dbh->query(' INSERT INTO election_handle_votes (election_id, handle) VALUES (?,?)', array($id, $this->user)); if (PEAR::isError($err)) { $this->dbh->query('DELETE FROM election_votes_abstain WHERE election_id=? and vote_hash=?', array($id, $vote_hash)); PEAR::popErrorHandling(); return false; } PEAR::popErrorHandling(); return true; } function retrieveVote($id, $salt) { if (!$this->electionExists($id)) { return false; } if (!$this->user) { return false; } if (!$this->hasVoted($id)) { return false; } $this->voteSalt = md5($this->user . $salt); if ($vote = $this->dbh->getOne(' SELECT * FROM election_votes_abstain WHERE election_id=? AND vote_hash=?', array($id, $this->voteSalt))) { return array('(abstain)'); } if ($vote = $this->dbh->getOne(' SELECT choice FROM election_votes_single e, election_choices c WHERE e.election_id=? AND e.vote_hash=? AND e.election_id = c.election_id AND e.vote = c.choice', array($id, $this->voteSalt))) { return array($vote); } $votes = $this->dbh->getAll(' SELECT choice FROM election_votes_multiple e, election_choices c WHERE e.election_id=? AND e.vote_hash=? AND e.election_id = c.election_id AND e.vote = c.choice', array($id, $this->voteSalt)); $ret = array(); foreach ($votes as $vote) { $ret[] = $vote[0]; } if (!$ret) { return false; } return $ret; } }
« previous php.pear.core (#5155) next »