Completed
Push — master ( 73b87b...be40cf )
by Daniel
10:47
created

poll::_get_poll_length_lang()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 3
eloc 2
nc 2
nop 2
crap 12
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\services;
11
12
class poll
13
{
14
	/** @var \phpbb\auth\auth */
15
	protected $auth;
16
17
	/** @var \phpbb\config\config */
18
	protected $config;
19
20
	/** @var \phpbb\db\driver\driver_interface */
21
	protected $db;
22
23
	/** @var \phpbb\request\request_interface */
24
	protected $request;
25
26
	/** @var \phpbb\user */
27
	protected $user;
28
29
	/** @var \blitze\sitemaker\services\util */
30
	protected $sitemaker;
31
32
	/** @var string */
33
	protected $phpbb_root_path;
34
35
	/** @var string */
36
	protected $php_ext;
37
38
	/**
39
	 * Constructor
40
	 *
41
	 * @param \phpbb\auth\auth						$auth				Permission object
42
	 * @param \phpbb\config\config					$config				Config object
43
	 * @param \phpbb\db\driver\driver_interface		$db	 				Database connection
44
	 * @param \phpbb\request\request_interface		$request			Request object
45
	 * @param \phpbb\user							$user				User object
46
	 * @param \blitze\sitemaker\services\util		$sitemaker			Sitemaker Object
47
	 * @param string								$phpbb_root_path	Path to the phpbb includes directory.
48
	 * @param string								$php_ext			php file extension
49
	 */
50
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\request\request_interface $request, \phpbb\user $user, \blitze\sitemaker\services\util $sitemaker, $phpbb_root_path, $php_ext)
51
	{
52
		$this->auth = $auth;
53
		$this->config = $config;
54
		$this->db = $db;
55
		$this->request = $request;
56
		$this->user = $user;
57
		$this->sitemaker = $sitemaker;
58
		$this->phpbb_root_path = $phpbb_root_path;
59
		$this->php_ext = $php_ext;
60
	}
61
62
	/**
63
	 * @param array $topic_data
64
	 * @param \phpbb\template\twig\twig $template
65
	 */
66
	public function build(array $topic_data, \phpbb\template\twig\twig &$template)
67
	{
68
		$this->user->add_lang('viewtopic');
69
70
		$forum_id = (int) $topic_data['forum_id'];
71
		$topic_id = (int) $topic_data['topic_id'];
72
73
		$cur_voted_id = $this->_get_users_votes($topic_id);
74
		$s_can_vote = $this->_user_can_vote($forum_id, $topic_data, $cur_voted_id);
75
		$viewtopic_url = append_sid("{$this->phpbb_root_path}viewtopic.{$this->php_ext}", "f=$forum_id&amp;t=$topic_id");
76
77
		$poll_total = $poll_most = 0;
78
		$poll_info = $this->_get_poll_info($topic_data, $poll_total, $poll_most);
79
		$poll_end = $topic_data['poll_length'] + $topic_data['poll_start'];
80
81
		$this->_build_poll_options($cur_voted_id, $poll_info, $poll_total, $poll_most, $template);
82
83
		$template->assign_vars(array(
84
			'POLL_QUESTION'		=> $topic_data['poll_title'],
85
			'TOTAL_VOTES' 		=> $poll_total,
86
			'POLL_LEFT_CAP_IMG'	=> $this->user->img('poll_left'),
87
			'POLL_RIGHT_CAP_IMG'=> $this->user->img('poll_right'),
88
89
			'MAX_VOTES'			=> $this->user->lang('MAX_OPTIONS_SELECT', (int) $topic_data['poll_max_options']),
90
			'POLL_LENGTH'		=> $this->_get_poll_length_lang($topic_data['poll_length'], $poll_end),
91
92
			'S_CAN_VOTE'		=> $s_can_vote,
93
			'S_DISPLAY_RESULTS'	=> $this->_show_results($s_can_vote, $cur_voted_id),
94
			'S_IS_MULTI_CHOICE'	=> $this->_poll_is_multiple_choice($topic_data['poll_max_options']),
95
			'S_POLL_ACTION'		=> $viewtopic_url,
96
			'S_FORM_TOKEN'		=> $this->sitemaker->get_form_key('posting'),
97
98
			'U_VIEW_RESULTS'	=> $viewtopic_url . '&amp;view=viewpoll',
99
		));
100
	}
