Completed
Push — patch_1-1-4 ( 3f780f...826343 )
by Emanuele
25:17 queued 11:40
created

Stats.template.php ➔ template_forum_history()   C

Complexity

Conditions 14
Paths 77

Size

Total Lines 141

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
nc 77
nop 0
dl 0
loc 141
rs 5.0133
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
 * @name      ElkArte Forum
5
 * @copyright ElkArte Forum contributors
6
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
7
 *
8
 * This file contains code covered by:
9
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
10
 * license:  	BSD, See included LICENSE.TXT for terms and conditions.
11
 *
12
 * @version 1.1
13
 *
14
 */
15
16
/**
17
 * Stats page.
18
 */
19
function template_statistics()
20
{
21
	global $context;
22
23
	echo '
24
	<div id="statistics" class="forum_category">
25
		<h2 class="category_header">
26
			', $context['page_title'], '
27
		</h2>
28
		<ul class="statistics">';
29
30
	foreach ($context['statistics_callbacks'] as $callback)
31
	{
32
		$function = 'template_' . $callback;
33
		$function();
34
	}
35
36
	echo '
37
		</ul>
38
	</div>';
39
40
	template_forum_history();
41
}
42
43
/**
44
 * Used to show the general statistics blocks
45
 */
46
function template_general_statistics()
47
{
48
	global $context, $settings, $txt;
49
50
	// These two are special formatting strings for special elements of the statistics:
51
	// The most_online value is an array composed of two elements: number and date,
52
	// they will be replaced in the foreach below with the corresponding values.
53
	// If you want to change the way to present the field, change this string,
54
	// for example if you want to show it as: "123 members on the 20/01/2010" you could use:
55
	// $settings['most_online'] = 'number members on the date';
56
	$settings['most_online'] = 'number - date';
57
58
	// Similarly to the previous one, this is a "template" for the latest_member stats
59
	// The elements available to style this entry are: id, name, href, link.
60
	// So, if you want to change it to the plain username you could use:
61
	// $settings['latest_member'] = 'name';
62
	$settings['latest_member'] = 'link';
63
64
	echo '
65
			<li class="flow_hidden" id="top_row">
66
				<h2 class="category_header hdicon cat_img_stats_info">
67
					', $txt['general_stats'], '
68
				</h2>
69
				<dl class="stats floatleft">';
70
71 View Code Duplication
	foreach ($context['general_statistics']['left'] as $key => $value)
72
	{
73
		if (is_array($value))
74
		{
75
			if (isset($settings[$key]))
76
				$value = strtr($settings[$key], $value);
77
			else
78
				continue;
79
		}
80
81
		echo '
82
					<dt>', $txt[$key], ':</dt>
83
					<dd>', $value, '</dd>';
84
	}
85
86
	echo '
87
				</dl>
88
				<dl class="stats">';
89
90 View Code Duplication
	foreach ($context['general_statistics']['right'] as $key => $value)
91
	{
92
		if (is_array($value))
93
		{
94
			if (isset($settings[$key]))
95
				$value = strtr($settings[$key], $value);
96
			else
97
				continue;
98
		}
99
100
		echo '
101
					<dt>', $txt[$key], ':</dt>
102
					<dd>', $value, '</dd>';
103
	}
104
105
	echo '
106
				</dl>
107
			</li>';
108
}
109
110
/**
111
 * Shows "top" statistics, like top posters, top boards, top replies, etc
112
 */
