Überarbeitete Version der 1. Version. Es bestehen noch grosse Feher in einzelnen Skripten.
This commit is contained in:
@@ -13,9 +13,14 @@ use ChrisKonnertz\StringCalc\StringCalc;
|
||||
|
||||
class NotenRechner {
|
||||
|
||||
private $Rfunctions = ["MAXR", "MINR", "AVGR", "SUMR"];
|
||||
private $RfunctionsSan = [];
|
||||
|
||||
public function __construct(
|
||||
private StringCalc $rechner = new StringCalc()
|
||||
) {}
|
||||
) {
|
||||
$this->RfunctionsSan = implode('|', array_map('preg_quote', $this->Rfunctions));
|
||||
}
|
||||
|
||||
/*
|
||||
Es wird ein Array mit allen Notentypen pro Gerät, Jahr und Person erwartet, so dass noten_bezeichnung_id als Key verwendet werden kann:
|
||||
@@ -95,7 +100,7 @@ class NotenRechner {
|
||||
}
|
||||
}
|
||||
|
||||
private function insertValuesComplex(string $schemaRaw, array $valuesArray, int $currentId)
|
||||
private function insertValuesComplex(string $schemaRaw, array $valuesArray, int $currentId, int $runNumber)
|
||||
{
|
||||
$schema = $this->santinzeString($schemaRaw);
|
||||
$idsNeededArray = $this->getBenoetigteIdsComplex($schemaRaw);
|
||||
@@ -104,7 +109,6 @@ class NotenRechner {
|
||||
foreach ($idsNeededArray as $sina) {
|
||||
$noteId = $sina['noteId'];
|
||||
$geraetIdSearch = ($sina['geraetId'] === 'A') ? $currentId : $sina['geraetId'];
|
||||
$runNumber = $sina['run'] ?? 1;
|
||||
|
||||
if (!isset($valuesArray[$geraetIdSearch][$noteId][$runNumber])) {
|
||||
return ['success' => false, 'value' => "Fehlende Daten für Gerät $geraetIdSearch, Note $noteId, Lauf $runNumber"];
|
||||
@@ -112,15 +116,13 @@ class NotenRechner {
|
||||
}
|
||||
|
||||
try {
|
||||
$returnStr = preg_replace_callback('/\{(\d+)(?:\[(\w*)\])?(?:\[(\d+)\])?\}/', function($m) use ($valuesArray, $currentId) {
|
||||
$returnStr = preg_replace_callback('/\{(\d+)(?:\[(\w*)\])?(?:\[(\d+)\])?\}/', function($m) use ($valuesArray, $currentId, $runNumber) {
|
||||
$noteId = $m[1];
|
||||
|
||||
// Get value inside brackets, default to 'S' if none exists
|
||||
$rawGeraet = $m[2] ?? 'S';
|
||||
$geraetId = ($rawGeraet === 'S' || $rawGeraet === '') ? $currentId : $rawGeraet;
|
||||
|
||||
$runNumber = isset($m[3]) ? (int)$m[3] : 1;
|
||||
|
||||
// Return value from [Device][Note][Run]
|
||||
return $valuesArray[$geraetId][$noteId][$runNumber] ?? 0;
|
||||
|
||||
@@ -160,16 +162,120 @@ class NotenRechner {
|
||||
return $this->calculate($rechnungArray['value']);
|
||||
}
|
||||
|
||||
public function berechneStringComplex(string $schemaRaw, array $valuesArray = [], int $geraetId = 0) {
|
||||
public function berechneStringComplex(string $schemaRaw, array $valuesArray = [], int $geraetId = 0, int $run_number = 0) {
|
||||
if ($schemaRaw === '') { return ['success' => false, 'value' => 'Leeres Schema']; }
|
||||
if ($geraetId === 0) { return ['success' => false, 'value' => 'Keine Geraet Id angegeben.']; }
|
||||
if ($run_number === 0) { return ['success' => false, 'value' => 'Keine Run_Number angegeben.']; }
|
||||
|
||||
$rechnungArray = $this->insertValuesComplex($schemaRaw, $valuesArray, $geraetId);
|
||||
$rechnungArray = $this->insertValuesComplex($schemaRaw, $valuesArray, $geraetId, $run_number);
|
||||
|
||||
if (!isset($rechnungArray['success']) || !isset($rechnungArray['value']) || !$rechnungArray['success']) {
|
||||
return ['success' => false, 'value' => $rechnungArray['value'] ?? 'Fehler beim Einsetzen der Werte oder Werte fehlen'];
|
||||
return ['success' => false, 'value' => $rechnungArray['value'] . " RECHNUNG: " . $schemaRaw ?? 'Fehler beim Einsetzen der Werte oder Werte fehlen'];
|
||||
}
|
||||
|
||||
return $this->calculate($rechnungArray['value']);
|
||||
}
|
||||
|
||||
public function berechneStringComplexRun(string $schemaRaw, array $valuesArray = [], int $geraetId = 0, int $programm = 0, array $arrayRuns = []) {
|
||||
if ($schemaRaw === '') { return ['success' => false, 'value' => 'Leeres Schema']; }
|
||||
if ($geraetId === 0) { return ['success' => false, 'value' => 'Keine Geraet Id angegeben.']; }
|
||||
if ($programm === 0) { return ['success' => false, 'value' => 'Kein Programm angegeben.']; }
|
||||
if ($arrayRuns === []) { return ['success' => false, 'value' => 'Leeres Array Runs Noten.']; }
|
||||
|
||||
$rechnung = $this->insertRunValues($schemaRaw, $valuesArray, $geraetId, $programm, $arrayRuns);
|
||||
|
||||
if (!isset($rechnung) || $rechnung === '') {
|
||||
return ['success' => false, 'value' => 'Fehler beim Einsetzen der Werte oder Werte fehlen'];
|
||||
}
|
||||
|
||||
return $this->calculate($rechnung);
|
||||
}
|
||||
|
||||
private function insertRunValues($schema, $valuesArray, $currentId, $programm, $arrayRuns) {
|
||||
$pattern = '/(?P<fn_name>' . $this->RfunctionsSan . ')\((?P<content>(?:(?R)|[^()])*)\)/';
|
||||
|
||||
return preg_replace_callback($pattern, function($m) use ($valuesArray, $currentId, $programm, $arrayRuns) {
|
||||
$name = $m['fn_name'];
|
||||
$content = $m['content'];
|
||||
|
||||
// Resolve inner nested functions first
|
||||
$content = $this->insertRunValues($content, $valuesArray, $currentId, $programm, $arrayRuns);
|
||||
|
||||
if (!in_array($name, $this->Rfunctions)) {
|
||||
return $m[0];
|
||||
}
|
||||
|
||||
$parts = [];
|
||||
|
||||
$ids = $this->getBenoetigteIdsComplex($content);
|
||||
|
||||
$runCount = 0;
|
||||
|
||||
foreach ($ids as $sid) {
|
||||
$geraetId = ($sid['geraetId'] === 'A') ? $currentId : $sid['geraetId'];
|
||||
$runCountN = $arrayRuns[$sid['noteId']][$geraetId][$programm] ?? $arrayRuns["default"] ?? 1;
|
||||
|
||||
//var_dump($arrayRuns[$sid['noteId']][$geraetId][$programm]);
|
||||
|
||||
if ($runCount !== $runCountN && $runCount !== 0) { return; }
|
||||
$runCount = $runCountN;
|
||||
}
|
||||
|
||||
$string = "";
|
||||
switch ($name) {
|
||||
case "AVGR":
|
||||
case "SUMR":
|
||||
for ($r = 1; $r <= $runCount; $r++) {
|
||||
|
||||
$res = $this->insertValuesComplex($content, $valuesArray, $currentId, $r);
|
||||
|
||||
// FIXED: Only bail if success is false
|
||||
if (!$res['success']) {
|
||||
return "0"; // Or handle error as needed
|
||||
}
|
||||
$parts[] = "(" . $res['value'] . ")";
|
||||
}
|
||||
|
||||
$innerMath = implode(" + ", $parts);
|
||||
$string = ($name === "AVGR") ? "(($innerMath) / $runCount)" : "($innerMath)";
|
||||
|
||||
//var_dump($string);
|
||||
break;
|
||||
|
||||
case "MAXR":
|
||||
case "MINR":
|
||||
$arrayRunValues = [];
|
||||
for ($r = 1; $r <= $runCount; $r++) {
|
||||
$res = $this->berechneStringComplex($content, $valuesArray, $currentId, $r);
|
||||
|
||||
// FIXED: Access the ['value'] key of the returned array
|
||||
if ($res['success']) {
|
||||
$arrayRunValues[] = floatval($res['value']);
|
||||
} else {
|
||||
$arrayRunValues[] = 0;
|
||||
}
|
||||
}
|
||||
// Safety check for empty arrays to prevent max() errors
|
||||
if (empty($arrayRunValues)) return "0";
|
||||
|
||||
$string = ($name === "MAXR") ? max($arrayRunValues) : min($arrayRunValues);
|
||||
break;
|
||||
}
|
||||
|
||||
return (string)$string;
|
||||
}, $schema);
|
||||
}
|
||||
|
||||
public function checkRunFunctions(string $schemaRaw) {
|
||||
|
||||
if ($schemaRaw === '') {
|
||||
return ['success' => false, 'value' => 'Leeres Schema'];
|
||||
}
|
||||
|
||||
$schema = $this->santinzeString($schemaRaw);
|
||||
|
||||
$pattern = '/(?P<fn_name>' . $this->RfunctionsSan . ')\((?:(?R)|[^()])*+\)/';
|
||||
|
||||
return (bool) preg_match($pattern, $schema);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user