2019-01-27 22:28:19 +05:00
|
|
|
<?php
|
|
|
|
|
/*
|
2022-05-27 11:54:24 +03:00
|
|
|
* Made by Samerton
|
2019-01-27 22:28:19 +05:00
|
|
|
* https://github.com/NamelessMC/Nameless/
|
2025-02-24 18:44:29 +00:00
|
|
|
* NamelessMC version 2.2.0
|
2019-01-27 22:28:19 +05:00
|
|
|
*
|
2025-02-24 18:44:29 +00:00
|
|
|
* Licence: MIT
|
2019-01-27 22:28:19 +05:00
|
|
|
*
|
|
|
|
|
* Statistics Widget // By Xemah // https://xemah.me
|
|
|
|
|
*/
|
2020-12-19 19:25:59 +01:00
|
|
|
|
2019-01-27 22:28:19 +05:00
|
|
|
class StatsWidget extends WidgetBase {
|
|
|
|
|
|
2021-10-30 16:44:05 -07:00
|
|
|
private Cache $_cache;
|
2022-05-22 18:11:40 -06:00
|
|
|
private Language $_language;
|
2020-12-14 14:20:28 -08:00
|
|
|
|
2025-02-24 18:44:29 +00:00
|
|
|
public function __construct(TemplateEngine $engine, Language $language, Cache $cache) {
|
2019-01-27 22:28:19 +05:00
|
|
|
$this->_module = 'Core';
|
|
|
|
|
$this->_name = 'Statistics';
|
|
|
|
|
$this->_description = 'Displays the basic statistics of your website.';
|
2025-02-24 18:44:29 +00:00
|
|
|
$this->_engine = $engine;
|
2023-06-21 10:19:46 -07:00
|
|
|
|
|
|
|
|
$this->_cache = $cache;
|
|
|
|
|
$this->_language = $language;
|
2019-01-27 22:28:19 +05:00
|
|
|
}
|
|
|
|
|
|
2021-10-30 16:44:05 -07:00
|
|
|
public function initialise(): void {
|
2019-01-27 22:28:19 +05:00
|
|
|
$this->_cache->setCache('statistics');
|
2020-12-19 19:25:59 +01:00
|
|
|
|
2025-05-20 18:37:24 -07:00
|
|
|
$users_query = $this->_cache->fetch('statistics', function () {
|
2022-11-18 19:16:24 +00:00
|
|
|
$users_query = DB::getInstance()->query('SELECT `id` FROM nl2_users ORDER BY `joined` DESC LIMIT 1')->first()->id;
|
|
|
|
|
$users_registered = DB::getInstance()->query('SELECT COUNT(*) as c FROM nl2_users')->first()->c;
|
2020-12-19 19:25:59 +01:00
|
|
|
|
2022-11-18 19:16:24 +00:00
|
|
|
$latest_user = new User($users_query);
|
2021-10-29 22:00:05 -07:00
|
|
|
$latest_member = [
|
2022-04-21 22:35:56 +02:00
|
|
|
'style' => $latest_user->getGroupStyle(),
|
2020-12-19 19:25:59 +01:00
|
|
|
'profile' => $latest_user->getProfileURL(),
|
|
|
|
|
'avatar' => $latest_user->getAvatar(),
|
|
|
|
|
'username' => $latest_user->getDisplayname(true),
|
|
|
|
|
'nickname' => $latest_user->getDisplayname(),
|
2022-11-18 19:16:24 +00:00
|
|
|
'id' => Output::getClean($users_query)
|
2021-10-29 22:00:05 -07:00
|
|
|
];
|
2020-12-19 19:25:59 +01:00
|
|
|
|
2025-05-20 18:37:24 -07:00
|
|
|
return [
|
|
|
|
|
'users_registered' => $users_registered,
|
|
|
|
|
'latest_member' => $latest_member
|
|
|
|
|
];
|
|
|
|
|
}, 120);
|
2020-12-19 19:25:59 +01:00
|
|
|
|
2025-05-20 18:37:24 -07:00
|
|
|
$users_registered = $users_query['users_registered'];
|
|
|
|
|
$latest_member = $users_query['latest_member'];
|
2020-12-19 19:25:59 +01:00
|
|
|
|
2025-05-20 18:37:24 -07:00
|
|
|
$online_users = $this->_cache->fetch('online_users', function () {
|
|
|
|
|
return DB::getInstance()->query('SELECT COUNT(*) as c FROM nl2_users WHERE last_online > ?', [strtotime('-5 minutes')])->first()->c;
|
|
|
|
|
}, 60);
|
2020-12-19 19:25:59 +01:00
|
|
|
|
2025-05-20 18:37:24 -07:00
|
|
|
$online_guests = $this->_cache->fetch('online_guests', function () {
|
|
|
|
|
return DB::getInstance()->query('SELECT COUNT(*) as c FROM nl2_online_guests WHERE last_seen > ?', [strtotime('-5 minutes')])->first()->c;
|
|
|
|
|
}, 60);
|
2020-12-19 19:25:59 +01:00
|
|
|
|
2022-11-18 19:16:24 +00:00
|
|
|
if (Util::isModuleEnabled('Forum')) {
|
2020-12-19 19:25:59 +01:00
|
|
|
$this->_cache->setCache('forum_stats');
|
2025-05-20 18:37:24 -07:00
|
|
|
$total_topics = $this->_cache->fetch('total_topics', function () {
|
|
|
|
|
return DB::getInstance()->query('SELECT COUNT(*) as c FROM nl2_topics WHERE deleted = 0')->first()->c;
|
|
|
|
|
}, 60);
|
2020-12-19 19:25:59 +01:00
|
|
|
|
2025-05-20 18:37:24 -07:00
|
|
|
$total_posts = $this->_cache->fetch('total_posts', function () {
|
|
|
|
|
return DB::getInstance()->query('SELECT COUNT(*) as c FROM nl2_posts WHERE deleted = 0')->first()->c;
|
|
|
|
|
}, 60);
|
2020-12-19 19:25:59 +01:00
|
|
|
|
2025-02-24 18:44:29 +00:00
|
|
|
$this->_engine->addVariables([
|
2022-05-22 18:11:40 -06:00
|
|
|
'FORUM_STATISTICS' => $this->_language->get('general', 'forum_statistics'),
|
|
|
|
|
'TOTAL_THREADS' => $this->_language->get('general', 'total_threads'),
|
|
|
|
|
'TOTAL_THREADS_VALUE' => $total_topics,
|
|
|
|
|
'TOTAL_POSTS' => $this->_language->get('general', 'total_posts'),
|
|
|
|
|
'TOTAL_POSTS_VALUE' => $total_posts,
|
|
|
|
|
]);
|
2021-10-29 22:09:38 -07:00
|
|
|
}
|
2020-12-19 19:25:59 +01:00
|
|
|
|
2025-02-24 18:44:29 +00:00
|
|
|
$this->_engine->addVariables([
|
2022-05-22 18:11:40 -06:00
|
|
|
'STATISTICS' => $this->_language->get('general', 'statistics'),
|
|
|
|
|
'USERS_REGISTERED' => $this->_language->get('general', 'users_registered'),
|
|
|
|
|
'USERS_REGISTERED_VALUE' => $users_registered,
|
|
|
|
|
'LATEST_MEMBER' => $this->_language->get('general', 'latest_member'),
|
|
|
|
|
'LATEST_MEMBER_VALUE' => $latest_member,
|
|
|
|
|
'USERS_ONLINE' => $this->_language->get('general', 'online_users'),
|
|
|
|
|
'USERS_ONLINE_VALUE' => $online_users,
|
|
|
|
|
'GUESTS_ONLINE' => $this->_language->get('general', 'online_guests'),
|
|
|
|
|
'GUESTS_ONLINE_VALUE' => $online_guests,
|
|
|
|
|
'TOTAL_ONLINE' => $this->_language->get('general', 'total_online'),
|
|
|
|
|
'TOTAL_ONLINE_VALUE' => $online_guests + $online_users,
|
|
|
|
|
]);
|
2020-12-19 19:25:59 +01:00
|
|
|
|
2025-02-24 18:44:29 +00:00
|
|
|
$this->_content = $this->_engine->fetch('widgets/statistics');
|
2019-01-27 22:28:19 +05:00
|
|
|
}
|
|
|
|
|
}
|