113
function template_top_statistics()
114
{
115
	global $context, $txt;
116
117
	echo '
118
			<li class="flow_hidden">
119
				<h2 class="category_header floatleft hdicon cat_img_star">
120
					', $txt['top_posters'], '
121
				</h2>
122
				<dl class="stats floatleft">';
123
124 View Code Duplication
	foreach ($context['top']['posters'] as $poster)
125
	{
126
		echo '
127
					<dt>
128
						', $poster['link'], '
129
					</dt>
130
					<dd class="statsbar">
131
						<div class="bar" style="width: ', !empty($poster['post_percent']) ? $poster['post_percent'] : '0', 'px;"></div>
132
						<span class="righttext">', $poster['num_posts'], '</span>
133
					</dd>';
134
	}
135
136
	echo '
137
				</dl>
138
				<h2 class="category_header hdicon cat_img_topics">
139
					', $txt['top_boards'], '
140
				</h2>
141
				<dl class="stats">';
142
143 View Code Duplication
	foreach ($context['top']['boards'] as $board)
144
	{
145
		echo '
146
					<dt>
147
						', $board['link'], '
148
					</dt>
149
					<dd class="statsbar">
150
						<div class="bar" style="width: ', !empty($board['post_percent']) ? $board['post_percent'] : '0', 'px;"></div>
151
						<span class="righttext">', $board['num_posts'], '</span>
152
					</dd>';
153
	}
154
155
	echo '
156
				</dl>
157
			</li>
158
			<li class="flow_hidden">
159
				<h2 class="category_header floatleft hdicon cat_img_talk">
160
					', $txt['top_topics_replies'], '
161
				</h2>
162
				<dl class="stats floatleft">';
163
164 View Code Duplication
	foreach ($context['top']['topics_replies'] as $topic)
165
	{
166
		echo '
167
					<dt>
168
						', $topic['link'], '
169
					</dt>
170
					<dd class="statsbar">
171
						<div class="bar" style="width: ', !empty($topic['post_percent']) ? $topic['post_percent'] : '0', 'px;"></div>
172
						<span class="righttext">' . $topic['num_replies'] . '</span>
173
					</dd>';
174
	}
175
176
	echo '
177
				</dl>
178
				<h2 class="category_header hdicon cat_img_eye">
179
					', $txt['top_topics_views'], '
180
				</h2>
181
				<dl class="stats">';
182
183 View Code Duplication
	foreach ($context['top']['topics_views'] as $topic)
184
	{
185
		echo '
186
					<dt>', $topic['link'], '</dt>
187
					<dd class="statsbar">
188
						<div class="bar" style="width: ', !empty($topic['post_percent']) ? $topic['post_percent'] : '0', 'px;"></div>
189
						<span class="righttext">' . $topic['num_views'] . '</span>
190
					</dd>';
191
	}
192
193
	echo '
194
				</dl>
195
			</li>
196
			<li class="flow_hidden">
197
				<h2 class="category_header floatleft hdicon cat_img_write">
198
					', $txt['top_starters'], '
199
				</h2>
200
				<dl class="stats floatleft">';
201
202 View Code Duplication
	foreach ($context['top']['starters'] as $poster)
203
	{
204
		echo '
205
					<dt>
206
						', $poster['link'], '
207
					</dt>
208
					<dd class="statsbar">
209
						<div class="bar" style="width: ', !empty($poster['post_percent']) ? $poster['post_percent'] : '0', 'px;"></div>
210
						<span class="righttext">', $poster['num_topics'], '</span>
211
					</dd>';
212
	}
213
214
	echo '
215
				</dl>
216
				<h2 class="category_header hdicon cat_img_clock">
217
					', $txt['most_time_online'], '
218
				</h2>
219
				<dl class="stats">';
220
221
	foreach ($context['top']['time_online'] as $poster)
222
	{
223
		echo '
224
					<dt>
225
						', $poster['link'], '
226
					</dt>
227
					<dd class="statsbar">
228
						<div class="bar" style="width: ', !empty($poster['time_percent']) ? $poster['time_percent'] : '0', 'px;"></div>
229
						<span class="righttext">', $poster['time_online'], '</span>
230
					</dd>';
231
	}
232
233
	echo '
234
				</dl>
235
			</li>';
236
}
237
238
/**
239
 * Shows the forum history, year/month breakdown of activity such as topics, posts, members, etc
240
 */
