false, 'message' => 'Forbidden']); exit; } // Validate editId from POST if (isset($_POST['editId'])) { $editId = intval($_POST['editId']); if ($editId === false || $editId < 1) { echo json_encode(['success' => false, 'message' => 'Falsche Personen ID']); exit; } } $editId = filter_var($editId, FILTER_VALIDATE_INT); if ($editId === false) { echo json_encode(['success' => true]); exit; } require $baseDir . '/../scripts/db/db-functions.php'; require $baseDir . '/../scripts/db/db-tables.php'; $type = 'kr'; $data = require $baseDir . '/../scripts/db/db-verbindung-script.php'; if (!($data['success'] ?? false)) { echo json_encode(['success' => false, 'message' => $data['message']]); exit; } $isAdmin = (($_SESSION['selectedFreigabeKampfrichter'] ?? '') === 'admin') ? true : false; $disciplines = db_select($mysqli, $tableGeraete, 'id', '', [], 'start_index ASC'); $disciplines = array_column($disciplines, "id"); if (!$isAdmin) { $discipline = intval($_POST['geraet']) ?? 0; if (!in_array($discipline, $disciplines)) { echo json_encode(['success' => false, 'message' => 'Falsche Geräte ID']); exit; } $disciplines = [$discipline]; $stmt = $mysqli->prepare(" SELECT t.name, t.vorname, t.programm, p.id as programm_id, agg.abteilung, agg.geraeteIndex, agg.startIndex FROM $tableTurnerinnen t LEFT JOIN $tableProgramme p ON p.programm = t.programm LEFT JOIN ( SELECT ta.turnerin_id, GROUP_CONCAT(DISTINCT a.name SEPARATOR ', ') AS abteilung, GROUP_CONCAT(DISTINCT g.start_index SEPARATOR ', ') AS geraeteIndex, ta.turnerin_index AS startIndex FROM $tableTurnerinnenAbt ta INNER JOIN $tableAbt a ON a.id = ta.abteilung_id LEFT JOIN $tableGeraete g ON g.id = ta.geraet_id GROUP BY ta.turnerin_id ) agg ON agg.turnerin_id = t.id WHERE t.id = ? "); } else { $stmt = $mysqli->prepare("SELECT t.`name`, t.`vorname`, t.`programm`, p.id as programm_id FROM $tableTurnerinnen t LEFT JOIN $tableProgramme p ON p.programm = t.programm WHERE t.id = ?"); } $stmt->bind_param('i', $editId); $stmt->execute(); $result = $stmt->get_result(); $dbresult = $result->fetch_all(MYSQLI_ASSOC); $stmt->close(); if (!$dbresult || !is_array($dbresult) || count($dbresult) < 1) { echo json_encode(['success' => false, 'message' => 'Falsche Personen ID']); exit; } $now = new DateTime(); $jahr = ($now->format('n') > 6) ? $now->modify('+1 year')->format('Y') : $now->format('Y'); if ($isAdmin) { $stmt = $mysqli->prepare("SELECT `note_bezeichnung_id`, `value`, `geraet_id`, `run_number` FROM $tableNoten WHERE `person_id` = ? AND `jahr` = ?"); $stmt->bind_param('ss', $editId, $jahr); } else { $stmt = $mysqli->prepare("SELECT `note_bezeichnung_id`, `value`, `geraet_id`, `run_number` FROM $tableNoten WHERE `person_id` = ? AND `geraet_id` = ? AND `jahr` = ?"); $stmt->bind_param('sss', $editId, $discipline, $jahr); } $stmt->execute(); $result = $stmt->get_result(); $notenDB = $result->fetch_all(MYSQLI_ASSOC); $indexedNotenDB = []; foreach ($notenDB as $sn) { $indexedNotenDB[$sn['geraet_id']][$sn['note_bezeichnung_id']][$sn['run_number']] = $sn['value']; } $stmt->close(); $stmt = $mysqli->prepare("SELECT `id`, `default_value`, `nullstellen`, `pro_geraet`, `geraete_json`, `anzahl_laeufe_json` FROM $tableNotenBezeichnungen"); $stmt->execute(); $result = $stmt->get_result(); $notenConfig = $result->fetch_all(MYSQLI_ASSOC); $stmt->close(); $noten = []; $row = $dbresult[0]; $programm_id = $row['programm_id']; foreach ($disciplines as $d) { foreach ($notenConfig as $snC) { $allowedGeraete = !empty($snC['geraete_json']) ? json_decode($snC['geraete_json'], true) : []; $isProGeraet = ($snC['pro_geraet'] === 1); if (!$isProGeraet && !in_array($d, $allowedGeraete)) { continue; } // Determine number of runs for this program $anzRunsConfig = !empty($snC['anzahl_laeufe_json']) ? json_decode($snC['anzahl_laeufe_json'], true) : []; $runs = $anzRunsConfig[$programm_id] ?? $anzRunsConfig['default'] ?? 1; for ($r = 1; $r <= $runs; $r++) { $value = $indexedNotenDB[$d][$snC['id']][$r] ?? $snC['default_value'] ?? 0; $noten[$d][$snC['id']][$r] = number_format($value, $snC['nullstellen'] ?? 2); } } } $titel = $row['vorname'].' '.$row['name'].', '.$row['programm']; if (!$isAdmin) { // $entries = db_select($mysqli, $tableTurnerinnen, 'name, vorname, programm, id', 'abteilung = ? AND startgeraet = ?', [$row['abteilung'], $row['startgeraet']]); $stmt = $mysqli->prepare(" SELECT t.name, t.vorname, t.programm, t.id, agg.abteilung, agg.geraeteIndex, agg.startIndex FROM $tableTurnerinnen t LEFT JOIN ( SELECT ta.turnerin_id, GROUP_CONCAT(DISTINCT a.name SEPARATOR ', ') AS abteilung, GROUP_CONCAT(DISTINCT g.start_index SEPARATOR ', ') AS geraeteIndex, ta.turnerin_index AS startIndex FROM $tableTurnerinnenAbt ta INNER JOIN $tableAbt a ON a.id = ta.abteilung_id LEFT JOIN $tableGeraete g ON g.id = ta.geraet_id GROUP BY ta.turnerin_id ) agg ON agg.turnerin_id = t.id WHERE agg.abteilung = ? AND agg.geraeteIndex = ? ORDER BY t.id DESC "); $bezahlt = 2; $bezahltoverride = 5; $stmt->bind_param('ss', $row['abteilung'], $row['geraeteIndex']); $stmt->execute(); $result = $stmt->get_result(); $entries = $result->fetch_all(MYSQLI_ASSOC); $stmt->close(); if (!$entries || !is_array($entries) || count($entries) < 1) { echo json_encode(['success' => false, 'message' => 'No DB Result for next Turnerin']); exit; } $maxstartindex = count($entries); if ($maxstartindex < 1) { $maxstartindex = 1; } $csti = (int)$row['startIndex']; $nsti = $csti + 1; if ($nsti > $maxstartindex){ $nsti -= $maxstartindex; } $rohstartindex = intval($row['startIndex']); $varstartgeraet = intval($row['geraeteIndex']); $aktsubabt = $_SESSION['currentsubabt']; foreach ($disciplines as $index => $sdiscipline) { if (isset($sdiscipline) && $sdiscipline === $discipline) { $indexuser = $index; break; } } $calculedstartindex = $rohstartindex - $indexuser; $calculedstartindex = $calculedstartindex >= 1 ? $calculedstartindex : $calculedstartindex + $maxstartindex; $nrow = null; if ($calculedstartindex !== count($entries)){ $nrow = null; foreach ($entries as $entry) { if ($entry['startIndex'] == $nsti) { $nrow = $entry; break; } } } if ($nrow) { $nturnerin = [ 'name' => $nrow['vorname'].' '.$nrow['name'].', '.$nrow['programm'], 'id' => $nrow['id'] ]; } else { $nturnerin = [ 'name' => '--- nächste Gruppe ---', 'id' => 0 ]; } } if ($isAdmin) { echo json_encode([ 'success' => true, 'id' => $editId, 'programm_id' => $programm_id, 'titel' => $titel, 'noten' => $noten ]); } else { echo json_encode([ 'success' => true, 'id' => $editId, 'programm_id' => $programm_id, 'titel' => $titel, 'noten' => $noten, 'nturnerin' => $nturnerin ]); }