101
102
	/**
103
	 * @param int $forum_id
104
	 * @param array $topic_data
105
	 * @param array $cur_voted_id
106
	 * @return bool
107
	 */
108
	private function _user_can_vote($forum_id, array $topic_data, array $cur_voted_id)
109
	{
110
		return ($this->auth->acl_get('f_vote', $forum_id) &&
111
			(($topic_data['poll_length'] != 0 && $topic_data['poll_start'] + $topic_data['poll_length'] > time()) || $topic_data['poll_length'] == 0) &&
112
			$topic_data['topic_status'] != ITEM_LOCKED &&
113
			$topic_data['forum_status'] != ITEM_LOCKED &&
114
			(!sizeof($cur_voted_id) ||
115
			($this->auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change']))) ? true : false;
116
	}
117
118
	/**
119
	 * @param array $topic_data
120
	 * @param int $poll_total
121
	 * @param int $poll_most
122
	 * @return array
123
	 */
124
	private function _get_poll_info(array $topic_data, &$poll_total, &$poll_most)
125
	{
126
		$topic_id = (int) $topic_data['topic_id'];
127
		$post_id = (int) $topic_data['topic_first_post_id'];
128
129
		$sql = 'SELECT o.*, p.bbcode_bitfield, p.bbcode_uid
130
			FROM ' . POLL_OPTIONS_TABLE . ' o, ' . POSTS_TABLE . " p
131
			WHERE o.topic_id = $topic_id
132
				AND p.post_id = $post_id
133
				AND p.topic_id = o.topic_id
134
			ORDER BY o.poll_option_id";
135
		$result = $this->db->sql_query($sql);
136
137
		$poll_info = array();
138
		while ($row = $this->db->sql_fetchrow($result))
139
		{
140
			$poll_info[] = $row;
141
			$poll_total += $row['poll_option_total'];
142
			$poll_most = ($row['poll_option_total'] >= $poll_most) ? $row['poll_option_total'] : $poll_most;
143
		}
144
		$this->db->sql_freeresult($result);
145
146
		return $this->_parse_poll($topic_data, $poll_info);
147
	}
148
149
	/**
150
	 * @param array $topic_data
151
	 * @param array $poll_info
152
	 * @return array
153
	 */
154
	private function _parse_poll(array &$topic_data, array $poll_info)
155
	{
156
		$parse_flags = ($poll_info[0]['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
157
158
		for ($i = 0, $size = sizeof($poll_info); $i < $size; $i++)
159
		{
160
			$poll_info[$i]['poll_option_text'] = generate_text_for_display($poll_info[$i]['poll_option_text'], $poll_info[$i]['bbcode_uid'], $poll_info[$i]['bbcode_bitfield'], $parse_flags, true);
161
		}
162
163
		$topic_data['poll_title'] = generate_text_for_display($topic_data['poll_title'], $poll_info[0]['bbcode_uid'], $poll_info[0]['bbcode_bitfield'], $parse_flags, true);
164
165
		return $poll_info;
166
	}
167
168
	/**
169
	 * @param array $cur_voted_id
170
	 * @param array $poll_info
171
	 * @param int $poll_total
172
	 * @param int $poll_most
173
	 * @param \phpbb\template\twig\twig $template
174
	 */
175
	private function _build_poll_options(array $cur_voted_id, array $poll_info, $poll_total, $poll_most, \phpbb\template\twig\twig &$template)
176
	{
177
		foreach ($poll_info as $poll_option)
178
		{
179
			$option_pct = $this->_calculate_option_percent($poll_option['poll_option_total'], $poll_total);
180
			$option_pct_rel = $this->_calculate_option_percent_rel($poll_option['poll_option_total'], $poll_most);
181
182
			$template->assign_block_vars('poll_option', array(
183
				'POLL_OPTION_ID' 			=> $poll_option['poll_option_id'],
184
				'POLL_OPTION_CAPTION' 		=> $poll_option['poll_option_text'],
185
				'POLL_OPTION_RESULT' 		=> $poll_option['poll_option_total'],
186
				'POLL_OPTION_PERCENT' 		=> sprintf("%.1d%%", round($option_pct * 100)),
187
				'POLL_OPTION_PERCENT_REL' 	=> sprintf("%.1d%%", round($option_pct_rel * 100)),
188
				'POLL_OPTION_PCT'			=> round($option_pct * 100),
189
				'POLL_OPTION_WIDTH'     	=> round($option_pct * 250),
190
				'POLL_OPTION_VOTED'			=> $this->_user_has_voted_option($poll_option['poll_option_id'], $cur_voted_id),
191
				'POLL_OPTION_MOST_VOTES'	=> $this->_is_most_voted($poll_option['poll_option_total'], $poll_most),
192
			));
193
		}
194
	}
195
196
	/**
197
	 * @param int $topic_id
198
	 * @return array
199
	 */
200
	private function _get_users_votes($topic_id)
201
	{
202
		$cur_voted_id = array();
203
		if ($this->user->data['is_registered'])
204
		{
205
			$sql = 'SELECT poll_option_id
206
			FROM ' . POLL_VOTES_TABLE . '
207
			WHERE topic_id = ' . (int) $topic_id . '
208
				AND vote_user_id = ' . $this->user->data['user_id'];
209
			$result = $this->db->sql_query($sql);
210
211
			while ($row = $this->db->sql_fetchrow($result))
212
			{
213
				$cur_voted_id[] = $row['poll_option_id'];
214
			}
215
			$this->db->sql_freeresult($result);
216
		}
217
		else
218
		{
219
			// Cookie based guest tracking ... I don't like this but hum ho
220
			// it's oft requested. This relies on "nice" users who don't feel
221
			// the need to delete cookies to mess with results.
222
			if ($this->request->is_set($this->config['cookie_name'] . '_poll_' . $topic_id, \phpbb\request\request_interface::COOKIE))
223
			{
224
				$cur_voted_id = explode(',', $this->request->variable($this->config['cookie_name'] . '_poll_' . $topic_id, '', true, \phpbb\request\request_interface::COOKIE));
225
				$cur_voted_id = array_map('intval', $cur_voted_id);
226
			}
227
		}
228
229
		return $cur_voted_id;
230
	}
231
232
	/**
233
	 * @param int $poll_option_id
234
	 * @param array $cur_voted_id
235
	 * @return bool
236
	 */
237
	private function _user_has_voted_option($poll_option_id, array $cur_voted_id)
238
	{
239
		return (in_array($poll_option_id, $cur_voted_id)) ? true : false;
240
	}
241
242
	/**
243
	 * @param int $poll_option_total
244
	 * @param int $poll_total
245
	 * @return float|int
246
	 */
247
	private function _calculate_option_percent($poll_option_total, $poll_total)
248
	{
249
		return ($poll_total > 0) ? $poll_option_total / $poll_total : 0;
250
	}
251
252
	/**
253
	 * @param int $poll_option_total
254
	 * @param int $poll_most
255
	 * @return float|int
256
	 */
257
	private function _calculate_option_percent_rel($poll_option_total, $poll_most)
258
	{
259
		return ($poll_most > 0) ? $poll_option_total / $poll_most : 0;
260
	}
261
262
	/**
263
	 * @param int $poll_option_total
264
	 * @param int $poll_most
265
	 * @return bool
266
	 */
267
	private function _is_most_voted($poll_option_total, $poll_most)
268
	{
269
		return ($poll_option_total > 0 && $poll_option_total == $poll_most) ? true : false;
270
	}
271
272
	/**
273
	 * @param int $poll_max_options
274
	 * @return bool
275
	 */
276
	private function _poll_is_multiple_choice($poll_max_options)
277
	{
278
		return ($poll_max_options > 1) ? true : false;
279
	}
280
281
	/**
282
	 * @param int $poll_length
283
	 * @param int $poll_end
284
	 * @return string
285
	 */
286
	private function _get_poll_length_lang($poll_length, $poll_end)
287
	{
288
		return ($poll_length) ? sprintf($this->user->lang(($poll_end > time()) ? 'POLL_RUN_TILL' : 'POLL_ENDED_AT'), $this->user->format_date($poll_end)) : '';
289
	}
290
291
	/**
292
	 * @param bool $s_can_vote
293
	 * @param array $cur_voted_id
294
	 * @return bool
295
	 */
296
	private function _show_results($s_can_vote, array $cur_voted_id)
297
	{
298
		return (!$s_can_vote || ($s_can_vote && sizeof($cur_voted_id))) ? true : false;
299
	}
300
}
301