statistics.php ➔ get_entity_statistics()   B
last analyzed

Complexity

Conditions 7
Paths 2

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
nc 2
nop 1
dl 0
loc 29
ccs 0
cts 23
cp 0
crap 56
rs 8.5226
c 0
b 0
f 0
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
	$owner_guid = (int) $owner_guid;
23
	$entity_stats = array();
24
25
	$grouped_entities = elgg_get_entities(array(
26
		'selects' => array('COUNT(*) as cnt'),
27
		'owner_guids' => ($owner_guid) ? : ELGG_ENTITIES_ANY_VALUE,
28
		'group_by' => 'e.type, e.subtype',
29
		'limit' => 0,
30
		'order_by' => 'cnt DESC',
31
	));
32
	
33
	if (!empty($grouped_entities)) {
34
		foreach ($grouped_entities as $entity) {
35
			$type = $entity->getType();
36
			if (!isset($entity_stats[$type]) || !is_array($entity_stats[$type])) {
37
				$entity_stats[$type] = array();
38
			}
39
			$subtype = $entity->getSubtype();
40
			if (!$subtype) {
41
				$subtype = '__base__';
42
			}
43
			$entity_stats[$type][$subtype] = $entity->getVolatileData('select:cnt');
44
		}
45
	}
46
47
	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
	global $CONFIG;
59
60
	$access = "";
61
62
	if (!$show_deactivated) {
63
		$access = "and " . _elgg_get_access_where_sql(array('table_alias' => ''));
64
	}
65
66
	$query = "SELECT count(*) as count
67
		from {$CONFIG->dbprefix}entities where type='user' $access";
68
69
	$result = get_data_row($query);
70
71
	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...
72
		return $result->count;
73
	}
74
75
	return false;
76
}
77
78
/**
79
 * Render a list of currently online users
80
 *
81
 * @tip This also support options from elgg_list_entities().
82
 *
83
 * @param array $options Options array with keys:
84
 *
85
 *    seconds (int) => Number of seconds (default 600 = 10min)
86
 *
87
 * @return string
88
 */
89
function get_online_users(array $options = array()) {
90
	$options = array_merge(array(
91
		'seconds' => 600,
92
	), $options);
93
94
	return elgg_list_entities($options, 'find_active_users');
95
}
96
97
/**
98
 * Initialise the statistics admin page.
99
 *
100
 * @return void
101
 * @access private
102
 */
103
function statistics_init() {
104
	elgg_extend_view('core/settings/statistics', 'core/settings/statistics/online');
105
	elgg_extend_view('core/settings/statistics', 'core/settings/statistics/numentities');
106
}
107
108
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
109
	$events->registerHandler('init', 'system', 'statistics_init');
110
};
111