misuzu/public-legacy/forum/leaderboard.php

117 lines
3.2 KiB
PHP
Raw Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu;
2023-08-28 01:17:34 +00:00
use RuntimeException;
if(!$msz->isLoggedIn() || !perms_check_user(MSZ_PERMS_FORUM, $msz->getActiveUser()->getId(), MSZ_PERM_FORUM_VIEW_LEADERBOARD)) {
2022-09-13 13:14:49 +00:00
echo render_error(403);
return;
}
2023-08-28 01:17:34 +00:00
$forum = $msz->getForum();
$users = $msz->getUsers();
$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);
if(($yearMonthLength !== 4 && $yearMonthLength !== 6) || !ctype_digit($yearMonth)) {
echo render_error(404);
return;
}
$year = (int)substr($yearMonth, 0, 4);
if($year < $config['forum_leader.first_year'] || $year > $currentYear) {
echo render_error(404);
return;
}
if($yearMonthLength === 6) {
$month = (int)substr($yearMonth, 4, 2);
if($month < 1 || $month > 12 || ($year === $config['forum_leader.first_year'] && $month < $config['forum_leader.first_month'])) {
echo render_error(404);
return;
}
}
}
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;
}
$rankings = $forum->generatePostRankings($year, $month, $unrankedForums, $unrankedTopics);
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 !== '')
try {
$ranking->user = $users->getUser($ranking->userId);
$ranking->colour = $users->getUserColour($ranking->user);
} catch(RuntimeException $ex) {}
}
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) {
$name = "Leaderboard {$year}";
if($month > 0)
$name .= "-{$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-28 01:17:34 +00:00
foreach($rankings as $ranking)
$markdown .= sprintf("| %s | [%s](%s%s) | %s |\r\n", $ranking->position,
$ranking->user?->getName() ?? 'Deleted User',
url_prefix(false), url('user-profile', ['user' => $ranking->userId]), $ranking->postsCount);
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
]);