aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/statistics.php
blob: 5ee640549f620c5f62f63521986f85f0c0f8d763 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
 * Elgg statistics library.
 *
 * This file contains a number of functions for obtaining statistics about the running system.
 * These statistics are mainly used by the administration pages, and is also where the basic
 * views for statistics are added.
 *
 * @package Elgg.Core
 * @subpackage Statistics
 */

/**
 * Return an array reporting the number of various entities in the system.
 *
 * @param int $owner_guid Optional owner of the statistics
 *
 * @return array
 */
function get_entity_statistics($owner_guid = 0) {
	global $CONFIG;

	$entity_stats = array();
	$owner_guid = (int)$owner_guid;

	$query = "SELECT distinct e.type,s.subtype,e.subtype as subtype_id
		from {$CONFIG->dbprefix}entities e left
		join {$CONFIG->dbprefix}entity_subtypes s on e.subtype=s.id";

	$owner_query = "";
	if ($owner_guid) {
		$query .= " where owner_guid=$owner_guid";
		$owner_query = "and owner_guid=$owner_guid ";
	}

	// Get a list of major types

	$types = get_data($query);
	foreach ($types as $type) {
		// assume there are subtypes for now
		if (!is_array($entity_stats[$type->type])) {
			$entity_stats[$type->type] = array();
		}

		$query = "SELECT count(*) as count
			from {$CONFIG->dbprefix}entities where type='{$type->type}' $owner_query";

		if ($type->subtype) {
			$query .= " and subtype={$type->subtype_id}";
		}

		$subtype_cnt = get_data_row($query);

		if ($type->subtype) {
			$entity_stats[$type->type][$type->subtype] = $subtype_cnt->count;
		} else {
			$entity_stats[$type->type]['__base__'] = $subtype_cnt->count;
		}
	}

	return $entity_stats;
}

/**
 * Return the number of users registered in the system.
 *
 * @param bool $show_deactivated Count not enabled users?
 *
 * @return int
 */
function get_number_users($show_deactivated = false) {
	global $CONFIG;

	$access = "";

	if (!$show_deactivated) {
		$access = "and " . get_access_sql_suffix();
	}

	$query = "SELECT count(*) as count
		from {$CONFIG->dbprefix}entities where type='user' $access";

	$result = get_data_row($query);

	if ($result) {
		return $result->count;
	}

	return false;
}

/**
 * Return a list of how many users are currently online, rendered as a view.
 *
 * @return string
  */
function get_online_users() {
	$count = find_active_users(600, 10, 0, true);
	$objects = find_active_users(600, 10);

	if ($objects) {
		return elgg_view_entity_list($objects, array(
			'count' => $count,
			'limit' => 10
		));
	}
}

/**
 * Initialise the statistics admin page.
 *
 * @return void
 * @access private
 */
function statistics_init() {
	elgg_extend_view('core/settings/statistics', 'core/settings/statistics/online');
	elgg_extend_view('core/settings/statistics', 'core/settings/statistics/numentities');
}

/// Register init function
elgg_register_event_handler('init', 'system', 'statistics_init');