Eingabetool noch nicht freigeschalten';
}
echo '
Alle Turnerinnen:
';
$total_count = 0;
if (!empty($grouped)) :
// Extract all 'id' values into a flat array
$ids = array_column($entries, 'id');
if (!empty($ids)) {
// Create placeholders: ?,?,?
$placeholders = implode(',', array_fill(0, count($ids), '?'));
$sql = "SELECT `person_id`, `note_bezeichnung_id`, `geraet_id`, `run_number`, `value`
FROM $tableNoten
WHERE `jahr` = ? AND `person_id` IN ($placeholders)";
$stmt = $mysqli->prepare($sql);
// In PHP 8.1+, you can simply pass an array to execute()
// We merge the year with the IDs into one flat array
$params = array_merge([$current_year], $ids);
$stmt->execute($params);
$noten = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
}
$notenIndexed = [];
foreach ($noten as $sn) {
$notenIndexed[$sn['person_id']][$sn['geraet_id']][$sn['note_bezeichnung_id']][$sn['run_number']] = $sn['value'];
}
$stmt->close();
// Extended Disciplines, contains the 0 disc
$disciplinesExtended = array_merge(
[["id" => 0, "name" => "None"]],
$disciplines
);
foreach ($grouped as $abteilung => $entries_group) :
// ... (PDF path and H3 tag)
// Nur sortieren, wenn der Benutzer nicht 'Admin' ist
if ($selecteduser !== 'admin') {
$entries_with_calculated_index = [];
// Optional: Vorab-Caching der MAX-Indices, um DB-Abfragen im Loop zu vermeiden
// (Nur aktivieren, wenn $entries_group sehr groß ist und Performance-Probleme auftreten)
$maxStartIndexCache = [];
foreach ($entries_group as $row) {
$rohstartindex = intval($row['startIndex']);
$geraetId = intval($row['geraetId']);
$abtId = intval($row['abtId']);
// Hole maxStartIndex (mit Caching-Logik optional)
if (!isset($maxStartIndexCache["$abtId-$geraetId"])) {
$maxStartIndexCache["$abtId-$geraetId"] = db_get_var($mysqli, "SELECT COUNT(*) FROM `$tableTurnerinnenAbt` WHERE abteilung_id = ? AND geraet_id = ?", [$abtId, $geraetId]);
}
$maxstartindex = $maxStartIndexCache["$abtId-$geraetId"];
// Sicherheit: Vermeiden Sie Division durch Null oder Modulo durch 0
if ($maxstartindex < 1) {
$maxstartindex = 1;
}
// Sicherstellen, dass $indexuser definiert ist (Falls nicht, auf 0 setzen oder Fehler werfen)
if (!isset($indexuser)) {
$indexuser = 0; // Fallback, sollte aber eigentlich definiert sein
}
// The order shift depends on the number of rotations the group has made
$rotation_shift = $aktsubabt - 1;
// Robuste Berechnung mit Modulo
// Ziel: (rohstartindex - rotation_shift - 1) % maxstartindex + 1
// -1 wird subtrahiert, weil Modulo oft 0-basiert ist, wir aber 1-basiert arbeiten
$calculedstartindex = (($rohstartindex - $rotation_shift - 1) % $maxstartindex) + 1;
// Sicherstellen, dass das Ergebnis positiv ist (PHP Modulo kann negative Ergebnisse liefern)
// Wenn das Ergebnis negativ ist, addieren wir maxstartindex
if ($calculedstartindex <= 0) {
$calculedstartindex += $maxstartindex;
}
// Kopie der Zeile erstellen, um Originaldaten nicht zu verändern
$row['calculedstartindex'] = $calculedstartindex;
$entries_with_calculated_index[] = $row;
}
// Sortieren nach dem berechneten Startindex
usort($entries_with_calculated_index, fn($a, $b) => $a['calculedstartindex'] <=> $b['calculedstartindex']);
$entries_to_display = $entries_with_calculated_index;
} else {
$entries_to_display = $entries_group;
}
// Table Header creator with run columns
$arrayIndexedNoten = [];
$arrayIndexedNotenHeader = [];
$uidN = 0;
foreach ($entries_group as $row) :
foreach ($disciplinesExtended as $discipline) :
// Check if the current user/admin is allowed to see this specific discipline
if ($discipline['id'] === 0 || $selecteduser === strtolower($discipline['name']) || $selecteduser === 'admin') :
// 2. Now iterate through the scoring configurations for this discipline
foreach ($notenConfig as $snC) {
$showAdmin = isAdmin() && intval($snC['zeige_in_tabelle_admin']) === 1;
$showPublic = !isAdmin() && intval($snC['zeige_in_tabelle']) === 1;
if (!($showAdmin || $showPublic)) { continue; }
if (intval($snC['pro_geraet']) === 1 && intval($discipline['id']) === 0) { continue; }
if (intval($snC['pro_geraet']) !== 1) {
$allowedGeraete = !empty($snC['geraete_json']) ? json_decode($snC['geraete_json'], true) : [];
if (!in_array($discipline['id'], $allowedGeraete)) { continue; }
}
$mobile = (intval($snC['zeige_in_tabelle_mobile']) === 1) ? "" : "notMobile";
// Logic to fetch and format the value
$defaultValue = $snC['default_value'] ?? 0;
$runsJSON = !empty($snC['anzahl_laeufe_json']) ? json_decode($snC['anzahl_laeufe_json'], true) : [];
$runs = $runsJSON[$discipline['id']][$indexedProgrammes[$row['programm'] ?? ''] ?? ''] ?? $runsJSON["default"] ?? 1;
for ($r = 1; $r <= $runs; $r++) :
$note = $notenIndexed[$row['id']][$discipline['id']][$snC['id']][$r] ?? $defaultValue;
$normalizedNote = number_format($note, $snC['nullstellen'] ?? 2);
$arrayIndexedNoten[intval($row['id'])][intval($discipline['id'])][intval($snC['id'])][intval($r)] = ["value" => $normalizedNote, "mobile" => $mobile];
$arrayIndexedNotenHeader[intval($discipline['id'])][intval($snC['id'])][intval($r)] = ["mobile" => $mobile];
$uidN++;
endfor;
}
endif;
endforeach;
endforeach;
$localPath = $_SERVER['DOCUMENT_ROOT'] . "/wp-content/ergebnisse/KTBB_Ergebnisse_" . $abteilung . "_" . $current_year . ".pdf";
echo '
';
endforeach;
$mehrzahl = $total_count > 1 ? 'Turnerinnen':'Turnerin';
if ($selecteduser === 'admin'){
echo '
Gesamt: '.$total_count.' '.$mehrzahl.'
';
}
?>
Noch keine Datensätze vorhanden.