LuminaWebsite/contributions_award.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

194 lines
9.4 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.manage');
$members = db()->query(
"SELECT u.id, u.username, u.display_name, u.avatar_filename, u.discord_id,
COALESCE(SUM(e.points), 0) AS points
FROM users u
LEFT JOIN contribution_entries e ON e.user_id = u.id
WHERE u.status = 'active'
GROUP BY u.id
ORDER BY COALESCE(NULLIF(u.display_name, ''), u.username)"
)->fetchAll();
$userIdByDiscord = [];
foreach ($members as $member) {
if (!empty($member['discord_id'])) {
$userIdByDiscord[(string) $member['discord_id']] = (int) $member['id'];
}
}
// Raids fuer den Anlass "Raid": die Teilnehmer (Anmeldungen in einer Gruppe) werden im
// Formular vorausgewaehlt - soweit sie ein Website-Konto mit Discord-Verknuepfung haben.
$raids = [];
$raidsError = null;
try {
$apiRaids = fetch_raids('all');
raid_process_auto_points($apiRaids);
usort($apiRaids, static fn (array $a, array $b): int => (int) ($b['timestamp'] ?? 0) <=> (int) ($a['timestamp'] ?? 0));
foreach (array_slice($apiRaids, 0, 40) as $raid) {
$participants = [];
$bench = [];
$withoutAccount = [];
foreach ((array) ($raid['signups'] ?? []) as $signup) {
$group = (int) ($signup['group'] ?? 0);
if ($group < 0 || ($signup['class'] ?? '') === 'Ausstehend') {
continue; // abgemeldet bzw. noch nicht bestaetigt
}
$userId = $userIdByDiscord[(string) ($signup['user_id'] ?? '')] ?? null;
if ($userId === null) {
$withoutAccount[] = (string) ($signup['name'] ?? '?');
} elseif ($group >= 1) {
$participants[] = $userId;
} else {
$bench[] = $userId;
}
}
// Anwesenheitsliste: Anwesende bekommen Punkte, Abwesenden kann man abziehen.
$present = [];
$absent = [];
$raidIdValue = (int) ($raid['raid_id'] ?? 0);
$attendance = raid_attendance_get($raidIdValue);
foreach ($attendance as $discordId => $entry) {
$userId = $userIdByDiscord[$discordId] ?? null;
if ($userId !== null && $entry['status'] === 'present') {
$present[] = $userId;
} elseif ($userId !== null && $entry['status'] === 'absent') {
$absent[] = $userId;
}
}
$timestamp = (int) ($raid['timestamp'] ?? 0);
$raids[] = [
// Als Text: Discord-IDs sind 19-stellig und in JavaScript als Zahl nicht exakt.
'id' => (string) $raidIdValue,
'attendance' => !empty($attendance),
'present' => $present,
'absent' => $absent,
'title' => (string) ($raid['title'] ?? 'Raid'),
'date' => $timestamp ? date('d.m.Y H:i', $timestamp) : (string) ($raid['date'] ?? ''),
'time' => $timestamp ? date('Y-m-d\TH:i', $timestamp) : '',
'participants' => $participants,
'bench' => $bench,
'withoutAccount' => $withoutAccount,
];
}
} catch (Throwable $e) {
$raidsError = $e->getMessage();
}
// Vorbelegung: nach einem Fehler die bisherigen Eingaben, sonst ggf. ein einzelnes Mitglied.
$old = $_SESSION['contribution_form'] ?? null;
unset($_SESSION['contribution_form']);
$preselectUser = isset($_GET['user']) ? (int) $_GET['user'] : 0;
$form = $old ?? [
'category' => $preselectUser ? 'other' : 'raid',
'raid_id' => '',
'title' => '',
'awarded_at' => contribution_now_input(),
'points' => '',
'note' => '',
'users' => $preselectUser ? [$preselectUser] : [],
];
$selected = array_map('intval', (array) ($form['users'] ?? []));
$pageTitle = 'Beitragspunkte vergeben';
$containerWide = true;
include __DIR__ . '/includes/header.php';
?>
<form class="panel contribution-form" method="post" action="/contributions_action.php" id="contribution-form">
<?= csrf_field() ?>
<input type="hidden" name="action" value="create">
<div class="contribution-head">
<h1><?= contribution_coin(32) ?> Beitragspunkte vergeben</h1>
<a href="/contributions.php">Zurück zur Übersicht</a>
</div>
<div class="contribution-form-grid">
<div>
<label for="category">Anlass</label>
<select id="category" name="category">
<?php foreach (CONTRIBUTION_CATEGORIES as $key => $label): ?>
<option value="<?= e($key) ?>" <?= ($form['category'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
<?php endforeach; ?>
</select>
</div>
<div id="raid-field">
<label for="raid_id">Raid <span class="text-muted">(optional)</span></label>
<?php if ($raidsError === null): ?>
<select id="raid_id" name="raid_id">
<option value="">– keinen Raid auswählen –</option>
<?php foreach ($raids as $raid): ?>
<option value="<?= e($raid['id']) ?>" <?= (string) ($form['raid_id'] ?? '') === $raid['id'] ? 'selected' : '' ?>>
<?= e($raid['title']) ?><?= $raid['date'] !== '' ? ' – ' . e($raid['date']) : '' ?>
</option>
<?php endforeach; ?>
</select>
<div class="field-hint" id="raid-hint">Wählt die Teilnehmer des Raids automatisch aus.</div>
<?php else: ?>
<input type="hidden" name="raid_id" value="">
<p class="field-hint">Die Raids konnten nicht geladen werden (<?= e($raidsError) ?>). Mitglieder bitte von Hand auswählen.</p>
<?php endif; ?>
</div>
<div class="contribution-form-wide">
<label for="title">Grund</label>
<input type="text" id="title" name="title" maxlength="150" required
value="<?= e((string) ($form['title'] ?? '')) ?>" placeholder="z.B. Festungskampf Verteron, Hilfe beim Handwerk …">
</div>
<div>
<label for="points">Punkte pro Mitglied</label>
<input type="number" id="points" name="points" required step="1"
min="-<?= CONTRIBUTION_MAX_POINTS ?>" max="<?= CONTRIBUTION_MAX_POINTS ?>"
value="<?= e((string) ($form['points'] ?? '')) ?>" placeholder="z.B. 10">
<div class="field-hint">Negativ für einen Abzug.</div>
</div>
<div>
<label for="awarded_at">Datum</label>
<input type="datetime-local" id="awarded_at" name="awarded_at" required value="<?= e((string) ($form['awarded_at'] ?? contribution_now_input())) ?>">
</div>
<div class="contribution-form-wide">
<label for="note">Bemerkung <span class="text-muted">(optional)</span></label>
<input type="text" id="note" name="note" maxlength="255" value="<?= e((string) ($form['note'] ?? '')) ?>">
</div>
</div>
<div class="contribution-picker">
<div class="contribution-picker-head">
<label>Mitglieder <span class="contribution-picker-count" id="picker-count"></span></label>
<input type="search" id="picker-search" placeholder="Mitglied suchen …" autocomplete="off">
<button type="button" class="button-secondary button-small" id="picker-all">Alle</button>
<button type="button" class="button-secondary button-small" id="picker-none">Keine</button>
</div>
<div class="contribution-raid-mode" id="raid-mode" hidden>
<span class="text-muted">Laut Anwesenheitsliste:</span>
<button type="button" class="button-secondary button-small" data-raid-mode="present">Anwesende auswählen (Punkte)</button>
<button type="button" class="button-secondary button-small" data-raid-mode="absent">Abwesende auswählen (Abzug)</button>
<button type="button" class="button-secondary button-small" data-raid-mode="participants">Alle Teilnehmer laut Anmeldung</button>
</div>
<p class="field-hint contribution-raid-info" id="raid-info" hidden></p>
<div class="contribution-picker-grid">
<?php foreach ($members as $member): ?>
<?php $name = (string) ($member['display_name'] !== '' ? $member['display_name'] : $member['username']); ?>
<label class="contribution-pick" data-name="<?= e(mb_strtolower($name)) ?>">
<input type="checkbox" name="users[]" value="<?= (int) $member['id'] ?>" <?= in_array((int) $member['id'], $selected, true) ? 'checked' : '' ?>>
<?= avatar_html($member, 32) ?>
<span class="contribution-pick-name"><?= e($name) ?></span>
<span class="contribution-pick-points"><?= contribution_coin(14) ?> <?= contribution_format((int) $member['points']) ?></span>
</label>
<?php endforeach; ?>
</div>
</div>
<div class="actions">
<button type="submit" id="contribution-submit">Punkte vergeben</button>
<a class="button button-secondary" href="/contributions.php">Abbrechen</a>
</div>
</form>
<script id="contribution-raids" type="application/json"><?= json_encode($raids, JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT) ?></script>
<script src="/assets/js/contributions.js"></script>
<?php include __DIR__ . '/includes/footer.php'; ?>