PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/Portal_XL50_Premod-3.0.8/phpBB3/portal/block/poll_simple.php

https://github.com/Lucky65/phpBB-Portal-XL-5.0-italian
PHP | 404 lines | 283 code | 67 blank | 54 comment | 59 complexity | 2608ee10109fc594f19fdb267fa1b649 MD5 | raw file
  1. <?php
  2. /*
  3. *
  4. * @name poll_simple.php
  5. * @package phpBB3 Portal XL 5.0
  6. * @version $Id: poll_simple.php,v 1.3 2010/10/31 damysterious Exp $
  7. *
  8. * @copyright (c) 2007, 2010 Portal XL Group
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. *
  11. */
  12. if (!defined('IN_PHPBB'))
  13. {
  14. exit;
  15. }
  16. /**
  17. */
  18. /*
  19. * Start session management
  20. */
  21. $user->add_lang('viewtopic');
  22. /*
  23. * Begin block script here
  24. */
  25. global $portal_config, $config, $phpbb_root_path, $db, $user, $template, $phpEx;
  26. // include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
  27. /**
  28. * @ignore
  29. */
  30. // Initial var setup
  31. $forum_id = request_var('f', $portal_config['portal_poll_forum_id']);
  32. $topic_id = request_var('t', $portal_config['portal_poll_topic_id']);
  33. $post_id = request_var('p', $portal_config['portal_poll_post_id']);
  34. $voted_id = request_var('vote_id', array('' => 0));
  35. $start = request_var('start', 0);
  36. $view = request_var('view', '');
  37. $sort_days = request_var('st', ((!empty($user->data['user_post_show_days'])) ? $user->data['user_post_show_days'] : 0));
  38. $sort_key = request_var('sk', ((!empty($user->data['user_post_sortby_type'])) ? $user->data['user_post_sortby_type'] : 't'));
  39. $sort_dir = request_var('sd', ((!empty($user->data['user_post_sortby_dir'])) ? $user->data['user_post_sortby_dir'] : 'a'));
  40. $update = request_var('update', false);
  41. $u_sort_param = '';
  42. // Do we have a topic or post id?
  43. if (!$topic_id && !$post_id)
  44. {
  45. // trigger_error('NO_TOPIC');
  46. $portal_config['portal_poll_topic_id'] = false;
  47. }
  48. // Find topic id if user requested a newer or older topic
  49. if ($view && !$post_id)
  50. {
  51. if (!$forum_id)
  52. {
  53. $sql = 'SELECT forum_id
  54. FROM ' . TOPICS_TABLE . "
  55. WHERE topic_id = $topic_id";
  56. $result = $db->sql_query($sql);
  57. $forum_id = (int) $db->sql_fetchfield('forum_id');
  58. $db->sql_freeresult($result);
  59. if (!$forum_id)
  60. {
  61. trigger_error('NO_TOPIC');
  62. }
  63. }
  64. // Check for global announcement correctness?
  65. if ((!isset($row) || !$row['forum_id']) && !$forum_id)
  66. {
  67. //trigger_error('NO_TOPIC');
  68. $portal_config['portal_poll_topic_id'] = false;
  69. }
  70. else if (isset($row) && $row['forum_id'])
  71. {
  72. $forum_id = $row['forum_id'];
  73. }
  74. }
  75. // This rather complex gaggle of code handles querying for topics but
  76. // also allows for direct linking to a post (and the calculation of which
  77. // page the post is on and the correct display of viewtopic)
  78. $sql_array = array(
  79. 'SELECT' => 't.*, f.*',
  80. 'FROM' => array(
  81. FORUMS_TABLE => 'f',
  82. )
  83. );
  84. if ($user->data['is_registered'])
  85. {
  86. $sql_array['SELECT'] .= ', tw.notify_status';
  87. $sql_array['LEFT_JOIN'] = array();
  88. $sql_array['LEFT_JOIN'][] = array(
  89. 'FROM' => array(TOPICS_WATCH_TABLE => 'tw'),
  90. 'ON' => 'tw.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tw.topic_id'
  91. );
  92. }
  93. if (!$post_id)
  94. {
  95. $sql_array['WHERE'] = "t.topic_id = $topic_id";
  96. }
  97. else
  98. {
  99. $sql_array['WHERE'] = "p.post_id = $post_id AND t.topic_id = p.topic_id" . ((!$auth->acl_get('m_approve', $forum_id)) ? ' AND p.post_approved = 1' : '');
  100. $sql_array['FROM'][POSTS_TABLE] = 'p';
  101. }
  102. $sql_array['WHERE'] .= ' AND (f.forum_id = t.forum_id';
  103. $sql_array['WHERE'] .= ')';
  104. $sql_array['FROM'][TOPICS_TABLE] = 't';
  105. // Join to forum table on topic forum_id unless topic forum_id is zero
  106. // whereupon we join on the forum_id passed as a parameter ... this
  107. // is done so navigation, forum name, etc. remain consistent with where
  108. // user clicked to view a global topic
  109. $sql = $db->sql_build_query('SELECT', $sql_array);
  110. $result = $db->sql_query($sql);
  111. $topic_data = $db->sql_fetchrow($result);
  112. $db->sql_freeresult($result);
  113. if (!$topic_data)
  114. {
  115. // If post_id was submitted, we try at least to display the topic as a last resort...
  116. if ($post_id && $forum_id && $topic_id)
  117. {
  118. redirect(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id"));
  119. }
  120. //trigger_error('NO_TOPIC');
  121. $portal_config['portal_poll_topic_id'] = false;
  122. }
  123. $forum_id = (int) $topic_data['forum_id'];
  124. $topic_id = (int) $topic_data['topic_id'];
  125. // Setup look and feel
  126. $user->setup('viewtopic', $topic_data['forum_style']);
  127. if (!$topic_data['topic_approved'] && !$auth->acl_get('m_approve', $forum_id))
  128. {
  129. //trigger_error('NO_TOPIC');
  130. $portal_config['portal_poll_topic_id'] = false;
  131. }
  132. // Start auth check
  133. if (!$auth->acl_get('f_read', $forum_id))
  134. {
  135. if ($user->data['user_id'] != ANONYMOUS)
  136. {
  137. //trigger_error('SORRY_AUTH_READ');
  138. $portal_config['portal_poll_topic_id'] = false;
  139. }
  140. //login_box('', $user->lang['LOGIN_VIEWFORUM']);
  141. $portal_config['portal_poll_topic_id'] = false;
  142. }
  143. // Forum is passworded ... check whether access has been granted to this
  144. // user this session, if not show login box
  145. if ($topic_data['forum_password'])
  146. {
  147. login_forum_box($topic_data);
  148. }
  149. // General Viewtopic URL for return links
  150. $viewtopic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;start=$start&amp;$u_sort_param");
  151. // Does this topic contain a poll?
  152. if (!empty($topic_data['poll_start']))
  153. {
  154. $sql = 'SELECT o.*, p.bbcode_bitfield, p.bbcode_uid
  155. FROM ' . POLL_OPTIONS_TABLE . ' o, ' . POSTS_TABLE . " p
  156. WHERE o.topic_id = $topic_id
  157. AND p.post_id = {$topic_data['topic_first_post_id']}
  158. AND p.topic_id = o.topic_id
  159. ORDER BY o.poll_option_id";
  160. $result = $db->sql_query($sql);
  161. $poll_info = array();
  162. while ($row = $db->sql_fetchrow($result))
  163. {
  164. $poll_info[] = $row;
  165. }
  166. $db->sql_freeresult($result);
  167. $cur_voted_id = array();
  168. if ($user->data['is_registered'])
  169. {
  170. $sql = 'SELECT poll_option_id
  171. FROM ' . POLL_VOTES_TABLE . '
  172. WHERE topic_id = ' . $topic_id . '
  173. AND vote_user_id = ' . $user->data['user_id'];
  174. $result = $db->sql_query($sql);
  175. while ($row = $db->sql_fetchrow($result))
  176. {
  177. $cur_voted_id[] = $row['poll_option_id'];
  178. }
  179. $db->sql_freeresult($result);
  180. }
  181. else
  182. {
  183. // Cookie based guest tracking ... I don't like this but hum ho
  184. // it's oft requested. This relies on "nice" users who don't feel
  185. // the need to delete cookies to mess with results.
  186. if (isset($_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]))
  187. {
  188. $cur_voted_id = explode(',', $_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]);
  189. $cur_voted_id = array_map('intval', $cur_voted_id);
  190. }
  191. }
  192. $s_can_vote = (((!sizeof($cur_voted_id) && $auth->acl_get('f_vote', $forum_id)) ||
  193. ($auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change'])) &&
  194. (($topic_data['poll_length'] != 0 && $topic_data['poll_start'] + $topic_data['poll_length'] > time()) || $topic_data['poll_length'] == 0) &&
  195. $topic_data['topic_status'] != ITEM_LOCKED &&
  196. $topic_data['forum_status'] != ITEM_LOCKED) ? true : false;
  197. $s_display_results = (!$s_can_vote || ($s_can_vote && sizeof($cur_voted_id)) || $view == 'viewpoll') ? true : false;
  198. if ($update && $s_can_vote)
  199. {
  200. if (!sizeof($voted_id) || sizeof($voted_id) > $topic_data['poll_max_options'])
  201. {
  202. $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id");
  203. meta_refresh(5, $redirect_url);
  204. $message = (!sizeof($voted_id)) ? 'NO_VOTE_OPTION' : 'TOO_MANY_VOTE_OPTIONS';
  205. $message = $user->lang[$message] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
  206. trigger_error($message);
  207. }
  208. foreach ($voted_id as $option)
  209. {
  210. if (in_array($option, $cur_voted_id))
  211. {
  212. continue;
  213. }
  214. $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
  215. SET poll_option_total = poll_option_total + 1
  216. WHERE poll_option_id = ' . (int) $option . '
  217. AND topic_id = ' . (int) $topic_id;
  218. $db->sql_query($sql);
  219. if ($user->data['is_registered'])
  220. {
  221. $sql_ary = array(
  222. 'topic_id' => (int) $topic_id,
  223. 'poll_option_id' => (int) $option,
  224. 'vote_user_id' => (int) $user->data['user_id'],
  225. 'vote_user_ip' => (string) $user->ip,
  226. );
  227. $sql = 'INSERT INTO ' . POLL_VOTES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
  228. $db->sql_query($sql);
  229. }
  230. }
  231. foreach ($cur_voted_id as $option)
  232. {
  233. if (!in_array($option, $voted_id))
  234. {
  235. $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
  236. SET poll_option_total = poll_option_total - 1
  237. WHERE poll_option_id = ' . (int) $option . '
  238. AND topic_id = ' . (int) $topic_id;
  239. $db->sql_query($sql);
  240. if ($user->data['is_registered'])
  241. {
  242. $sql = 'DELETE FROM ' . POLL_VOTES_TABLE . '
  243. WHERE topic_id = ' . (int) $topic_id . '
  244. AND poll_option_id = ' . (int) $option . '
  245. AND vote_user_id = ' . (int) $user->data['user_id'];
  246. $db->sql_query($sql);
  247. }
  248. }
  249. }
  250. if ($user->data['user_id'] == ANONYMOUS && !$user->data['is_bot'])
  251. {
  252. $user->set_cookie('poll_' . $topic_id, implode(',', $voted_id), time() + 31536000);
  253. }
  254. $sql = 'UPDATE ' . TOPICS_TABLE . '
  255. SET poll_last_vote = ' . time() . "
  256. WHERE topic_id = $topic_id";
  257. //, topic_last_post_time = ' . time() . " -- for bumping topics with new votes, ignore for now
  258. $db->sql_query($sql);
  259. $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id");
  260. meta_refresh(5, $redirect_url);
  261. trigger_error($user->lang['VOTE_SUBMITTED'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>'));
  262. }
  263. $poll_total = 0;
  264. foreach ($poll_info as $poll_option)
  265. {
  266. $poll_total += $poll_option['poll_option_total'];
  267. }
  268. if ($poll_info[0]['bbcode_bitfield'])
  269. {
  270. $poll_bbcode = new bbcode();
  271. }
  272. else
  273. {
  274. $poll_bbcode = false;
  275. }
  276. for ($i = 0, $size = sizeof($poll_info); $i < $size; $i++)
  277. {
  278. $poll_info[$i]['poll_option_text'] = censor_text($poll_info[$i]['poll_option_text']);
  279. $poll_info[$i]['poll_option_text'] = str_replace("\n", '<br />', $poll_info[$i]['poll_option_text']);
  280. if ($poll_bbcode !== false)
  281. {
  282. $poll_bbcode->bbcode_second_pass($poll_info[$i]['poll_option_text'], $poll_info[$i]['bbcode_uid'], $poll_option['bbcode_bitfield']);
  283. }
  284. $poll_info[$i]['poll_option_text'] = smiley_text($poll_info[$i]['poll_option_text']);
  285. }
  286. $topic_data['poll_title'] = censor_text($topic_data['poll_title']);
  287. $topic_data['poll_title'] = str_replace("\n", '<br />', $topic_data['poll_title']);
  288. if ($poll_bbcode !== false)
  289. {
  290. $poll_bbcode->bbcode_second_pass($topic_data['poll_title'], $poll_info[0]['bbcode_uid'], $poll_info[0]['bbcode_bitfield']);
  291. }
  292. $topic_data['poll_title'] = smiley_text($topic_data['poll_title']);
  293. unset($poll_bbcode);
  294. foreach ($poll_info as $poll_option)
  295. {
  296. $option_pct = ($poll_total > 0) ? $poll_option['poll_option_total'] / $poll_total : 0;
  297. $option_pct_txt = sprintf("%.1d%%", ($option_pct * 100));
  298. $template->assign_block_vars('poll_option', array(
  299. 'POLL_OPTION_ID' => $poll_option['poll_option_id'],
  300. 'POLL_OPTION_CAPTION' => $poll_option['poll_option_text'],
  301. 'POLL_OPTION_RESULT' => $poll_option['poll_option_total'],
  302. 'POLL_OPTION_PERCENT' => $option_pct_txt,
  303. 'POLL_OPTION_PCT' => round($option_pct * 100),
  304. 'POLL_OPTION_IMG' => $user->img('poll_center', $option_pct_txt, round($option_pct * 10)),
  305. 'POLL_OPTION_VOTED' => (in_array($poll_option['poll_option_id'], $cur_voted_id)) ? true : false)
  306. );
  307. }
  308. // www.phpBB-SEO.com SEO TOOLKIT BEGIN
  309. $phpbb_seo->set_url($topic_data['forum_name'], $topic_data['forum_id'], $phpbb_seo->seo_static['forum']);
  310. $phpbb_seo->prepare_iurl($topic_data, 'topic', $topic_data['topic_type'] == POST_GLOBAL ? $phpbb_seo->seo_static['global_announce'] : $phpbb_seo->seo_url['forum'][$topic_data['forum_id']]);
  311. $phpbb_seo->set_user_url( $topic_data['username'], $topic_data['user_id'] );
  312. // www.phpBB-SEO.com SEO TOOLKIT END
  313. $portalvote_url = append_sid("{$phpbb_root_path}portal.$phpEx", 'f=' . $forum_id . '&amp;t=' . $topic_id);
  314. // $portalvote_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $forum_id . '&amp;t=' . $topic_id);
  315. $viewtopic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $forum_id . '&amp;t=' . $topic_id);
  316. $poll_end = $topic_data['poll_length'] + $topic_data['poll_start'];
  317. $template->assign_vars(array(
  318. 'POLL_QUESTION' => $topic_data['poll_title'],
  319. 'TOTAL_VOTES' => $poll_total,
  320. 'POLL_LEFT_CAP_IMG' => $user->img('poll_left'),
  321. 'POLL_RIGHT_CAP_IMG'=> $user->img('poll_right'),
  322. 'L_MAX_VOTES' => ($topic_data['poll_max_options'] == 1) ? $user->lang['MAX_OPTION_SELECT'] : sprintf($user->lang['MAX_OPTIONS_SELECT'], $topic_data['poll_max_options']),
  323. 'L_POLL_LENGTH' => ($topic_data['poll_length']) ? sprintf($user->lang[($poll_end > time()) ? 'POLL_RUN_TILL' : 'POLL_ENDED_AT'], $user->format_date($poll_end)) : '',
  324. 'S_HAS_POLL' => true,
  325. 'S_CAN_VOTE' => $s_can_vote,
  326. 'S_DISPLAY_RESULTS' => $s_display_results,
  327. 'S_IS_MULTI_CHOICE' => ($topic_data['poll_max_options'] > 1) ? true : false,
  328. 'S_POLL_ACTION' => $portalvote_url,
  329. 'U_VIEW_RESULTS' => $viewtopic_url
  330. ));
  331. unset($poll_end, $poll_info, $voted_id);
  332. }
  333. // Set the filename of the template you want to use for this file.
  334. $template->set_filenames(array(
  335. 'body' => 'portal/block/poll_simple.html',
  336. ));
  337. ?>