Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/lib/statistics.php (1 issue)

implicit conversion of array to boolean.

Best Practice Bug Minor
1
<?php
2
/**
3
 * Elgg statistics library.
4
 *
5
 * This file contains a number of functions for obtaining statistics about the running system.
6
 * These statistics are mainly used by the administration pages, and is also where the basic
7
 * views for statistics are added.
8
 *
9
 * @package Elgg.Core
10
 * @subpackage Statistics
11
 */
12
13
/**
14
 * Return an array reporting the number of various entities in the system.
15
 *
16
 * @param int $owner_guid Optional owner of the statistics
17
 *
18
 * @return array
19
 */
20
function get_entity_statistics($owner_guid = 0) {
21
22 4
	$owner_guid = (int) $owner_guid;
23 4
	$entity_stats = [];
24
25 4
	$grouped_entities = elgg_get_entities([
26 4
		'selects' => ['COUNT(*) as cnt'],
27
		'owner_guids' => ($owner_guid) ? : ELGG_ENTITIES_ANY_VALUE,
28 4
		'group_by' => 'e.type, e.subtype',
29 4
		'limit' => 0,
30 4
		'order_by' => 'cnt DESC',
31
	]);
32
	
33 4
	if (!empty($grouped_entities)) {
34 2
		foreach ($grouped_entities as $entity) {
35 2
			$type = $entity->getType();
36 2
			if (!isset($entity_stats[$type]) || !is_array($entity_stats[$type])) {
37 2
				$entity_stats[$type] = [];
38
			}
39 2
			$subtype = $entity->getSubtype();
40 2
			if (!$subtype) {
41
				$subtype = '__base__';
42
			}
43 2
			$entity_stats[$type][$subtype] = $entity->getVolatileData('select:cnt');
44
		}
45
	}
46
47 4
	return $entity_stats;
48
}
49
50
/**
51
 * Return the number of users registered in the system.
52
 *
53
 * @param bool $show_deactivated Count not enabled users?
54
 *
55
 * @return int
56
 */
57
function get_number_users($show_deactivated = false) {
58
59
	$where = new \Elgg\Database\Clauses\EntityWhereClause();
60
	$where->type_subtype_pairs = [
61
		'user' => null,
62
	];
63
64
	if ($show_deactivated) {
65
		$where->use_enabled_clause = false;
66
	}
67
68
	$select = \Elgg\Database\Select::fromTable('entities', 'e');
69
	$select->select('COUNT(DISTINCT e.guid) AS count');
70
	$select->addClause($where, 'e');
71
72
	$result = _elgg_services()->db->getDataRow($select);
73
74
	if ($result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
75
		return $result->count;
76
	}
77
78
	return 0;
79
}
80
81
/**
82
 * Render a list of currently online users
83
 *
84
 * @tip This also support options from elgg_list_entities().
85
 *
86
 * @param array $options Options array with keys:
87
 *
88
 *    seconds (int) => Number of seconds (default 600 = 10min)
89
 *
90
 * @return string
91
 */
92
function get_online_users(array $options = []) {
93 1
	$options = array_merge([
94 1
		'seconds' => 600,
95 1
	], $options);
96
97 1
	return elgg_list_entities($options, 'find_active_users');
98
}
99
100
/**
101
 * Initialise the statistics admin page.
102
 *
103
 * @return void
104
 * @access private
105
 */
106
function statistics_init() {
107 31
	elgg_extend_view('core/settings/statistics', 'core/settings/statistics/online');
108 31
	elgg_extend_view('core/settings/statistics', 'core/settings/statistics/numentities');
109 31
}
110
111
/**
112
 * @see \Elgg\Application::loadCore Do not do work here. Just register for events.
113
 */
114
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
115 18
	$events->registerHandler('init', 'system', 'statistics_init');
116
};
117