"`note $d`", $disciplines)); $columns_array = array_merge( ['id', 'programm'], array_map(fn($d) => "note $d", $disciplines) ); $all_rows = db_select($mysqli, $tableTurnerinnen, $columns_array, 'bezahlt = ? OR bezahltoverride = ?', ['2', '5']); $success = 0; // Step 1: Calculate gesamtpunktzahl and update per row foreach ($all_rows as $row) { $sum = 0; foreach ($disciplines as $discipline) { $value = $row["note $discipline"]; if (is_numeric($value)) { $sum += floatval($value); } } db_update($mysqli, $tableTurnerinnen, ['gesamtpunktzahl' => $sum], ['id' => $row['id']]); $success++; } // Step 2: Re-fetch rows grouped by programm with updated gesamtpunktzahl $all_programms = db_get_col($mysqli, "SELECT DISTINCT programm FROM ".$tableTurnerinnen); foreach ($all_programms as $programm) { $group = db_select($mysqli, $tableTurnerinnen, ['id', 'gesamtpunktzahl', 'note sprung', 'note barren', 'note balken', 'note boden', 'geburtsdatum'], 'programm = ? AND (bezahlt = ? OR bezahltoverride = ?)', [$programm, '2', '5']); usort($group, function ($a, $b) { $scoreA = floatval($a['gesamtpunktzahl']); $scoreB = floatval($b['gesamtpunktzahl']); if ($scoreA !== $scoreB) return $scoreB <=> $scoreA; // Only for top 3 tie-breaking $scoresA = [ floatval($a['note sprung']), floatval($a['note barren']), floatval($a['note balken']), floatval($a['note boden']), ]; $scoresB = [ floatval($b['note sprung']), floatval($b['note barren']), floatval($b['note balken']), floatval($b['note boden']), ]; rsort($scoresA); rsort($scoresB); $sumTop3A = $scoresA[0] + $scoresA[1] + $scoresA[2]; $sumTop3B = $scoresB[0] + $scoresB[1] + $scoresB[2]; if (abs($sumTop3A - $sumTop3B) > 0.001) return $sumTop3B <=> $sumTop3A; $sumTop2A = $scoresA[0] + $scoresA[1]; $sumTop2B = $scoresB[0] + $scoresB[1]; if (abs($sumTop2A - $sumTop2B) > 0.001) return $sumTop2B <=> $sumTop2A; if (abs($scoresA[0] - $scoresB[0]) > 0.001) return $scoresB[0] <=> $scoresA[0]; // Younger participant ranks higher in case of full tie $dateA = strtotime($a['geburtsdatum']); $dateB = strtotime($b['geburtsdatum']); return $dateB <=> $dateA; // later birthdate = younger = better }); // Step 2: Assign ranks $ranked = []; $current_rank = 1; $i = 0; while ($i < count($group)) { $current = $group[$i]; $tie_group = [$current]; $j = $i + 1; while ($j < count($group)) { $next = $group[$j]; // Tie logic if ($current_rank <= 3) { // Top 3: full tie-breaking $is_tie = round(floatval($current['gesamtpunktzahl']), 3) === round(floatval($next['gesamtpunktzahl']), 3) && round(floatval($current['note sprung']), 3) === round(floatval($next['note sprung']), 3) && round(floatval($current['note barren']), 3) === round(floatval($next['note barren']), 3) && round(floatval($current['note balken']), 3) === round(floatval($next['note balken']), 3) && round(floatval($current['note boden']), 3) === round(floatval($next['note boden']), 3) && $current['geburtsdatum'] === $next['geburtsdatum']; } else { // Ranks > 3: only check gesamtpunktzahl $is_tie = round(floatval($current['gesamtpunktzahl']), 3) === round(floatval($next['gesamtpunktzahl']), 3); } if ($is_tie) { $tie_group[] = $next; $j++; } else { break; } } // Assign the same rank for all ties in ranks > 3 foreach ($tie_group as $entry) { $ranked[] = [ 'id' => $entry['id'], 'rang' => $current_rank ]; } $i += count($tie_group); $current_rank += count($tie_group); } // Step 3: Write all ranks to DB foreach ($ranked as $r) { db_update($mysqli, $tableTurnerinnen, ['rang' => $r['rang']], ['id' => $r['id']]); } } $_SESSION['form_message'] = $success . ' Einträge wurden aktualisiert und neu gerankt.'; header("Location: ". $_SERVER['REQUEST_URI']); exit; } } if ($selecteduser === 'admin' && isset($_POST['reset_scores'])) { if (!verify_csrf()) { $form_message = 'Sicherheitsproblem bei der Neuberechnung.'; } else { $all_rows = db_select($mysqli, $tableTurnerinnen, 'id'); $success = 0; foreach ($all_rows as $row) { db_update($mysqli, $tableTurnerinnen, ['gesamtpunktzahl' => 0, 'rang' => 0], ['id' => $row['id']]); $success++; } $_SESSION['form_message'] = '
' . $success . ' Einträge wurden zurückgesetzt.
'; header("Location: ". $_SERVER['REQUEST_URI']); exit; } } // === EDIT MODE: Load existing data if edit_id is present in URL === $edit_row = null; if (isset($_GET['edit_id']) && is_numeric($_GET['edit_id']) && !isset($_POST['submit_turnerinnen_form'])) { $edit_id = intval($_GET['edit_id']); $edit_row = db_select($mysqli, $tableTurnerinnen, '*', 'id = ?', [$edit_id]); foreach ($disciplines as $discipline) { if ($selecteduser === ucfirst($discipline) || $selecteduser === 'admin') { $_POST["d-note_{$discipline}"] = $edit_row["d-note {$discipline}"] ?? ''; $_POST["note_{$discipline}"] = $edit_row["note {$discipline}"] ?? ''; } } if ($selecteduser === 'admin') { $gesamt = 0; foreach ($disciplines as $discipline) { if (isset($edit_row[0]["note {$discipline}"]) && is_numeric($edit_row[0]["note {$discipline}"])) { $gesamt += floatval($edit_row[0]["note {$discipline}"]); } } $_POST["gesamtpunktzahl"] = $gesamt; $rang = isset($_POST['rang']) ? intval($_POST['rang']) : 0; // Safely get and cast to int $data_to_insert["rang"] = $rang; $data_formats[] = '%d'; } $_POST['edit_id'] = $edit_id; } if (isset($_POST['submit_turnerinnen_form'])) { // Check nonce if (!verify_csrf()) { $form_message = 'Sicherheitsproblem: Ungültige Formularübermittlung.'; } else { foreach ($disciplines as $discipline) { if ($selecteduser === ucfirst($discipline) || $selecteduser === 'admin') { ${"d_note_$discipline"} = floatval($_POST["d-note_{$discipline}"]); ${"note_$discipline"} = floatval($_POST["note_{$discipline}"]); // Add to data array $data_to_insert["d-note $discipline"] = ${"d_note_$discipline"}; $data_to_insert["note $discipline"] = ${"note_$discipline"}; $data_formats[] = '%f'; // float format for d-note $data_formats[] = '%f'; // float format for note } } if ($selecteduser === 'admin') { $gesamtpunktzahl = isset($_POST['gesamtpunktzahl']) ? intval($_POST['gesamtpunktzahl']) : 0; // Safely get and cast to int $data_to_insert["gesamtpunktzahl"] = $gesamtpunktzahl; $data_formats[] = '%f'; // CORRECTED LINE: Get 'rang' from $_POST $rang = isset($_POST['rang']) ? intval($_POST['rang']) : 0; // Safely get and cast to int $data_to_insert["rang"] = $rang; $data_formats[] = '%d'; } // Check if we are editing an existing entry $is_editing = isset($_POST['edit_id']) && is_numeric($_POST['edit_id']) && $_POST['edit_id'] > 0; if ($is_editing) { $edit_id = intval($_POST['edit_id']); $updated = db_update($mysqli, $tableTurnerinnen, $data_to_insert, ['id' => $edit_id]); if ($updated === false) { $form_message = 'Fehler beim Aktualisieren des Eintrags.'; } else if ($updated === 0) { $form_message = 'Keine Änderungen vorgenommen.'; } else { $_SESSION['form_message'] = 'Eintrag erfolgreich aktualisiert!'; $_POST = []; $parsed = parse_url($_SERVER['REQUEST_URI']); if (!isset($parsed['query'])) { return $url; } parse_str($parsed['query'], $query); unset($query[$param]); // remove the parameter $base = $parsed['path'] ?? ''; $new_query = http_build_query($query); $url = $new_query ? $base . '?' . $new_query : $base; header("Location: ". $url); exit; } } } } if ((isset($_POST['prev_abt'])) && !empty($_POST['prev_abt_submit'])) { $value = $aktabt; if ($value > 1){ $value -= 1; $name = 'wk_panel_current_abt'; $stmt = $mysqli->prepare("INSERT INTO $tableVar (`name`, `value`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `value` = VALUE(`value`)"); $stmt->bind_param("ss", $name, $value); $stmt->execute(); $stmt->close(); } header("Location: /intern/kampfrichter"); exit; } if ((isset($_POST['next_abt'])) && !empty($_POST['next_abt_submit'])) { $value = $aktabt; $maxvalue = db_get_var($mysqli, "SELECT name FROM $tableAbt ORDER BY name DESC LIMIT 1"); if ($value < $maxvalue){ $value += 1; $name = 'wk_panel_current_abt'; $stmt = $mysqli->prepare("INSERT INTO $tableVar (`name`, `value`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `value` = VALUE(`value`)"); $stmt->bind_param("ss", $name, $value); $stmt->execute(); $stmt->close(); } header("Location: /intern/kampfrichter"); exit; } if (!isset($_SESSION['currentsubabt'])){ $_SESSION['currentsubabt'] = 1; } if (!isset($_SESSION['last_abt'])){ $_SESSION['last_abt'] = $aktabt; } if ($_SESSION['last_abt'] !== $aktabt){ $_SESSION['currentsubabt'] = 1; $_SESSION['last_abt'] = $aktabt; } if ((isset($_POST['prev_subabt'])) && !empty($_POST['prev_subabt_submit'])) { $value = $_SESSION['currentsubabt']; if ($value > 1){ $_SESSION['currentsubabt']--; $_SESSION['currentEditId'] = false; $_SESSION['last_abt'] = $aktabt; } header("Location: /intern/kampfrichter"); exit; } if ((isset($_POST['next_subabt'])) && !empty($_POST['next_subabt_submit'])) { $value = $_SESSION['currentsubabt']; if ($value < $maxsubabt){ $_SESSION['currentsubabt']++; $_SESSION['currentEditId'] = false; $_SESSION['last_abt'] = $aktabt; } header("Location: /intern/kampfrichter"); exit; } if ( isset($_POST['togle_advanced_mode_admin']) && !empty($_POST['togle_advanced_mode_admin_submit']) ) { $current_value = $focus_view_admin; $new_value = !$current_value; $_SESSION['abtViewAdmin'] = $new_value; header("Location: /intern/kampfrichter"); exit; } if ((isset($_POST['upload_remove_pdf_for_programm'])) && !empty($_POST['programm_remove_export'])) { $current_year = date('Y'); $monat = date('n'); if ($monat > 6) $current_year++; $programm = trim($_POST['programm_remove_export']); $dir = $_SERVER['DOCUMENT_ROOT'] . '/wp-content/ergebnisse'; if (!file_exists($dir)) { mkdir($dir, 0755, true); } $localPath = $_SERVER['DOCUMENT_ROOT'] . "/wp-content/ergebnisse/KTBB_Ergebnisse_" . $programm . "_" . $current_year . ".pdf"; // --- ADDED CODE START --- // Check if the file already exists and delete it if (file_exists($localPath)) { unlink($localPath); } // --- ADDED CODE END --- $_SESSION['form_message'] = 'PDF wurde gelöscht'; header("Location: ". $_SERVER['REQUEST_URI']); }