LuminaWebsite/contributions.php
Maaxxs fe1561f4f7 Raids: attendance list, automatic points on archive, reminder callback
Each raid gets an attendance list for raids.manage (present, absent, excused)
with quick actions, members added by hand and a "checked" mark with who and
when. The contribution form can now select a raid's present members for
points or its absent members for a deduction.

Raids can carry points for present members and a deduction for absent ones
(raid_settings). When a raid is archived - on the website or via the button
in Discord, detected on the next raid or contribution page view - the site
awards them once: by attendance if a list exists, otherwise to everyone in a
group without deduction, and not at all if points were already given for
that raid. Settings, attendance and awards follow a raid_id change when the
API re-posts a deleted message.

bot_api.php lets the bot ask which signed-up members have raids.manage when a
raid starts (shared key in config.php bot_callback.key); the bot then sends
them a DM with a link to the list. The bot side lives in the separate bot
repository (cogs/attendance.py).

Also fixes contribution_awards.raid_id being too small for Discord IDs (it
is widened automatically) and raid IDs losing precision in the contribution
form's JavaScript, which kept the raid preselection from ever matching.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 09:52:34 +02:00

270 lines
14 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/contributions.php';
require_once __DIR__ . '/includes/raid_attendance.php';
require_permission('contributions.view');
// Automatische Punkte fuer inzwischen archivierte Raids, bevor die Punktestaende angezeigt werden.
raid_process_auto_points();
$canManage = user_has_permission('contributions.manage');
$canViewAll = $canManage || user_has_permission('contributions.view_all');
// Der Verlauf ist einstellbar oeffentlich oder nur fuer Leute mit Bearbeitungsrechten.
$historyPublic = contributions_history_public();
$canSeeHistory = $canManage || $historyPublic;
$currentUser = current_user();
$balances = contribution_balances();
$myBalance = contribution_user_balance((int) $currentUser['id']);
// Rang: gleiche Punktzahl = gleicher Platz.
$rank = 0;
$lastPoints = null;
foreach ($balances as $index => $member) {
if ($lastPoints === null || (int) $member['points'] !== $lastPoints) {
$rank = $index + 1;
$lastPoints = (int) $member['points'];
}
$balances[$index]['rank'] = $rank;
}
// Ohne volle Sicht nur die besten Plaetze - bei Gleichstand an der Grenze alle, die sich den
// letzten Platz teilen. Der eigene Platz wird trotzdem angezeigt.
$myEntry = null;
foreach ($balances as $member) {
if ((int) $member['id'] === (int) $currentUser['id']) {
$myEntry = $member;
}
}
$visibleBalances = $canViewAll
? $balances
: array_values(array_filter($balances, static fn (array $m): bool => (int) $m['rank'] <= CONTRIBUTION_PUBLIC_RANKS));
$visibleIds = array_map(static fn (array $m): int => (int) $m['id'], $visibleBalances);
$showMyEntrySeparately = $myEntry !== null && !in_array((int) $myEntry['id'], $visibleIds, true);
$hiddenCount = count($balances) - count($visibleBalances);
// Verlauf, optional nur fuer ein Mitglied (Klick auf eine Karte).
$filterUserId = $canSeeHistory && isset($_GET['user']) ? (int) $_GET['user'] : null;
$filterUser = null;
// Den Verlauf von Mitgliedern ausserhalb der sichtbaren Plaetze nur mit voller Sicht - sonst
// liesse sich die Rangliste ueber den Filter doch vollstaendig abfragen.
if ($filterUserId !== null && !$canViewAll && $filterUserId !== (int) $currentUser['id'] && !in_array($filterUserId, $visibleIds, true)) {
$filterUserId = null;
}
if ($filterUserId !== null) {
foreach ($balances as $member) {
if ((int) $member['id'] === $filterUserId) {
$filterUser = $member;
}
}
if ($filterUser === null) {
$stmt = db()->prepare("SELECT id, username, display_name, avatar_filename FROM users WHERE id = ?");
$stmt->execute([$filterUserId]);
$filterUser = $stmt->fetch() ?: null;
if ($filterUser) {
$filterUser['points'] = contribution_user_balance($filterUserId);
} else {
$filterUserId = null;
}
}
}
$perPage = 20;
$totalAwards = contribution_award_count($filterUserId);
$pageCount = max(1, (int) ceil($totalAwards / $perPage));
$page = min($pageCount, max(1, (int) ($_GET['page'] ?? 1)));
$awards = $canSeeHistory ? contribution_awards($filterUserId, $perPage, ($page - 1) * $perPage) : [];
$displayName = static fn (array $u): string => (string) ($u['display_name'] !== '' ? $u['display_name'] : $u['username']);
$renderCard = static function (array $member) use ($currentUser, $canManage, $canSeeHistory, $displayName): void {
?>
<?php
$isMe = (int) $member['id'] === (int) $currentUser['id'];
$rankClass = in_array((int) $member['rank'], [1, 2, 3], true) && (int) $member['points'] > 0 ? ' rank-' . (int) $member['rank'] : '';
?>
<div class="contribution-card<?= $isMe ? ' is-me' : '' ?><?= $rankClass ?>" data-name="<?= e(mb_strtolower($displayName($member))) ?>">
<?php if ($canSeeHistory): ?>
<a class="contribution-card-link" href="/contributions.php?user=<?= (int) $member['id'] ?>#verlauf" title="Verlauf von <?= e($displayName($member)) ?>">
<?php else: ?>
<div class="contribution-card-link">
<?php endif; ?>
<span class="contribution-rank">#<?= (int) $member['rank'] ?></span>
<?= avatar_html($member, 52) ?>
<span class="contribution-card-name"><?= e($displayName($member)) ?></span>
<span class="contribution-card-points"><?= contribution_coin(18) ?> <?= contribution_format((int) $member['points']) ?></span>
<?= $canSeeHistory ? '</a>' : '</div>' ?>
<?php if ($canManage): ?>
<a class="icon-button contribution-card-add" href="/contributions_award.php?user=<?= (int) $member['id'] ?>"
title="Punkte an <?= e($displayName($member)) ?> vergeben" aria-label="Punkte vergeben">+</a>
<?php endif; ?>
</div>
<?php
};
$pageTitle = 'Beitragspunkte';
$containerWide = true;
include __DIR__ . '/includes/header.php';
?>
<div class="panel contribution-hero">
<img class="contribution-hero-coin" src="/assets/images/beitragspunkt.svg" alt="" width="72" height="72">
<div class="contribution-hero-text">
<h1>Beitragspunkte</h1>
<p class="text-muted">Punkte für Raids, Events und alles, was die Legion voranbringt.</p>
</div>
<div class="contribution-hero-balance" title="Dein Punktestand">
<span class="text-muted">Dein Punktestand</span>
<strong><?= contribution_coin(26) ?> <?= contribution_format($myBalance) ?></strong>
</div>
<?php if ($canManage): ?>
<a class="button" href="/contributions_award.php">Punkte vergeben</a>
<?php endif; ?>
</div>
<div class="panel">
<div class="contribution-head">
<h2>Rangliste<?= $canViewAll ? '' : ' <span class="contribution-top-label">Top ' . CONTRIBUTION_PUBLIC_RANKS . '</span>' ?></h2>
<input type="search" id="contribution-search" placeholder="Mitglied suchen …" autocomplete="off">
</div>
<?php if (empty($balances)): ?>
<p class="text-muted">Noch keine Mitglieder.</p>
<?php else: ?>
<div class="contribution-grid" id="contribution-ranking">
<?php foreach ($visibleBalances as $member): ?>
<?php $renderCard($member); ?>
<?php endforeach; ?>
</div>
<p class="text-muted" id="contribution-no-match" hidden>Kein Mitglied passt zur Suche.</p>
<?php if ($showMyEntrySeparately): ?>
<div class="contribution-own">
<h3>Dein Platz</h3>
<div class="contribution-grid">
<?php $renderCard($myEntry); ?>
</div>
</div>
<?php endif; ?>
<?php if ($hiddenCount > 0): ?>
<p class="text-muted contribution-limit-note">
Angezeigt werden die besten <?= CONTRIBUTION_PUBLIC_RANKS ?> Plätze. Die vollständige Rangliste
(<?= count($balances) ?> Mitglieder) ist der Legionsleitung vorbehalten.
</p>
<?php endif; ?>
<?php endif; ?>
</div>
<?php if ($canSeeHistory): ?>
<div class="panel" id="verlauf">
<div class="contribution-head">
<h2>
Verlauf<?= $filterUser ? ': ' . e($displayName($filterUser)) : '' ?>
<?php if ($filterUser): ?>
<span class="contribution-filter-balance"><?= contribution_coin(20) ?> <?= contribution_format((int) $filterUser['points']) ?></span>
<?php endif; ?>
</h2>
<?php if ($filterUser): ?>
<a href="/contributions.php#verlauf">Alle Vergaben anzeigen</a>
<?php endif; ?>
</div>
<?php if ($canManage): ?>
<form class="settings-toggle" method="post" action="/admin/settings_action.php">
<?= csrf_field() ?>
<input type="hidden" name="setting" value="contributions_history_public">
<label>
<input type="checkbox" name="value" value="1" <?= $historyPublic ? 'checked' : '' ?> onchange="this.form.submit()">
Verlauf für alle sichtbar
<span class="text-muted">- sonst sehen ihn nur Leute, die Punkte vergeben dürfen</span>
</label>
<noscript><button type="submit" class="button-secondary button-small">Speichern</button></noscript>
</form>
<?php endif; ?>
<?php if (empty($awards)): ?>
<p class="text-muted">Noch keine Beitragspunkte vergeben.</p>
<?php else: ?>
<div class="contribution-awards">
<?php foreach ($awards as $award): ?>
<?php
$entries = $award['entries'];
$pointValues = array_unique(array_map(static fn (array $entry): int => (int) $entry['points'], $entries));
$uniform = count($pointValues) === 1 ? (int) reset($pointValues) : null;
$mine = null;
if ($filterUserId !== null) {
foreach ($entries as $entry) {
if ((int) $entry['user_id'] === $filterUserId) {
$mine = (int) $entry['points'];
}
}
}
$shownPoints = $mine ?? $uniform;
?>
<article class="contribution-award">
<div class="contribution-award-points<?= $shownPoints !== null && $shownPoints < 0 ? ' negative' : '' ?>">
<?= contribution_coin(22) ?>
<strong><?= $shownPoints !== null ? contribution_format($shownPoints, true) : 'versch.' ?></strong>
<?php if ($mine === null && count($entries) > 1): ?>
<span class="text-muted">je Mitglied</span>
<?php endif; ?>
</div>
<div class="contribution-award-body">
<div class="contribution-award-title">
<span class="badge contribution-badge-<?= e((string) $award['category']) ?>"><?= e(contribution_category_label((string) $award['category'])) ?></span>
<strong><?= e((string) $award['title']) ?></strong>
</div>
<?php if (!empty($award['note'])): ?>
<p class="contribution-award-note"><?= e((string) $award['note']) ?></p>
<?php endif; ?>
<div class="contribution-award-meta text-muted">
<?= e(contribution_format_time($award['awarded_at'])) ?>
· <?= count($entries) ?> <?= count($entries) === 1 ? 'Mitglied' : 'Mitglieder' ?>
<?php if (!empty($award['author'])): ?>· vergeben von <?= e((string) $award['author']) ?><?php endif; ?>
</div>
<details class="contribution-award-members"<?= count($entries) <= 6 ? ' open' : '' ?>>
<summary>Mitglieder anzeigen</summary>
<div class="contribution-chips">
<?php foreach ($entries as $entry): ?>
<span class="contribution-chip<?= (int) $entry['user_id'] === $filterUserId ? ' highlight' : '' ?>">
<a href="/contributions.php?user=<?= (int) $entry['user_id'] ?>#verlauf"><?= e((string) $entry['name']) ?></a>
<?php if ($uniform === null): ?>
<span class="text-muted"><?= contribution_format((int) $entry['points'], true) ?></span>
<?php endif; ?>
<?php if ($canManage): ?>
<form class="inline-form" method="post" action="/contributions_action.php"
onsubmit="return confirm(<?= e(json_encode('„' . $entry['name'] . '“ aus dieser Vergabe entfernen? Die Punkte werden abgezogen.', JSON_UNESCAPED_UNICODE)) ?>);">
<?= csrf_field() ?>
<input type="hidden" name="action" value="remove_entry">
<input type="hidden" name="entry_id" value="<?= (int) $entry['id'] ?>">
<button type="submit" class="contribution-chip-remove" title="Aus der Vergabe entfernen" aria-label="Entfernen">×</button>
</form>
<?php endif; ?>
</span>
<?php endforeach; ?>
</div>
</details>
</div>
<?php if ($canManage): ?>
<form class="contribution-award-delete" method="post" action="/contributions_action.php"
onsubmit="return confirm(<?= e(json_encode('Vergabe „' . $award['title'] . '“ komplett löschen? Allen ' . count($entries) . ' Mitgliedern werden die Punkte wieder abgezogen.', JSON_UNESCAPED_UNICODE)) ?>);">
<?= csrf_field() ?>
<input type="hidden" name="action" value="delete_award">
<input type="hidden" name="award_id" value="<?= (int) $award['id'] ?>">
<button type="submit" class="icon-button icon-button-danger" title="Vergabe löschen" aria-label="Vergabe löschen"><?= icon('trash') ?></button>
</form>
<?php endif; ?>
</article>
<?php endforeach; ?>
</div>
<div class="contribution-pages">
<span class="text-muted">
Vergaben <?= ($page - 1) * $perPage + 1 ?>–<?= min($page * $perPage, $totalAwards) ?>
von <?= number_format($totalAwards, 0, ',', '.') ?>
</span>
<?php render_pagination('/contributions.php?' . ($filterUserId !== null ? 'user=' . $filterUserId . '&' : ''), $page, $pageCount, '#verlauf'); ?>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
<script src="/assets/js/contributions.js"></script>
<?php include __DIR__ . '/includes/footer.php'; ?>