api.pollsreport.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. /**
  3. * Polls/votes report class
  4. */
  5. class PollsReport extends Polls {
  6. /**
  7. * returns all addres
  8. *
  9. * @var array as login => full adress
  10. */
  11. protected $alladdress = array();
  12. const URL_REPORT = '?module=report_polls';
  13. public function __construct() {
  14. $this->initMessages();
  15. $this->initCache();
  16. $this->setPollId();
  17. $this->loadAdminsName();
  18. $this->getFulladdress();
  19. $this->loadAvaiblePollsCached();
  20. $this->loadPollsOptionsCached();
  21. $this->loadPollsVotesCached();
  22. }
  23. /**
  24. * Loads full address list from cache
  25. *
  26. * @return void
  27. */
  28. protected function getFulladdress() {
  29. $this->alladdress = zb_AddressGetFulladdresslistCached();
  30. }
  31. /**
  32. * Loads the number of all users
  33. *
  34. * @return string
  35. */
  36. protected function getNumberAllUsers() {
  37. $result = count($this->alladdress);
  38. return ($result);
  39. }
  40. /**
  41. * Draw 3DPie about poll votes
  42. *
  43. * @return void
  44. */
  45. protected function draw3DPie($poll_id) {
  46. // Create parametr for Number of voters
  47. $params_users = array();
  48. ($this->pollsAvaible[$poll_id]['voting'] == 'Users') ? $params_users[__('All users')] = $this->getNumberAllUsers() : $params_users[__('Total existing employees')] = count($this->adminsName);
  49. $params_users[__('Voted Users')] = $this->pollsVotesCount[$poll_id];
  50. // Create parametr for votes
  51. $params_votes = array();
  52. foreach ($this->pollsVotes[$poll_id]['option_id'] as $login => $value) {
  53. $string = $this->pollsOptions[$poll_id][$value];
  54. $count = $this->pollsOptionVotesCount[$poll_id][$value];
  55. $params_votes[$string] = $count;
  56. }
  57. $chartOptsUsers = "chartArea: { width: '90%', height: '90%' }, legend : {position: 'right'}, ";
  58. $chartUsers = wf_gcharts3DPie($params_users, __('Number of voted users'), '400px', '400px', $chartOptsUsers);
  59. $chartOptsVotes = "chartArea: { width: '90%', height: '90%' }, legend : {position: 'right'}, pieSliceText: 'value-and-percentage', ";
  60. $chartVotes= wf_gcharts3DPie($params_votes, __('Number of votes'), '400px', '400px', $chartOptsVotes);
  61. $cells = wf_TableCell($chartUsers);
  62. $cells.= wf_TableCell($chartVotes);
  63. $rows = wf_TableRow($cells);
  64. $votes_3D = wf_TableBody($rows, '100%', 0, '');
  65. $result = show_window(__('Number of votes on a 3D chart'), $votes_3D);
  66. return ($result);
  67. }
  68. /**
  69. * Returns polls search form
  70. *
  71. * @return string
  72. */
  73. protected function renderPollsSearchForm() {
  74. $param_selector_status = array(
  75. '',
  76. 'disabled' => __('Disabled'),
  77. 'nostarted' => __('Not yet started'),
  78. 'finished' => __('Finished'),
  79. 'progress' => __('Poll in progress'),
  80. );
  81. $param_selector_polls = array('');
  82. foreach ($this->pollsOptions as $poll_id => $poll_opt) {
  83. $param_selector_polls[$poll_id] = $this->pollsAvaible[$poll_id]['title'];
  84. }
  85. $param_can_voting = array('Users' => __('Users'), 'Employee' => __('Employee'));
  86. $cells = wf_TableCell(__('Poll'));
  87. $cells.= wf_TableCell(wf_RadioInput('polls_search[search_by]', '', 'poll_id', false));
  88. $cells.= wf_TableCell(wf_Selector('polls_search[poll_id]', $param_selector_polls, '', $this->poll_id, false));
  89. $rows = wf_TableRow($cells, 'row2');
  90. $cells = wf_TableCell(__('Date'));
  91. $cells.= wf_TableCell(wf_RadioInput('polls_search[search_by]', '', 'date', false));
  92. $cells.= wf_TableCell(wf_DatePickerPreset('polls_search[date][start_date]', '') . ' ' . __('From') . wf_DatePickerPreset('polls_search[date][end_date]', '') . ' ' . __('To'));
  93. $rows.= wf_TableRow($cells, 'row2');
  94. $cells = wf_TableCell(__('Status'));
  95. $cells.= wf_TableCell(wf_RadioInput('polls_search[search_by]', '', 'status', false, false));
  96. $cells.= wf_TableCell(wf_Selector('polls_search[status]', $param_selector_status, '', '', false));
  97. $rows.= wf_TableRow($cells, 'row2');
  98. $cells = wf_TableCell(__('Voting'));
  99. $cells.= wf_TableCell(wf_RadioInput('polls_search[search_by]', '', 'voting', false, false));
  100. $cells.= wf_TableCell(wf_Selector('polls_search[voting]', $param_can_voting, '', '', false));
  101. $rows.= wf_TableRow($cells, 'row2');
  102. $rows.= wf_TableRow(wf_TableCell(wf_Submit('Search')));
  103. $form = wf_TableBody($rows, '', 0);
  104. $result = show_window(__('Search polls'), wf_Form("", "POST", $form, 'glamour'));
  105. return ($result);
  106. }
  107. /**
  108. * Loads the number of all users
  109. *
  110. * @return string
  111. */
  112. protected function searchPollsOptions($search_data) {
  113. $result = array();
  114. $where = '';
  115. $query = "SELECT `polls`.`id` AS `polls_id`,`polls_options`.`id`,`polls_options`.`text` FROM `polls_options` LEFT JOIN `polls` ON (`polls_options`.`poll_id` = `polls`.`id`) "; // On lust must be space
  116. if ($search_data['search_by'] == 'poll_id') {
  117. $where = " WHERE `polls`.`id` = '" . $search_data['poll_id'] . "'";
  118. }
  119. if ($search_data['search_by'] == 'date') {
  120. if ($search_data['date']['start_date'] AND $search_data['date']['end_date']) {
  121. $where = " WHERE `start_date` > '" . $search_data['date']['start_date']. "' AND `end_date` < '" . $search_data['date']['end_date']. "'";
  122. } elseif ($search_data['date']['start_date'] AND ! $search_data['date']['end_date']) {
  123. $where = " WHERE `start_date` > '" . $search_data['date']['start_date']. "'";
  124. } elseif (! $search_data['date']['start_date'] AND $search_data['date']['end_date']) {
  125. $where = " WHERE `end_date` < '" . $search_data['date']['end_date']. "'";
  126. }
  127. }
  128. if ($search_data['search_by'] == 'status') {
  129. if ($search_data['status'] == 'disabled') {
  130. $where = " WHERE `polls`.`enabled` = '0' AND `end_date` > '" . date("Y-m-d H:i:s"). "'";
  131. } elseif ($search_data['status'] == 'nostarted') {
  132. $where = " WHERE `polls`.`enabled` = '1' AND `start_date` > '" . date("Y-m-d H:i:s"). "'";
  133. } elseif ($search_data['status'] == 'finished') {
  134. $where = " WHERE `end_date` < '" . date("Y-m-d H:i:s"). "'";
  135. } elseif ($search_data['status'] == 'progress') {
  136. $where = " WHERE `polls`.`enabled` = '1' AND `start_date` < '" . date("Y-m-d H:i:s"). "' AND `end_date` > '" . date("Y-m-d H:i:s"). "'";
  137. }
  138. }
  139. if ($search_data['search_by'] == 'voting') {
  140. $where = " WHERE `polls`.`voting` = '" . $search_data['voting'] . "'";
  141. }
  142. if ($where) {
  143. $get_result = simple_queryall($query . $where . " ORDER BY `polls_id` ASC");
  144. if ($get_result){
  145. foreach ($get_result as $data) {
  146. $result[$data['polls_id']][$data['id']] = $data['text'];
  147. }
  148. }
  149. }
  150. return ($result);
  151. }
  152. /**
  153. * Renders polls module control panel
  154. *
  155. * @return void
  156. */
  157. public function panel() {
  158. $result = '';
  159. // Add backlink
  160. if (wf_CheckGet(array('action')) OR wf_CheckGet(array('show_votes')) OR wf_CheckPost(array('polls_search'))) {
  161. $result.= wf_BackLink(self::URL_REPORT);
  162. }
  163. if (cfr('POLLS')) {
  164. $result.= wf_Link(self::URL_ME, wf_img('skins/icon_star.gif') . ' ' . __('Show polls'), true, 'ubButton') . ' ';
  165. }
  166. return ($result);
  167. }
  168. /**
  169. * Renders polls module control panel interface
  170. *
  171. * @return string
  172. */
  173. public function renderPollsSearchVotes(array $search_data) {
  174. $result = '';
  175. if ( ! empty($search_data)) {
  176. // Check for empty search value
  177. if (! isset($search_data['search_by'])) {
  178. $result.= show_window('', $this->messages->getStyledMessage(__('You did not select the search parameter'), 'warning'));
  179. } else {
  180. $search_results = $this->searchPollsOptions($search_data);
  181. if ( ! empty($search_results)) {
  182. foreach ($search_results as $poll_id => $poll_opt) {
  183. $window = __('ID') . ': ' . $poll_id;
  184. $window.= ', ' . __('Title') . ': ' . $this->pollsAvaible[$poll_id]['title'];
  185. $window.= ', ' . __('Status') . ': ' . $this->renderPollStatus($poll_id);
  186. $window.= ', ' . __('Voting') . ': ' . __($this->pollsAvaible[$poll_id]['voting']);
  187. $window.= ', ' . __('Actions') . ': ' . wf_Link(self::URL_REPORT . '&action=show_poll_votes&poll_id=' . $poll_id, web_stats_icon('View poll results'));
  188. $columns = array('ID', 'Options', 'Number of votes', 'Visual', 'Actions');
  189. $opts = '"order": [[ 0, "asc" ]]';
  190. $loader = wf_JqDtLoader($columns, self::URL_REPORT . '&ajaxavaiblevotes=true&poll_id=' . $poll_id, false, 'Option', 100, $opts);
  191. $result.= show_window($window, $loader);
  192. }
  193. } else {
  194. $result.= show_window('', $this->messages->getStyledMessage(__('Empty reply received'), 'warning'));
  195. }
  196. }
  197. }
  198. return ($result);
  199. }
  200. /**
  201. * Renders polls module control panel interface
  202. *
  203. * @return string
  204. */
  205. public function renderOptionVotes() {
  206. $result = '';
  207. $opt_arr = array();
  208. $opt_name = '';
  209. if (isset($this->pollsAvaible[$this->poll_id])) {
  210. $result.= $this->renderPollData($this->poll_id);
  211. $cells = wf_TableCell(__('ID'));
  212. $cells.= wf_TableCell(__('Date'));
  213. $cells.= wf_TableCell(__('Login'));
  214. ($this->pollsAvaible[$this->poll_id]['voting'] == 'Users') ? $cells.= wf_TableCell(__('Address')) : '';
  215. $rows = wf_TableRow($cells, 'row2');
  216. // Check that we get option_id and array optionVotes have this option
  217. if (wf_CheckGet(array('option_id'))) {
  218. $option_id = vf($_GET['option_id']);
  219. if (isset($this->pollsOptions[$this->poll_id][$option_id])) {
  220. if (isset($this->pollsVotes[$this->poll_id])) {
  221. $opt_arr = array_keys($this->pollsVotes[$this->poll_id]['option_id'], $option_id);
  222. if ( ! empty($opt_arr) ) {
  223. foreach ($opt_arr as $login) {
  224. $cells = wf_TableCell($this->pollsVotes[$this->poll_id]['id'][$login]);
  225. $cells.= wf_TableCell($this->pollsVotes[$this->poll_id]['date'][$login]);
  226. if ($this->pollsAvaible[$this->poll_id]['voting'] == 'Users') {
  227. $cells.= wf_TableCell(wf_Link('?module=userprofile&username=' . $login, $login, false));
  228. $address = (isset($this->alladdress[$login])) ? $this->alladdress[$login] : '';
  229. $cells.= wf_TableCell($address);
  230. } else {
  231. $cells.= wf_TableCell($this->initAdminName($login));
  232. }
  233. $rows.= wf_TableRow($cells, 'row3');
  234. }
  235. $opt_name = $this->pollsOptions[$this->poll_id][$option_id];
  236. $table = wf_TableBody($rows, '100%', 0);
  237. $result.= show_window(__('Voting results for option') . ': ' . $opt_name, $table);
  238. } else {
  239. $result.= show_window('', $this->messages->getStyledMessage(__('For this option, no one voted yet'), 'info'));
  240. }
  241. } else {
  242. $result.= show_window('', $this->messages->getStyledMessage(__('No one voted on this poll yet'), 'info'));
  243. }
  244. } else {
  245. $result.= show_window('', $this->messages->getStyledMessage(__('The answer option passed to the module does not exist'), 'error'));
  246. }
  247. } else {
  248. $result.= show_window('', $this->messages->getStyledMessage(__('The module was not given the answer option'), 'error'));
  249. }
  250. } else {
  251. $result.= show_window('', $this->messages->getStyledMessage(__('This poll does not exist'), 'error'));
  252. }
  253. return ($result);
  254. }
  255. /**
  256. * Renders polls module control panel interface
  257. *
  258. * @return string
  259. */
  260. public function renderAvaibleVotes() {
  261. $result = '';
  262. // Render search poll form
  263. $result.= $this->renderPollsSearchForm();
  264. if (wf_CheckPost(array('polls_search'))) {
  265. $result.= $this->renderPollsSearchVotes($_POST['polls_search']);
  266. } else {
  267. foreach (array_reverse($this->pollsOptions, TRUE) as $poll_id => $poll_opt) {
  268. $window = __('ID') . ': ' . $poll_id;
  269. $window.= ', ' . __('Title') . ': ' . $this->pollsAvaible[$poll_id]['title'];
  270. $window.= ', ' . __('Status') . ': ' . $this->renderPollStatus($poll_id);
  271. $window.= ', ' . __('Voting') . ': ' . __($this->pollsAvaible[$poll_id]['voting']);
  272. $window.= ', ' . __('Actions') . ': ' . wf_Link(self::URL_REPORT . '&action=show_poll_votes&poll_id=' . $poll_id, web_stats_icon('View poll results'));
  273. $columns = array('ID', 'Options', 'Number of votes', 'Visual', 'Actions');
  274. $opts = '"order": [[ 0, "asc" ]]';
  275. $loader = wf_JqDtLoader($columns, self::URL_REPORT . '&ajaxavaiblevotes=true&poll_id=' . $poll_id, false, 'Option', 100, $opts);
  276. $result.= show_window($window, $loader);
  277. }
  278. }
  279. return ($result);
  280. }
  281. /**
  282. * Renders json formatted data about Polls
  283. *
  284. * @return void
  285. */
  286. public function ajaxAvaibleVotes() {
  287. $json = new wf_JqDtHelper();
  288. if(isset($this->pollsAvaible[$this->poll_id])) {
  289. foreach ($this->pollsOptions[$this->poll_id] as $id => $options) {
  290. if (isset($this->pollsOptionVotesCount[$this->poll_id][$id])) {
  291. $votes = $this->pollsOptionVotesCount[$this->poll_id][$id];
  292. $act = ' ' . wf_Link(self::URL_REPORT . '&action=show_option_votes&poll_id=' . $this->poll_id . '&option_id=' . $id, web_icon_search(__('Show voters')));
  293. } else {
  294. $votes = 0;
  295. $act = '';
  296. }
  297. $total_votes = (isset($this->pollsVotesCount[$this->poll_id])) ? $this->pollsVotesCount[$this->poll_id] : 0;
  298. $data[] = $id;
  299. $data[] = $options;
  300. $data[] = $votes;
  301. $data[] = web_bar($votes, $total_votes);
  302. $data[] = $act;
  303. $json->addRow($data);
  304. unset($data);
  305. }
  306. }
  307. $json->getJson();
  308. }
  309. /**
  310. * Renders polls votes control panel
  311. *
  312. * @return string
  313. */
  314. public function renderPollVotes() {
  315. $result = '';
  316. if(isset($this->pollsAvaible[$this->poll_id])) {
  317. if (isset($this->pollsVotes[$this->poll_id])) {
  318. $result.= $this->renderPollData($this->poll_id);
  319. $result.= $this->draw3DPie($this->poll_id);
  320. $columns = ($this->pollsAvaible[$this->poll_id]['voting'] == 'Users') ? array('ID', 'Option', 'Date', 'User', 'Address') : array('ID', 'Option', 'Date', 'Worker');
  321. $opts = '"order": [[ 0, "desc" ]]';
  322. $result = show_window(__('Poll results'), wf_JqDtLoader($columns, self::URL_REPORT . '&ajaxapollvotes=true&poll_id=' . $this->poll_id, false, 'votes', 100, $opts));
  323. } else {
  324. $result.= show_window('', $this->messages->getStyledMessage(__('No one voted on this poll yet'), 'info'));
  325. }
  326. } else {
  327. $result.= show_window('', $this->messages->getStyledMessage(__('This poll does not exist'), 'error'));
  328. }
  329. return ($result);
  330. }
  331. /**
  332. * Renders json formatted data about Polls votes
  333. *
  334. * @return void
  335. */
  336. public function ajaxPollVotes() {
  337. $json = new wf_JqDtHelper();
  338. if(isset($this->pollsAvaible[$this->poll_id])) {
  339. foreach ($this->pollsVotes[$this->poll_id]['id'] as $login => $value) {
  340. $data[] = $value;
  341. $data[] = $this->pollsOptions[$this->poll_id][$this->pollsVotes[$this->poll_id]['option_id'][$login]];
  342. $data[] = $this->pollsVotes[$this->poll_id]['date'][$login];
  343. if ($this->pollsAvaible[$this->poll_id]['voting'] == 'Users') {
  344. $data[] = wf_Link('?module=userprofile&username=' . $login, $login, false);
  345. $address = (isset($this->alladdress[$login])) ? $this->alladdress[$login] : '';
  346. $data[] = $address;
  347. } else {
  348. $data[] = $this->initAdminName($login);
  349. }
  350. $json->addRow($data);
  351. unset($data);
  352. }
  353. }
  354. $json->getJson();
  355. }
  356. }
  357. ?>