api.pollvoteadmin.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Administrators Votes/Polls class
  4. */
  5. class PollVoteAdmin {
  6. /**
  7. * Contains current user login
  8. *
  9. * @var string
  10. */
  11. protected $myLogin = '';
  12. public function __construct() {
  13. $this->setLogin();
  14. $this->checkBaseAvail();
  15. }
  16. /**
  17. * Sets current user login
  18. *
  19. * @return void
  20. */
  21. protected function setLogin() {
  22. $this->myLogin = whoami();
  23. }
  24. /**
  25. * Must prevent update troubles and make billing usable between 0.8.5 and 0.8.6 releases
  26. *
  27. * @return bool
  28. */
  29. protected function checkBaseAvail() {
  30. if (version_compare(file_get_contents("RELEASE"), "0.8.5", ">")) {
  31. $query_check = "SHOW COLUMNS FROM `polls` WHERE FIELD = 'voting'";
  32. $result_check = simple_query($query_check);
  33. if (! $result_check) {
  34. $query = "ALTER TABLE `polls` ADD `voting` VARCHAR(255) NOT NULL DEFAULT 'Users'";
  35. nr_query($query);
  36. }
  37. }
  38. }
  39. /**
  40. * gets poll that user not voted yet
  41. *
  42. * @return string
  43. */
  44. protected function loadPollForVoiting() {
  45. $result = '';
  46. $date = date("Y-m-d H:i:s");
  47. $query = "SELECT `id` FROM `polls`WHERE id NOT IN (SELECT poll_id FROM `polls_votes` "
  48. . "WHERE `login` = '" .$this->myLogin . "') "
  49. . "AND `enabled` = '1' "
  50. . "AND `start_date` <= '" . $date . "' "
  51. . "AND `end_date` >= '" . $date . "' "
  52. . "AND `voting` = 'Employee' "
  53. . "LIMIT 1";
  54. $result_q = simple_query($query);
  55. if ($result_q) {
  56. $result = $result_q['id'];
  57. }
  58. return ($result);
  59. }
  60. /**
  61. * Load poll data
  62. *
  63. * @param type $poll_id
  64. * @return type
  65. */
  66. protected function getPollData($poll_id) {
  67. $result = array();
  68. $query = "SELECT * FROM `polls` WHERE `id` = '" . $poll_id . "'";
  69. $poll_data = simple_queryall($query);
  70. if ($poll_data) {
  71. foreach ($poll_data as $value) {
  72. $result['title'] = $value['title'];
  73. $result['start_date'] = $value['start_date'];
  74. $result['end_date'] = $value['end_date'];
  75. $result['id'] = $value['id'];
  76. }
  77. }
  78. return ($result);
  79. }
  80. /**
  81. * gets poll options
  82. *
  83. * @return string
  84. */
  85. protected function loadPollOptoins($avaible_poll) {
  86. $result = array();
  87. $query = "SELECT `polls_options`.`id`,`poll_id`,`text` FROM `polls_options`
  88. LEFT JOIN `polls` ON (`polls_options`.`poll_id` = `polls`.`id`)
  89. WHERE `polls`.`id` = '" . $avaible_poll . "' ORDER BY `polls_options`.`id`";
  90. $options = simple_queryall($query);
  91. if ($options) {
  92. foreach ($options as $value) {
  93. $result[$value['poll_id']][$value['id']] = $value['text'];
  94. }
  95. }
  96. return ($result);
  97. }
  98. /**
  99. * Renders Poll voiting form
  100. *
  101. * @return string
  102. */
  103. public function renderVotingForm() {
  104. $result = '';
  105. $avaible_poll = $this->loadPollForVoiting();
  106. if ($avaible_poll) {
  107. $option_data = $this->loadPollOptoins($avaible_poll);
  108. if ($option_data) {
  109. $inputs = '';
  110. $poll_data = $this->getPollData($avaible_poll);
  111. foreach ($option_data[$avaible_poll] as $id => $option) {
  112. $inputs.= wf_RadioInput('vote', $option, $id, true);
  113. }
  114. $inputs.= wf_HiddenInput('poll_id', $avaible_poll);
  115. $inputs.= wf_tag('br');
  116. $inputs.= wf_Submit('Vote');
  117. $form = wf_Form("", "POST", $inputs, 'glamour');
  118. $result = wf_modalOpened($poll_data['title'], $form, '600', '400');
  119. }
  120. }
  121. return ($result);
  122. }
  123. /**
  124. * Add user's vote to the database
  125. *
  126. * @param type $option_id, $poll_id
  127. */
  128. public function createAdminVoteOnDB($option_id, $poll_id) {
  129. $check_query = "SELECT 1 FROM `polls_options`
  130. LEFT JOIN `polls` ON (`polls_options`.`poll_id` = `polls`.`id`)
  131. WHERE `poll_id` NOT IN (SELECT `poll_id` FROM `polls_votes` WHERE `login` = '" . $this->myLogin . "')
  132. AND `polls_options`.`id` = '" . $option_id . "'
  133. AND `polls`.`id` = '" . $poll_id . "'";
  134. $check_result = simple_query($check_query);
  135. if ($check_result) {
  136. $date = date("Y-m-d H:i:s");
  137. $query = "INSERT INTO `polls_votes` (`id`, `date`, `option_id`, `poll_id`, `login`)
  138. VALUES (NULL, '" . $date . "', '" . $option_id . "', '" . $poll_id . "', '" . $this->myLogin . "');";
  139. nr_query($query);
  140. }
  141. }
  142. }
  143. ?>