statistics.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. * Return an array reporting the number of various entities in the system.
  14. *
  15. * @param int $owner_guid Optional owner of the statistics
  16. *
  17. * @return array
  18. */
  19. function get_entity_statistics($owner_guid = 0) {
  20. $owner_guid = (int) $owner_guid;
  21. $entity_stats = array();
  22. $grouped_entities = elgg_get_entities(array(
  23. 'selects' => array('COUNT(*) as cnt'),
  24. 'owner_guids' => ($owner_guid) ? : ELGG_ENTITIES_ANY_VALUE,
  25. 'group_by' => 'e.type, e.subtype',
  26. 'limit' => 0,
  27. 'order_by' => 'cnt DESC',
  28. ));
  29. if (!empty($grouped_entities)) {
  30. foreach ($grouped_entities as $entity) {
  31. $type = $entity->getType();
  32. if (!isset($entity_stats[$type]) || !is_array($entity_stats[$type])) {
  33. $entity_stats[$type] = array();
  34. }
  35. $subtype = $entity->getSubtype();
  36. if (!$subtype) {
  37. $subtype = '__base__';
  38. }
  39. $entity_stats[$type][$subtype] = $entity->getVolatileData('select:cnt');
  40. }
  41. }
  42. return $entity_stats;
  43. }
  44. /**
  45. * Return the number of users registered in the system.
  46. *
  47. * @param bool $show_deactivated Count not enabled users?
  48. *
  49. * @return int
  50. */
  51. function get_number_users($show_deactivated = false) {
  52. global $CONFIG;
  53. $access = "";
  54. if (!$show_deactivated) {
  55. $access = "and " . _elgg_get_access_where_sql(array('table_alias' => ''));
  56. }
  57. $query = "SELECT count(*) as count
  58. from {$CONFIG->dbprefix}entities where type='user' $access";
  59. $result = get_data_row($query);
  60. if ($result) {
  61. return $result->count;
  62. }
  63. return false;
  64. }
  65. /**
  66. * Render a list of currently online users
  67. *
  68. * @tip This also support options from elgg_list_entities().
  69. *
  70. * @param array $options Options array with keys:
  71. *
  72. * seconds (int) => Number of seconds (default 600 = 10min)
  73. *
  74. * @return string
  75. */
  76. function get_online_users(array $options = array()) {
  77. $options = array_merge(array(
  78. 'seconds' => 600,
  79. ), $options);
  80. return elgg_list_entities($options, 'find_active_users');
  81. }
  82. /**
  83. * Initialise the statistics admin page.
  84. *
  85. * @return void
  86. * @access private
  87. */
  88. function statistics_init() {
  89. elgg_extend_view('core/settings/statistics', 'core/settings/statistics/online');
  90. elgg_extend_view('core/settings/statistics', 'core/settings/statistics/numentities');
  91. }
  92. return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
  93. $events->registerHandler('init', 'system', 'statistics_init');
  94. };