Completed
Push — 3.0 ( a46cca...861f1c )
by Jeroen
204:32 queued 99:44
created

engine/lib/statistics.php (1 issue)

Severity
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
use Elgg\Database\Clauses\OrderByClause;
14
15
/**
16
 * Return an array reporting the number of various entities in the system.
17
 *
18
 * @param int $owner_guid Optional owner of the statistics
19
 *
20
 * @return array
21
 */
22
function get_entity_statistics($owner_guid = 0) {
23
24 4
	$owner_guid = (int) $owner_guid;
25 4
	$entity_stats = [];
26
27 4
	$grouped_entities = elgg_get_entities([
28 4
		'selects' => ['COUNT(*) as cnt'],
29
		'owner_guids' => ($owner_guid) ? : ELGG_ENTITIES_ANY_VALUE,
30
		'group_by' => ['e.type', 'e.subtype'],
31 4
		'limit' => 0,
32 4
		'order_by' => new OrderByClause('cnt', 'DESC'),
33
	]);
34
	
35 4
	if (!empty($grouped_entities)) {
36 2
		foreach ($grouped_entities as $entity) {
37 2
			$type = $entity->getType();
38 2
			if (!isset($entity_stats[$type]) || !is_array($entity_stats[$type])) {
39 2
				$entity_stats[$type] = [];
40
			}
41 2
			$subtype = $entity->getSubtype();
42 2
			if (!$subtype) {
43
				$subtype = '__base__';
44
			}
45 2
			$entity_stats[$type][$subtype] = $entity->getVolatileData('select:cnt');
46
		}
47
	}
48
49 4
	return $entity_stats;
50
}
51
52
/**
53
 * Return the number of users registered in the system.
54
 *
55
 * @param bool $show_deactivated Count not enabled users?
56
 *
57
 * @return int
58
 */
59
function get_number_users($show_deactivated = false) {
60
61
	$where = new \Elgg\Database\Clauses\EntityWhereClause();
62
	$where->type_subtype_pairs = [
63
		'user' => null,
64
	];
65
66
	if ($show_deactivated) {
67
		$where->use_enabled_clause = false;
68
	}
69
70
	$select = \Elgg\Database\Select::fromTable('entities', 'e');
71
	$select->select('COUNT(DISTINCT e.guid) AS count');
72
	$select->addClause($where, 'e');
73
74
	$result = _elgg_services()->db->getDataRow($select);
75
	if (!empty($result)) {
76
		return (int) $result->count;
77
	}
78
79
	return 0;
80
}
81
82
/**
83
 * Render a list of currently online users
84
 *
85
 * @tip This also support options from elgg_list_entities().
86
 *
87
 * @param array $options Options array with keys:
88
 *
89
 *    seconds (int) => Number of seconds (default 600 = 10min)
90
 *
91
 * @return string
92
 */
93
function get_online_users(array $options = []) {
94 1
	$options = array_merge([
95 1
		'seconds' => 600,
96 1
	], $options);
97
98 1
	return elgg_list_entities($options, 'find_active_users');
99
}
100
101
/**
102
 * Initialise the statistics admin page.
103
 *
104
 * @return void
105
 * @internal
106
 */
107
function statistics_init() {
108 84
	elgg_extend_view('core/settings/statistics', 'core/settings/statistics/online');
109 84
	elgg_extend_view('core/settings/statistics', 'core/settings/statistics/numentities');
110 84
}
111
112
/**
113
 * @see \Elgg\Application::loadCore Do not do work here. Just register for events.
114
 */
115
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
1 ignored issue
show
The parameter $hooks is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

115
return function(\Elgg\EventsService $events, /** @scrutinizer ignore-unused */ \Elgg\HooksRegistrationService $hooks) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116 73
	$events->registerHandler('init', 'system', 'statistics_init');
117
};
118