leaders::get_template_side()   D
last analyzed

Complexity

Conditions 19
Paths 48

Size

Total Lines 160
Code Lines 89

Duplication

Lines 39
Ratio 24.38 %

Code Coverage

Tests 0
CRAP Score 380

Importance

Changes 0
Metric Value
cc 19
eloc 89
nc 48
nop 1
dl 39
loc 160
ccs 0
cts 145
cp 0
crap 380
rs 4.764
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
*
4
* @package Board3 Portal v2.1
5
* @copyright (c) 2013 Board3 Group ( www.board3.de )
6
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
*
8
*/
9
10
namespace board3\portal\modules;
11
12
/**
13
* @package Leaders
14
*/
15
class leaders extends module_base
16
{
17
	/**
18
	* Allowed columns: Just sum up your options (Exp: left + right = 10)
19
	* top		1
20
	* left		2
21
	* center	4
22
	* right		8
23
	* bottom	16
24
	*/
25
	public $columns = 10;
26
27
	/**
28
	* Default modulename
29
	*/
30
	public $name = 'THE_TEAM';
31
32
	/**
33
	* Default module-image:
34
	* file must be in "{T_THEME_PATH}/images/portal/"
35
	*/
36
	public $image_src = 'portal_team.png';
37
38
	/**
39
	* module-language file
40
	* file must be in "language/{$user->lang}/mods/portal/"
41
	*/
42
	public $language = 'portal_leaders_module';
43
44
	/** @var \phpbb\auth\auth */
45
	protected $auth;
46
47
	/** @var \phpbb\config\config */
48
	protected $config;
49
50
	/** @var \phpbb\db\driver\driver_interface */
51
	protected $db;
52
53
	/** @var \phpbb\template\template */
54
	protected $template;
55
56
	/** @var string PHP file extension */
57
	protected $php_ext;
58
59
	/** @var string phpBB root path */
60
	protected $phpbb_root_path;
61
62
	/** @var \phpbb\user */
63
	protected $user;
64
65
	/**
66
	* Construct a leaders object
67
	*
68
	* @param \phpbb\auth\auth $auth phpBB auth service
69
	* @param \phpbb\config\config $config phpBB config
70
	* @param \phpbb\db\driver\driver_interface $db phpBB db driver
71
	* @param \phpbb\template\template $template phpBB template
72
	* @param string $phpEx php file extension
73
	* @param string $phpbb_root_path phpBB root path
74
	* @param \phpbb\user $user phpBB user object
75
	*/
76 View Code Duplication
	public function __construct($auth, $config, $db, $template, $phpbb_root_path, $phpEx, $user)
77
	{
78
		$this->auth = $auth;
79
		$this->config = $config;
80
		$this->db = $db;
81
		$this->template = $template;
82
		$this->php_ext = $phpEx;
83
		$this->phpbb_root_path = $phpbb_root_path;
84
		$this->user = $user;
85
	}
86
87
	/**
88
	* {@inheritdoc}
89
	*/
90
	public function get_template_side($module_id)
91
	{
92
		// Display a listing of board admins, moderators
93
		$this->user->add_lang('groups');
94
		$order_legend = ($this->config['legend_sort_groupname']) ? 'group_name' : 'group_legend';
95
96
		if ($this->config['board3_leaders_ext_' . $module_id])
97
		{
98
			$legends = array();
99
			$groups = array();
100
101 View Code Duplication
			if ($this->auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel'))
102
			{
103
				$sql = 'SELECT group_id, group_name, group_colour, group_type, group_legend
104
					FROM ' . GROUPS_TABLE . '
105
					WHERE group_legend >= 1
106
					ORDER BY ' . $order_legend . ' ASC';
107
			}
108
			else
109
			{
110
				$sql = 'SELECT g.group_id, g.group_name, g.group_colour, g.group_type, g.group_legend
111
					FROM ' . GROUPS_TABLE . ' g
112
					LEFT JOIN ' . USER_GROUP_TABLE . ' ug
113
						ON (
114
							g.group_id = ug.group_id
115
							AND ug.user_id = ' . $this->user->data['user_id'] . '
116
							AND ug.user_pending = 0
117
						)
118
					WHERE g.group_legend >= 1
119
						AND (g.group_type <> ' . GROUP_HIDDEN . ' OR ug.user_id = ' . $this->user->data['user_id'] . ')
120
					ORDER BY g.' . $order_legend . ' ASC';
121
			}
122
			$result = $this->db->sql_query($sql, 600);
123
124 View Code Duplication
			while ($row = $this->db->sql_fetchrow($result))
125
			{
126
				$groups[$row['group_id']] = array(
127
					'group_name'	=> $row['group_name'],
128
					'group_colour'	=> $row['group_colour'],
129
					'group_type'	=> $row['group_type'],
130
					'group_users'	=> array(),
131
				);
132
				$legends[] = $row['group_id'];
133
			}
134
			$this->db->sql_freeresult($result);
135
136
			if (sizeof($legends))
137
			{
138
				$sql = 'SELECT
139
							u.user_id AS user_id, u.username AS username, u.username_clean AS username_clean,
140
							u.user_colour AS user_colour, ug.group_id AS group_id
141
						FROM
142
							' . USERS_TABLE . ' AS u,
143
							' . USER_GROUP_TABLE . ' AS ug
144
						WHERE
145
							ug.user_id = u.user_id
146
							AND '. $this->db->sql_in_set('ug.group_id', $legends) . '
147
						ORDER BY u.username_clean ASC';
148
				$result = $this->db->sql_query($sql, 600);
149
150 View Code Duplication
				while ($row = $this->db->sql_fetchrow($result))
151
				{
152
					$groups[$row['group_id']]['group_users'][] = array(
153
						'user_id'		=> $row['user_id'],
154
						'username'		=> $row['username'],
155
						'user_colour'	=> $row['user_colour'],
156
					);
157
				}
158
				$this->db->sql_freeresult($result);
159
			}
160
161
			if (sizeof($groups))
162
			{
163
				foreach ($groups as $group_id => $group)
164
				{
165
					if (sizeof($group['group_users']))
166
					{
167
						$group_name = ($group['group_type'] == GROUP_SPECIAL) ? $this->user->lang['G_' . $group['group_name']] : $group['group_name'];
168
						$u_group = append_sid("{$this->phpbb_root_path}memberlist.{$this->php_ext}", 'mode=group&amp;g=' . $group_id);
169
170
						$this->template->assign_block_vars('group', array(
171
							'GROUP_NAME'	=> $group_name,
172
							'GROUP_COLOUR'	=> $group['group_colour'],
173
							'U_GROUP'		=> $u_group,
174
						));
175
176
						foreach ($group['group_users'] as $group_user)
177
						{
178
							$this->template->assign_block_vars('group.member', array(
179
								'USER_ID'			=> $group_user['user_id'],
180
								'USERNAME_FULL'		=> get_username_string('full', $group_user['user_id'], $group_user['username'], $group_user['user_colour']),
181
							));
182
						}
183
					}
184
				}
185
			}
186
			return 'leaders_ext_side.html';
187
		}
188
		else
189
		{
190
			$sql = $this->db->sql_build_query('SELECT', array(
191
				'SELECT'	=> 'u.user_id, u.group_id as default_group, u.username, u.user_colour, u.user_allow_pm, g.group_id, g.group_name, g.group_colour, g.group_type, g.group_legend, ug.user_id as ug_user_id',
192
				'FROM'		=> array(
193
					USERS_TABLE		=> 'u',
194
					GROUPS_TABLE	=> 'g'
195
				),
196
				'LEFT_JOIN'	=> array(
197
					array(
198
						'FROM'	=> array(USER_GROUP_TABLE => 'ug'),
199
						'ON'	=> 'ug.group_id = g.group_id AND ug.user_pending = 0 AND ug.user_id = ' . $this->user->data['user_id']
200
					)),
201
				'WHERE'		=> 'u.group_id = g.group_id AND ' . $this->db->sql_in_set('g.group_name', array('ADMINISTRATORS', 'GLOBAL_MODERATORS')),
202
				'ORDER_BY'	=> 'g.' . $order_legend . ' ASC, u.username_clean ASC'
203
			));
204
205
			$result = $this->db->sql_query($sql, 600);
206
207
			while ($row = $this->db->sql_fetchrow($result))
208
			{
209
				if ($row['group_name'] == 'ADMINISTRATORS')
210
				{
211
					$which_row = 'b3p_admins';
212
				}
213
				else if ($row['group_name'] == 'GLOBAL_MODERATORS')
214
				{
215
					$which_row = 'b3p_moderators';
216
				}
217
				else
218
				{
219
					continue;
220
				}
221
222
				if ($row['group_type'] == GROUP_HIDDEN && !$this->auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel') && $row['ug_user_id'] != $this->user->data['user_id'])
223
				{
224
					$group_name = $this->user->lang['GROUP_UNDISCLOSED'];
225
					$u_group = '';
226
				}
227
				else
228
				{
229
					$group_name = ($row['group_type'] == GROUP_SPECIAL) ? $this->user->lang['G_' . $row['group_name']] : $row['group_name'];
230
					$u_group = append_sid("{$this->phpbb_root_path}memberlist.{$this->php_ext}", 'mode=group&amp;g=' . $row['group_id']);
231
				}
232
233
				$this->template->assign_block_vars($which_row, array(
234
					'USER_ID'			=> $row['user_id'],
235
					'GROUP_NAME'		=> $group_name,
236
					'GROUP_COLOR'		=> $row['group_colour'],
237
238
					'U_GROUP'			=> $u_group,
239
240
					'USERNAME_FULL'		=> get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']),
241
					'USERNAME'			=> get_username_string('username', $row['user_id'], $row['username'], $row['user_colour']),
242
					'USER_COLOR'		=> get_username_string('colour', $row['user_id'], $row['username'], $row['user_colour']),
243
					'U_VIEW_PROFILE'	=> get_username_string('profile', $row['user_id'], $row['username'], $row['user_colour']),
244
				));
245
			}
246
			$this->db->sql_freeresult($result);
247
			return 'leaders_side.html';
248
		}
249
	}
250
251
	/**
252
	* {@inheritdoc}
253
	*/
254
	public function get_template_acp($module_id)
255
	{
256
		return array(
257
			'title'	=> 'ACP_PORTAL_LEADERS',
258
			'vars'	=> array(
259
				'legend1'				=> 'ACP_PORTAL_LEADERS',
260
				'board3_leaders_ext_' . $module_id	=> array('lang' => 'PORTAL_LEADERS_EXT',		'validate' => 'bool',	'type' => 'radio:yes_no',	'explain' => true),
261
			),
262
		);
263
	}
264
265
	/**
266
	* {@inheritdoc}
267
	*/
268
	public function install($module_id)
269
	{
270
		// Show normal team block by default
271
		$this->config->set('board3_leaders_ext_' . $module_id, 0);
272
		return true;
273
	}
274
275
	/**
276
	* {@inheritdoc}
277
	*/
278 View Code Duplication
	public function uninstall($module_id, $db)
279
	{
280
		$del_config = array(
281
			'board3_leaders_ext_' . $module_id,
282
		);
283
		$sql = 'DELETE FROM ' . CONFIG_TABLE . '
284
			WHERE ' . $db->sql_in_set('config_name', $del_config);
285
		return $db->sql_query($sql);
286
	}
287
}
288