241
function template_forum_history()
242
{
243
	global $context, $settings, $txt, $modSettings;
244
245
	echo '
246
	<div id="forum_history" class="forum_category">
247
		<h2 class="category_header hdicon cat_img_clock">
248
			', $txt['forum_history'], '
249
		</h2>
250
		<div class="flow_hidden">';
251
252
	if (!empty($context['yearly']))
253
	{
254
		echo '
255
			<table class="table_grid" id="stats">
256
				<thead>
257
					<tr>
258
						<th class="history_head lefttext">', $txt['yearly_summary'], '</th>
259
						<th class="history_head">', $txt['stats_new_topics'], '</th>
260
						<th class="history_head">', $txt['stats_new_posts'], '</th>
261
						<th class="history_head">', $txt['stats_new_members'], '</th>
262
						<th class="history_head">', $txt['most_online'], '</th>';
263
264
		if (!empty($modSettings['hitStats']))
265
			echo '
266
						<th class="history_head">', $txt['page_views'], '</th>';
267
268
		echo '
269
					</tr>
270
				</thead>
271
				<tbody>';
272
273
		foreach ($context['yearly'] as $id => $year)
274
		{
275
			echo '
276
					<tr id="year_', $id, '">
277
						<th class="stats_year lefttext">
278
							<img id="year_img_', $id, '" src="', $settings['images_url'], '/selected_open.png" alt="*" /> <a href="#year_', $id, '" id="year_link_', $id, '">', $year['year'], '</a>
279
						</th>
280
						<th>', $year['new_topics'], '</th>
281
						<th>', $year['new_posts'], '</th>
282
						<th>', $year['new_members'], '</th>
283
						<th>', $year['most_members_online'], '</th>';
284
285
			if (!empty($modSettings['hitStats']))
286
				echo '
287
						<th>', $year['hits'], '</th>';
288
289
			echo '
290
					</tr>';
291
292
			foreach ($year['months'] as $month)
293
			{
294
				echo '
295
					<tr id="tr_month_', $month['id'], '">
296
						<th class="stats_month lefttext">
297
							<img src="', $settings['images_url'], '/', $month['expanded'] ? 'selected_open.png' : 'selected.png', '" alt="" id="img_', $month['id'], '" /> <a id="m', $month['id'], '" href="', $month['href'], '">', $month['month'], ' ', $month['year'], '</a>
298
						</th>
299
						<th>', $month['new_topics'], '</th>
300
						<th>', $month['new_posts'], '</th>
301
						<th>', $month['new_members'], '</th>
302
						<th>', $month['most_members_online'], '</th>';
303
304
				if (!empty($modSettings['hitStats']))
305
					echo '
306
						<th>', $month['hits'], '</th>';
307
308
				echo '
309
					</tr>';
310
311
				if ($month['expanded'])
312
				{
313
					foreach ($month['days'] as $day)
314
					{
315
						echo '
316
					<tr id="tr_day_', $day['year'], '-', $day['month'], '-', $day['day'], '">
317
						<td class="stats_day lefttext">', $day['year'], '-', $day['month'], '-', $day['day'], '</td>
318
						<td>', $day['new_topics'], '</td>
319
						<td>', $day['new_posts'], '</td>
320
						<td>', $day['new_members'], '</td>
321
						<td>', $day['most_members_online'], '</td>';
322
323
						if (!empty($modSettings['hitStats']))
324
							echo '
325
						<td>', $day['hits'], '</td>';
326
327
						echo '
328
					</tr>';
329
					}
330
				}
331
			}
332
		}
333
334
		echo '
335
				</tbody>
336
			</table>
337
		</div>
338
	</div>
339
	<script>
340
		var oStatsCenter = new elk_StatsCenter({
341
			sTableId: \'stats\',
342
343
			reYearPattern: /year_(\d+)/,
344
			sYearImageCollapsed: \'selected.png\',
345
			sYearImageExpanded: \'selected_open.png\',
346
			sYearImageIdPrefix: \'year_img_\',
347
			sYearLinkIdPrefix: \'year_link_\',
348
349
			reMonthPattern: /tr_month_(\d+)/,
350
			sMonthImageCollapsed: \'selected.png\',
351
			sMonthImageExpanded: \'selected_open.png\',
352
			sMonthImageIdPrefix: \'img_\',
353
			sMonthLinkIdPrefix: \'m\',
354
355
			reDayPattern: /tr_day_(\d+-\d+-\d+)/,
356
			sDayRowClassname: \'\',
357
			sDayRowIdPrefix: \'tr_day_\',
358
359
			aCollapsedYears: [';
360
361
		foreach ($context['collapsed_years'] as $id => $year)
362
		{
363
			echo '
364
				\'', $year, '\'', $id != count($context['collapsed_years']) - 1 ? ',' : '';
365
		}
366
367
		echo '
368
			],
369
370
			aDataCells: [
371
				\'date\',
372
				\'new_topics\',
373
				\'new_posts\',
374
				\'new_members\',
375
				\'most_members_online\'', empty($modSettings['hitStats']) ? '' : ',
376
				\'hits\'', '
377
			]
378
		});
379
	</script>';
380
	}
381
}
382