/Vote/Model/Vote.php

https://bitbucket.org/brainbox/shared · PHP · 270 lines · 139 code · 123 blank · 8 comment · 3 complexity · 37fd42585ddb38936e90375b2f02bac2 MD5 · raw file

  1. <?php
  2. Class Model_Vote{
  3. public static function checkEligibility($dbPDO, $voteId, $user){
  4. $memberId = $user->userId;
  5. $sql = "SELECT
  6. count(*)
  7. FROM
  8. vote_member_xref
  9. WHERE
  10. 1=1
  11. AND vote_id = $voteId
  12. AND member_id= $memberId";
  13. //echo $sql;
  14. $stmnt = $dbPDO->prepare($sql);
  15. $stmnt->execute();
  16. $result = $stmnt->fetch();
  17. if($result['count(*)']){
  18. return false; //already voted
  19. } else {
  20. return true; //not yet voted.
  21. }
  22. }
  23. public function incrementCount($dbPDO, $voteId, $response){
  24. if($response=='yes'){
  25. $sql = "UPDATE
  26. vote
  27. SET
  28. yes_count = yes_count+1
  29. WHERE
  30. id = $voteId";
  31. } elseif ($response=='no'){
  32. $sql = "UPDATE
  33. vote
  34. SET
  35. no_count = no_count+1
  36. WHERE
  37. id = $voteId";
  38. }
  39. //echo $sql;
  40. $stmnt = $dbPDO->prepare($sql);
  41. $stmnt->execute();
  42. }
  43. public function recordVote($dbPDO, $voteId, $user){
  44. $memberId = $user->userId;
  45. $sql = "INSERT INTO
  46. vote_member_xref (vote_id, member_id, the_time)
  47. VALUES
  48. ( $voteId , $memberId , NOW() )
  49. ";
  50. //echo $sql;
  51. $dbPDO->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  52. $stmnt = $dbPDO->prepare($sql);
  53. try {
  54. $stmnt->execute();
  55. } catch (PDOException $e) {
  56. //Typically an integrity violation from multiple voting
  57. //This is an exception, because it shouldn't really happen
  58. print '<p>Vote already registered. Please <a href="/">click here</a>
  59. to return to the Home Page.</p>';
  60. die();
  61. //return false;
  62. }
  63. return true;
  64. }
  65. public function getVoters($dbPDO, $voteId){
  66. $sql = "SELECT
  67. u.user_id as id
  68. , u.login as email
  69. , u.firstName AS forename
  70. , u.lastName AS surname
  71. FROM
  72. user_new AS u
  73. LEFT OUTER JOIN vote_member_xref
  74. ON (u.user_id = vote_member_xref.member_id)
  75. AND vote_member_xref.vote_id = $voteId -- WOW never done that before
  76. -- http://searchoracle.techtarget.com/answer/OUTER-JOIN-instead-of-a-NOT-EXISTS-subquery
  77. WHERE
  78. 1=1
  79. AND u.active = '1'
  80. AND vote_member_xref.member_id IS NULL -- want the unmatched ones only
  81. ";
  82. $stmnt = $dbPDO->prepare($sql);
  83. $stmnt->execute();
  84. return $stmnt->fetchAll();
  85. }
  86. public function getVotersASLTIP($dbPDO, $voteId){
  87. $sql = "SELECT
  88. u.id
  89. , u.email
  90. , u.forename
  91. , u.surname
  92. FROM
  93. member AS u
  94. LEFT OUTER JOIN vote_member_xref
  95. ON (u.id = vote_member_xref.member_id)
  96. AND vote_member_xref.vote_id = $voteId -- WOW never done that before
  97. -- http://searchoracle.techtarget.com/answer/OUTER-JOIN-instead-of-a-NOT-EXISTS-subquery
  98. WHERE
  99. 1=1
  100. AND u.active = '1'
  101. AND u.email IS NOT NULL
  102. AND vote_member_xref.member_id IS NULL -- want the unmatched ones only
  103. ";
  104. $stmnt = $dbPDO->prepare($sql);
  105. $stmnt->execute();
  106. return $stmnt->fetchAll();
  107. }
  108. function updateFromPOST($dbPDO, $id){
  109. $sql = "UPDATE
  110. vote
  111. SET
  112. email_subject = :email_subject
  113. , question = :question
  114. , start_date = :start_date
  115. , end_date = DATE_ADD(:start_date, INTERVAL :weeks*7 DAY)
  116. , is_published = :is_published
  117. , background = :background
  118. , banner_text = :banner_text
  119. WHERE
  120. id = $id";
  121. $stmnt=$dbPDO->prepare($sql);
  122. $stmnt->bindParam(':email_subject', $_POST['email_subject'] );
  123. $stmnt->bindParam(':question', $_POST['question'] );
  124. $stmnt->bindParam(':is_published', $_POST['is_published'] );
  125. $stmnt->bindParam(':start_date', $_POST['start_date']);
  126. $stmnt->bindParam(':weeks', $_POST['weeks']);
  127. $stmnt->bindParam(':background', $_POST['background']);
  128. $stmnt->bindParam(':banner_text', $_POST['banner_text']);
  129. $stmnt->execute();
  130. //var_dump($dbPDO->errorInfo() );
  131. }
  132. function createNew($dbPDO){
  133. //Find Next Tuesday
  134. $startDate = date('Y-m-d', strtotime('next Tuesday') );
  135. $endDate = date('Y-m-d', strtotime('third Tuesday') );
  136. $sql = "INSERT INTO
  137. vote (question, start_date, end_date)
  138. VALUES
  139. (
  140. '******* NEW ********'
  141. , '$startDate'
  142. , '$endDate'
  143. )";
  144. $stmnt=$dbPDO->prepare($sql);
  145. $stmnt->execute();
  146. }
  147. }