*/ function activity_log_fields(int $id): array { try { $check = activity_api_request('GET', '/' . $id)['check'] ?? null; } catch (Throwable $e) { $check = null; } if (!is_array($check)) { return ['Check' => '#' . $id]; } $responses = (array) ($check['responses'] ?? []); $present = count(array_filter($responses, static fn ($r) => ($r['status'] ?? '') === 'present')); return [ 'Check' => '#' . $id . ' – ' . (string) ($check['title'] ?? ''), 'Zielgruppe' => (string) ($check['target_role_name'] ?? ''), 'Status vorher' => ['open' => 'offen', 'closing' => 'wird abgeschlossen', 'closed' => 'abgeschlossen', 'cancelled' => 'abgebrochen'][$check['status'] ?? ''] ?? (string) ($check['status'] ?? ''), 'Rückmeldungen' => $present . ' da, ' . (count($responses) - $present) . ' nicht da, ' . count((array) ($check['pending'] ?? [])) . ' ohne Antwort', ]; } $me = current_user(); $myName = $me['display_name'] !== '' ? $me['display_name'] : $me['username']; /** Ganze Zahl aus einem ID-Feld (Discord-IDs sind 17-20 Stellen), null wenn leer. */ $idField = static function (string $name): ?int { $value = trim((string) ($_POST[$name] ?? '')); return preg_match('/^\d{5,20}$/', $value) ? (int) $value : null; }; try { switch ($action) { case 'create': $title = trim((string) ($_POST['title'] ?? '')); $message = trim(str_replace("\r\n", "\n", (string) ($_POST['message'] ?? ''))); $channelId = $idField('channel_id'); $roleId = $idField('target_role_id'); $value = max(1, min(60, (int) ($_POST['duration_value'] ?? 3))); $minutes = ($_POST['duration_unit'] ?? 'days') === 'hours' ? $value * 60 : $value * 1440; // Farbe: eigene Farbe gewinnt, wenn sie von der Auswahl abweicht $color = (string) ($_POST['color'] ?? '#206694'); $custom = (string) ($_POST['color_custom'] ?? ''); if (preg_match('/^#[0-9a-f]{6}$/i', $custom) && strtolower($custom) !== '#206694') { $color = $custom; } $image = trim((string) ($_POST['image_url'] ?? '')); if ($title === '' || $message === '' || $channelId === null || $roleId === null) { throw new InvalidArgumentException('Bitte Titel, Nachricht, Kanal und Zielrolle ausfüllen.'); } if ($image !== '' && !preg_match('#^https://\S+$#', $image)) { throw new InvalidArgumentException('Das Bild muss ein https-Link sein.'); } $dmEnabled = !empty($_POST['dm_enabled']); $body = [ 'guild_id' => events_guild_id(), 'channel_id' => $channelId, 'target_role_id' => $roleId, 'title' => mb_substr($title, 0, 100), 'message' => mb_substr($message, 0, 3500), 'color' => preg_match('/^#[0-9a-f]{6}$/i', $color) ? hexdec(substr($color, 1)) : 0x206694, 'image_url' => $image !== '' ? $image : null, 'ping_role' => !empty($_POST['ping_role']), 'yes_label' => mb_substr(trim((string) ($_POST['yes_label'] ?? '')), 0, 60) ?: null, 'no_label' => mb_substr(trim((string) ($_POST['no_label'] ?? '')), 0, 60) ?: null, 'duration_minutes' => $minutes, 'dm_enabled' => $dmEnabled, 'dm_message' => $dmEnabled ? mb_substr(trim((string) ($_POST['dm_message'] ?? '')), 0, 1800) : null, 'remove_role_id' => $idField('remove_role_id'), 'add_role_id' => $idField('add_role_id'), 'apply_to_absent' => !empty($_POST['apply_to_absent']), 'created_by' => (int) ($me['discord_id'] ?? 0), ]; $points = max(0, min(CONTRIBUTION_MAX_POINTS, (int) ($_POST['points_present'] ?? 0))); $result = activity_api_request('POST', '', [], $body); // Belohnung fuer "Ich bin da" merken - vergeben wird nach dem Abschluss activity_points_save((int) ($result['check_id'] ?? 0), $points, time() + $minutes * 60); $consequences = array_filter([ $dmEnabled ? 'PN' : null, $body['remove_role_id'] ? 'Rolle entfernen' : null, $body['add_role_id'] ? 'Rolle geben' : null, ]); admin_log('📋 Aktivitätscheck gestartet', [ 'Titel' => $body['title'], 'Dauer' => $minutes >= 1440 ? ($minutes / 1440) . ' Tage' : ($minutes / 60) . ' Stunden', 'Folgen' => $consequences ? implode(', ', $consequences) : 'keine', 'Belohnung' => $points > 0 ? $points . ' Lumipunkte für „Ich bin da“' : null, ], 'info', 'events'); set_flash('success', 'Aktivitätscheck gepostet - die Leute können sich jetzt im Discord zurückmelden.'); redirect('/activity.php?id=' . (int) ($result['check_id'] ?? 0)); case 'close': case 'cancel': $checkLog = activity_log_fields($id); // Abschliessen: der Bot antwortet sofort und arbeitet PNs/Rollen im Hintergrund ab // (bei vielen Leuten dauert das laenger, als die Website warten kann). $response = activity_api_request('POST', '/' . $id . '/close', [], [ 'cancel' => $action === 'cancel', 'closed_by' => $myName . ' (Website)', ]); admin_log($action === 'cancel' ? '🚫 Aktivitätscheck abgebrochen' : '📋 Aktivitätscheck abgeschlossen', $checkLog + [ 'Folgen' => $action === 'cancel' ? 'keine (abgebrochen)' : 'werden jetzt ausgeführt', ], $action === 'cancel' ? 'warning' : 'success', 'events'); // Lumipunkte gleich vergeben (bzw. beim Abbruch verwerfen) - nicht erst nach Ablauf der Frist foreach (activity_process_points($id) as $pointsMessage) { set_flash('success', $pointsMessage); } set_flash('success', $action === 'cancel' ? 'Aktivitätscheck abgebrochen - ohne Folgen.' : (($response['state'] ?? '') === 'closing' ? 'Aktivitätscheck wird abgeschlossen - PNs und Rollen werden gerade verschickt. Das Ergebnis erscheint gleich hier und im Log-Kanal.' : 'Aktivitätscheck ist abgeschlossen.')); redirect('/activity.php?id=' . $id); case 'delete': $checkLog = activity_log_fields($id); activity_api_request('DELETE', '/' . $id); admin_log('🗑️ Aktivitätscheck gelöscht', $checkLog, 'warning', 'events'); set_flash('success', 'Aktivitätscheck gelöscht.'); redirect('/activity.php'); } } catch (InvalidArgumentException $e) { set_flash('error', $e->getMessage()); redirect('/activity.php'); } catch (Throwable $e) { set_flash('error', 'Der Discord-Bot hat nicht mitgespielt: ' . $e->getMessage()); redirect($id > 0 ? '/activity.php?id=' . $id : '/activity.php'); } redirect('/activity.php');