add showvotes functionality and remove notification when people vote
[cacert-boardvoting.git] / database.php
1 <?php
2 class DB {
3 var $board = "cacert-board@lists.cacert.org";
4
5 function __construct() {
6 $this->dbh = new PDO("sqlite:".dirname(__FILE__)."/database.sqlite");
7 $this->statement = array();
8 $this->statement['list decisions'] = $this->dbh->prepare("SELECT decisions.id AS id, decisions.tag, voters.name AS proposer, decisions.proposed, decisions.title, decisions.content, decisions.quorum, decisions.majority, decisions.status, decisions.due, decisions.modified, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=1) AS ayes, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=-1) AS nayes, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=0) AS abstains FROM decisions, voters WHERE decisions.proponent=voters.id ORDER BY proposed DESC LIMIT 10 OFFSET 10 * (:page - 1);");
9 $this->statement['closed decisions'] = $this->dbh->prepare("SELECT decisions.id, decisions.tag, voters.name AS proposer, decisions.proposed, decisions.title, decisions.content, decisions.quorum, decisions.majority, decisions.status, decisions.due, decisions.modified, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=1) AS ayes, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=-1) AS nayes, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=0) AS abstains FROM decisions, voters WHERE decisions.proponent=voters.id AND decisions.status=0 AND datetime('now','utc') > datetime(due);");
10 $this->statement['get decision'] = $this->dbh->prepare("SELECT decisions.id, decisions.tag, decisions.proponent, voters.name AS proposer, decisions.proposed, decisions.title, decisions.content, decisions.quorum, decisions.majority, decisions.status, decisions.due, decisions.modified, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=1) AS ayes, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=-1) AS nayes, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=0) AS abstains FROM decisions, voters WHERE decisions.proponent=voters.id AND decisions.id=:decision;");
11 $this->statement['get new decision'] = $this->dbh->prepare("SELECT decisions.id, decisions.tag, decisions.proponent, voters.name AS proposer, decisions.proposed, decisions.title, decisions.content, decisions.quorum, decisions.majority, decisions.status, decisions.due, decisions.modified, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=1) AS ayes, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=-1) AS nayes, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=0) AS abstains FROM decisions, voters WHERE decisions.proponent=voters.id AND decisions.id=last_insert_rowid();");
12 $this->statement['get voter'] = $this->dbh->prepare("SELECT voters.id, voters.name FROM voters, emails WHERE voters.id=emails.voter AND emails.address=? AND voters.enabled=1");
13 $this->statement['get voter by id'] = $this->dbh->prepare("SELECT voters.id, voters.name FROM voters WHERE id=:id;");
14 $this->statement['get voters'] = $this->dbh->prepare("SELECT voters.id, voters.name FROM voters WHERE voters.enabled=1 ORDER BY name ASC;");
15 $this->statement['del vote'] = $this->dbh->prepare("DELETE FROM votes WHERE decision=:decision AND voter=:voter;");
16 $this->statement['do vote'] = $this->dbh->prepare("INSERT INTO votes (decision, voter, vote, voted, notes) VALUES (:decision, :voter, :vote, datetime('now','utc'), :notes);");
17 $this->statement['stats'] = $this->dbh->prepare("SELECT COUNT(*) AS voters FROM voters WHERE enabled=1;");
18 $this->statement['list votes'] = $this->dbh->prepare("SELECT voters.name AS name, votes.vote AS vote FROM voters,votes WHERE voters.id=votes.voter AND votes.decision=:id;");
19 $this->statement['create decision'] = $this->dbh->prepare("INSERT INTO decisions (proposed, proponent, title, content, quorum, majority, status, due, modified) VALUES (datetime('now','utc'), :proponent, :title, :content, :quorum, :majority, 0, datetime('now','utc', :due), datetime('now','utc'));");
20 $this->statement['post create'] = $this->dbh->prepare(" UPDATE decisions SET tag='m' || strftime('%Y%m%d','now') || '.' || id WHERE id=last_insert_rowid();");
21 $this->statement['update decision'] = $this->dbh->prepare("UPDATE decisions SET proposed=datetime('now','utc'), proponent=:proponent, title=:title, content=:content, quorum=:quorum, majority=:majority, status=0, due=datetime('now','utc',:due), modified=datetime('now','utc') WHERE id=:id;");
22 $this->statement['close decision'] = $this->dbh->prepare("UPDATE decisions SET status=:status, modified=datetime('now','utc') WHERE id=:decision");
23 }
24 function getStatement($name) {
25 return $this->statement[$name];
26 }
27 function closeVotes() {
28 $stmt = $this->getStatement("closed decisions");
29 $upd = $this->getStatement("close decision");
30 if ($stmt->execute()) {
31 while ($decision = $stmt->fetch()) {
32 $votes = $decision['ayes'] + $decision['nayes'] + $decision['abstains'];
33 if ($votes < $decision['quorum']) {
34 $decision['status'] = -1;
35 } else {
36 $votes = $decision['ayes'] + $decision['nayes'];
37 if (($decision['ayes'] / $votes) >= ($decision['majority'] / 100)) {
38 $decision['status'] = 1;
39 } else {
40 $decision['status'] = -1;
41 }
42 }
43 $upd->bindParam(":decision",$decision['id']);
44 $upd->bindParam(":status",$decision['status']);
45 $upd->execute();
46 $state = $decision['status']==1?"accepted":"declined";
47 $tag = $decision['tag'];
48 $title = $decision['title'];
49 $content = $decision['content'];
50 $quorum = $decision['quorum'];
51 $majority = $decision['majority'];
52 $ayes = $decision['ayes'];
53 $nayes = $decision['nayes'];
54 $abstains = $decision['abstains'];
55 $totalvotes = $decision['ayes']+$decision['nayes'];
56 if ($totalvotes <= 0) $percent = 0;
57 else $percent = $decision['ayes'] * 100 / $totalvotes;
58 $body = <<<BODY
59 Dear Board,
60
61 The motion with the identifier $tag has been $state.
62
63 Motion:
64 $title
65 $content
66
67 Votes:
68 Quorum: $quorum
69 Majority: $majority%
70
71 Ayes: $ayes
72 Nayes: $nayes
73 Abstentions: $abstains
74
75 Percentage: $percent%
76
77 Kind regards,
78 the voting system.
79
80 BODY;
81 $this->notify("Re: ".$decision['tag']." - ".$decision['title'],$body);
82 }
83 }
84 }
85 function notify($subject,$body)
86 {
87 mail($this->board,$subject,$body,"From: Voting System <returns@cacert.org>");
88 //mail("testsympa@lists.cacert.org",$subject,$body,"From: Voting System <returns@cacert.org>");
89 }
90 }
91 ?>