Source for file pear-voter.php
Documentation is available at pear-voter.php
require_once 'Damblan/Karma.php';
$this->dbh = &$GLOBALS['dbh'];
$this->user = isset ($GLOBALS['auth_user']) ? $GLOBALS['auth_user']->handle : false;
$this->damblan = new Damblan_Karma ($this->dbh);
function listCurrentElections ()
$all = $this->dbh->getAll ('
IF(e.votestart > NOW(),"no","yes") as active,
(e.votestart < NOW() AND e.voteend >= NOW())
', array (), DB_FETCHMODE_ASSOC );
foreach ($all as $i => $election) {
$vote = $this->dbh->getOne ('SELECT COUNT(*) FROM
WHERE election_id=? AND handle=?', array ($election['id'], $this->user));
$all[$i]['voted'] = $vote ? 'yes' : 'no';
$all = $this->dbh->getAll ('
IF(e.votestart > NOW(),"no","yes") as active,
(e.votestart < NOW() AND e.voteend >= NOW())
', array (), DB_FETCHMODE_ASSOC );
function listCompletedElections ($old = false )
$extra = ' AND UNIX_TIMESTAMP() - UNIX_TIMESTAMP(e.voteend) < 2592000';
$all = $this->dbh->getAll ('
elections e, election_results r
e.voteend < NOW()' . $extra . '
', 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
WHERE election_id=? AND handle=?', array ($election['id'], $this->user));
$all[$i]['voted'] = $vote ? 'yes' : 'no';
$all[$i]['results'] = $results;
$all = $this->dbh->getAll ('
c.summary_link as winnerlink,
elections e, election_choices c, election_results r
e.voteend < NOW()' . $extra . '
', 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;
function listAllElections ()
$all = $this->dbh->getAll ('
elections e, election_handle_votes v
', array ($this->user), DB_FETCHMODE_ASSOC );
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 );
$choices = $this->dbh->getAll ('
SELECT * FROM election_choices WHERE election_id = ?
', array ($id), DB_FETCHMODE_ASSOC );
$info['choices'] = $choices;
$info['results'] = $this->dbh->getAll ('
SELECT e.votepercent, e.votetotal, c.choice, c.summary, c.summary_link
FROM election_results e, election_choices c
WHERE e.election_id = ? AND
c.election_id = e.election_id AND
ORDER BY e.votetotal DESC
', array ($id), DB_FETCHMODE_ASSOC );
foreach ($info['results'] as $result) {
$order[$result['votetotal']][] = $result;
foreach ($order as $results) {
if (count($winners) >= $info['maximum_choices']) {
foreach ($results as $result) {
$winners[] = $result['choice'];
$info['winners'] = $winners;
$abstain = $this->dbh->getOne ('
SELECT COUNT(*) FROM election_votes_abstain
$allvoters = $this->dbh->getOne ("
k.level in ('pear.dev', 'pear.voter', 'pear.admin')", array ());
$votedthis = $this->dbh->getOne ('
SELECT count(*) FROM election_handle_votes where election_id=?
$info['turnout'] = $votedthis / $allvoters;
$wiki = & new Text_Wiki ();
$wiki->disableRule ('wikilink');
$info['detail'] = $wiki->transform ($info['detail']);
if ($info['maximum_choices'] > 1 ) {
$total = $this->dbh->getOne ('
SELECT COUNT(*) FROM election_votes_multiple WHERE
$total = $this->dbh->getOne ('
SELECT COUNT(*) FROM election_votes_single WHERE
// percentage of abstaining voters
if ($total + $abstain > 0 ) {
$info['abstain'] = $abstain / ($total + $abstain);
$info['abstaincount'] = $abstain;
$info['abstaincount'] = 0;
return $this->dbh->getOne ('
SELECT COUNT(*) FROM election_handle_votes WHERE election_id=?
AND handle=?', array ($id, $this->user));
function pendingElection ($id)
$info = $this->electionInfo ($id);
// election is not active
if ($this->hasVoted ($id)) {
$info = $this->electionInfo ($id);
// election is not active
if ($info['eligiblevoters'] == 1 ) {
if ($this->damblan->has ($this->user, 'pear.dev')) {
} elseif ($info['eligiblevoters'] == 2 ) {
// general PHP public + PEAR developers
if ($this->damblan->has ($this->user, 'pear.dev')) {
if ($this->damblan->has ($this->user, 'pear.voter')) {
if (!isset ($this->voteSalt)) {
function vote ($id, $votes)
if (!$this->electionExists ($id)) {
if ($this->hasVoted ($id)) {
$info = $this->electionInfo ($id);
if ($info['maximum_choices'] > 1 ) {
if (count($votes) > $info['maximum_choices'] || count($votes) <
$info['minimum_choices']) {
$table = 'election_votes_multiple';
if (count($votes) != 1 ) {
$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)) {
$this->dbh->query ('DELETE FROM ' . $table . '
WHERE election_id=? AND vote_hash=?', array ($id, $vote_hash));
PEAR ::popErrorHandling ();
$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 ' . $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 ();
PEAR ::popErrorHandling ();
$this->email ($info, $votes, $this->getVoteSalt ());
function prettifyVotes ($election, $votes)
foreach ($election['choices'] as $choice) {
if (in_array($choice['choice'], $votes)) {
$res .= $choice['summary'] . "\n";
function email ($election, $votes, $salt)
include_once 'pear-database-user.php';
$info = user ::info ($this->user);
$email = '"' . $info['name'] . '" <' . $info['email'] . '>';
$headers = "From: bounce-no-user@php.net\n";
$headers .= "X-Mailer: PEAR election voting interface\n";
$headers .= "X-PEAR-Election: " . $election['id'] . "\n";
$subject = '[PEAR-ELECTION] Your vote in election ' . $election['purpose'];
$votes = $this->prettifyVotes ($election, $votes);
$text = 'Your vote for the election: ' . $election['purpose'] . "\n" .
'has been registered. You voted for:
$text .= 'Your vote salt is ' . $salt . "\n";
$text .= 'this is your only record of the vote salt, without it your vote ' .
'cannot be retrieved. Thank you for voting';
$text = 'Your abstaining vote for the election: ' . $election['purpose'] . "\n" .
'has been registered.' . "\n";
$text .= 'Your vote salt is ' . $salt . "\n";
$text .= 'this is your only record of the vote salt, without it your vote ' .
'cannot be retrieved. Thank you for voting';
$text .= "\nVisit http://pear.php.net/election/ to retrieve your vote";
$res = mail($email, $subject, $text,
$headers, '-f bounce-no-user@php.net');
if (!$this->electionExists ($id)) {
if ($this->hasVoted ($id)) {
$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 ();
$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 ();
PEAR ::popErrorHandling ();
$this->email ($this->electionInfo ($id), false , $this->getVoteSalt ());
function retrieveVote ($id, $salt)
if (!$this->electionExists ($id)) {
if (!$this->hasVoted ($id)) {
$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
AND e.election_id = c.election_id
AND e.vote = c.choice', array ($id, $this->voteSalt))) {
$votes = $this->dbh->getAll ('
SELECT choice FROM election_votes_multiple e, election_choices c
AND e.election_id = c.election_id
AND e.vote = c.choice', array ($id, $this->voteSalt));
foreach ($votes as $vote) {
Documentation generated on Mon, 11 Mar 2019 15:34:36 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|