2022-09-13 13:14:49 +00:00
|
|
|
<?php
|
|
|
|
namespace Misuzu;
|
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
use RuntimeException;
|
|
|
|
|
2023-08-31 15:59:53 +00:00
|
|
|
if(!$msz->getAuthInfo()->getPerms('global')->check(Perm::G_FORUM_LEADERBOARD_VIEW))
|
|
|
|
Template::throwError(403);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-09-08 13:22:46 +00:00
|
|
|
$forumCtx = $msz->getForumContext();
|
2023-09-06 13:50:19 +00:00
|
|
|
$usersCtx = $msz->getUsersContext();
|
2023-08-28 01:17:34 +00:00
|
|
|
$config = $cfg->getValues([
|
|
|
|
['forum_leader.first_year:i', 2018],
|
|
|
|
['forum_leader.first_month:i', 12],
|
|
|
|
'forum_leader.unranked.forum:a',
|
|
|
|
'forum_leader.unranked.topic:a',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$mode = (string)filter_input(INPUT_GET, 'mode');
|
|
|
|
$yearMonth = (string)filter_input(INPUT_GET, 'id');
|
|
|
|
$year = $month = 0;
|
|
|
|
|
|
|
|
$currentYear = (int)date('Y');
|
|
|
|
$currentMonth = (int)date('m');
|
|
|
|
|
|
|
|
if(!empty($yearMonth)) {
|
|
|
|
$yearMonthLength = strlen($yearMonth);
|
2023-08-31 15:59:53 +00:00
|
|
|
if(($yearMonthLength !== 4 && $yearMonthLength !== 6) || !ctype_digit($yearMonth))
|
|
|
|
Template::throwError(404);
|
2023-08-28 01:17:34 +00:00
|
|
|
|
|
|
|
$year = (int)substr($yearMonth, 0, 4);
|
2023-08-31 15:59:53 +00:00
|
|
|
if($year < $config['forum_leader.first_year'] || $year > $currentYear)
|
|
|
|
Template::throwError(404);
|
2023-08-28 01:17:34 +00:00
|
|
|
|
|
|
|
if($yearMonthLength === 6) {
|
|
|
|
$month = (int)substr($yearMonth, 4, 2);
|
2023-08-31 15:59:53 +00:00
|
|
|
if($month < 1 || $month > 12 || ($year === $config['forum_leader.first_year'] && $month < $config['forum_leader.first_month']))
|
|
|
|
Template::throwError(404);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(filter_has_var(INPUT_GET, 'allow_unranked')) {
|
|
|
|
$unrankedForums = $unrankedTopics = [];
|
|
|
|
} else {
|
|
|
|
$unrankedForums = $config['forum_leader.unranked.forum'];
|
|
|
|
$unrankedTopics = $config['forum_leader.unranked.topic'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$years = $months = [];
|
|
|
|
|
|
|
|
for($i = $currentYear; $i >= $config['forum_leader.first_year']; $i--)
|
|
|
|
$years[(string)$i] = sprintf('Leaderboard %d', $i);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
for($i = $currentYear, $j = $currentMonth;;) {
|
|
|
|
$months[sprintf('%d%02d', $i, $j)] = sprintf('Leaderboard %d-%02d', $i, $j);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
if($j <= 1) {
|
|
|
|
$i--; $j = 12;
|
|
|
|
} else $j--;
|
2023-07-18 21:48:44 +00:00
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
if($i <= $config['forum_leader.first_year'] && $j < $config['forum_leader.first_month'])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-09-08 13:22:46 +00:00
|
|
|
$rankings = $forumCtx->getPosts()->generatePostRankings($year, $month, $unrankedForums, $unrankedTopics);
|
2023-08-28 01:17:34 +00:00
|
|
|
foreach($rankings as $ranking) {
|
|
|
|
$ranking->user = $ranking->colour = null;
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
if($ranking->userId !== '')
|
2023-09-06 13:50:19 +00:00
|
|
|
$ranking->user = $usersCtx->getUserInfo($ranking->userId);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
$name = 'All Time';
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
if($year > 0) {
|
2023-08-31 00:54:17 +00:00
|
|
|
$name = sprintf("Leaderboard %04d", $year);
|
2023-08-28 01:17:34 +00:00
|
|
|
|
|
|
|
if($month > 0)
|
2023-08-31 00:54:17 +00:00
|
|
|
$name .= sprintf("-%02d", $month);
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
if($mode === 'markdown') {
|
2022-09-13 13:14:49 +00:00
|
|
|
$markdown = <<<MD
|
2023-08-28 01:17:34 +00:00
|
|
|
# {$name}
|
2022-09-13 13:14:49 +00:00
|
|
|
|
|
|
|
| Rank | Usename | Post count |
|
|
|
|
| ----:|:------- | ----------:|
|
|
|
|
|
|
|
|
MD;
|
|
|
|
|
2023-08-31 00:52:14 +00:00
|
|
|
$totalPostsCount = 0;
|
|
|
|
|
|
|
|
foreach($rankings as $ranking) {
|
|
|
|
$totalPostsCount += $ranking->postsCount;
|
2023-08-28 01:17:34 +00:00
|
|
|
$markdown .= sprintf("| %s | [%s](%s%s) | %s |\r\n", $ranking->position,
|
|
|
|
$ranking->user?->getName() ?? 'Deleted User',
|
2023-08-31 00:52:14 +00:00
|
|
|
url_prefix(false), url('user-profile', ['user' => $ranking->userId]), number_format($ranking->postsCount));
|
|
|
|
}
|
|
|
|
|
|
|
|
$markdown .= sprintf("\r\nIn total %s posts were made!\r\n", number_format($totalPostsCount));
|
2022-09-13 13:14:49 +00:00
|
|
|
|
|
|
|
Template::set('leaderboard_markdown', $markdown);
|
|
|
|
}
|
|
|
|
|
|
|
|
Template::render('forum.leaderboard', [
|
2023-08-28 01:17:34 +00:00
|
|
|
'leaderboard_id' => $yearMonth,
|
|
|
|
'leaderboard_name' => $name,
|
|
|
|
'leaderboard_years' => $years,
|
|
|
|
'leaderboard_months' => $months,
|
|
|
|
'leaderboard_data' => $rankings,
|
|
|
|
'leaderboard_mode' => $mode,
|
2022-09-13 13:14:49 +00:00
|
|
|
]);
|