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>
67 lines
2.5 KiB
PHP
67 lines
2.5 KiB
PHP
<?php
|
||
require_once __DIR__ . '/includes/bootstrap.php';
|
||
require_once __DIR__ . '/includes/raid_attendance.php';
|
||
require_permission('raids.manage');
|
||
|
||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||
redirect('/raids.php');
|
||
}
|
||
verify_csrf();
|
||
|
||
$raidId = (int) ($_POST['raid_id'] ?? 0);
|
||
if ($raidId <= 0) {
|
||
redirect('/raids.php');
|
||
}
|
||
$userId = (int) current_user()['id'];
|
||
$statuses = (array) ($_POST['status'] ?? []);
|
||
$names = (array) ($_POST['names'] ?? []);
|
||
|
||
// Nachgetragenes Mitglied kommt als anwesend dazu.
|
||
$addDiscordId = trim((string) ($_POST['add_discord_id'] ?? ''));
|
||
if ($addDiscordId !== '' && ctype_digit($addDiscordId)) {
|
||
$stmt = db()->prepare("SELECT COALESCE(NULLIF(display_name, ''), username) FROM users WHERE discord_id = ?");
|
||
$stmt->execute([$addDiscordId]);
|
||
$name = $stmt->fetchColumn();
|
||
if ($name !== false) {
|
||
$statuses[$addDiscordId] = 'present';
|
||
$names[$addDiscordId] = (string) $name;
|
||
}
|
||
}
|
||
|
||
$pdo = db();
|
||
$pdo->beginTransaction();
|
||
$upsert = $pdo->prepare(
|
||
'INSERT INTO raid_attendance (raid_id, discord_id, name, status, updated_by, updated_at) VALUES (?, ?, ?, ?, ?, NOW())
|
||
ON DUPLICATE KEY UPDATE name = VALUES(name), status = VALUES(status), updated_by = VALUES(updated_by), updated_at = NOW()'
|
||
);
|
||
$delete = $pdo->prepare('DELETE FROM raid_attendance WHERE raid_id = ? AND discord_id = ?');
|
||
$counts = array_fill_keys(array_keys(RAID_ATTENDANCE_STATUSES), 0);
|
||
|
||
foreach ($statuses as $discordId => $status) {
|
||
$discordId = (string) $discordId;
|
||
if (!ctype_digit($discordId)) {
|
||
continue;
|
||
}
|
||
$status = (string) $status;
|
||
if (isset(RAID_ATTENDANCE_STATUSES[$status])) {
|
||
$upsert->execute([$raidId, $discordId, mb_substr((string) ($names[$discordId] ?? ''), 0, 100), $status, $userId]);
|
||
$counts[$status]++;
|
||
} else {
|
||
$delete->execute([$raidId, $discordId]); // "–" = nicht erfasst
|
||
}
|
||
}
|
||
|
||
if (!empty($_POST['mark_checked'])) {
|
||
$pdo->prepare(
|
||
'INSERT INTO raid_settings (raid_id, attendance_checked_at, attendance_checked_by) VALUES (?, NOW(), ?)
|
||
ON DUPLICATE KEY UPDATE attendance_checked_at = NOW(), attendance_checked_by = VALUES(attendance_checked_by)'
|
||
)->execute([$raidId, $userId]);
|
||
}
|
||
$pdo->commit();
|
||
|
||
set_flash('success', sprintf(
|
||
'Anwesenheit gespeichert: %d anwesend, %d abwesend, %d entschuldigt%s.',
|
||
$counts['present'], $counts['absent'], $counts['excused'],
|
||
!empty($_POST['mark_checked']) ? ' - als kontrolliert markiert' : ''
|
||
));
|
||
redirect('/raid_attendance.php?raid=' . $raidId);
|