/models/polls_m.php

https://github.com/vmichnowicz/polls · PHP · 246 lines · 128 code · 26 blank · 92 comment · 7 complexity · 191d980026553d38c39ddffc385acd9c MD5 · raw file

  1. <?php defined('BASEPATH') or exit('No direct script access allowed');
  2. /**
  3. * Add, edit, delete, and update polls
  4. *
  5. * @author Victor Michnowicz
  6. * @category Modules
  7. */
  8. class Polls_m extends MY_Model {
  9. const TYPE_SINGLE = 'single';
  10. const TYPE_MULTIPLE = 'multiple';
  11. protected static $types = array(self::TYPE_SINGLE, self::TYPE_MULTIPLE);
  12. /**
  13. * Get all polls
  14. *
  15. * Optionallym get an individual poll by providing a poll ID. We can also
  16. * only get active polls, however, by default both active and inactive
  17. * polls are retrieved.
  18. *
  19. * @access public
  20. * @param bool By default we will return both active and inactive polls
  21. * @param int Optionally, only retrieve an individual poll
  22. * @return array
  23. */
  24. public function retrieve_polls($only_active = FALSE, $id = NULL)
  25. {
  26. $return = array();
  27. // If a poll ID was provided (and the ID is a valid integer)
  28. if ( isset($id) AND ( is_int($id) OR ctype_digit($id) ) )
  29. {
  30. // Get only this poll
  31. $this->db->where('id', $id)->limit(1);
  32. }
  33. // If we only want to bring back active polls
  34. if ($only_active === TRUE)
  35. {
  36. $this->db->where('active', 1);
  37. }
  38. $query = $this->db->get('polls');
  39. if ($query->num_rows() > 0)
  40. {
  41. foreach ($query->result() as $row)
  42. {
  43. // DateTime objects
  44. $open_date = $row->open_date ? DateTime::createFromFormat('U', $row->open_date) : NULL;
  45. $close_date = $row->close_date ? DateTime::createFromFormat('U', $row->close_date) : NULL;
  46. $now_date = new DateTime();
  47. // Default to open poll status
  48. $is_open = TRUE;
  49. // If this poll has an open date
  50. if ($open_date instanceof DateTime)
  51. {
  52. // If the open date is in the future
  53. if ($open_date > $now_date)
  54. {
  55. $is_open = FALSE;
  56. }
  57. }
  58. // If this poll has a close date
  59. if ($close_date instanceof DateTime)
  60. {
  61. // If the close date has already passed
  62. if ($close_date < $now_date)
  63. {
  64. $is_open = FALSE;
  65. }
  66. }
  67. $return[ (int)$row->id ] = array(
  68. 'id' => (int)$row->id,
  69. 'slug' => $row->slug,
  70. 'title' => $row->title,
  71. 'description' => $row->description,
  72. 'open_date' => $open_date,
  73. 'close_date' => $close_date,
  74. 'is_open' => $is_open,
  75. 'created' => $row->created ? DateTime::createFromFormat('U', $row->created) : NULL,
  76. 'last_updated' => $row->last_updated ? DateTime::createFromFormat('U', $row->last_updated) : NULL,
  77. 'type' => $row->type,
  78. 'multiple_votes' => (bool)$row->multiple_votes,
  79. 'comments_enabled' => (bool)$row->comments_enabled,
  80. 'members_only' => (bool)$row->members_only,
  81. 'active' => (bool)$row->active
  82. );
  83. }
  84. }
  85. // Return all polls
  86. return $return;
  87. }
  88. /**
  89. * Get an individual poll
  90. *
  91. * @access public
  92. * @param int Poll ID
  93. * @param boolean By default we shall get both active and inactive polls
  94. * @return array|null
  95. */
  96. public function retrieve_poll($id, $only_active = FALSE)
  97. {
  98. $poll = $this->retrieve_polls($only_active, $id);
  99. return ( is_array($poll) AND count($poll) === 1 ) ? array_shift($poll) : NULL;
  100. }
  101. /**
  102. * Make sure a poll exists
  103. *
  104. * @access public
  105. * @param int poll ID
  106. * @return bool
  107. */
  108. public function poll_exists($id)
  109. {
  110. $query = $this->db
  111. ->where('id', $id)
  112. ->limit(1)
  113. ->get('polls');
  114. return $query->num_rows();
  115. }
  116. /**
  117. * Get the poll ID from its slug
  118. *
  119. * @access public
  120. * @param int poll slug
  121. * @return int|bool
  122. */
  123. public function get_poll_id_from_slug($slug)
  124. {
  125. $query = $this->db
  126. ->select('id')
  127. ->where('slug', $slug)
  128. ->limit(1)
  129. ->get('polls');
  130. return $query->num_rows() > 0 ? (int) $query->row()->id : FALSE;
  131. }
  132. /**
  133. * Insert a new poll into the database
  134. *
  135. * @access public
  136. * @param string Title
  137. * @param string Slug
  138. * @param string Description
  139. * @param DateTime Open date
  140. * @param boolean MultipleTime Close date
  141. * @param string Type
  142. * @param boolean Multiple votes
  143. * @param boolean Comments enabled
  144. * @param boolean Members only
  145. * @return int|bool
  146. */
  147. public function insert_poll($title, $slug, $description = '', DateTime $open_date = NULL, DateTime $close_date = NULL, $type = self::TYPE_SINGLE, $multiple_votes = FALSE, $comments_enabled = FALSE, $members_only = FALSE, $active = FALSE)
  148. {
  149. // Prep data for insertion into the database
  150. $data = array(
  151. 'title' => $title,
  152. 'slug' => $slug,
  153. 'description' => $description,
  154. 'open_date' => $open_date instanceof DateTime ? $open_date->format('U') : NULL,
  155. 'close_date' => $close_date instanceof DateTime ? $close_date->format('U') : NULL,
  156. 'type' => in_array($type, self::$types) ? $type : self::TYPE_SINGLE,
  157. 'multiple_votes' => (bool)$multiple_votes,
  158. 'comments_enabled' => (bool)$comments_enabled,
  159. 'members_only' => (bool)$members_only,
  160. 'active' => (bool)$active,
  161. 'created' => time()
  162. );
  163. // Insert that data
  164. $this->db->insert('polls', $data);
  165. return $this->db->affected_rows() > 0 ? $this->db->insert_id() : FALSE;
  166. }
  167. /**
  168. * Delete a poll from the database
  169. *
  170. * @access public
  171. * @param int poll ID
  172. * @return bool
  173. */
  174. public function delete($id)
  175. {
  176. $this->db
  177. ->from('polls')
  178. ->where('id', $id)
  179. ->delete();
  180. return $this->db->affected_rows() > 0 ? TRUE : FALSE;
  181. }
  182. /**
  183. * Update an existing poll
  184. *
  185. * @access public
  186. * @param int Poll ID
  187. * @param string Title
  188. * @param string Slug
  189. * @param string Description
  190. * @param DateTime Open date
  191. * @param bool MultipleTime Close date
  192. * @param string Type
  193. * @param bool Multiple votes
  194. * @param bool Comments enabled
  195. * @param bool Members only
  196. * @return bool
  197. */
  198. public function update_poll($id, $title, $slug, $description = '', DateTime $open_date = NULL, DateTime $close_date = NULL, $type = self::TYPE_SINGLE, $multiple_votes = FALSE, $comments_enabled = FALSE, $members_only = FALSE, $active = FALSE)
  199. {
  200. // Get the poll data
  201. $data = array(
  202. 'title' => $title,
  203. 'slug' => $slug,
  204. 'description' => $description,
  205. 'open_date' => $open_date instanceof DateTime ? $open_date->format('U') : NULL,
  206. 'close_date' => $close_date instanceof DateTime ? $close_date->format('U') : NULL,
  207. 'type' => in_array($type, self::$types) ? $type : self::TYPE_SINGLE,
  208. 'multiple_votes' => (bool)$multiple_votes,
  209. 'comments_enabled' => (bool)$comments_enabled,
  210. 'members_only' => (bool)$members_only,
  211. 'active' => (bool)$active,
  212. 'last_updated' => time()
  213. );
  214. // Update poll data
  215. $this->db
  216. ->where('id', $id)
  217. ->update('polls', $data);
  218. return $this->db->affected_rows() > 0 ? TRUE : FALSE;
  219. }
  220. }