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