api.polls.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. class Polls {
  3. protected $userLogin = '';
  4. public function __construct($login) {
  5. $this->setLogin($login);
  6. }
  7. /**
  8. * gets current user login
  9. *
  10. * @return string
  11. */
  12. protected function setLogin($login) {
  13. $this->userLogin = $login;
  14. }
  15. /**
  16. * gets poll that user not voted yet
  17. *
  18. * @return string
  19. */
  20. protected function loadPollForVoiting() {
  21. $result = '';
  22. $date = date("Y-m-d H:i:s");
  23. $query = "SELECT `id` FROM `polls`WHERE id NOT IN (SELECT poll_id FROM `polls_votes` "
  24. . "WHERE `login` = '" . $this->userLogin . "') "
  25. . "AND `enabled` = '1' "
  26. . "AND `start_date` <= '" . $date . "' "
  27. . "AND `end_date` >= '" . $date . "' "
  28. . "AND `voting` = 'Users' "
  29. . "LIMIT 1";
  30. $result_q = simple_query($query);
  31. if ($result_q) {
  32. $result = $result_q['id'];
  33. }
  34. return ($result);
  35. }
  36. /**
  37. * Load poll data
  38. *
  39. * @param type $poll_id
  40. * @return type
  41. */
  42. protected function getPollData($poll_id) {
  43. $result = array();
  44. $query = "SELECT * FROM `polls` WHERE `id` = '" . $poll_id . "'";
  45. $poll_data = simple_queryall($query);
  46. if ($poll_data) {
  47. foreach ($poll_data as $value) {
  48. $result['title'] = $value['title'];
  49. $result['start_date'] = $value['start_date'];
  50. $result['end_date'] = $value['end_date'];
  51. $result['id'] = $value['id'];
  52. }
  53. }
  54. return ($result);
  55. }
  56. /**
  57. * gets poll options
  58. *
  59. * @return string
  60. */
  61. protected function loadPollOptoins($avaible_poll) {
  62. $result = array();
  63. $query = "SELECT `polls_options`.`id`,`poll_id`,`text` FROM `polls_options`
  64. LEFT JOIN `polls` ON (`polls_options`.`poll_id` = `polls`.`id`)
  65. WHERE `polls`.`id` = '" . $avaible_poll . "' ORDER BY `polls_options`.`id`";
  66. $options = simple_queryall($query);
  67. if ($options) {
  68. foreach ($options as $value) {
  69. $result[$value['poll_id']][$value['id']] = $value['text'];
  70. }
  71. }
  72. return ($result);
  73. }
  74. /**
  75. * Load users votes
  76. *
  77. * @return string
  78. */
  79. protected function loadUserVotes() {
  80. $result = array();
  81. $query = "SELECT `title`,`start_date`,`end_date`,`text`,`date`
  82. FROM `polls_votes`
  83. LEFT JOIN `polls` ON (`polls_votes`.`poll_id` = `polls`.`id`)
  84. LEFT JOIN `polls_options` ON (`polls_votes`.`option_id` = `polls_options`.`id`)
  85. WHERE `login` = '" . $this->userLogin . "'";
  86. $result = simple_queryall($query);
  87. return ($result);
  88. }
  89. /**
  90. * Renders Poll voiting form
  91. *
  92. * @return string
  93. */
  94. public function renderVotingForm() {
  95. $result = '';
  96. $avaible_poll = $this->loadPollForVoiting();
  97. if ($avaible_poll) {
  98. $option_data = $this->loadPollOptoins($avaible_poll);
  99. if ($option_data) {
  100. $inputs = '';
  101. $poll_data = $this->getPollData($avaible_poll);
  102. foreach ($option_data[$avaible_poll] as $id => $option) {
  103. $inputs.= la_RadioInput('vote', $option, $id, true);
  104. }
  105. $inputs.= la_HiddenInput('poll_id', $avaible_poll);
  106. $inputs.= la_tag('br');
  107. $inputs.= la_Submit('Vote');
  108. $form = la_Form("", "POST", $inputs, 'glamour');
  109. $result = la_modalOpened($poll_data['title'], $form);
  110. }
  111. }
  112. return ($result);
  113. }
  114. /**
  115. * Add user's vote to the database
  116. *
  117. * @param type $option_id, $poll_id
  118. */
  119. public function createUserVoteOnDB($option_id, $poll_id) {
  120. $check_query = "SELECT 1 FROM `polls_options`
  121. LEFT JOIN `polls` ON (`polls_options`.`poll_id` = `polls`.`id`)
  122. WHERE `poll_id` NOT IN (SELECT `poll_id` FROM `polls_votes` WHERE `login` = '" . $this->userLogin . "')
  123. AND `polls_options`.`id` = '" . $option_id . "'
  124. AND `polls`.`id` = '" . $poll_id . "'";
  125. $check_result = simple_query($check_query);
  126. if ($check_result) {
  127. $date = date("Y-m-d H:i:s");
  128. $query = "INSERT INTO `polls_votes` (`id`, `date`, `option_id`, `poll_id`, `login`)
  129. VALUES (NULL, '" . $date . "', '" . $option_id . "', '" . $poll_id . "', '" . $this->userLogin . "');";
  130. nr_query($query);
  131. }
  132. }
  133. /**
  134. * Load user votes
  135. *
  136. * @return void
  137. */
  138. public function renderUserVotes() {
  139. $result = '';
  140. $votes_data = $this->loadUserVotes();
  141. if ($votes_data) {
  142. $cells = la_TableCell(__('Title'));
  143. $cells.= la_TableCell(__('Start date poll'));
  144. $cells.= la_TableCell(__('End date poll'));
  145. $cells.= la_TableCell(__('Answer'));
  146. $cells.= la_TableCell(__('Voting date'));
  147. $rows = la_TableRow($cells, 'row1');
  148. foreach ($votes_data as $value) {
  149. $cells = la_TableCell($value['title']);
  150. $cells.= la_TableCell($value['start_date']);
  151. $cells.= la_TableCell($value['end_date']);
  152. $cells.= la_TableCell($value['text']);
  153. $cells.= la_TableCell($value['date']);
  154. $rows.= la_TableRow($cells, 'row2');
  155. }
  156. $result = la_TableBody($rows, '100%', '');
  157. } else {
  158. $result = __('You have not yet responded to polls');
  159. }
  160. return ($result);
  161. }
  162. }
  163. ?>