Merge pull request #10 from arieeefajar/fix/assessment-form
fix(assessment-form): fix store function
This commit is contained in:
commit
479e8b97df
|
@ -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);
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
@ -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();
|
||||
});
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -46,54 +46,24 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="" class="form-label">pH Tanah</label>
|
||||
<div class="input-group">
|
||||
<input type="text" class="form-control" name="1" id="pH-field"
|
||||
placeholder="Masukan nilai pH Tanah" aria-label="Masukan nilai pH Tanah"
|
||||
required>
|
||||
<span class="input-group-text" id="pH-addon">pH</span>
|
||||
@foreach ($indicators as $indicator)
|
||||
<div class="mb-3">
|
||||
<label for="" class="form-label">{{ $indicator->name }}</label>
|
||||
<select name="{{ $indicator->id }}" class="form-control"
|
||||
id="{{ $indicator->id }}-field" required>
|
||||
<option value="" selected disabled>Pilih Jawaban</option>
|
||||
<option value="0">Tidak</option>
|
||||
<option value="0.2">Tidak Tahu</option>
|
||||
<option value="0.4">Mungkin</option>
|
||||
<option value="0.6">Kemungkinan Besar</option>
|
||||
<option value="0.8">Hampir Pasti</option>
|
||||
<option value="1">Pasti</option>
|
||||
</select>
|
||||
<div class="invalid-feedback">
|
||||
Masukan nilai pH
|
||||
Pilih Jawaban
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="" class="form-label">Ketinggian Tempat</label>
|
||||
<div class="input-group">
|
||||
<input type="text" class="form-control" name="2"
|
||||
id="ketinggian_tempat-field" placeholder="Masukan nilai Ketinggian Tempat"
|
||||
aria-label="Masukan nilai Ketinggian Tempat" required>
|
||||
<span class="input-group-text" id="ketinggian_tempat-addon">m</span>
|
||||
<div class="invalid-feedback">
|
||||
Masukan nilai Ketinggian Tempat
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="" class="form-label">Ketersediaan Air</label>
|
||||
<div class="input-group">
|
||||
<input type="text" class="form-control" name="3"
|
||||
id="ketersediaan_air-field" placeholder="Masukan nilai Ketersediaan Air"
|
||||
aria-label="Masukan nilai Ketersediaan Air" required>
|
||||
<span class="input-group-text" id="ketersediaan_air-addon">%</span>
|
||||
<div class="invalid-feedback">
|
||||
Masukan nilai Ketersediaan Air
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="" class="form-label">Curah Hujan</label>
|
||||
<div class="input-group">
|
||||
<input type="text" class="form-control" name="4" id="curah_hujan-field"
|
||||
placeholder="Masukan nilai Curah Hujan" aria-label="Masukan nilai Curah Hujan"
|
||||
required>
|
||||
<span class="input-group-text" id="curah_hujan-addon">mm</span>
|
||||
<div class="invalid-feedback">
|
||||
Masukan nilai Curah Hujan
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endforeach
|
||||
</div>
|
||||
<div class="card-footer d-flex justify-content-end">
|
||||
<button type="submit" class="btn btn-success">Hitung</button>
|
||||
|
@ -107,16 +77,15 @@
|
|||
<h4 class="card-title mb-0">Hasil Penilaian</h4>
|
||||
</div>
|
||||
<form action="">
|
||||
<div class="card-body">
|
||||
{{-- @dd(request()) --}}
|
||||
{{-- <div class="flex-grow-1 ms-3">
|
||||
<h2 class="mb-0"><span class="counter-value" data-target="197">0</span></h2>
|
||||
</div> --}}
|
||||
@isset(request()->session->result)
|
||||
<h1 class="mb-0"><span class="counter-value"
|
||||
data-target="{{ request()->session->result }}">0</span>%
|
||||
<div class="card-body text-center">
|
||||
@if (session('presentase'))
|
||||
<h1 class="mb-0 mt-2"><span class="counter-value"
|
||||
data-target="{{ session('presentase') }}">0</span>%
|
||||
</h1>
|
||||
@endisset
|
||||
<p class="text-muted mb-0 mt-2">Presentase kelayakan tanah berdasarkan perhitungan
|
||||
Certainty
|
||||
Factor.</p>
|
||||
@endif
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue