From bc49cf38c427e938cf56679d3a14bc47810b740c Mon Sep 17 00:00:00 2001 From: arieeefajar Date: Fri, 7 Feb 2025 22:33:03 +0700 Subject: [PATCH] fix(assessment-form): fix store function --- .../Controllers/AssesmentFormController.php | 152 ++++++------------ app/Models/{Evalutaion.php => Evaluation.php} | 2 +- ...5_02_06_085110_create_evaluation_table.php | 3 +- .../js/pages/customJs/assesment-form/index.js | 21 --- .../views/assesment-form/index.blade.php | 77 +++------ 5 files changed, 78 insertions(+), 177 deletions(-) rename app/Models/{Evalutaion.php => Evaluation.php} (90%) diff --git a/app/Http/Controllers/AssesmentFormController.php b/app/Http/Controllers/AssesmentFormController.php index e992e52..d8362c3 100644 --- a/app/Http/Controllers/AssesmentFormController.php +++ b/app/Http/Controllers/AssesmentFormController.php @@ -2,137 +2,91 @@ namespace App\Http\Controllers; +use App\Models\Evaluation; use App\Models\EvaluationDetail; -use App\Models\Evalutaion; +use App\Models\Indicator; use App\Models\Land; use App\Models\Rule; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Validator; class AssesmentFormController extends Controller { public function index() { - $lands = Land::select('id', 'name')->orderBy('created_at', 'desc')->get(); - return view('assesment-form.index', compact('lands')); + $lands = Land::select('id', 'name')->whereNotIn('id', function ($query) { + $query->select('land_id')->from('evaluation'); + })->orderBy('created_at', 'desc')->get(); + $indicators = Indicator::select('id', 'name')->whereIn('id', function ($query) { + $query->select('indicator_id')->from('rule'); + })->get(); + return view('assesment-form.index', compact('lands', 'indicators')); } public function store(Request $request) { - // Ambil semua nilai input - $parameters = [ - 1 => $request->input('1'), // pH - 2 => $request->input('2'), // Ketinggian Tempat - 3 => $request->input('3'), // Ketersediaan Air - 4 => $request->input('4') // Curah Hujan + $customMessages = [ + 'land.required' => 'Lahan tidak boleh kosong', + 'land.exists' => 'Lahan tidak ditemukan', ]; - // Ambil rule untuk setiap parameter - $rules = Rule::whereIn('indicator_id', array_keys($parameters))->get()->keyBy('indicator_id'); + $validator = Validator::make($request->all(), [ + 'land' => ['required', 'exists:land,id'], + ], $customMessages); - // Hitung Certainty Factor (CF) untuk setiap parameter - $cfValues = []; - foreach ($parameters as $indicatorId => $value) { - if (isset($rules[$indicatorId])) { - $cfValues[$indicatorId] = $rules[$indicatorId]->mb - $rules[$indicatorId]->md; + if ($validator->fails()) { + toast($validator->messages()->all()[0], 'error')->position('top-right')->autoclose(3000); + return redirect()->back()->withInput(); + } + + $indicators = $request->except('_token', 'land'); + if (count($indicators) == 0) { + toast('Harap pilih jawaban untuk nilai indikator', 'error')->position('top-right')->autoclose(3000); + return redirect()->back(); + } + + $rules = Rule::whereIn('indicator_id', array_keys($indicators))->get()->keyBy('indicator_id'); + + $cfValue = []; + foreach ($indicators as $id => $value) { + if (isset($rules[$id])) { + $rule = $rules[$id]; + $cfValue[$id] = $rule->mb * $value; } else { - toast("Rule untuk indikator ID {$indicatorId} tidak ditemukan.", 'error')->position('top')->autoclose(3000); + toast("Rule untuk indikator ID {$id} tidak ditemukan.", 'error')->position('top-right')->autoclose(3000); return redirect()->back(); } } - // Simpan ke database dalam satu transaksi + $cfCombine = array_shift($cfValue); + + foreach ($cfValue as $cf) { + $cfCombine = $cfCombine + $cf * (1 - $cfCombine); + } + DB::beginTransaction(); try { - // Simpan data evaluasi utama - $evaluation = new Evalutaion; + $evaluation = new Evaluation; $evaluation->land_id = $request->land; + $evaluation->cf_value = $cfCombine; $evaluation->save(); - // Simpan semua detail evaluasi - foreach ($parameters as $indicatorId => $value) { - EvaluationDetail::create([ - 'evaluation_id' => $evaluation->id, - 'indicator_id' => $indicatorId, - 'value' => $value, - 'cf' => $cfValues[$indicatorId] ?? 0 - ]); + foreach ($indicators as $id => $value) { + $evaluationDetail = new EvaluationDetail(); + $evaluationDetail->evaluation_id = $evaluation->id; + $evaluationDetail->indicator_id = $id; + $evaluationDetail->md_value = $value; + $evaluationDetail->save(); } - - $result = "OKOKO"; - DB::commit(); - toast('Evaluasi berhasil disimpan!', 'success')->position('top')->autoclose(3000); - return redirect()->back()->with("result", $result); + $presentase = round($cfCombine * 100, 2); + toast('Data berhasil disimpan', 'success')->position('top-right')->autoclose(3000); + return redirect()->back()->with('presentase', $presentase); } catch (\Throwable $th) { DB::rollBack(); - toast($th->getMessage(), 'error')->position('top')->autoclose(3000); + toast('Terjadi kesalahan', 'error')->position('top-right')->autoclose(3000); return redirect()->back(); } } - - - // public function store(Request $request) - // { - // // dd($request->all()); - // $pH = $request->input('1'); - // $ketinggianTempat = $request->input('2'); - // $ketersediaanAir = $request->input('3'); - // $curahHujan = $request->input('4'); - - // $rulepH = Rule::where('indicator_id', 1)->where('range_min', '<=', $pH)->where('range_max', '>=', $pH)->first(); - // $ruleKetinggianTempat = Rule::where('indicator_id', 2)->where('range_min', '<=', $ketinggianTempat)->where('range_max', '>=', $ketinggianTempat)->first(); - // $ruleKetersediaanAir = Rule::where('indicator_id', 3)->where('range_min', '<=', $ketersediaanAir)->where('range_max', '>=', $ketersediaanAir)->first(); - // $ruleCurahHujan = Rule::where('indicator_id', 4)->where('range_min', '<=', $curahHujan)->where('range_max', '>=', $curahHujan)->first(); - - // // dd([ - // // "pH" => $pH, - // // "ketinggian_tempat" => $ketinggianTempat, - // // "ketersediaan_air" => $ketersediaanAir, - // // "curah_hujan" => $curahHujan, - // // ], [ - - // // "rulepH" => $rulepH, - // // "ruleKetinggianTempat" => $ruleKetinggianTempat, - // // "ruleKetersediaanAir" => $ruleKetersediaanAir, - // // "ruleCurahHujan" => $ruleCurahHujan - // // ]); - - // $cfpH = $rulepH->mb - $rulepH->md; - // $cfketinggianTempat = $ruleKetinggianTempat->mb - $ruleKetinggianTempat->md; - // $cfketersediaanAir = $ruleKetersediaanAir->mb - $ruleKetersediaanAir->md; - // $cfcurahHujan = $ruleCurahHujan->mb - $ruleCurahHujan->md; - - // // dd([ - // // "pH" => $pH, - // // "ketinggian_tempat" => $ketinggianTempat, - // // "ketersediaan_air" => $ketersediaanAir, - // // "curah_hujan" => $curahHujan, - // // ], [ - - // // "rulepH" => $rulepH, - // // "ruleKetinggianTempat" => $ruleKetinggianTempat, - // // "ruleKetersediaanAir" => $ruleKetersediaanAir, - // // "ruleCurahHujan" => $ruleCurahHujan - // // ], [ - // // "pH" => $cfpH, - // // "ketinggian_tempat" => $cfketinggianTempat, - // // "ketersediaan_air" => $cfketersediaanAir, - // // "curah_hujan" => $cfcurahHujan - // // ]); - - // // $cfCombine1 = $cfpH + ($cfketinggianTempat * (1 - $cfpH)); - // // $cfCombine2 = $cfCombine1 + ($cfketersediaanAir * (1 - $cfCombine1)); - // // $cfCombine3 = $cfCombine2 + ($cfcurahHujan * (1 - $cfCombine2)); - - // // dd([ - // // "cfCombine(cfpH,cfketinggian)" => $cfCombine1, - // // "cfCombine(cfOld, cfketersediaanAir)" => $cfCombine2, - // // "cfCombine(cfOld, cfCurahHujan)" => $cfCombine3, - // // ]); - - // // $presentaseKeyakinan = $cfCombine3 * 100; - // // dd($presentaseKeyakinan); - // // return view('assesment-form.index')->with('result', $presentaseKeyakinan); - // } } diff --git a/app/Models/Evalutaion.php b/app/Models/Evaluation.php similarity index 90% rename from app/Models/Evalutaion.php rename to app/Models/Evaluation.php index 96852e4..f6ef448 100644 --- a/app/Models/Evalutaion.php +++ b/app/Models/Evaluation.php @@ -5,7 +5,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; -class Evalutaion extends Model +class Evaluation extends Model { use HasFactory; diff --git a/database/migrations/2025_02_06_085110_create_evaluation_table.php b/database/migrations/2025_02_06_085110_create_evaluation_table.php index eb1c94d..4687d64 100644 --- a/database/migrations/2025_02_06_085110_create_evaluation_table.php +++ b/database/migrations/2025_02_06_085110_create_evaluation_table.php @@ -14,8 +14,7 @@ public function up(): void Schema::create('evaluation', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('land_id'); - $table->float('cf_value'); - $table->string('conclusion'); + $table->double('cf_value', 15, 10); $table->timestamps(); }); diff --git a/public/assets/js/pages/customJs/assesment-form/index.js b/public/assets/js/pages/customJs/assesment-form/index.js index 230466e..dd37b1a 100644 --- a/public/assets/js/pages/customJs/assesment-form/index.js +++ b/public/assets/js/pages/customJs/assesment-form/index.js @@ -1,23 +1,2 @@ -document.addEventListener("DOMContentLoaded", function () { - const inputs = document.querySelectorAll( - "#pH-field, #ketinggian_tempat-field", - "#ketersediaan_air-field", - "#curah_hujan-field" - ); - - inputs.forEach((input) => { - input.addEventListener("input", function () { - this.value = this.value.replace(/[^0-9.]/g, ""); - this.value = this.value.replace(/^(\.)/, ""); - if ((this.value.match(/\./g) || []).length > 1) { - this.value = this.value.substring( - 0, - this.value.lastIndexOf(".") - ); - } - }); - }); -}); - var landField = document.getElementById("lahan-field"); var landVal = new Choices(landField); diff --git a/resources/views/assesment-form/index.blade.php b/resources/views/assesment-form/index.blade.php index 189e838..6f91d07 100644 --- a/resources/views/assesment-form/index.blade.php +++ b/resources/views/assesment-form/index.blade.php @@ -46,54 +46,24 @@ -
- -
- - pH + @foreach ($indicators as $indicator) +
+ +
- Masukan nilai pH + Pilih Jawaban
-
-
- -
- - m -
- Masukan nilai Ketinggian Tempat -
-
-
-
- -
- - % -
- Masukan nilai Ketersediaan Air -
-
-
-
- -
- - mm -
- Masukan nilai Curah Hujan -
-
-
+ @endforeach
-
- {{-- @dd(request()) --}} - {{--
-

0

-
--}} - @isset(request()->session->result) -

0% +
+ @if (session('presentase')) +

0%

- @endisset +

Presentase kelayakan tanah berdasarkan perhitungan + Certainty + Factor.

+ @endif