Files
alec.turner 4a89356390 Import latest app updates from streamline
An updated set of source files and initialization was provided by streamline to
address issues observed during initial testing. These files have been updated in
order to generate a new set of app images.
2024-04-11 11:16:51 -07:00

255 lines
11 KiB
PHP

<?php
namespace Modules\Patients\Http\Controllers;
use Illuminate\Http\Request;
use Streamline\Models\TriageNutrition;
class NutritionController extends Controller {
public function get_nutrition_status(Request $request): string {
$age_diff_months = round($request->age_diff_months);
$weight = $request->weight;
$height = $request->height * 100;
$bmi = $request->bmi;
$gender = $request->gender;
$patient_id = $request->patient_id;
$episode_id = $request->episode_id;
$muac = $request->muac;
$oedema = $request->oedema;
$reference_age = NULL;
$reference_bmi = NULL;
$reference_height = NULL;
$reference_weight = NULL;
$texts_array = ["Normal" => 1, "Risk of overweight" => 2, "MAM" => 3,
"SAM without oedema" => 4, "SAM with oedema" => 5];
if (!is_null($request->weight) && !is_null($request->height) && !is_null($request->bmi)) {
if ($age_diff_months > 5 && $age_diff_months < 60) {
if ($gender == 2){
$data = file(base_path('public/uploads/nutrition_csv_lookup/female_6_60.csv'));
} else {
$data = file(base_path('public/uploads/nutrition_csv_lookup/male_6_60.csv'));
}
$formatted_data = [];
$lengths = [];
foreach($data as $item) {
$split_item = explode(',', $item);
$lengths[] = intval($split_item[0]);
$formatted_data[intval($split_item[0])] = [intval($split_item[1]), intval($split_item[2]), intval($split_item[3]), intval($split_item[4]), intval(str_replace(["\r", "\n"], "", $split_item[5]))];
}
// check if height is out of bounds
if ($height < $lengths[0] || $height > $lengths[count($lengths) - 1]) {
$text = "Nutrition Status Not Available";
$reason = "Height is out of range";
$text_color = "red";
return $text . "&&&&" . $reason . "&&&&" . $text_color;
}
$reference_height = get_closest_element_in_array($lengths, $height);
$reference_weight = get_closest_element_in_array($formatted_data[$reference_height], $weight);
$weight_key = array_search($reference_weight, $formatted_data[$reference_height]);
if ($weight_key == 0) {
$text = "SAM without oedema";
$reason = "Weight for height < -3 SD";
$text_color = "red";
} elseif ($weight_key == 1) {
$text = "MAM";
$reason = "Weight for height between -3 SD and -2 SD";
$text_color = "darkorange";
} elseif ($weight_key == 2 || $weight_key == 3) {
$text = "Normal";
$reason = "Weight for height between -2 SD and 2 SD";
$text_color = "black";
} else {
$text = "Risk of overweight";
$reason = "Weight for height > 2 SD";
$text_color = "black";
}
} elseif ($age_diff_months > 59 && $age_diff_months < 228) {
if ($age_diff_months < 120) {
if ($gender == 2){
$data = file(base_path('public/uploads/nutrition_csv_lookup/female_60_120.csv'));
} else {
$data = file(base_path('public/uploads/nutrition_csv_lookup/male_60_120.csv'));
}
} else {
if ($gender == 2){
$data = file(base_path('public/uploads/nutrition_csv_lookup/female_120_228.csv'));
} else {
$data = file(base_path('public/uploads/nutrition_csv_lookup/male_120_228.csv'));
}
}
$formatted_data = [];
$ages_in_months = [];
foreach($data as $item) {
$split_item = explode(',', $item);
$ages_in_months[] = intval($split_item[0]);
$formatted_data[intval($split_item[0])] = [intval($split_item[1]), intval($split_item[2]), intval($split_item[3]), intval($split_item[4]), intval(str_replace(["\r", "\n"], "", $split_item[5]))];
}
$reference_age = get_closest_element_in_array($ages_in_months, $age_diff_months);
$reference_bmi = get_closest_element_in_array($formatted_data[$reference_age], $bmi);
$bmi_key = array_search($reference_bmi, $formatted_data[$reference_age]);
if ($bmi_key == 0) {
$text = "SAM without oedema";
$reason = "BMI < -3 SD";
$text_color = "red";
} elseif ($bmi_key == 1) {
$text = "MAM";
$reason = "BMI between -3 SD and -2 SD";
$text_color = "darkorange";
} elseif ($bmi_key == 2 || $bmi_key == 3) {
$text = "Normal";
$reason = "BMI between -2 SD and 1 SD";
$text_color = "black";
} else {
$text = "Risk of overweight";
$reason = "BMI > 1 SD";
$text_color = "black";
}
} elseif ($age_diff_months > 227 && $age_diff_months < 1200) {
if ($bmi < 16) {
$text = "SAM without oedema";
$reason = "BMI < 16";
$text_color = "red";
} elseif ($bmi > 15.9 && $bmi < 17) {
$text = "MAM";
$reason = "BMI between 16 and 17";
$text_color = "darkorange";
} elseif ($bmi > 16.9 && $bmi < 25) {
$text = "Normal";
$reason = "BMI between 17 and 25";
$text_color = "black";
} else {
$text = "Risk of overweight";
$reason = "BMI ≥ 25";
$text_color = "black";
}
} else {
return "0";
}
} else {
$text = "Normal";
$reason = "Normal";
$text_color = 'black';
}
// check for oedema status
if ($oedema == 1) {
$oedema = "Yes";
$oedema_text = "SAM with oedema";
$oedema_reason = "Oedema of both feet";
$oedema_text_color = "red";
} else {
$oedema = "No";
$oedema_text = "Normal";
$oedema_reason = "";
$oedema_text_color = "black";
}
// check if oedema has a higher category
if (($texts_array[$oedema_text] > $texts_array[$text])) {
$text = $oedema_text;
$reason = $oedema_reason;
$text_color = $oedema_text_color;
}
if ($age_diff_months < 1.59 && $muac < 11.0) {
$muac_text = "SAM without oedema";
$muac_reason = "MUAC below 11.0";
$muac_text_color = 'red';
} else if(between($age_diff_months, 1.6, 5) && $muac < 11.5) {
$muac_text = "SAM without oedema";
$muac_reason = "MUAC below 11.5";
$muac_text_color = 'red';
} else if(between($age_diff_months, 6, 59) && between($muac, 11.5, 12.4)) {
$muac_text = "MAM";
$muac_reason = "MUAC between 11.5 and 12.4";
$muac_text_color = 'darkorange';
} else if(between($age_diff_months, 6, 59) && $muac < 11.5) {
$muac_text = "SAM without oedema";
$muac_reason = "MUAC below 11.5";
$muac_text_color = 'red';
} else if(between($age_diff_months, 60, 119) && between($muac, 13.5, 14.4)) {
$muac_text = "MAM";
$muac_reason = "MUAC between 13.5 and 14.4";
$muac_text_color = 'darkorange';
} else if(between($age_diff_months, 60, 119) && $muac < 13.5) {
$muac_text = "SAM without oedema";
$muac_reason = "MUAC below 13.5";
$muac_text_color = 'red';
} else if(between($age_diff_months, 120, 179) && between($muac, 16.0, 18.4)) {
$muac_text = "MAM";
$muac_reason = "MUAC between 16.0 and 18.4";
$muac_text_color = 'darkorange';
} else if(between($age_diff_months, 120, 179) && $muac < 16.0) {
$muac_text = "SAM without oedema";
$muac_reason = "MUAC below 16.0";
$muac_text_color = 'red';
} else if(between($age_diff_months, 180, 215) && between($muac, 18.5, 20.9)) {
$muac_text = "MAM";
$muac_reason = "MUAC between 18.5 and 21.0";
$muac_text_color = 'darkorange';
} else if(between($age_diff_months, 180, 215) && $muac < 18.5) {
$muac_text = "SAM without oedema";
$muac_reason = "MUAC below 18.5";
$muac_text_color = 'red';
} else if($age_diff_months > 215 && between($muac, 19.0, 21.9)) {
$muac_text = "MAM";
$muac_reason = "MUAC between 19.0 and 21.9";
$muac_text_color = 'darkorange';
} else if($age_diff_months > 215 && $muac < 19.0) {
$muac_text = "SAM without oedema";
$muac_reason = "MUAC below 19.0";
$muac_text_color = 'red';
} else {
$muac_text = "Normal";
$muac_reason = "Normal";
$muac_text_color = 'black';
}
if (($texts_array[$muac_text] > $texts_array[$text])) {
$text = $muac_text;
$reason = $muac_reason;
$text_color = $muac_text_color;
}
// save data to the nutrition table
$nutrition = TriageNutrition::where(['patient_id' => $patient_id, 'episode_id' => $episode_id])->first();
if (!$nutrition) {
$nutrition = new TriageNutrition();
}
$nutrition->patient_id = $patient_id;
$nutrition->episode_id = $episode_id;
$nutrition->age_diff_months = $age_diff_months;
$nutrition->weight = $weight;
$nutrition->height = $height;
$nutrition->bmi = $bmi;
$nutrition->muac = $muac;
$nutrition->oedema = $oedema;
$nutrition->gender = $gender;
$nutrition->reference_height = $reference_height;
$nutrition->reference_weight = $reference_weight;
$nutrition->text = $text;
$nutrition->reason = $reason;
$nutrition->reference_age = $reference_age;
$nutrition->reference_bmi = $reference_bmi;
$nutrition->updated_at = now();
$nutrition->save();
return $text . "&&&&" . $reason . "&&&&" . $text_color;
}
}