consolidate mail functions. typo in mail message fix. div by zero fix if no votes...
authorcommunity.cacert.org <community.cacert.org@d4452222-2f33-11de-9270-010000000000>
Tue, 26 May 2009 02:32:18 +0000 (02:32 +0000)
committercommunity.cacert.org <community.cacert.org@d4452222-2f33-11de-9270-010000000000>
Tue, 26 May 2009 02:32:18 +0000 (02:32 +0000)
git-svn-id: http://svn.cacert.cl/Software/Voting/vote@39 d4452222-2f33-11de-9270-010000000000

database.php
motion.php
proxy.php
vote.php

index 07f3318..a522bd8 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 <?php
-       $board = "cacert-board@lists.cacert.org";
        class DB {
        class DB {
+               var $board = "cacert-board@lists.cacert.org";
+
                function __construct() {
                        $this->dbh = new PDO("sqlite:".dirname(__FILE__)."/database.sqlite");
                        $this->statement = array();
                function __construct() {
                        $this->dbh = new PDO("sqlite:".dirname(__FILE__)."/database.sqlite");
                        $this->statement = array();
@@ -13,7 +14,7 @@
                        $this->statement['get voters'] = $this->dbh->prepare("SELECT voters.id, voters.name FROM voters WHERE voters.enabled=1 ORDER BY name ASC;");
                        $this->statement['del vote'] = $this->dbh->prepare("DELETE FROM votes WHERE decision=:decision AND voter=:voter;");
                        $this->statement['do vote'] = $this->dbh->prepare("INSERT INTO votes (decision, voter, vote, voted, notes) VALUES (:decision, :voter, :vote, datetime('now','utc'), :notes);");
                        $this->statement['get voters'] = $this->dbh->prepare("SELECT voters.id, voters.name FROM voters WHERE voters.enabled=1 ORDER BY name ASC;");
                        $this->statement['del vote'] = $this->dbh->prepare("DELETE FROM votes WHERE decision=:decision AND voter=:voter;");
                        $this->statement['do vote'] = $this->dbh->prepare("INSERT INTO votes (decision, voter, vote, voted, notes) VALUES (:decision, :voter, :vote, datetime('now','utc'), :notes);");
-                       $this->statement['stats'] = $this->dbh->prepare("SELECT (SELECT COUNT(*) FROM voters WHERE enabled=1) AS voters;");
+                       $this->statement['stats'] = $this->dbh->prepare("SELECT COUNT(*) AS voters FROM voters WHERE enabled=1;");
                        $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'));");
                        $this->statement['post create'] = $this->dbh->prepare(" UPDATE decisions SET tag='m' || strftime('%Y%m%d','now') || '.' || id WHERE id=last_insert_rowid();");
                        $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;");
                        $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'));");
                        $this->statement['post create'] = $this->dbh->prepare(" UPDATE decisions SET tag='m' || strftime('%Y%m%d','now') || '.' || id WHERE id=last_insert_rowid();");
                        $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;");
@@ -50,7 +51,9 @@
                                        $ayes = $decision['ayes'];
                                        $nayes = $decision['nayes'];
                                        $abstains = $decision['abstains'];
                                        $ayes = $decision['ayes'];
                                        $nayes = $decision['nayes'];
                                        $abstains = $decision['abstains'];
-                                       $percent = $decision['ayes'] * 100 / $decision['ayes']+$decision['nayes'];
+                                       $totalvotes = $decision['ayes']+$decision['nayes'];
+                                       if ($totalvotes <= 0) $percent = 0;
+                                       else $percent = $decision['ayes'] * 100 / $totalvotes;
                                        $body = <<<BODY
 Dear Board,
 
                                        $body = <<<BODY
 Dear Board,
 
@@ -74,9 +77,14 @@ Kind regards,
 the voting system.
 
 BODY;
 the voting system.
 
 BODY;
-                                       mail($board,"Re: ".$decision['tag']." - ".$decision['title'],$body,"From: Voting System <returns@caert.org>");
+                                       $this->notify("Re: ".$decision['tag']." - ".$decision['title'],$body);
                                }
                        }
                }
                                }
                        }
                }
+               function notify($subject,$body)
+               {
+                       mail($this->board,$subject,$body,"From: Voting System <returns@cacert.org>");
+                       //mail("testsympa@lists.cacert.org",$subject,$body,"From: Voting System <returns@cacert.org>");
+               }
        }
 ?>
        }
 ?>
index 60cfb0d..4672cf7 100644 (file)
@@ -74,14 +74,14 @@ Aye: $voteurl&vote=1
 Naye: $voteurl&vote=-1
 Abstain: $voteurl&vote=0
 
 Naye: $voteurl&vote=-1
 Abstain: $voteurl&vote=0
 
-Please be aware, that if you have voted already your votr is still registered and valid.
+Please be aware, that if you have voted already your vote is still registered and valid.
 If this modification has an impact on how you wish to vote, you are responsible for voting
 again.
 
 Kind regards,
 the voting system
 BODY;
 If this modification has an impact on how you wish to vote, you are responsible for voting
 again.
 
 Kind regards,
 the voting system
 BODY;
-                                       mail($board,"Re: $tag - $title",$body,"From: Voting System <returns@caert.org>");
+                                       $db->notify("Re: $tag - $title",$body);
                                } else {
                                        ?>
                                        <b>The motion has NOT been proposed!</b><br/>
                                } else {
                                        ?>
                                        <b>The motion has NOT been proposed!</b><br/>
@@ -136,7 +136,7 @@ Abstain: $voteurl&vote=0
 Kind regards,
 the voting system
 BODY;
 Kind regards,
 the voting system
 BODY;
-                                       mail($board,"$tag - $title",$body,"From: Voting System <returns@caert.org>");
+                                       $db->notify("$tag - $title",$body);
                                } else {
                                        ?>
                                        <b>The motion has NOT been proposed!</b><br/>
                                } else {
                                        ?>
                                        <b>The motion has NOT been proposed!</b><br/>
index 6d4731f..69e086a 100644 (file)
--- a/proxy.php
+++ b/proxy.php
@@ -83,7 +83,7 @@ Kind regards,
 the vote system
 
 BODY;
 the vote system
 
 BODY;
-                                               mail($board,"Re: $tag - $title",$body,"From: Voting System <returns@caert.org>");
+                                               $db->notify("Re: $tag - $title",$body);
                                        } else {
                                                ?>
                                                <b>The vote has NOT been registered.</b><br/>
                                        } else {
                                                ?>
                                                <b>The vote has NOT been registered.</b><br/>
index f2445c0..819f03a 100644 (file)
--- a/vote.php
+++ b/vote.php
@@ -67,7 +67,7 @@ Kind regards,
 the vote system
 
 BODY;
 the vote system
 
 BODY;
-                                                               mail($board,"Re: $tag - $title",$body,"From: Voting System <returns@caert.org>");
+                                                               $db->notify("Re: $tag - $title",$body);
                                                        } else {
                                                                ?>
                                                                <b>Your vote has NOT been registered.</b><br/>
                                                        } else {
                                                                ?>
                                                                <b>Your vote has NOT been registered.</b><br/>