From c15441762c2f6ab4438eaf2854c0ee3146a98b30 Mon Sep 17 00:00:00 2001 From: canyonknight Date: Fri, 25 May 2012 17:42:42 -0400 Subject: Pull out DB code from trusted user page * Move DB code in tu.php and tu.php and tu_list.php to new functions in accfuncs.inc.php * Centralization of DB code important in a future transition to PDO interface Signed-off-by: canyonknight Signed-off-by: Lukas Fleischer --- web/html/tu.php | 68 +++++++----------------------- web/lib/acctfuncs.inc.php | 104 ++++++++++++++++++++++++++++++++++++++++++++++ web/template/tu_list.php | 26 ++++++------ 3 files changed, 130 insertions(+), 68 deletions(-) (limited to 'web') diff --git a/web/html/tu.php b/web/html/tu.php index 362679a..5537d3a 100644 --- a/web/html/tu.php +++ b/web/html/tu.php @@ -23,43 +23,15 @@ if ($atype == "Trusted User" || $atype == "Developer") { if (isset($_GET['id'])) { if (is_numeric($_GET['id'])) { - - $q = "SELECT * FROM TU_VoteInfo "; - $q.= "WHERE ID = " . $_GET['id']; - - $dbh = db_connect(); - $results = db_query($q, $dbh); - $row = mysql_fetch_assoc($results); + $row = vote_details($_GET['id']); if (empty($row)) { print __("Could not retrieve proposal details."); } else { $isrunning = $row['End'] > time() ? 1 : 0; - $qvoted = "SELECT * FROM TU_Votes WHERE "; - $qvoted.= "VoteID = " . $row['ID'] . " AND "; - $qvoted.= "UserID = " . uid_from_sid($_COOKIE["AURSID"]); - $result = db_query($qvoted, $dbh); - if ($result) { - $hasvoted = mysql_num_rows($result); - } - else { - $hasvoted = 0; - } - # List voters of a proposal. - $qwhoVoted = "SELECT tv.UserID,U.Username - FROM TU_Votes tv, Users U - WHERE tv.VoteID = {$row['ID']} - AND tv.UserID = U.ID - ORDER BY Username"; - $result = db_query($qwhoVoted,$dbh); - if (mysql_num_rows($result) > 0) { - $whovoted = ''; - while ($who = mysql_fetch_assoc($result)) { - $whovoted.= ''.$who['Username'].' '; - } - } + $whovoted = voter_list($row['ID']); $canvote = 1; $errorvote = ""; @@ -69,8 +41,9 @@ if ($atype == "Trusted User" || $atype == "Developer") { } else if ($row['User'] == username_from_sid($_COOKIE["AURSID"])) { $canvote = 0; $errorvote = __("You cannot vote in an proposal about you."); - } else if ($hasvoted != 0) { + } else if (tu_voted($row['ID'], uid_from_sid($_COOKIE["AURSID"]))) { $canvote = 0; + $hasvoted = 1; $errorvote = __("You've already voted for this proposal."); } @@ -84,25 +57,18 @@ if ($atype == "Trusted User" || $atype == "Developer") { $myvote = "Abstain"; } - $qvote = "UPDATE TU_VoteInfo SET " . $myvote . " = " . ($row[$myvote] + 1) . " WHERE ID = " . $row['ID']; - db_query($qvote, $dbh); - $qvote = "INSERT INTO TU_Votes (VoteID, UserID) VALUES (" . $row['ID'] . ", " . uid_from_sid($_COOKIE["AURSID"]) . ")"; - db_query($qvote, $dbh); + cast_proposal_vote($row['ID'], uid_from_sid($_COOKIE["AURSID"]), $myvote, $row[$myvote] + 1); # Can't vote anymore # $canvote = 0; $errorvote = __("You've already voted for this proposal."); - # Update if they voted - $result = db_query($qvoted, $dbh); - if ($result) { - $hasvoted = mysql_num_rows($result); - } - $results = db_query($q, $dbh); - if ($results) { - $row = mysql_fetch_assoc($results); + # Update if they voted + if (tu_voted($row['ID'], uid_from_sid($_COOKIE["AURSID"]))) { + $hasvoted = 1; } + $row = vote_details($_GET['id']); } } include("tu_details.php"); @@ -112,8 +78,6 @@ if ($atype == "Trusted User" || $atype == "Developer") { } } else { - $dbh = db_connect(); - $limit = $pp; if (isset($_GET['off'])) $offset = $_GET['off']; @@ -137,33 +101,29 @@ if ($atype == "Trusted User" || $atype == "Developer") { $lim = ($limit > 0) ? " LIMIT $limit OFFSET $off" : ""; $by_next = ($by == 'desc') ? 'asc' : 'desc'; - $q = "SELECT * FROM TU_VoteInfo WHERE End > " . time() . " ORDER BY Submitted " . $order; - $result = db_query($q, $dbh); - + $result = current_proposal_list($order); $type = __("Current Votes"); include("tu_list.php"); ?>

- - diff --git a/web/lib/acctfuncs.inc.php b/web/lib/acctfuncs.inc.php index 7ea423e..32c9815 100644 --- a/web/lib/acctfuncs.inc.php +++ b/web/lib/acctfuncs.inc.php @@ -779,3 +779,107 @@ function own_account_details($sid, $dbh=NULL) { return $row; } + +function tu_voted($voteid, $uid, $dbh=NULL) { + if (!$dbh) { + $dbh = db_connect(); + } + + $q = "SELECT * FROM TU_Votes WHERE VoteID = " . intval($voteid) . " AND UserID = " . intval($uid); + $result = db_query($q, $dbh); + if (mysql_num_rows($result)) { + return true; + } + else { + return false; + } +} + +function current_proposal_list($order, $dbh=NULL) { + if (!$dbh) { + $dbh = db_connect(); + } + + $q = "SELECT * FROM TU_VoteInfo WHERE End > " . time() . " ORDER BY Submitted " . $order; + $result = db_query($q, $dbh); + + while ($row = mysql_fetch_assoc($result)) { + $details[] = $row; + } + + return $details; +} + +function past_proposal_list($order, $lim, $dbh=NULL) { + if (!$dbh) { + $dbh = db_connect(); + } + + $q = "SELECT * FROM TU_VoteInfo WHERE End < " . time() . " ORDER BY Submitted " . $order . $lim; + $result = db_query($q, $dbh); + + while ($row = mysql_fetch_assoc($result)) { + $details[] = $row; + } + + return $details; +} + +function proposal_count($dbh=NULL) { + if (!$dbh) { + $dbh = db_connect(); + } + + $q = "SELECT COUNT(*) FROM TU_VoteInfo"; + $result = db_query($q, $dbh); + $row = mysql_fetch_row($result); + + return $row[0]; +} + +function vote_details($voteid, $dbh=NULL) { + if (!$dbh) { + $dbh = db_connect(); + } + + $q = "SELECT * FROM TU_VoteInfo "; + $q.= "WHERE ID = " . intval($voteid); + + $result = db_query($q, $dbh); + $row = mysql_fetch_assoc($result); + + return $row; +} + +function voter_list($voteid, $dbh=NULL) { + if (!$dbh) { + $dbh = db_connect(); + } + + $q = "SELECT tv.UserID,U.Username "; + $q.= "FROM TU_Votes tv, Users U "; + $q.= "WHERE tv.VoteID = " . intval($voteid); + $q.= " AND tv.UserID = U.ID "; + $q.= "ORDER BY Username"; + + $result = db_query($q, $dbh); + if ($result) { + while ($row = mysql_fetch_assoc($result)) { + $whovoted.= ''.$row['Username'].' '; + } + } + return $whovoted; +} + +function cast_proposal_vote($voteid, $uid, $vote, $newtotal, $dbh=NULL) { + if (!$dbh) { + $dbh = db_connect(); + } + + $q = "UPDATE TU_VoteInfo SET " . $vote . " = " . ($newtotal) . " WHERE ID = " . $voteid; + db_query($q, $dbh); + + $q = "INSERT INTO TU_Votes (VoteID, UserID) VALUES (" . $voteid . ", " . $uid . ")"; + db_query($q, $dbh); + +} diff --git a/web/template/tu_list.php b/web/template/tu_list.php index d8acd71..ce19da3 100644 --- a/web/template/tu_list.php +++ b/web/template/tu_list.php @@ -14,9 +14,15 @@ - + - + @@ -34,23 +40,15 @@ - - - + + + -- cgit v1.2.3-54-g00ecf