MIF_E31221322/app/Http/Controllers/AssesmentFormController.php

130 lines
4.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Evaluation;
use App\Models\EvaluationDetail;
use App\Models\Indicator;
use App\Models\Land;
use App\Models\Rule;
use Dotenv\Parser\Value;
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', 'owner')->whereNotIn('id', function ($query) {
$query->select('land_id')->from('evaluation');
})->orderBy('created_at', 'desc')->get();
$indicators = Indicator::select('id', 'name')->whereHas('rules')->with('rules')->get();
return view('assessment.form', compact('lands', 'indicators'));
}
public function calculateCFc($cf1, $cf2)
{
if ($cf1 >= 0 && $cf2 >= 0) {
return $cf1 + $cf2 * (1 - $cf1);
} elseif ($cf1 < 0 && $cf2 < 0) {
return $cf1 + $cf2 * (1 + $cf1);
} else {
return ($cf1 + $cf2) / (1 - min(abs($cf1), abs($cf2)));
}
}
public function store(Request $request)
{
$customMessages = [
'land.required' => 'Lahan tidak boleh kosong',
'land.exists' => 'Lahan tidak ditemukan',
];
$validator = Validator::make($request->all(), [
'land' => ['required', 'exists:land,id'],
], $customMessages);
if ($validator->fails()) {
toast($validator->messages()->all()[0], 'error')->position('top-right')->autoclose(3000);
return redirect()->back()->withInput();
}
$cf_e = $request->except('_token', 'land');
if (count($cf_e) == 0) {
toast('Harap pilih jawaban untuk nilai indikator', 'error')->position('top-right')->autoclose(3000);
return redirect()->back();
}
$cf_h = collect(Rule::whereIn('indicator_id', array_keys($cf_e))
->orderByDesc('cf')
->get()
->unique('indicator_id')
->pluck('cf', 'indicator_id')
->sortKeys());
$cf_he = collect($cf_e)->map(function ($value, $key) use ($cf_h) {
return [
"cf(h,e)" . $key => (float) $value * (float) ($cf_h[$key] ?? 1)
];
})->collapse();
$cf_he_array = $cf_he->toArray();
$cf_combined = array_shift($cf_he_array);
foreach ($cf_he_array as $cf) {
$cf_combined = $this->calculateCFc($cf_combined, $cf);
}
$cf_pakar = Rule::pluck('cf')->toArray();
$mean_cf = collect($cf_pakar)->avg();
$std_dev_cf = sqrt(collect($cf_pakar)->map(function ($cf) use ($mean_cf) {
return pow($cf - $mean_cf, 2);
})->avg());
$threshold_cocok = $mean_cf + $std_dev_cf;
$threshold_cocok_bersyarat = $mean_cf - $std_dev_cf;
if ($cf_combined > $threshold_cocok) {
$hasil = 'cocok';
} elseif ($cf_combined > $threshold_cocok_bersyarat) {
$hasil = 'cocok bersyarat';
} else {
$hasil = 'tidak cocok';
}
// dd($cf_e, $cf_h, $cf_he, $cf_combined, $hasil);
$landName = Land::find($request->land)->owner;
DB::beginTransaction();
try {
$evaluation = new Evaluation;
$evaluation->land_id = $request->land;
$evaluation->threshold_value = $hasil;
$evaluation->cf_value = $cf_combined;
$evaluation->user_id = auth()->user()->id;
$evaluation->save();
foreach ($cf_e as $id => $value) {
$evaluationDetail = new EvaluationDetail;
$evaluationDetail->evaluation_id = $evaluation->id;
$evaluationDetail->indicator_id = $id;
$evaluationDetail->cf_e = $value;
$evaluationDetail->save();
}
DB::commit();
$presentase = round($cf_combined * 100, 2);
toast("Data berhasil disimpan.", "success")->position('top-right')->autoclose(3000);
return redirect()->back()->with(['land' => $landName, 'presentase' => $presentase, 'hasil' => $hasil]);
} catch (\Throwable $th) {
DB::rollBack();
toast($th->getMessage(), 'error')->position('top-right')->autoclose(3000);
return redirect()->back();
}
}
}