PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/Poll.php

https://github.com/subdesign/codeigniter-subpoll
PHP | 260 lines | 190 code | 27 blank | 43 comment | 10 complexity | deab17a851833593e30519535207fcfa MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * SubPoll poll library
  4. *
  5. * Codeigniter poll library with KendoUI chart widget
  6. *
  7. * @author Barna Szalai <sz.b@subdesign.hu>
  8. * @copyright Copyright (c) 2011, Barna Szalai
  9. * @version 0.0.1
  10. *
  11. */
  12. class Poll
  13. {
  14. public function __construct()
  15. {
  16. $this->CI =& get_instance();
  17. }
  18. /**
  19. * Gets the active poll's vote datas
  20. * @return array
  21. *
  22. */
  23. public function get_poll()
  24. {
  25. $query = $this->CI->db->select('id, question, answers')
  26. ->where('status', 'active')
  27. ->get('polls');
  28. if($query->num_rows() > 0)
  29. {
  30. $row = $query->row();
  31. $answers = array();
  32. $answers = unserialize($row->answers);
  33. $poll_form = form_open('subpoll/poll_item');
  34. foreach($answers as $answer)
  35. {
  36. $poll_form .= form_radio('answer', $answer);
  37. $poll_form .= form_label($answer, $answer).'<br/>';
  38. }
  39. $poll_form .= form_submit('submit', 'Vote');
  40. $data['poll_form'] = $poll_form;
  41. $data['question'] = $row->question;
  42. return $data;
  43. }
  44. }
  45. /**
  46. * Sets the active poll's vote datas
  47. * @param string $vote
  48. */
  49. public function set_poll($vote)
  50. {
  51. $query = $this->CI->db->select('answers, votes')
  52. ->where('status', 'active')
  53. ->get('polls');
  54. if($query->num_rows() > 0)
  55. {
  56. $row = $query->row();
  57. $answers = unserialize($row->answers);
  58. $key = array_search($vote, $answers);
  59. $votes = unserialize($row->votes);
  60. $actual_vote = $votes[$key];
  61. $actual_vote = $actual_vote + 1;
  62. $votes[$key] = $actual_vote;
  63. $votes = serialize($votes);
  64. $this->CI->db->update('polls', array('votes' => $votes), array('status' => 'active'));
  65. $this->CI->session->set_userdata('voted', 'true');
  66. }
  67. }
  68. /**
  69. * Get the active poll and builds the javascript chart
  70. * @return array
  71. *
  72. */
  73. public function get_result()
  74. {
  75. $query = $this->CI->db->select('question, answers, votes')
  76. ->where('status', 'active')
  77. ->get('polls');
  78. if($query->num_rows() > 0)
  79. {
  80. $row = $query->row();
  81. $answers = unserialize($row->answers);
  82. $votes = unserialize($row->votes);
  83. (is_array($votes)) ? ksort($votes) : '';
  84. $question = $row->question;
  85. <<<<<<< HEAD
  86. =======
  87. >>>>>>> 626076d000ba60ad2c7cab1cd7d9da31472d4674
  88. $vote_ = '';
  89. $answer_ = '';
  90. if($votes != FALSE)
  91. {
  92. for($i = 1; $i <= count($answers); $i++)
  93. {
  94. if(array_key_exists($i, $votes))
  95. {
  96. $vote_ .= ($i < count($answers)) ? $votes[$i].', ' : $votes[$i];
  97. $answer_ .= ($i < count($answers)) ? '"'.$answers[$i].' ('.$votes[$i].')", ' : '"'.$answers[$i].' ('.$votes[$i].')"';
  98. }
  99. else
  100. {
  101. $vote_ .= ($i < count($answers)) ? 0 .', ' : 0;
  102. $answer_ .= ($i < count($answers)) ? '"'.$answers[$i].' (0)", ' : '"'.$answers[$i].' (0)"';
  103. }
  104. }
  105. }
  106. else
  107. {
  108. for($k = 1; $k <= count($answers); $k++)
  109. {
  110. $vote_ .= ($k < count($answers)) ? 0 .', ' : 0;
  111. $answer_ .= ($k < count($answers)) ? '"'.$answers[$k].' (0)", ' : '"'.$answers[$k].' (0)"';
  112. }
  113. }
  114. $embed = '';
  115. $embed .= '<link href="http://cdn.kendostatic.com/2011.2.804/styles/examples.min.css" rel="stylesheet"/>'.
  116. '<link href="http://cdn.kendostatic.com/2011.2.804/styles/kendo.common.min.css" rel="stylesheet"/>'.
  117. '<link href="http://cdn.kendostatic.com/2011.2.804/styles/kendo.kendo.min.css" rel="stylesheet"/>'.
  118. '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>'.
  119. '<script src="http://cdn.kendostatic.com/2011.2.804/js/kendo.all.min.js"></script>';
  120. $js = "";
  121. $js = "<script type=\"text/javascript\">
  122. function createChart() {
  123. $(\"#chart\").kendoChart({
  124. theme: \"kendo\",
  125. title: {
  126. font: \"13px Verdana, sans-serif\",
  127. text: \"'".$question."'\"
  128. },
  129. legend: {
  130. visible: false
  131. },
  132. seriesDefaults: {
  133. type: \"column\"
  134. },
  135. series: [
  136. {
  137. type: \"bar\",
  138. data: [".$vote_."]
  139. }
  140. ],
  141. categoryAxis: {
  142. labels: {
  143. rotation: 0
  144. },
  145. categories: [".$answer_."]
  146. },
  147. valueAxis: {
  148. labels: {
  149. visible: false
  150. },
  151. majorGridLines: false,
  152. majosUnit: 1,
  153. majorTickType: \"none\",
  154. minorGridLines: false
  155. },
  156. tooltip: {
  157. visible: true
  158. }
  159. });
  160. }
  161. $(document).ready(function() {
  162. createChart();
  163. $(document).bind(\"kendo:skinChange\", function(e) {
  164. createChart();
  165. });
  166. });
  167. </script>";
  168. $data['embed'] = $embed;
  169. $data['js'] = $js;
  170. return $data;
  171. }
  172. }
  173. /**
  174. * Get all polls (Admin)
  175. * @return string
  176. *
  177. */
  178. public function get_all_polls()
  179. {
  180. $query = $this->CI->db->select('id, question, status')
  181. ->get('polls');
  182. if($query->num_rows() > 0)
  183. {
  184. $html = '<table id="polls">';
  185. $html .= '<thead>';
  186. $html .= '<th>Id</th>';
  187. $html .= '<th>Question</th>';
  188. $html .= '<th>Status</th>';
  189. $html .= '<th>Values</th>';
  190. $html .= '</thead>';
  191. foreach($query->result() as $row)
  192. {
  193. $html .= '<tr>';
  194. $html .= '<td>'.$row->id.'</td><td>'.$row->question.'</td><td>'.anchor('subpoll/set_poll_status/'.$row->id, $row->status).'</td><td>'.anchor('subpoll/reset_poll/'.$row->id, 'Reset').'</td>';
  195. $html .= '</tr>';
  196. }
  197. $html .= '</table>';
  198. return $html;
  199. }
  200. }
  201. /**
  202. * Set poll status (Admin)
  203. * @params integer $id
  204. */
  205. public function set_poll_status($id)
  206. {
  207. $this->CI->db->update('polls', array('status' => 'inactive'));
  208. $this->CI->db->update('polls', array('status' => 'active'), array('id' => $id));
  209. }
  210. /**
  211. * Save new poll (Admin)
  212. * @params array $datas
  213. *
  214. */
  215. public function save_new_poll($datas)
  216. {
  217. $data = array(
  218. 'question' => $datas['question'],
  219. 'answers' => serialize($datas['answers']),
  220. 'status' => 'inactive'
  221. );
  222. $this->CI->db->insert('polls', $data);
  223. }
  224. /**
  225. * Reset poll values (Admin)
  226. * @params integer $id
  227. *
  228. */
  229. public function reset_poll($id)
  230. {
  231. $this->CI->db->update('polls', array('votes' => ''), array('id' => $id));
  232. }
  233. }