mc-cms-namelessmc/modules/Core/widgets/StatsWidget.php

97 lines
4 KiB
PHP
Raw Permalink Normal View History

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/
* NamelessMC version 2.2.0
2019-01-27 22:28:19 +05: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;
private Language $_language;
2020-12-14 14:20:28 -08: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.';
$this->_engine = $engine;
Reactions revamp & profile widgets (#3272) * Initial work on reactions revamp and profile widgets * Remove unneeded changes * Remove unneeded change * fix style * Render emojis in reaction list StaffCP * wip minecraft account profile widget, sticky widgets on profile page * Add server name and IP to last seen * remove * rename to reaction score to match member list * align reactions table contents center * fix * wip - nicer abstraction, widgets never query db, skin 3d viewer (undecided) * wip - make reaction modals ajax, copy new reaction bar to profile posts * wip reaction modals * wip proper sorting * wip modals * wip get reaction submission working on profile wall posts * add back helpful and creative default reactions * reduce query amount on view topic * remove debug statement * wip * wip * simplify ProfileWidget pages * Revert forum dropdown * fix css syntax * fix emoji title and alt being strange * add back order input * sort widgets by order in list * fix settings link always showing * phpdoc * add widget name to exception * insert default widget data if not found * missing end if * properly cache forum news per-group * fix * fix cache key, news permission check * fix styling * wip * respect reaction ordering * wip * wip * support custom reaction scores, update member list to properly calculate * don't hardcode words * wip * allow multiple reactions * redirect to post * code style * phpstan * wip * remove hardcoded terms + fix copy * use npm for skinview3d * create ReactionContext and refactor * fix cache * docblocks * fix spelling * pr amendments
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
$users_query = $this->_cache->fetch('statistics', function () {
$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
$latest_user = new User($users_query);
2021-10-29 22:00:05 -07:00
$latest_member = [
'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(),
'id' => Output::getClean($users_query)
2021-10-29 22:00:05 -07:00
];
2020-12-19 19:25:59 +01:00
return [
'users_registered' => $users_registered,
'latest_member' => $latest_member
];
}, 120);
2020-12-19 19:25:59 +01:00
$users_registered = $users_query['users_registered'];
$latest_member = $users_query['latest_member'];
2020-12-19 19:25:59 +01: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
$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
if (Util::isModuleEnabled('Forum')) {
2020-12-19 19:25:59 +01:00
$this->_cache->setCache('forum_stats');
$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
$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
$this->_engine->addVariables([
'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
$this->_engine->addVariables([
'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
$this->_content = $this->_engine->fetch('widgets/statistics');
2019-01-27 22:28:19 +05:00
}
}