api.polls.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. <?php
  2. /**
  3. * Polls/Votes management here
  4. */
  5. class Polls {
  6. /**
  7. * Contains current user login
  8. *
  9. * @var string
  10. */
  11. protected $myLogin = '';
  12. /**
  13. * Contains Poll ID from $_GET
  14. *
  15. * @var string
  16. */
  17. protected $poll_id = '';
  18. /**
  19. * Contains admns Name as admin_login => admin_name
  20. *
  21. * @var array
  22. */
  23. protected $adminsName = array();
  24. /**
  25. * Contains all polls as id => array (title, enabled, start_date, end_date, params, admin, voting)
  26. *
  27. * @var array
  28. */
  29. protected $pollsAvaible = array();
  30. /**
  31. * Contains all polls options as poll_id => array (id => [text])
  32. *
  33. * @var array
  34. */
  35. protected $pollsOptions = array();
  36. /**
  37. * Contains poll options count as poll_id => count
  38. *
  39. * @var array
  40. */
  41. protected $pollsOptionsCount = array();
  42. /**
  43. * Contains all polls votes as [poll_id] => Array ( parametr => Array ( [login] => $value))
  44. *
  45. * @var array
  46. */
  47. protected $pollsVotes = array();
  48. /**
  49. * Contains poll votes count as poll_id => count
  50. *
  51. * @var array
  52. */
  53. protected $pollsVotesCount = array();
  54. /**
  55. * Contains votes count by options as option_id => count
  56. *
  57. * @var array
  58. */
  59. protected $pollsOptionVotesCount = array();
  60. /**
  61. * Contains java scipt for dynamic add and remove input form field
  62. *
  63. * @var void
  64. */
  65. protected $pollJavaScript = '';
  66. /**
  67. * Contains STYLE design for form
  68. *
  69. * @var void
  70. */
  71. protected $pollCss = '';
  72. /**
  73. * Polls caching time
  74. *
  75. * @var int
  76. */
  77. protected $cacheTime = 2592000; //month by default
  78. const URL_ME = '?module=polls';
  79. public function __construct() {
  80. $this->initMessages();
  81. $this->setLogin();
  82. $this->initCache();
  83. $this->setPollId();
  84. $this->loadAdminsName();
  85. $this->loadAvaiblePollsCached();
  86. $this->loadPollsOptionsCached();
  87. $this->loadPollsVotesCached();
  88. if ( ! wf_CheckGet(array('ajaxavaiblepolls'))) {
  89. $this->loadPollsVotesCached();
  90. }
  91. }
  92. /**
  93. *
  94. *
  95. * @return string
  96. */
  97. protected function loadPollJavaScript($input_name = 'val', $label = 'Variant', $value = '', $width = 120) {
  98. $this->pollJavaScript = '
  99. <script type="text/javascript">
  100. // Function add input field in form
  101. function addField () {
  102. var telnum = parseInt($("#add_field_area").find("div.add:last").attr("id").slice(3))+1;
  103. $("div#add_field_area").append(\' \
  104. <div id="add\'+telnum+\'" class="add"> \
  105. <label>' . __($label) . ' №\'+telnum+\'</label> \
  106. <input type="text" width="' . $width . '" name="' . $input_name . '[]" id="' . $input_name . '" value="' . htmlspecialchars($value) . '"/> \
  107. <div class="deletebutton" onclick="deleteField(\'+telnum+\');"></div> \
  108. </div> \
  109. \');
  110. }
  111. // Function remove input field in form
  112. function deleteField (id) {
  113. $(\'div#add\'+id).remove();
  114. }
  115. </script>
  116. ';
  117. }
  118. /**
  119. * Load style for form
  120. *
  121. * @return string
  122. */
  123. protected function loadPollCss() {
  124. $this->pollCss = "
  125. <style>
  126. input {
  127. height: 20px;
  128. margin: 5px;
  129. width:400px;
  130. }
  131. .addbutton {
  132. text-align: center;
  133. vertical-align:middle;
  134. font-size: 13px;
  135. width: 283px;
  136. border: 1px solid #70A9FD;
  137. -webkit-border-radius: 7px;
  138. -moz-border-radius: 7px;
  139. border-radius: 7px;
  140. cursor: pointer;
  141. margin: 2px 0 0 110px;
  142. color: #326DC5;
  143. padding: 4px;
  144. background-color:#BED6FF;
  145. }
  146. .deletebutton {
  147. width: 20px;
  148. height: 22px;
  149. cursor: pointer;
  150. margin: 5px;
  151. display:inline-block;
  152. background: url(skins/icon_del.gif) repeat;
  153. background-position: center center;
  154. background-repeat: no-repeat;
  155. position:absolute;
  156. top: 1px;
  157. left: 480px;
  158. }
  159. .add {
  160. position:relative;
  161. }
  162. .createbutton {
  163. text-align: center;
  164. vertical-align:middle;
  165. font-size: 13px;
  166. width: 293px;
  167. -webkit-border-radius: 7px;
  168. -moz-border-radius: 7px;
  169. border-radius: 7px;
  170. cursor: pointer;
  171. margin: 20px 0 0 110px;
  172. border: 1px solid #378137;
  173. color: #fff;
  174. padding: 4px;
  175. height: 40px;;
  176. background-color: #46a546;
  177. }
  178. </style>
  179. ";
  180. }
  181. /**
  182. * Inits system messages helper object for further usage
  183. *
  184. * @return void
  185. */
  186. protected function initMessages() {
  187. $this->messages = new UbillingMessageHelper();
  188. }
  189. /**
  190. * Sets current user login
  191. *
  192. * @return void
  193. */
  194. protected function setLogin() {
  195. $this->myLogin = whoami();
  196. }
  197. /**
  198. * Initalizes system cache object
  199. *
  200. * @return void
  201. */
  202. protected function initCache() {
  203. $this->cache = new UbillingCache();
  204. }
  205. /**
  206. * Initalizes $poll_id
  207. *
  208. * @return void
  209. */
  210. protected function setPollId() {
  211. if (wf_CheckGet(array('poll_id'))) {
  212. $this->poll_id = vf($_GET['poll_id']);
  213. }
  214. }
  215. /**
  216. * Loads admis Name
  217. *
  218. * @return void
  219. */
  220. protected function loadAdminsName() {
  221. @$employeeLogins = unserialize(ts_GetAllEmployeeLoginsCached());
  222. if ( ! empty($employeeLogins)) {
  223. foreach ($employeeLogins as $login => $name){
  224. $this->adminsName[$login] = $name;
  225. }
  226. }
  227. }
  228. /**
  229. * Init admin Name
  230. *
  231. * @param string $admin
  232. * @return void
  233. */
  234. protected function initAdminName($admin) {
  235. $result = '';
  236. if ( ! empty($admin)) {
  237. $result = (isset($this->adminsName[$admin])) ? $this->adminsName[$admin] : $admin;
  238. }
  239. return ($result);
  240. }
  241. /**
  242. * Loads All avaible Polls from cache
  243. *
  244. * @return array
  245. */
  246. protected function loadAvaiblePollsCached() {
  247. $obj = $this;
  248. $polls_arr = $this->cache->getCallback('POLLS', function() use ($obj) {
  249. return ($obj->loadAvaiblePolls());
  250. }, $this->cacheTime);
  251. if ( ! empty($polls_arr)) {
  252. foreach ($polls_arr as $key => $data) {
  253. $this->pollsAvaible[$data['id']]['title'] = $data['title'];
  254. $this->pollsAvaible[$data['id']]['enabled'] = $data['enabled'];
  255. $this->pollsAvaible[$data['id']]['start_date'] = $data['start_date'];
  256. $this->pollsAvaible[$data['id']]['end_date'] = $data['end_date'];
  257. $this->pollsAvaible[$data['id']]['params'] = $data['params'];
  258. $this->pollsAvaible[$data['id']]['admin'] = $data['admin'];
  259. $this->pollsAvaible[$data['id']]['voting'] = $data['voting'];
  260. }
  261. }
  262. }
  263. /**
  264. * Loads All avaible Polls from databases
  265. *
  266. * @return array
  267. */
  268. public function loadAvaiblePolls() {
  269. $query = "SELECT * FROM `polls` ORDER BY `id` ASC";
  270. $result = simple_queryall($query);
  271. return ($result);
  272. }
  273. /**
  274. * Loads all avaible polls options from cache
  275. *
  276. * @return array pollsOptions
  277. * @return array pollsOptionsCount
  278. */
  279. protected function loadPollsOptionsCached() {
  280. $obj=$this;
  281. $option_arr = $this->cache->getCallback('POLLS_OPTIONS', function() use ($obj) {
  282. return ($obj->loadPollsOptions());
  283. }, $this->cacheTime);
  284. if ( ! empty($option_arr)) {
  285. foreach ($option_arr as $data) {
  286. $this->pollsOptions[$data['poll_id']][$data['id']] = $data['text'];
  287. }
  288. foreach ($this->pollsOptions as $id => $data) {
  289. $this->pollsOptionsCount[$id] = count($data);
  290. }
  291. }
  292. }
  293. /**
  294. * Loads all avaible polls options from databases
  295. *
  296. * @return array pollsOptions
  297. * @return array pollsOptionsCount
  298. */
  299. public function loadPollsOptions() {
  300. $query = "SELECT * FROM `polls_options` ORDER BY `id` ASC";
  301. $result = simple_queryall($query);
  302. return ($result);
  303. }
  304. /**
  305. * Check for last cache data and if need clean
  306. *
  307. * @return void
  308. */
  309. protected function pollsVotesCacheInfoClean($poll_id) {
  310. $query = "SELECT `id` FROM `polls_votes` WHERE `poll_id` = '" . $poll_id . "' ORDER BY `id` DESC LIMIT 1";
  311. $last_db_uniqueid = simple_query($query);
  312. $last_cache_id = $this->cache->get('POLL_' . $poll_id . '_VOTES_LAST', $this->cacheTime);
  313. if ($last_db_uniqueid != $last_cache_id) {
  314. $this->cache->delete('POLL_' . $poll_id . '_VOTES', $this->cacheTime);
  315. $this->cache->set('POLL_' . $poll_id . '_VOTES_LAST', $last_db_uniqueid, $this->cacheTime);
  316. }
  317. }
  318. /**
  319. * Loads all avaible votes result from cache
  320. *
  321. * @return array pollsVotes
  322. * @return array pollsVotesCount
  323. * @return array pollsOptionVotesCount
  324. */
  325. protected function loadPollsVotesCached($poll_id = '') {
  326. // Initialises poll_id
  327. $poll_id = ($poll_id) ? $poll_id : $this->poll_id;
  328. if (isset($this->pollsAvaible[$poll_id])) {
  329. // Check for needed cache by poll_id
  330. $this->pollsVotesCacheInfoClean($poll_id);
  331. $obj=$this;
  332. $votes_arr = $this->cache->getCallback('POLL_' . $poll_id . '_VOTES', function() use ($poll_id,$obj) {
  333. return ($obj->loadPollsVotes($poll_id));
  334. }, $this->cacheTime);
  335. if ( ! empty($votes_arr)) {
  336. foreach ($votes_arr as $data) {
  337. $this->pollsVotes[$data['poll_id']]['id'][$data['login']] = $data['id'];
  338. $this->pollsVotes[$data['poll_id']]['option_id'][$data['login']] = $data['option_id'];
  339. $this->pollsVotes[$data['poll_id']]['date'][$data['login']] = $data['date'];
  340. }
  341. // Count poll votes
  342. $this->pollsVotesCount[$data['poll_id']] = count($this->pollsVotes[$data['poll_id']]['id']);
  343. // Count votes by options
  344. $this->pollsOptionVotesCount[$data['poll_id']] = array_count_values($this->pollsVotes[$data['poll_id']]['option_id']);
  345. }
  346. }
  347. }
  348. /**
  349. * Loads all avaible votes result from databases
  350. *
  351. * @return array polls_votes
  352. */
  353. public function loadPollsVotes($poll_id) {
  354. $query = "SELECT * FROM `polls_votes` WHERE `poll_id` = '" . $poll_id . "'";
  355. $result = simple_queryall($query);
  356. return ($result);
  357. }
  358. /**
  359. * Create poll on database
  360. *
  361. * @param int $title, $status, $startDateTime, $endDateTime, $endDateTime, $parametr = '', $voting = ''
  362. * @return void
  363. */
  364. protected function createPoll($title, $status, $startDateTime, $endDateTime, $parametr = '', $voting = 'Users') {
  365. $poll_id = '';
  366. $query = "INSERT INTO `polls` (`id`, `title`, `enabled`, `start_date`, `end_date`, `params`, `admin`, `voting`)
  367. VALUES (NULL, '" . mysql_real_escape_string($title) . "', '" . $status . "', '" . $startDateTime . "', '" . $endDateTime . "', '" . mysql_real_escape_string($parametr) . "', '" . mysql_real_escape_string($this->myLogin) . "', '" . mysql_real_escape_string($voting) . "')";
  368. nr_query($query);
  369. $query_poll_id = "SELECT LAST_INSERT_ID() as id";
  370. $poll_id = simple_query($query_poll_id);
  371. $poll_id = $poll_id['id'];
  372. $this->cache->delete('POLLS', $this->cacheTime);
  373. return ($poll_id);
  374. }
  375. /**
  376. * Change poll data on database
  377. *
  378. * @param int $poll_id, array $new_poll_data
  379. * @return void
  380. */
  381. protected function editPoll($poll_id, $new_poll_data) {
  382. $old_poll_data = $this->pollsAvaible[$poll_id];
  383. $diff_data = array_diff_assoc($new_poll_data, $old_poll_data);
  384. if ( ! empty($diff_data)) {
  385. foreach ($diff_data as $field => $value) {
  386. simple_update_field('polls', $field, mysql_real_escape_string($value), "WHERE `id`='" . $poll_id . "'");
  387. }
  388. $this->cache->delete('POLLS', $this->cacheTime);
  389. log_register('POLL UPDATE [' . $poll_id . '] `' . $this->pollsAvaible[$poll_id]['title'] . '`');
  390. }
  391. }
  392. /**
  393. * Delete poll from database
  394. *
  395. * @param int $poll_id
  396. * @return void
  397. */
  398. protected function deletePoll($poll_id) {
  399. $this->deletePollOptions($poll_id);
  400. $query = "DELETE FROM `polls` WHERE `id` ='" . $poll_id . "'";
  401. nr_query($query);
  402. $this->cache->delete('POLLS', $this->cacheTime);
  403. }
  404. /**
  405. * Delete polls options from database
  406. *
  407. * @param int $poll_id
  408. * @return void
  409. */
  410. protected function deletePollOptions($poll_id) {
  411. $this->deletePollVotes($poll_id);
  412. $query = "DELETE FROM `polls_options` WHERE `poll_id` ='" . $poll_id . "'";
  413. nr_query($query);
  414. $this->cache->delete('POLLS_OPTIONS', $this->cacheTime);
  415. }
  416. /**
  417. * Delete polls votes from database
  418. *
  419. * @param int $poll_id
  420. * @return void
  421. */
  422. protected function deletePollVotes($poll_id) {
  423. $query = "DELETE FROM `polls_votes` WHERE `poll_id` ='" . $poll_id . "'";
  424. nr_query($query);
  425. $this->cache->delete('POLL_' . $poll_id . '_VOTES', $this->cacheTime);
  426. $this->cache->delete('POLL_' . $poll_id . '_VOTES_LAST', $this->cacheTime);
  427. }
  428. /**
  429. * Change poll options on database
  430. *
  431. * @param int $poll_id, array $poll_options
  432. * @return void
  433. */
  434. protected function editPollConfigs($poll_id, $poll_options) {
  435. $update_cache = FALSE;
  436. if (! isset ($this->pollsOptions[$poll_id])) {
  437. foreach ($poll_options as $value) {
  438. $query = " INSERT INTO `polls_options` (`id`, `poll_id`, `text`)
  439. VALUES (NULL, '" . $poll_id . "', '" . mysql_real_escape_string($value) . "')";
  440. nr_query($query);
  441. }
  442. $update_cache = TRUE;
  443. log_register('POLL OPTIONS CREATE [' . $poll_id . ']');
  444. } else {
  445. $need_create = array_diff_key($poll_options, $this->pollsOptions[$poll_id]);
  446. if ($need_create) {
  447. foreach ($need_create as $value) {
  448. $query = " INSERT INTO `polls_options` (`id`, `poll_id`, `text`)
  449. VALUES (NULL, '" . $poll_id . "', '" . mysql_real_escape_string($value) . "')";
  450. nr_query($query);
  451. }
  452. $update_cache = TRUE;
  453. }
  454. // Search options that need delete from database. Return as $key => $id (id option on database)
  455. $need_delete = array_keys(array_diff_key($this->pollsOptions[$poll_id], $poll_options));
  456. if ($need_delete) {
  457. foreach ($need_delete as $id) {
  458. $query = "DELETE from `polls_options` WHERE `id`='" . $id . "';";
  459. nr_query($query);
  460. $query_votes = "DELETE from `polls_votes` WHERE `option_id`='" . $id . "';";
  461. nr_query($query_votes);
  462. }
  463. $update_cache = TRUE;
  464. }
  465. $need_update = array_diff_assoc($poll_options, $need_create, $this->pollsOptions[$poll_id]);
  466. if ($need_update) {
  467. foreach ($need_update as $id => $value) {
  468. simple_update_field('polls_options', 'text', mysql_real_escape_string($value), "WHERE `id`='" . $id . "'");
  469. }
  470. $update_cache = TRUE;
  471. }
  472. }
  473. // Delete Options cache if need
  474. if ($update_cache) {
  475. $this->cache->delete('POLLS_OPTIONS', $this->cacheTime);
  476. }
  477. }
  478. /**
  479. * Render poll status
  480. *
  481. * @param int $poll_id
  482. * @return void
  483. */
  484. protected function renderPollStatus($poll_id = '') {
  485. $result = '';
  486. // Initialises poll_id
  487. $poll_id = ($poll_id) ? $poll_id : $this->poll_id;
  488. if (isset($this->pollsAvaible[$poll_id])) {
  489. if ($this->pollsAvaible[$poll_id]['enabled'] == 0 AND time() < strtotime($this->pollsAvaible[$poll_id]['end_date'])) {
  490. $result = wf_img('skins/icon_inactive.gif') . ' ' . __('Disabled');
  491. } elseif ($this->pollsAvaible[$poll_id]['enabled'] AND time() < strtotime($this->pollsAvaible[$poll_id]['start_date'])) {
  492. $result = wf_img('skins/yellow_led.png') . ' '. __('Not yet started');
  493. } elseif (time() > strtotime($this->pollsAvaible[$poll_id]['end_date'])) {
  494. $result = wf_img('skins/icon_active2.gif') . ' ' . __('Finished');
  495. } elseif ($this->pollsAvaible[$poll_id]['enabled'] AND time() > strtotime($this->pollsAvaible[$poll_id]['start_date']) AND time() < strtotime($this->pollsAvaible[$poll_id]['end_date'])) {
  496. $result = wf_img('skins/icon_active.gif') . ' ' . __('Poll in progress');
  497. }
  498. }
  499. return ($result);
  500. }
  501. /**
  502. * Renders Poll data
  503. *
  504. * @param int $poll_id
  505. * @return string
  506. */
  507. protected function renderPollData() {
  508. $result = '';
  509. if (isset($this->pollsAvaible[$this->poll_id])) {
  510. $cells = wf_TableCell(__('Status'));
  511. $cells.= wf_TableCell(__('Start date'));
  512. $cells.= wf_TableCell(__('End date'));
  513. $cells.= wf_TableCell(__('Voting'));
  514. $cells.= wf_TableCell(__('Admin'));
  515. $rows = wf_TableRow($cells, 'row1');
  516. $window = @$this->poll_id . ' - ' . @htmlspecialchars($this->pollsAvaible[$this->poll_id]['title']);
  517. $cells = wf_TableCell($this->renderPollStatus($this->poll_id));
  518. $cells.= wf_TableCell($this->pollsAvaible[$this->poll_id]['start_date']);
  519. $cells.= wf_TableCell($this->pollsAvaible[$this->poll_id]['end_date']);
  520. $cells.= wf_TableCell(__($this->pollsAvaible[$this->poll_id]['voting']));
  521. $cells.= wf_TableCell($this->initAdminName($this->pollsAvaible[$this->poll_id]['admin']));
  522. $rows.= wf_TableRow($cells, 'row4');
  523. $table = wf_TableBody($rows, '', 0);
  524. $result.= show_window($window, $table);
  525. }
  526. return ($result);
  527. }
  528. /**
  529. * Renders Poll config container
  530. *
  531. * @return string
  532. */
  533. public function renderFormPoll() {
  534. $result = '';
  535. // Preset start date and time
  536. if (isset($this->pollsAvaible[$this->poll_id])) {
  537. $poll_action = 'editpoll';
  538. $poll_name = $this->pollsAvaible[$this->poll_id]['title'];
  539. $start_date = date("Y-m-d", strtotime($this->pollsAvaible[$this->poll_id]['start_date']));
  540. $start_time = date("H:i", strtotime($this->pollsAvaible[$this->poll_id]['start_date']));
  541. $end_date = date("Y-m-d", strtotime($this->pollsAvaible[$this->poll_id]['end_date']));
  542. $end_time = date("H:i", strtotime($this->pollsAvaible[$this->poll_id]['end_date']));
  543. $poll_status = $this->pollsAvaible[$this->poll_id]['enabled'];
  544. $voting = __($this->pollsAvaible[$this->poll_id]['voting']);
  545. $post_submit = 'Save';
  546. } else {
  547. $poll_action = 'createpoll';
  548. $param_can_voting = array('Users' => __('Users'), 'Employee' => __('Employee'));
  549. $poll_name = '';
  550. $start_date = date("Y-m-d");
  551. $start_time = date("H:i");
  552. $end_date = '';
  553. $end_time = '';
  554. $poll_status = true;
  555. $voting = wf_Selector($poll_action . '[voting]', $param_can_voting, '', '', false);
  556. $post_submit = 'Create';
  557. }
  558. $cells = wf_TableCell(__('Poll title'));
  559. $cells.= wf_TableCell(wf_TextInput($poll_action . '[title]', '', htmlspecialchars($poll_name), false, '27'));
  560. $rows = wf_TableRow($cells, 'row2');
  561. $cells = wf_TableCell(__('Start date'));
  562. $cells.= wf_TableCell(wf_DatePickerPreset($poll_action . '[startdate]', $start_date) . wf_TimePickerPreset($poll_action . '[starttime]', $start_time, '', false));
  563. $rows.= wf_TableRow($cells, 'row2');
  564. $cells = wf_TableCell(__('End date'));
  565. $cells.= wf_TableCell(wf_DatePickerPreset($poll_action . '[enddate]', $end_date) . wf_TimePickerPreset($poll_action . '[endtime]', $end_time, '', false));
  566. $rows.= wf_TableRow($cells, 'row2');
  567. $cells = wf_TableCell(__('Enabled'));
  568. $cells.= wf_TableCell(wf_CheckInput($poll_action . '[enabled]', '', false, $poll_status));
  569. $rows.= wf_TableRow($cells, 'row2');
  570. $cells = wf_TableCell(__('Voting'));
  571. $cells.= wf_TableCell($voting);
  572. $rows.= wf_TableRow($cells, 'row2');
  573. $rows.= wf_TableRow(wf_TableCell(wf_Submit($post_submit)));
  574. $table = wf_TableBody($rows, '', 0);
  575. $result = wf_Form("", "POST", $table, 'glamour');
  576. return ($result);
  577. }
  578. /**
  579. * Renders Poll options preview container
  580. *
  581. * @return string
  582. */
  583. public function renderPreviewPollOption() {
  584. $result = '';
  585. if (isset($this->pollsAvaible[$this->poll_id])) {
  586. if (isset($this->pollsOptions[$this->poll_id])) {
  587. $poll_options = $this->pollsOptions[$this->poll_id];
  588. $inputs = '';
  589. foreach ($poll_options as $id => $option) {
  590. $inputs.= wf_RadioInput('option', $option, $id, true);
  591. }
  592. $result.= wf_Form("", "POST", $inputs, 'glamour polls');
  593. } else {
  594. $result.= $this->messages->getStyledMessage(__('You have not created any answer options yet'), 'info');
  595. }
  596. $result.= $this->renderPollData();
  597. } else {
  598. $result.= $this->messages->getStyledMessage(__('This poll does not exist'), 'error');
  599. }
  600. return ($result);
  601. }
  602. /**
  603. * Poll control function
  604. *
  605. * @param array $polls_data
  606. * @return void
  607. */
  608. public function controlPoll(array $polls_data) {
  609. $result = '';
  610. $message_warn = '';
  611. if ( ! empty($polls_data)) {
  612. // Check poll name
  613. if ( ! empty($polls_data['title'])) {
  614. $name = ($polls_data['title']) ;
  615. } else {
  616. $message_warn.= $this->messages->getStyledMessage(__('Poll title cannot be empty'), 'warning');
  617. }
  618. // Check poll start time
  619. if ( ! empty($polls_data['startdate']) and ! empty($polls_data['starttime'])) {
  620. $startDateTime = date("Y-m-d H:i:s", strtotime(mysql_real_escape_string($polls_data['startdate'] . $polls_data['starttime'])));
  621. } else {
  622. $message_warn.= $this->messages->getStyledMessage(__('Poll start time cannot be empty'), 'warning');
  623. }
  624. // Check poll end time
  625. if ( ! empty($polls_data['enddate']) and ! empty($polls_data['endtime'])) {
  626. $endDateTime = date("Y-m-d H:i:s", strtotime(mysql_real_escape_string($polls_data['enddate'] . $polls_data['endtime'])));
  627. } else {
  628. $message_warn.= $this->messages->getStyledMessage(__('Poll end time cannot be empty'), 'warning');
  629. }
  630. // Check that poll end time more that start time
  631. if (isset($startDateTime) AND isset($endDateTime) AND strtotime($startDateTime) >= strtotime($endDateTime)) {
  632. $message_warn.= $this->messages->getStyledMessage(__('Poll start time cannot be more than end time'), 'warning');
  633. }
  634. // Check poll status enabled
  635. $status = (isset($polls_data['enabled'])) ? 1 : 0;
  636. // Set who voting on this poll
  637. $voting = (isset($polls_data['voting'])) ? $polls_data['voting'] : '';
  638. // Check that we dont have warning message and create poll
  639. if (empty($message_warn) and @$_POST['createpoll']) {
  640. $poll_id = $this->createPoll($name, $status, $startDateTime, $endDateTime, $parametr = '', $voting);
  641. // Check that we create poll, get his $poll_id and redirect to create variants
  642. if ($poll_id) {
  643. rcms_redirect(self::URL_ME . '&action=polloptions&poll_id=' . $poll_id);
  644. }
  645. } elseif (empty($message_warn) and @$_POST['editpoll']) {
  646. $new_poll_data = array('title' => $name, 'enabled' => $status, 'start_date' => $startDateTime, 'end_date' => $endDateTime, 'params' => $parametr = '');
  647. $this->editPoll($this->poll_id, $new_poll_data);
  648. rcms_redirect(self::URL_ME . '&action=polloptions&poll_id=' . $this->poll_id);
  649. }
  650. } else {
  651. $result.= $this->messages->getStyledMessage(__('Poll data cannot be empty '), 'warning');
  652. }
  653. $result.= $message_warn;
  654. return ($result);
  655. }
  656. /**
  657. * Poll options control
  658. *
  659. * @param array $poll_options
  660. * @return void
  661. */
  662. public function controlPollOptions(array $poll_options) {
  663. $result = '';
  664. $message_warn = '';
  665. if ( ! empty($poll_options)) {
  666. // Count options
  667. if (count($poll_options) < 2) {
  668. $message_warn = $this->messages->getStyledMessage(__('The number of options cannot be less than two'), 'warning');
  669. }
  670. // Check for empty options value
  671. if (array_intersect($poll_options, array(''))) {
  672. $message_warn = $this->messages->getStyledMessage(__('Options for voting responses can not be empty'), 'warning');
  673. }
  674. // Check that we dont have warning message and create poll
  675. if (empty($message_warn) AND @$_POST['polloptions'] AND isset($this->pollsAvaible[$this->poll_id])) {
  676. $this->editPollConfigs($this->poll_id, $poll_options);
  677. // If dont have Message warninng - go to Poll option preview container
  678. rcms_redirect(self::URL_ME . '&show_options=true&poll_id=' . $this->poll_id);
  679. }
  680. } else {
  681. $result.= $this->messages->getStyledMessage(__('Poll options cannot be empty'), 'warning');
  682. }
  683. $result.= $message_warn;
  684. return ($result);
  685. }
  686. /**
  687. * Renders Polls options from container
  688. *
  689. * @param int $poll_id
  690. * @return string
  691. */
  692. public function renderFormPollOption() {
  693. $result = '';
  694. $form = '';
  695. if (isset($this->pollsAvaible[$this->poll_id])) {
  696. // Form parameter for future, if on next we want use this function global
  697. $method = "POST";
  698. $input_name = "polloptions";
  699. $input_width = 150;
  700. $label = 'Option';
  701. $value = '';
  702. // Loads needed function
  703. $this->loadPollJavaScript($input_name, $label, $value, $input_width);
  704. $this->loadPollCss();
  705. $form = $this->pollJavaScript;
  706. $form.= $this->pollCss;
  707. // Create form
  708. $form.= '
  709. <form method="' . $method . '">
  710. <div id="add_field_area">';
  711. if (isset($this->pollsOptions[$this->poll_id])) {
  712. $n = 1;
  713. foreach ($this->pollsOptions[$this->poll_id] as $opt_id => $text) {
  714. $form.= '
  715. <div id="add' . $n . '" class="add">
  716. <label>' . __($label) . ' №' . $n . '</label>
  717. <input type="text" width="' . $input_width . '" name="' . $input_name . '[' . $opt_id . ']" id="' . $input_name . '" value="' . htmlspecialchars($text) . '"/>';
  718. if ($n >= 3 ) {
  719. $form.= '<div class="deletebutton" onclick="if(!confirm(\'' . __('Be careful! If you delete the option, you also delete the poll results by this option.') . '\')) {return false;} deleteField(\'' . $n . '\');"></div>';
  720. }
  721. $form.= '
  722. </div>';
  723. $n++;
  724. }
  725. } else {
  726. $form.= '
  727. <div id="add1" class="add">
  728. <label>' . __($label) . ' №1</label>
  729. <input type="text" width="' . $input_width . '" name="' . $input_name . '[]" id="' . $input_name . '" value="' . htmlspecialchars($value) . '"/>
  730. </div>
  731. <div id="add2" class="add">
  732. <label>' . __($label) . ' №2</label>
  733. <input type="text" width="' . $input_width . '" name="' . $input_name . '[]" id="' . $input_name . '" value="' . htmlspecialchars($value) . '"/>
  734. </div>';
  735. }
  736. $form.= '
  737. </div>';
  738. $form.= '
  739. <div onclick="addField();" class="addbutton">' . __('Add new field') . '</div>
  740. <div>
  741. <input type="submit" value="' . __('Save') . '" class="createbutton">
  742. </div>
  743. </form>
  744. ';
  745. }
  746. $result.= $this->renderPollData();
  747. $result.= $form;
  748. return ($result);
  749. }
  750. /**
  751. * Renders polls module control panel
  752. *
  753. * @return void
  754. */
  755. public function panel() {
  756. $result = '';
  757. // Add backlink
  758. if (wf_CheckGet(array('action')) OR wf_CheckGet(array('show_options'))) {
  759. $result.= wf_BackLink(self::URL_ME);
  760. }
  761. if (cfr('POLLSCONFIG') AND @$_GET['action'] != 'create_poll') {
  762. $result.= wf_Link(self::URL_ME . '&action=create_poll', wf_img('skins/add_icon.png') . ' ' . __('Create poll'), false, 'ubButton') . ' ';
  763. }
  764. if (cfr('POLLSREPORT') AND @$_GET['action'] != 'create_poll') {
  765. $result.= wf_Link('index.php?module=report_polls', wf_img('skins/icon_star.gif') . ' ' . __('Show voting results'), false, 'ubButton') . ' ';
  766. }
  767. if (cfr('POLLSCONFIG') AND @$_GET['action'] == 'polloptions' OR (cfr('POLLSCONFIG') AND wf_CheckGet(array('show_options'))) AND isset($this->pollsAvaible[$this->poll_id])) {
  768. $result.= wf_Link(self::URL_ME . '&action=edit_poll&poll_id=' . $this->poll_id, wf_img('skins/icon_extended.png') . ' ' . __('Configure Poll'), false, 'ubButton') . ' ';
  769. }
  770. if (cfr('POLLSCONFIG') AND (@$_GET['action'] == 'edit_poll' OR wf_CheckGet(array('show_options'))) AND isset($this->pollsAvaible[$this->poll_id])) {
  771. $result.= wf_Link(self::URL_ME . '&action=polloptions&poll_id=' . $this->poll_id, wf_img('skins/icon_edit.gif') . ' ' . __('Edit answer options'), false, 'ubButton') . ' ';
  772. }
  773. if (cfr('POLLSCONFIG') AND wf_CheckGet(array('action')) AND $_GET['action'] != 'create_poll' AND isset($this->pollsAvaible[$this->poll_id])) {
  774. $result.= wf_Link(self::URL_ME . '&show_options=true&poll_id=' . $this->poll_id, wf_img('skins/icon_eye.gif') . ' ' . __('Show preliminary voting form'), false, 'ubButton') . ' ';
  775. }
  776. return ($result);
  777. }
  778. /**
  779. * Deletes all data about poll from database by ID
  780. *
  781. * @param int $poll_id
  782. * @return void
  783. */
  784. public function deletePollData() {
  785. if(isset($this->pollsAvaible[$this->poll_id])) {
  786. $this->deletePoll($this->poll_id);
  787. }
  788. rcms_redirect(self::URL_ME);
  789. }
  790. /**
  791. * Renders polls module control panel interface
  792. *
  793. * @return string
  794. */
  795. public function renderAvaiblePolls() {
  796. $columns = array('ID', 'Poll title', 'Status', 'Start date', 'End date', 'Number of votes', 'Number of options', 'Voting', 'Admin');
  797. if (cfr('POLLSCONFIG')) {
  798. $columns[] = 'Actions';
  799. }
  800. $opts = '"order": [[ 0, "desc" ]]';
  801. $result = wf_JqDtLoader($columns, self::URL_ME . '&ajaxavaiblepolls=true', false, 'polls', 100, $opts);
  802. return ($result);
  803. }
  804. /**
  805. * Renders json formatted data about Polls
  806. *
  807. * @return void
  808. */
  809. public function ajaxAvaiblePolls() {
  810. $json = new wf_JqDtHelper();
  811. if (!empty($this->pollsAvaible)) {
  812. foreach ($this->pollsAvaible as $poll_id => $poll) {
  813. $this->loadPollsVotesCached($poll_id);
  814. $acts = '';
  815. if (cfr('POLLSCONFIG')) {
  816. $acts.= wf_JSAlert(self::URL_ME . '&action=delete_poll&poll_id=' . $poll_id, web_delete_icon(), 'If you delete this poll, you will delete all data including voting results') . ' ';
  817. $acts.= wf_JSAlert(self::URL_ME . '&action=edit_poll&poll_id=' . $poll_id, web_icon_extended('Configure Poll'), 'Are you serious') . ' ';
  818. }
  819. if (isset($this->pollsOptionsCount[$poll_id])) {
  820. $options = $this->pollsOptionsCount[$poll_id];
  821. $options.= ' ' . wf_Link(self::URL_ME . '&show_options=true&poll_id=' . $poll_id, web_icon_search('Show preliminary voting form'));
  822. if (cfr('POLLSCONFIG')) {
  823. $options.= wf_JSAlert(self::URL_ME . '&action=polloptions&poll_id=' . $poll_id, ' ' . web_edit_icon('Edit answer options'), 'Are you serious') . ' ';
  824. }
  825. } else {
  826. $options = 0;
  827. if (cfr('POLLSCONFIG')) {
  828. $options.= ' ' . wf_Link(self::URL_ME . '&action=polloptions&poll_id=' . $poll_id, web_icon_create('Create answer options'));
  829. }
  830. }
  831. if (isset($this->pollsVotesCount[$poll_id])) {
  832. $votes = $this->pollsVotesCount[$poll_id];
  833. $votes.= ' ' . wf_Link('?module=report_polls&action=show_poll_votes&poll_id=' . $poll_id, web_stats_icon('View poll results'));
  834. } else {
  835. $votes = 0;
  836. }
  837. $data[] = $poll_id;
  838. $data[] = htmlspecialchars($poll['title']);
  839. $data[] = $this->renderPollStatus($poll_id);
  840. $data[] = $poll['start_date'];
  841. $data[] = $poll['end_date'];
  842. $data[] = $votes;
  843. $data[] = $options;
  844. $data[] = __($poll['voting']);
  845. $data[] = $this->initAdminName($poll['admin']);
  846. if (cfr('POLLSCONFIG')) {
  847. $data[] = $acts;
  848. }
  849. $json->addRow($data);
  850. unset($data);
  851. }
  852. }
  853. $json->getJson();
  854. }
  855. }
  856. ?>