320 lines
10 KiB
PHP
320 lines
10 KiB
PHP
<?php
|
|
|
|
if (!isset($baseDir)) {
|
|
$baseDir = $_SERVER['DOCUMENT_ROOT'];
|
|
}
|
|
|
|
require_once $baseDir . '/../scripts/session_functions.php';
|
|
|
|
ini_wkvs_session();
|
|
|
|
check_user_permission('kampfrichter');
|
|
|
|
verify_csrf();
|
|
|
|
$type = 'kr';
|
|
|
|
$data = require $baseDir . '/../scripts/db/db-verbindung-script.php';
|
|
|
|
if ($data['success'] === false){
|
|
echo json_encode(['success' => false, 'message' => $data['message']]);
|
|
exit;
|
|
}
|
|
|
|
require $baseDir . '/../scripts/db/db-functions.php';
|
|
require $baseDir . '/../scripts/db/db-tables.php';
|
|
|
|
|
|
$person_id = isset($_POST['personId']) ? intval($_POST['personId']) : 0;
|
|
$field_type_id = intval($_POST['fieldTypeId'] ?? 0);
|
|
$gereat_id = intval($_POST['gereatId'] ?? 0);
|
|
$jahr = isset($_POST['jahr']) ? intval($_POST['jahr']) : 0;
|
|
$run_number = isset($_POST['run']) ? intval($_POST['run']) : 1;
|
|
|
|
if (!isset($_POST['value'])) {
|
|
echo json_encode(['success' => false, 'message' => 'Kein Value angegeben']);
|
|
exit;
|
|
}
|
|
|
|
$valueNoteUpdate = floatval($_POST['value']);
|
|
|
|
if ($person_id < 1) {
|
|
echo json_encode(['success' => false, 'message' => 'Invalide Personen-ID']);
|
|
exit;
|
|
}
|
|
|
|
if ($jahr < 1) {
|
|
echo json_encode(['success' => false, 'message' => 'Invalides Jahr']);
|
|
exit;
|
|
}
|
|
|
|
if ($gereat_id < 1) {
|
|
echo json_encode(['success' => false, 'message' => 'Invalide Geraet-ID']);
|
|
exit;
|
|
}
|
|
|
|
$geratExistiert = db_get_var($mysqli, "SELECT 1 FROM $tableGeraete WHERE id = ? LIMIT 1", [$gereat_id]);
|
|
|
|
if (!$geratExistiert) {
|
|
echo json_encode(['success' => false, 'message' => 'Invalide Geraet-ID']);
|
|
exit;
|
|
}
|
|
|
|
if ($field_type_id < 1) {
|
|
echo json_encode(['success' => false, 'message' => 'Invalide Notentyp-ID']);
|
|
exit;
|
|
}
|
|
|
|
$noteConfig = db_select($mysqli, $tableNotenBezeichnungen, "*", "id = ?", [$field_type_id]);
|
|
|
|
if (count($noteConfig) !== 1) {
|
|
echo json_encode(['success' => false, 'message' => 'Invalide Notentyp-ID']);
|
|
exit;
|
|
}
|
|
|
|
$singleNoteConfig = $noteConfig[0];
|
|
|
|
if ($singleNoteConfig['max_value'] !== null && $valueNoteUpdate > $singleNoteConfig['max_value']) {
|
|
echo json_encode(['success' => false, 'message' => "Wert zu hoch (max " . $singleNoteConfig['max_value'].")"]);
|
|
exit;
|
|
}
|
|
|
|
if ($singleNoteConfig['min_value'] !== null && $valueNoteUpdate < $singleNoteConfig['min_value']){
|
|
echo json_encode(['success' => false, 'message' => "Wert zu niedrig (min " . $singleNoteConfig['min_value'].")"]);
|
|
exit;
|
|
}
|
|
|
|
$sql = "INSERT INTO $tableNoten (`value`, `person_id`, `note_bezeichnung_id`, `geraet_id`, `jahr`, `run_number`)
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
ON DUPLICATE KEY UPDATE `value` = VALUES(`value`)";
|
|
|
|
$stmt = $mysqli->prepare($sql);
|
|
|
|
$stmt->bind_param("siiiii", $valueNoteUpdate, $person_id, $field_type_id, $gereat_id, $jahr, $run_number);
|
|
|
|
$stmt->execute();
|
|
|
|
$stmt->close();
|
|
|
|
if ($singleNoteConfig['berechnung_json'] === null) {
|
|
echo json_encode(['success' => true, 'message' => "Wert aktualisiert"]);
|
|
exit;
|
|
}
|
|
|
|
require $baseDir . "/../scripts/string-calculator/string-calculator-functions.php";
|
|
|
|
$updateNoten = [];
|
|
|
|
$notenRechner = new NotenRechner();
|
|
|
|
try {
|
|
$abhaenigeRechnungen = json_decode($singleNoteConfig['berechnung_json']);
|
|
} catch (Exception $e) {
|
|
echo json_encode(['success' => true, 'message' => "Wert aktualisiert, fehler bei der Berechnung der weiteren Werte"]);
|
|
exit;
|
|
}
|
|
|
|
$geraete = db_select($mysqli, $tableGeraete, "id");
|
|
|
|
$programmName = db_get_var($mysqli, "SELECT `programm` FROM $tableTurnerinnen WHERE `id` = ?", [$person_id]);
|
|
|
|
$programmId = db_get_var($mysqli, "SELECT `id` FROM $tableProgramme WHERE `programm` = ?", [$programmName]);
|
|
|
|
// Alle Werte werden von der Datenbank geholt und werden, wenn nicht vorhanden, durch den Standartwert ersetzt.
|
|
|
|
$alleNoten = db_select($mysqli, $tableNotenBezeichnungen, "id, berechnung, default_value, nullstellen, pro_geraet, geraete_json, anzahl_laeufe_json");
|
|
|
|
$noten = db_select($mysqli, $tableNoten, "`value`, `note_bezeichnung_id`, `geraet_id`, `run_number`", "`person_id` = ? AND `jahr` = ?", [$person_id, $jahr]);
|
|
|
|
$ascArrayDefaultValues = array_column($alleNoten, 'default_value', 'id');
|
|
$ascArrayProGeraet = array_column($alleNoten, 'pro_geraet', 'id');
|
|
$ascArrayGeraeteJSON = array_column($alleNoten, 'geraete_json', 'id');
|
|
$ascArrayAnzahlLaeufeJSON = array_column($alleNoten, 'anzahl_laeufe_json', 'id');
|
|
$ascArrayRechnungen = array_column($alleNoten, 'berechnung', 'id');
|
|
|
|
// $proGeraet = intval($calc['pro_geraet']) !== 1;
|
|
|
|
$mRunFunctions = [];
|
|
|
|
foreach ($abhaenigeRechnungen as $saRechnung) {
|
|
$sRechnung = $ascArrayRechnungen[$saRechnung[0]] ?? 0;
|
|
//var_dump($sRechnung);
|
|
$mRunCalc = $notenRechner->checkRunFunctions($sRechnung) ?? false;
|
|
|
|
if ($mRunCalc) {
|
|
$mRunFunctions[] = $saRechnung[0];
|
|
break;
|
|
}
|
|
}
|
|
|
|
$indexedNotenArray = [];
|
|
|
|
foreach ($ascArrayGeraeteJSON as $key => $saagj) {
|
|
$ascArrayGeraeteJSON[$key] = json_decode($saagj, true);
|
|
}
|
|
|
|
foreach ($ascArrayAnzahlLaeufeJSON as $key => $saalj) {
|
|
$ascArrayAnzahlLaeufeJSON[$key] = json_decode($saalj, true) ?? [];
|
|
}
|
|
|
|
foreach ($geraete as $g) {
|
|
$indexedNotenArray[$g['id']] = [];
|
|
}
|
|
|
|
$indexedNotenArray[0] = [];
|
|
|
|
foreach ($noten as $sn) {
|
|
$indexedNotenArray[$sn['geraet_id']][$sn['note_bezeichnung_id']][$sn['run_number']] = $sn['value'];
|
|
}
|
|
|
|
$alleNotenIds = array_column($alleNoten, 'id') ?? [];
|
|
|
|
if (count($mRunFunctions) > 0) {
|
|
foreach ($indexedNotenArray as $sG => $siNA) {
|
|
|
|
foreach ($alleNotenIds as $neni) { // Use $neni as the ID
|
|
|
|
// 1. Skip if no default value is defined
|
|
if (!isset($ascArrayDefaultValues[$neni])) {
|
|
continue;
|
|
}
|
|
|
|
// 2. Logic Check: Is this note assigned to this device?
|
|
$isProGeraet = (int)($ascArrayProGeraet[$neni] ?? 0);
|
|
|
|
if ($isProGeraet === 1 && (int)$sG === 0) {
|
|
continue;
|
|
}
|
|
|
|
if ($isProGeraet !== 1) {
|
|
$allowedGeraete = $ascArrayGeraeteJSON[$neni] ?? [];
|
|
if (!is_array($allowedGeraete) || !in_array($sG, $allowedGeraete)) {
|
|
continue;
|
|
}
|
|
}
|
|
|
|
$runs = $ascArrayAnzahlLaeufeJSON[$neni][$sG][$programmId] ?? $ascArrayAnzahlLaeufeJSON[$neni]["default"] ?? 1;
|
|
|
|
for ($r = 1; $r <= $runs; $r++) {
|
|
if (isset($indexedNotenArray[$sG][$neni][$r])) {
|
|
continue;
|
|
}
|
|
$indexedNotenArray[$sG][$neni][$r] = $ascArrayDefaultValues[$neni];
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
foreach ($indexedNotenArray as $sG => $siNA) {
|
|
|
|
foreach ($alleNotenIds as $neni) { // Use $neni as the ID
|
|
|
|
// 1. Skip if value already exists for this specific run
|
|
if (isset($indexedNotenArray[$sG][$neni][$run_number])) {
|
|
continue;
|
|
}
|
|
|
|
// 2. Skip if no default value is defined
|
|
if (!isset($ascArrayDefaultValues[$neni])) {
|
|
continue;
|
|
}
|
|
|
|
// 3. Logic Check: Is this note assigned to this device?
|
|
$isProGeraet = (int)($ascArrayProGeraet[$neni] ?? 0);
|
|
|
|
if ($isProGeraet === 1 && (int)$sG === 0) {
|
|
continue;
|
|
}
|
|
|
|
if ($isProGeraet !== 1) {
|
|
$allowedGeraete = $ascArrayGeraeteJSON[$neni] ?? [];
|
|
if (!is_array($allowedGeraete) || !in_array($sG, $allowedGeraete)) {
|
|
continue;
|
|
}
|
|
}
|
|
|
|
// 4. Assign the default value
|
|
$indexedNotenArray[$sG][$neni][$run_number] = $ascArrayDefaultValues[$neni];
|
|
}
|
|
}
|
|
}
|
|
|
|
// We only want to save the IDs that were actually recalculated
|
|
$idsToSave = [];
|
|
|
|
foreach ($abhaenigeRechnungen as $sRechnung) {
|
|
$targetNoteId = $sRechnung[0];
|
|
$rechnungType = $sRechnung[1];
|
|
|
|
// 1. Initial Filter
|
|
if ($rechnungType !== "A" && intval($rechnungType) !== $gereat_id) continue;
|
|
|
|
$rechnung = $ascArrayRechnungen[$targetNoteId] ?? null;
|
|
if ($rechnung === null) {
|
|
echo json_encode(['success' => true, 'message' => "Fehler: Rechnung $targetNoteId nicht gefunden"]);
|
|
exit;
|
|
}
|
|
|
|
// 2. Determine Target Device ID
|
|
$isProGeraet = (intval($ascArrayProGeraet[$targetNoteId] ?? 0) === 1);
|
|
$allowedGeraete = $ascArrayGeraeteJSON[$targetNoteId] ?? [];
|
|
|
|
if ($rechnungType === "A" || $isProGeraet || in_array($gereat_id, $allowedGeraete)) {
|
|
$targetGeraetKey = $gereat_id;
|
|
} else {
|
|
$targetGeraetKey = 0;
|
|
}
|
|
|
|
// 3. Calculation Logic
|
|
$runsConfig = $ascArrayAnzahlLaeufeJSON[$targetNoteId] ?? [];
|
|
$runs = $runsConfig[$gereat_id][$programmId] ?? $runsConfig["default"] ?? 1;
|
|
|
|
$acrun = min($runs, $run_number);
|
|
|
|
if (in_array($targetNoteId, $mRunFunctions)) {
|
|
$calcResult = $notenRechner->berechneStringComplexRun($rechnung, $indexedNotenArray, $gereat_id, $programmId, $ascArrayAnzahlLaeufeJSON);
|
|
} else {
|
|
$calcResult = $notenRechner->berechneStringComplex($rechnung, $indexedNotenArray, $gereat_id, $acrun);
|
|
}
|
|
|
|
if (!($calcResult['success'] ?? false)) {
|
|
echo json_encode(['success' => true, 'message' => "Rechenfehler in $targetNoteId: " . ($calcResult['value'] ?? '')]);
|
|
exit;
|
|
}
|
|
|
|
// 4. Update State
|
|
$val = $calcResult['value'];
|
|
$indexedNotenArray[$targetGeraetKey][$targetNoteId][$acrun] = $val;
|
|
$updatedValues[$targetGeraetKey][$targetNoteId][$acrun] = $val;
|
|
}
|
|
|
|
// Prepare the statement once
|
|
$sql = "INSERT INTO $tableNoten (`value`, `person_id`, `note_bezeichnung_id`, `geraet_id`, `jahr`, `run_number`)
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
ON DUPLICATE KEY UPDATE `value` = VALUES(`value`)";
|
|
|
|
$stmt = $mysqli->prepare($sql);
|
|
|
|
$indexedNullstellen = array_column($alleNoten, 'nullstellen', 'id');
|
|
|
|
$formatedNoten = [];
|
|
|
|
foreach ($updatedValues as $gereat => $notenArray) {
|
|
foreach ($notenArray as $note => $runArray) {
|
|
foreach ($runArray as $run => $value) {
|
|
$stmt->bind_param("siiiii", $value, $person_id, $note, $gereat, $jahr, $run);
|
|
$stmt->execute();
|
|
$formatedNoten[$gereat][$note][$run] = number_format($value ,$indexedNullstellen[$note] ?? 2);
|
|
}
|
|
}
|
|
}
|
|
|
|
$formatedNoten[$gereat_id][$field_type_id][$run_number] = number_format($valueNoteUpdate ,$indexedNullstellen[$field_type_id] ?? 2);
|
|
|
|
$stmt->close();
|
|
$mysqli->close();
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => "Wert aktualisiert, alle Berechnungen durchgeführt",
|
|
"noten" => $formatedNoten
|
|
]); |