PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/polls.php

https://github.com/Bigjoos/U-232-V1
PHP | 391 lines | 253 code | 90 blank | 48 comment | 32 complexity | 5b09485369bb7130f2ff4d9596804696 MD5 | raw file
  1. <?php
  2. /**
  3. * http://btdev.net:1337/svn/test/Installer09_Beta
  4. * Licence Info: GPL
  5. * Copyright (C) 2010 BTDev Installer v.1
  6. * A bittorrent tracker source based on TBDev.net/tbsource/bytemonsoon.
  7. * Project Leaders: Mindless,putyn.
  8. **/
  9. function parse_poll()
  10. {
  11. global $CURUSER, $TBDEV, $mc1;
  12. $htmlout = "";
  13. $check = 0;
  14. $poll_footer = "";
  15. $GVARS = array('allow_creator_vote' => 1,
  16. 'allow_result_view' => 1,
  17. 'allow_poll_tags' => 1); // move this elsewhere later!
  18. if (($poll_data = $mc1->get_value('poll_data_'.$CURUSER['id'])) == false) {
  19. //$poll_data = array();
  20. //search for a poll with given ID
  21. $query = sql_query("SELECT * FROM polls
  22. LEFT JOIN poll_voters ON polls.pid = poll_voters.poll_id
  23. AND poll_voters.user_id = {$CURUSER['id']}
  24. ORDER BY polls.start_date DESC
  25. LIMIT 1");
  26. //Did we find the poll?
  27. if ( ! mysql_num_rows($query) )
  28. {
  29. return "";
  30. }
  31. while( $row = mysql_fetch_assoc( $query ) )
  32. {
  33. $poll_data = $row;
  34. }
  35. $mc1->cache_value('poll_data_'.$CURUSER['id'], $poll_data, $TBDEV['expires']['poll_data']);
  36. }
  37. //return $poll_data;
  38. $member_voted = 0;
  39. $total_votes = 0;
  40. //Has they ever posticated before?
  41. if ( $poll_data['user_id'] ) {
  42. $member_voted = 1;
  43. //return "true";
  44. }
  45. // Make sure they can't post again
  46. if ( $member_voted )
  47. {
  48. $check = 1;
  49. $poll_footer = 'You have already voted';
  50. }
  51. //Does we want the creator to vote on their own poll?
  52. if ( ($poll_data['starter_id'] == $CURUSER['id']) and ($GVARS['allow_creator_vote'] != 1) )
  53. {
  54. $check = 1;
  55. $poll_footer = 'poll_you_created';
  56. }
  57. //The following can be setup for guest ie; no loggedinorreturn() on index
  58. /*
  59. if ( ! $CURUSER['id'] ) //$poll_data['user_id'] )
  60. {
  61. if ( !$GVARS['allow_result_view'] )
  62. {
  63. $check = 2;
  64. }
  65. else
  66. {
  67. $check = 1;
  68. }
  69. return $check.$poll_footer;
  70. $poll_footer = 'Guests can\'t view polls!';
  71. }
  72. */
  73. //allow viewing of poll results before voting?
  74. if ( $GVARS['allow_result_view'] == 1 )
  75. {
  76. if ( isset($_GET['mode']) && $_GET['mode'] == 'show' )
  77. {
  78. $check = 1;
  79. $poll_footer = "";
  80. }
  81. }
  82. if ( $check == 1 )
  83. {
  84. //ok, lets get this show on the road!
  85. $htmlout = poll_header( $poll_data['pid'], htmlentities($poll_data['poll_question'], ENT_QUOTES) );
  86. $poll_answers = unserialize(stripslashes($poll_data['choices']));
  87. reset($poll_answers);
  88. foreach ( $poll_answers as $id => $data )
  89. {
  90. //subtitle question
  91. $question = htmlentities($data['question'], ENT_QUOTES);
  92. $choice_html = "";
  93. $tv_poll = 0;
  94. //get total votes for each choice
  95. foreach( $poll_answers[ $id ]['votes'] as $number)
  96. {
  97. $tv_poll += intval( $number );
  98. }
  99. // Get the choises from the unserialised array
  100. foreach( $data['choice'] as $choice_id => $text )
  101. {
  102. $choice = htmlentities($text, ENT_QUOTES);
  103. $votes = intval($data['votes'][ $choice_id ]);
  104. if ( strlen($choice) < 1 )
  105. {
  106. continue;
  107. }
  108. if ( $GVARS['allow_poll_tags'] )
  109. {
  110. $choice = preg_replace("/\[url=([^()<>\s]+?)\]((\s|.)+?)\[\/url\]/i",
  111. "<a href=\"\\1\">\\2</a>", $choice);
  112. }
  113. $percent = $votes == 0 ? 0 : $votes / $tv_poll * 100;
  114. $percent = sprintf( '%.2f' , $percent );
  115. $width = $percent > 0 ? intval($percent * 2) : 0;
  116. $choice_html .= poll_show_rendered_choice($choice_id, $votes, $id, $choice, $percent, $width);
  117. }
  118. $htmlout .= poll_show_rendered_question( $id, $question, $choice_html );
  119. }
  120. $htmlout .= show_total_votes($tv_poll);
  121. }
  122. else if ( $check == 2 )
  123. {
  124. // only for guests when view before vote is off
  125. $htmlout = poll_header($poll_data['pid'], htmlentities($poll_data['poll_question'], ENT_QUOTES));
  126. $htmlout .= poll_show_no_guest_view( );
  127. $htmlout .= show_total_votes($total_votes);
  128. }
  129. else
  130. {
  131. $poll_answers = unserialize(stripslashes($poll_data['choices']));
  132. reset($poll_answers);
  133. //output poll form
  134. $htmlout = poll_header($poll_data['pid'], htmlentities($poll_data['poll_question'], ENT_QUOTES));
  135. foreach ( $poll_answers as $id => $data )
  136. {
  137. // get the question again!
  138. $question = htmlentities($data['question'], ENT_QUOTES);
  139. $choice_html = "";
  140. // get choices for this question
  141. foreach( $data['choice'] as $choice_id => $text )
  142. {
  143. $choice = htmlentities($text, ENT_QUOTES);
  144. $votes = intval($data['votes'][ $choice_id ]);
  145. if ( strlen($choice) < 1 )
  146. {
  147. continue;
  148. }
  149. //do we wanna allow URL's and if so convert them
  150. if ($GVARS['allow_poll_tags'])
  151. {
  152. $choice = $s = preg_replace("/\[url=([^()<>\s]+?)\]((\s|.)+?)\[\/url\]/i",
  153. "<a href=\"\\1\">\\2</a>", $choice);
  154. }
  155. if( isset($data['multi']) AND $data['multi'] == 1 )
  156. {
  157. $choice_html .= poll_show_form_choice_multi($choice_id, $votes, $id, $choice);
  158. }
  159. else
  160. {
  161. $choice_html .= poll_show_form_choice($choice_id, $votes, $id, $choice);
  162. }
  163. }
  164. $choice_html = "<table cellpadding='4' cellspacing='0'>{$choice_html}</table>";
  165. $htmlout .= poll_show_form_question( $id, $question, $choice_html );
  166. }
  167. $htmlout .= show_total_votes($total_votes);
  168. }
  169. $htmlout .= poll_footer();
  170. if ( $poll_footer != "" )
  171. {
  172. $htmlout = str_replace( "<!--VOTE-->", $poll_footer, $htmlout );
  173. }
  174. else
  175. {
  176. if ( $GVARS['allow_result_view'] == 1 )
  177. {
  178. if ( isset( $_GET['mode'] ) && $_GET['mode'] == 'show' )
  179. {
  180. $htmlout = str_replace( "<!--SHOW-->", button_show_voteable(), $htmlout );
  181. }
  182. else
  183. {
  184. $htmlout = str_replace( "<!--SHOW-->", button_show_results(), $htmlout );
  185. $htmlout = str_replace( "<!--VOTE-->", button_vote(), $htmlout );
  186. }
  187. }
  188. else
  189. {
  190. //this section not for reviewing votes!
  191. $htmlout = str_replace( "<!--VOTE-->", button_vote(), $htmlout );
  192. $htmlout = str_replace( "<!--SHOW-->", button_null_vote(), $htmlout );
  193. }
  194. }
  195. return $htmlout;
  196. }
  197. ///////////////////////////////////////////////
  198. ///////////////////////////////////////////////
  199. //AUX FUNCTIONS
  200. ///////////////////////////////////////////////
  201. ///////////////////////////////////////////////
  202. function poll_header($pid="",$poll_q="") {
  203. global $TBDEV;
  204. $HTMLOUT = "";
  205. $HTMLOUT .= "<script type=\"text/javascript\">
  206. <!-- // Hide js from html validator
  207. function go_gadget_show()
  208. {
  209. window.location = \"{$TBDEV['baseurl']}/index.php?pollid={$pid}&mode=show&st=main\";
  210. }
  211. function go_gadget_vote()
  212. {
  213. window.location = \"{$TBDEV['baseurl']}/index.php?pollid={$pid}&st=main\";
  214. }
  215. //-->
  216. </script>
  217. <form action='{$TBDEV['baseurl']}/polls_take_vote.php?pollid={$pid}&amp;st=main&amp;addpoll=1' method='post'>
  218. <div class='roundedCorners' style='text-align: left; width: 80%; border: 1px solid black; padding: 5px;'>
  219. <div style='background: none repeat scroll 0% 0% transparent; height: 25px;'><span style='font-weight: bold; font-size: 12pt;'>{$poll_q}</span></div>";
  220. return $HTMLOUT;
  221. }
  222. function poll_footer() {
  223. $HTMLOUT = "";
  224. $HTMLOUT .= "<span><!--VOTE-->&nbsp;<!--SHOW--></span>
  225. <span><!-- no content --></span>
  226. </div>
  227. </form><br />";
  228. return $HTMLOUT;
  229. }
  230. function poll_show_rendered_choice($choice_id="",$votes="",$id="",$answer="",$percentage="",$width="") {
  231. global $TBDEV;
  232. $HTMLOUT = "";
  233. $HTMLOUT .= "<tr>
  234. <td width='25%' colspan='2'>$answer</td>
  235. <td width='10%' nowrap='nowrap'> [ <b>$votes</b> ] </td>
  236. <td width='70%' nowrap='nowrap'>
  237. <img src='{$TBDEV['pic_base_url']}polls/bar.gif' width='$width' height='11' align='middle' alt='' />
  238. &nbsp;[$percentage%]
  239. </td>
  240. </tr>";
  241. return $HTMLOUT;
  242. }
  243. function poll_show_rendered_question($id="",$question="",$choice_html="") {
  244. $HTMLOUT = "";
  245. $HTMLOUT .= "
  246. <div align='center'>
  247. <div class='roundedCorners' style='text-align:center;padding:4px;'><span class='postdetails'><strong>{$question}</strong></span></div>
  248. <table cellpadding='4' cellspacing='0'>
  249. $choice_html
  250. </table>
  251. </div><br />";
  252. return $HTMLOUT;
  253. }
  254. function show_total_votes($total_votes="") {
  255. $HTMLOUT = "";
  256. $HTMLOUT .= "<div align='center'><b>Total Votes: $total_votes</b></div>";
  257. return $HTMLOUT;
  258. }
  259. function poll_show_form_choice_multi($choice_id="",$votes="",$id="",$answer="") {
  260. $HTMLOUT = "";
  261. $HTMLOUT .= "<tr>
  262. <td colspan='3'><input type='checkbox' name='choice_{$id}_{$choice_id}' value='1' />&nbsp;<b>$answer</b></td>
  263. </tr>";
  264. return $HTMLOUT;
  265. }
  266. function poll_show_form_choice($choice_id="",$votes="",$id="",$answer="") {
  267. $HTMLOUT = "";
  268. $HTMLOUT .= "
  269. <tr><td nowrap='nowrap'><input type='radio' name='choice[{$id}]' value='$choice_id' />&nbsp;<strong>$answer</strong></td></tr>";
  270. return $HTMLOUT;
  271. }
  272. function poll_show_form_question($id="",$question="",$choice_html="") {
  273. $HTMLOUT = "";
  274. $HTMLOUT .= "
  275. <div align='left'>
  276. <div style='padding:4px;'><span class='postdetails'><strong>{$question}</strong></span></div>
  277. $choice_html
  278. </div>";
  279. return $HTMLOUT;
  280. }
  281. function button_show_voteable() {
  282. $HTMLOUT = "";
  283. $HTMLOUT .= "<input class='btn' type='button' name='viewresult' value='Show Votes' title='Goto poll voting' onclick=\"go_gadget_vote()\" />";
  284. return $HTMLOUT;
  285. }
  286. function button_show_results() {
  287. $HTMLOUT = "";
  288. $HTMLOUT .= "<input class='btn' type='button' value='Results' title='Show all poll rsults' onclick=\"go_gadget_show()\" />";
  289. return $HTMLOUT;
  290. }
  291. function button_vote() {
  292. $HTMLOUT = "";
  293. $HTMLOUT .= "<input class='btn' type='submit' name='submit' value='Vote' title='Poll Vote' />";
  294. return $HTMLOUT;
  295. }
  296. function button_null_vote() {
  297. $HTMLOUT = "";
  298. $HTMLOUT .= "<input class='btn' type='submit' name='nullvote' value='View Results (Null Vote)' title='View results, but forfeit your vote in this poll' />";
  299. return $HTMLOUT;
  300. }
  301. ?>