feat(assessment-form): create store function

This commit is contained in:
arieeefajar 2025-02-06 12:03:18 +07:00
parent 19328a7ff6
commit e3ffb2efcd
50 changed files with 277 additions and 51 deletions

View File

@ -2,12 +2,137 @@
namespace App\Http\Controllers;
use App\Models\EvaluationDetail;
use App\Models\Evalutaion;
use App\Models\Land;
use App\Models\Rule;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class AssesmentFormController extends Controller
{
public function index()
{
return view('assesment-form.index');
$lands = Land::select('id', 'name')->orderBy('created_at', 'desc')->get();
return view('assesment-form.index', compact('lands'));
}
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
];
// Ambil rule untuk setiap parameter
$rules = Rule::whereIn('indicator_id', array_keys($parameters))->get()->keyBy('indicator_id');
// 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;
} else {
toast("Rule untuk indikator ID {$indicatorId} tidak ditemukan.", 'error')->position('top')->autoclose(3000);
return redirect()->back();
}
}
// Simpan ke database dalam satu transaksi
DB::beginTransaction();
try {
// Simpan data evaluasi utama
$evaluation = new Evalutaion;
$evaluation->land_id = $request->land;
$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
]);
}
$result = "OKOKO";
DB::commit();
toast('Evaluasi berhasil disimpan!', 'success')->position('top')->autoclose(3000);
return redirect()->back()->with("result", $result);
} catch (\Throwable $th) {
DB::rollBack();
toast($th->getMessage(), 'error')->position('top')->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);
// }
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class EvaluationDetail extends Model
{
use HasFactory;
protected $table = 'evaluation_details';
protected $primaryKey = 'id';
protected $guarded = [];
// protected $fillable = [
// 'evaluation_id',
// 'indicator_id',
// 'value',
// 'cf',
// ];
public function evaluation()
{
return $this->belongsTo(Evalutaion::class);
}
}

18
app/Models/Evalutaion.php Normal file
View File

@ -0,0 +1,18 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Evalutaion extends Model
{
use HasFactory;
protected $table = 'evaluations';
public function land()
{
return $this->belongsTo(Land::class);
}
}

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('evaluations', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('land_id');
$table->timestamps();
});
Schema::table('evaluations', function (Blueprint $table) {
$table->foreign('land_id')->references('id')->on('land')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('evaluations');
}
};

View File

@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('evaluation_details', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('evaluation_id');
$table->unsignedBigInteger('indicator_id');
$table->float('value');
$table->float('cf');
$table->timestamps();
});
Schema::table('evaluation_details', function (Blueprint $table) {
$table->foreign('evaluation_id')->references('id')->on('evaluations')->onDelete('cascade');
$table->foreign('indicator_id')->references('id')->on('indicators')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('evaluation_details');
}
};

View File

@ -18,3 +18,6 @@ document.addEventListener("DOMContentLoaded", function () {
});
});
});
var landField = document.getElementById("lahan-field");
var landVal = new Choices(landField);

View File

@ -28,14 +28,30 @@
<div class="card-header">
<h4 class="card-title mb-0">Form Penilaian</h4>
</div>
<form action="" class="needs-validation" method="POST" novalidate id="add-form">
<form action="{{ route('assesment_form.store') }}" class="needs-validation" method="POST"
novalidate id="add-form">
@csrf
<div class="card-body">
<div class="mb-3">
<label for="" class="form-label">Lahan</label>
<select name="land" class="form-control" id="lahan-field" required>
<option value="" selected disabled>Pilih Lahan</option>
@foreach ($lands as $land)
<option value="{{ $land->id }}">{{ $land->name }}</option>
@endforeach
</select>
<div class="invalid-feedback">
Pilih Lahan
</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="pH" id="pH-field"
<input type="text" class="form-control" name="1" id="pH-field"
placeholder="Masukan nilai pH Tanah" aria-label="Masukan nilai pH Tanah"
required autofocus>
required>
<span class="input-group-text" id="pH-addon">pH</span>
<div class="invalid-feedback">
Masukan nilai pH
@ -45,7 +61,7 @@
<div class="mb-3">
<label for="" class="form-label">Ketinggian Tempat</label>
<div class="input-group">
<input type="text" class="form-control" name="ketinggian_tempat"
<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>
@ -57,7 +73,7 @@
<div class="mb-3">
<label for="" class="form-label">Ketersediaan Air</label>
<div class="input-group">
<input type="text" class="form-control" name="ketersediaan_air"
<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>
@ -69,7 +85,7 @@
<div class="mb-3">
<label for="" class="form-label">Curah Hujan</label>
<div class="input-group">
<input type="text" class="form-control" name="curah_hujan" id="curah_hujan-field"
<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>
@ -80,7 +96,7 @@
</div>
</div>
<div class="card-footer d-flex justify-content-end">
<button type="submit" class="btn btn-success">Simpan</button>
<button type="submit" class="btn btn-success">Hitung</button>
</div>
</form>
</div>
@ -91,7 +107,17 @@
<h4 class="card-title mb-0">Hasil Penilaian</h4>
</div>
<form action="">
<div class="card-body"></div>
<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>%
</h1>
@endisset
</div>
</form>
</div>
</div>

View File

@ -98,6 +98,7 @@
Route::prefix('form-penilaian')->controller(AssesmentFormController::class)->name('assesment_form.')->group(function () {
Route::get('/', 'index')->name('index');
Route::post('/', 'store')->name('store');
});
});
});

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long