PageRenderTime 1119ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/Poll.php

https://bitbucket.org/ryanhowdy/family-connections
PHP | 475 lines | 266 code | 71 blank | 138 comment | 22 complexity | 0af2e629b382bb417d16fe8a8b51f2ae MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0
  1. <?php
  2. /**
  3. * Poll
  4. *
  5. * @package Family Connections
  6. * @copyright Copyright (c) 2010 Haudenschilt LLC
  7. * @author Ryan Haudenschilt <r.haudenschilt@gmail.com>
  8. * @license http://www.gnu.org/licenses/gpl-2.0.html
  9. */
  10. class Poll
  11. {
  12. private $user;
  13. private $error;
  14. /**
  15. * __construct
  16. *
  17. * @param string $user
  18. * @param string $error
  19. *
  20. * @return void
  21. */
  22. public function __construct ($user, $error)
  23. {
  24. $this->user = $user;
  25. $this->error = $error;
  26. }
  27. /**
  28. * getLatestPollData
  29. *
  30. * Returns an array of poll data. See getPollData() for example.
  31. *
  32. * @return array
  33. */
  34. function getLatestPollData ()
  35. {
  36. $data = array();
  37. // Get Latest poll
  38. $sql = "SELECT MAX(`id`) AS max
  39. FROM `fcms_polls`";
  40. $result = mysql_query($sql);
  41. if (!$result)
  42. {
  43. $msg = T_('Could not get latest poll information.');
  44. $debugInfo = $sql."\n".mysql_error();
  45. $this->error->add($msg, $debugInfo);
  46. return false;
  47. }
  48. $r = mysql_fetch_assoc($result);
  49. $pollId = $r['max'];
  50. // No polls exist
  51. if (is_null($pollId))
  52. {
  53. return;
  54. }
  55. return $this->getPollData($pollId);
  56. }
  57. /**
  58. * getPollData
  59. *
  60. * Example array:
  61. * Where the int keys are ids for that element.
  62. *
  63. * Array
  64. * (
  65. * [1] => Array
  66. * (
  67. * [question] => Family Connections software is...
  68. * [total_votes] => 1
  69. * [options] => Array
  70. * (
  71. * [1] => Array
  72. * (
  73. * [option] => Easy to use!
  74. * [votes] => Array
  75. * (
  76. * [total] => 0
  77. * [users] => Array
  78. * (
  79. * )
  80. * )
  81. * )
  82. * [2] => Array
  83. * (
  84. * [option] => Visually appealing!
  85. * [votes] => Array
  86. * (
  87. * [total] => 0
  88. * [users] => Array
  89. * (
  90. * )
  91. * )
  92. * )
  93. * [3] => Array
  94. * (
  95. * [option] => Just what our family needed!
  96. * [votes] => Array
  97. * (
  98. * [total] => 1
  99. * [users] => Array
  100. * (
  101. * [1] => 1
  102. * )
  103. * )
  104. * )
  105. * )
  106. * )
  107. * [users_who_voted] => Array
  108. * (
  109. * [1] => 1
  110. * )
  111. * )
  112. *
  113. * @param id $id
  114. *
  115. * @return array
  116. */
  117. function getPollData ($id)
  118. {
  119. // Get poll questions
  120. $sql = "SELECT p.`id`, `question`, o.`id` AS option_id, `option`
  121. FROM `fcms_polls` AS p
  122. LEFT JOIN `fcms_poll_options` AS o ON p.`id` = o.`poll_id`
  123. WHERE p.`id` = '$id'";
  124. $result = mysql_query($sql);
  125. if (!$result)
  126. {
  127. $msg = T_('Could not get poll information.');
  128. $debugInfo = $sql."\n".mysql_error();
  129. $this->error->add($msg, $debugInfo);
  130. return false;
  131. }
  132. while ($r = mysql_fetch_assoc($result))
  133. {
  134. $data[$r['id']]['question'] = $r['question'];
  135. $data[$r['id']]['total_votes'] = 0;
  136. $data[$r['id']]['options'][$r['option_id']] = array(
  137. 'option' => $r['option'],
  138. 'votes' => array(
  139. 'total' => 0,
  140. 'users' => array(),
  141. ),
  142. );
  143. }
  144. // Get votes
  145. $sql = "SELECT `id`, `user`, `option`, `poll_id`
  146. FROM `fcms_poll_votes`
  147. WHERE `poll_id` = '$id'";
  148. $result = mysql_query($sql);
  149. if (!$result)
  150. {
  151. $msg = T_('Could not get poll information.');
  152. $debugInfo = $sql."\n".mysql_error();
  153. $this->error->add($msg, $debugInfo);
  154. return false;
  155. }
  156. while ($r = mysql_fetch_assoc($result))
  157. {
  158. $data[$r['poll_id']]['total_votes']++;
  159. $data[$r['poll_id']]['options'][$r['option']]['votes']['total']++;
  160. $data[$r['poll_id']]['options'][$r['option']]['votes']['users'][$r['user']] = 1;
  161. $data['users_who_voted'][$r['user']] = 1;
  162. }
  163. return $data;
  164. }
  165. /**
  166. * formatPollResults
  167. *
  168. * Given an array of poll options data (from getPollData), will
  169. * return a string of html, showing the results.
  170. *
  171. * @param array $pollOptionsData
  172. *
  173. * @return void
  174. */
  175. function formatPollResults ($pollData)
  176. {
  177. $pollId = key($pollData);
  178. $totalVotes = $pollData[$pollId]['total_votes'];
  179. $usersLkup = array();
  180. if (isset($pollData['users_who_voted']))
  181. {
  182. $usersLkup = $this->getUsersAvatarName($pollData['users_who_voted']);
  183. if ($usersLkup === false)
  184. {
  185. // Errors already set
  186. return false;
  187. }
  188. }
  189. $pollOptions = '
  190. <ul class="poll-results">';
  191. $i = 1;
  192. foreach ($pollData[$pollId]['options'] as $optionId => $optionData)
  193. {
  194. $votes = $optionData['votes']['total'];
  195. $users = null;
  196. $percent = 0;
  197. $width = 0;
  198. if ($totalVotes > 0)
  199. {
  200. $percent = $votes/$totalVotes;
  201. $width = round((140 * $percent) + 10, 0);
  202. $percent = round((($votes/$totalVotes) * 100), 0);
  203. }
  204. foreach ($optionData['votes']['users'] as $userId => $val)
  205. {
  206. $users .= '<li><div onmouseover="showTooltip(this)" onmouseout="hideTooltip(this)">';
  207. $users .= '<img src="'.$usersLkup[$userId]['avatar'].'"/></div><div class="tooltip" style="display:none;">';
  208. $users .= '<h5>'.$usersLkup[$userId]['name'].'</h5></div></li>';
  209. }
  210. if (is_null($users))
  211. {
  212. $users = '<li>'.T_('None').'</li>';
  213. }
  214. $pollOptions .= '
  215. <li>
  216. <b>'.cleanOutput($optionData['option'], 'html').'</b>
  217. <span>'.sprintf(T_('%s votes'), $votes).'</span>
  218. <a href="#" onclick="$(\'who'.$i.'\').toggle(); return false;" class="progress" title="'.T_('Click to see who voted for this.').'">
  219. <div class="bar" style="width:'.$percent.'%"></div>
  220. </a>
  221. <div id="who'.$i.'" class="who-voted" style="display:none">
  222. <ul class="avatar-member-list-small">
  223. '.$users.'
  224. </ul>
  225. </div>
  226. </li>';
  227. $i++;
  228. }
  229. $pollOptions .= '
  230. </ul>';
  231. return $pollOptions;
  232. }
  233. /**
  234. * getPolls
  235. *
  236. * @param int $page
  237. *
  238. * @return array
  239. */
  240. function getPolls ($page)
  241. {
  242. $polls = array();
  243. $from = (($page * 25) - 25);
  244. $sql = "SELECT `id`, `question`, `started`
  245. FROM `fcms_polls`
  246. ORDER BY started DESC
  247. LIMIT $from, 25";
  248. $result = mysql_query($sql);
  249. if (!$result)
  250. {
  251. $msg = T_('Could not get polls information.');
  252. $debugInfo = $sql."\n".mysql_error();
  253. $this->error->add($msg, $debugInfo);
  254. return false;
  255. }
  256. while ($r = mysql_fetch_assoc($result))
  257. {
  258. $polls[] = $r;
  259. $polls['ids'][] = $r['id'];
  260. }
  261. return $polls;
  262. }
  263. /**
  264. * placeVote
  265. *
  266. * @param int $optionId
  267. * @param int $pollId
  268. *
  269. * @return boolean
  270. */
  271. function placeVote ($optionId, $pollId)
  272. {
  273. $optionId = (int)$optionId;
  274. $pollId = (int)$pollId;
  275. $sql = "SELECT `user`
  276. FROM `fcms_poll_votes`
  277. WHERE `user` = '".$this->user->id."'
  278. AND `poll_id` = '$pollId'";
  279. $result = mysql_query($sql);
  280. if (!$result)
  281. {
  282. $msg = T_('Could not get poll information.');
  283. $debugInfo = $sql."\n".mysql_error();
  284. $this->error->add($msg, $debugInfo);
  285. return false;
  286. }
  287. if (mysql_num_rows($result) > 0)
  288. {
  289. return false;
  290. }
  291. $sql = "UPDATE `fcms_poll_options`
  292. SET `votes` = `votes`+1
  293. WHERE `id` = '$optionId'";
  294. if (!mysql_query($sql))
  295. {
  296. $msg = T_('Could not update poll.');
  297. $debugInfo = $sql."\n".mysql_error();
  298. $this->error->add($msg, $debugInfo);
  299. return false;
  300. }
  301. $sql = "INSERT INTO `fcms_poll_votes`(`user`, `option`, `poll_id`)
  302. VALUES ('".$this->user->id."', '$optionId', '$pollId')";
  303. if (!mysql_query($sql))
  304. {
  305. $msg = T_('Could not add vote.');
  306. $debugInfo = $sql."\n".mysql_error();
  307. $this->error->add($msg, $debugInfo);
  308. return false;
  309. }
  310. return true;
  311. }
  312. /**
  313. * getPollsTotalVotes
  314. *
  315. * Will return the vote counts for the given list of polls.
  316. *
  317. * @param string $ids
  318. *
  319. * @return void
  320. */
  321. function getPollsTotalVotes ($ids)
  322. {
  323. $data = array();
  324. $ids = implode(',', $ids);
  325. $sql = "SELECT p.`id`, SUM(`votes`) AS total
  326. FROM `fcms_polls` AS p
  327. LEFT JOIN `fcms_poll_options` AS o ON p.`id` = o.`poll_id`
  328. WHERE p.`id` IN ($ids)
  329. GROUP BY `id`";
  330. $result = mysql_query($sql);
  331. if (!$result)
  332. {
  333. $msg = T_('Could not get polls votes.');
  334. $debugInfo = $sql."\n".mysql_error();
  335. $this->error->add($msg, $debugInfo);
  336. return false;
  337. }
  338. while ($r = mysql_fetch_assoc($result))
  339. {
  340. $data[$r['id']] = $r['total'];
  341. }
  342. return $data;
  343. }
  344. /**
  345. * getPollCommentsData
  346. *
  347. * @param int $id
  348. *
  349. * @return array
  350. */
  351. function getPollCommentsData ($id)
  352. {
  353. $comments = array('total' => 0);
  354. $sql = "SELECT c.`id`, c.`comment`, c.`created`, c.`created_id`, u.`fname`, u.`lname`, u.`avatar`, u.`gravatar`
  355. FROM `fcms_poll_comment` AS c
  356. LEFT JOIN `fcms_users` AS u ON c.`created_id` = u.`id`
  357. WHERE `poll_id` = '$id'";
  358. $result = mysql_query($sql);
  359. if (!$result)
  360. {
  361. $msg = T_('Could not get poll comments.');
  362. $debugInfo = $sql."\n".mysql_error();
  363. $this->error->add($msg, $debugInfo);
  364. return false;
  365. }
  366. while ($r = mysql_fetch_assoc($result))
  367. {
  368. $comments[] = $r;
  369. $comments['total']++;
  370. }
  371. return $comments;
  372. }
  373. /**
  374. * getUsersAvatarName
  375. *
  376. * Gets the avatar and name for the given members.
  377. *
  378. * @param array $users
  379. *
  380. * @return array
  381. */
  382. function getUsersAvatarName ($users)
  383. {
  384. $avatars = array();
  385. $ids = implode(',', array_keys($users));
  386. $sql = "SELECT `id`, `avatar`, `gravatar`, `fname`, `lname`
  387. FROM `fcms_users`
  388. WHERE `id` IN ($ids)";
  389. $result = mysql_query($sql);
  390. if (!$result)
  391. {
  392. $msg = T_('Could not get member information.');
  393. $debugInfo = $sql."\n".mysql_error();
  394. $this->error->add($msg, $debugInfo);
  395. return false;
  396. }
  397. while ($r = mysql_fetch_assoc($result))
  398. {
  399. $avatars[$r['id']]['avatar'] = getAvatarPath($r['avatar'], $r['gravatar']);
  400. $avatars[$r['id']]['name'] = $r['fname'].' '.$r['lname'];
  401. }
  402. return $avatars;
  403. }
  404. }