Board_Stats_Block::formatAvg()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @package SimplePortal
5
 *
6
 * @author SimplePortal Team
7
 * @copyright 2015-2021 SimplePortal Team
8
 * @license BSD 3-clause
9
 * @version 1.0.0
10
 */
11
12
13
/**
14
 * Board Stats block, shows count of users online names
15
 *
16
 * @param mixed[] $parameters
17
 *		'averages' => Will calculate the daily average (posts, topics, registrations, etc)
18
 * @param int $id - not used in this block
19
 * @param boolean $return_parameters if true returns the configuration options for the block
20
 */
21
class Board_Stats_Block extends SP_Abstract_Block
22
{
23
	protected $total_days_up = 0;
24
25
	/**
26
	 * Constructor, used to define block parameters
27
	 *
28
	 * @param Database|null $db
29
	 */
30
	public function __construct($db = null)
31
	{
32
		$this->block_parameters = array(
33
			'averages' => 'check',
34
			'refresh_value' => 'int'
35
		);
36
37
		parent::__construct($db);
38
	}
39
40
	/**
41
	 * Initializes a block for use.
42
	 *
43
	 * - Called from portal.subs as part of the sportal_load_blocks process
44
	 *
45
	 * @param mixed[] $parameters
46
	 * @param int $id
47
	 */
48
	public function setup($parameters, $id)
49
	{
50
		global $user_info;
51
52
		$this->data['averages'] = !empty($parameters['averages']);
53
54
		loadLanguage('Stats');
55
56
		// Basic totals are easy
57
		$this->data['totals'] = ssi_boardStats('array');
58
		$this->data['totals']['mostOnline'] = $this->_modSettings['mostOnline'];
59
60
		// Get the averages from the activity log, its the most recent snapshot
61
		if ($this->data['averages'])
62
		{
63
			require_once(SUBSDIR . '/Stats.subs.php');
64
65
			// All of the average data for the system
66
			$averages = getAverages();
67
68
			// The number of days the forum has been up...
69
			$this->total_days_up = ceil((time() - strtotime($averages['date'])) / (60 * 60 * 24));
70
71
			$this->data['totals'] += array(
72
				'average_members' => $this->formatAvg($averages['registers']),
73
				'average_posts' => $this->formatAvg($averages['posts']),
74
				'average_topics' => $this->formatAvg($averages['topics']),
75
				'average_online' => $this->formatAvg($averages['most_on']),
76
			);
77
		}
78
79
		// Set the template to use
80
		$this->setTemplate('template_sp_boardStats');
81
82
		// Enabling auto refresh?
83
		if (!empty($parameters['refresh_value']) && !$user_info['is_guest'])
84
		{
85
			$this->refresh = array('sa' => 'boardstats', 'class' => '.sp_board_stats', 'id' => $id, 'refresh_value' => $parameters['refresh_value']);
86
			$this->auto_refresh();
87
		}
88
	}
89
90
	/**
91
	 * Make the averages look normal 1,222.xx
92
	 *
93
	 * @param $value
94
	 *
95
	 * @return float|null|string
96
	 */
97
	protected function formatAvg($value)
98
	{
99
		if (empty($this->total_days_up))
100
			return null;
101
102
		return comma_format(round($value / $this->total_days_up, 2));
103
	}
104
}
105
106
/**
107
 * Main template for this block
108
 *
109
 * @param mixed[] $data
110
 */
111
function template_sp_boardStats($data)
112
{
113
	global $txt, $scripturl;
114
115
	echo '
116
		<ul class="sp_list">
117
			<li ', sp_embed_class('portalstats'), '>', $txt['total_members'], ': <a href="', $scripturl . '?action=memberlist">', comma_format($data['totals']['members']), '</a></li>
118
			<li ', sp_embed_class('portalstats'), '>', $txt['total_posts'], ': <a href="', $scripturl . '?action=stats">', comma_format($data['totals']['posts']), '</a></li>
119
			<li ', sp_embed_class('portalstats'), '>', $txt['total_topics'], ': ', comma_format($data['totals']['topics']), '</li>
120
			<li ', sp_embed_class('portalstats'), '>', $txt['total_cats'], ': ', comma_format($data['totals']['categories']), '</li>
121
			<li ', sp_embed_class('portalstats'), '>', $txt['total_boards'], ': ', comma_format($data['totals']['boards']), '</li>
122
			<li ', sp_embed_class('portalstats'), '>', $txt['most_online'], ': ', comma_format($data['totals']['mostOnline']), '</li>
123
		</ul>';
124
125
	// And the averages if required
126
	if ($data['averages'])
127
	{
128
		echo '
129
		<hr />
130
		<ul class="sp_list">
131
			<li ', sp_embed_class('portalaverages'), '>', $txt['sp-average_posts'], ': ', $data['totals']['average_posts'], '</li>
132
			<li ', sp_embed_class('portalaverages'), '>', $txt['sp-average_topics'], ': ', $data['totals']['average_topics'], '</li>
133
			<li ', sp_embed_class('portalaverages'), '>', $txt['sp-average_members'], ': ', $data['totals']['average_members'], '</li>
134
			<li ', sp_embed_class('portalaverages'), '>', $txt['sp-average_online'], ': ', $data['totals']['average_online'], '</li>
135
		</ul>';
136
	}
137
}
138