MIF_E31211891/app/Http/Controllers/PerbandinganController.php

172 lines
5.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Kriteria;
use App\Models\Nilai;
use App\Models\Perbandingan;
use Illuminate\Http\Request;
class PerbandinganController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$no = 1;
$kriteria = Kriteria::where('status', 'Aktif')->get();
$id_kriteria = Kriteria::where('status', 'Aktif')->select('id')->get();
$count = Kriteria::count();
if (Perbandingan::get()->count() != pow($count, 2)) {
Perbandingan::truncate();
foreach ($kriteria as $k1) {
foreach ($kriteria as $k2) {
Perbandingan::create([
'id_kriteria_1' => $k1->id,
'id_kriteria_2' => $k2->id,
'nilai' => 1
]);
}
}
}
$perbandingan = Perbandingan::get();
$nilai = Nilai::get();
if ($nilai->count() > 0) {
$arr_idk = array();
foreach ($id_kriteria as $idk) {
array_push($arr_idk, $idk->id);
}
$total = array_fill(0, $count, 0);
for ($i = 0; $i < $count; $i++) {
$temp = 0;
for ($j = 0; $j < $count; $j++) {
$p = Perbandingan::where('id_kriteria_1', '=', $arr_idk[$j])->where('id_kriteria_2', '=', $arr_idk[$i])->get()->first();
$temp += $p->nilai;
}
$total[$i] = $temp;
}
$tabel_nilai = array_fill(0, $count, array_fill(0, $count, 0));
for ($i = 0; $i < $count; $i++) {
for ($j = 0; $j < $count; $j++) {
$p = Perbandingan::where('id_kriteria_1', '=', $arr_idk[$i])->where('id_kriteria_2', '=', $arr_idk[$j])->get()->first();
$tabel_nilai[$j][$i] = $p->nilai / $total[$j];
}
}
$tabel_nilai_total = array_fill(0, $count, 0);
$pv = array_fill(0, $count, 0);
for ($i = 0; $i < $count; $i++) {
$tt = 0;
for ($j = 0; $j < $count; $j++) {
$tt += $tabel_nilai[$j][$i];
}
$tabel_nilai_total[$i] = $tt;
$pv[$i] = $tt / $count;
}
$lambdaMax = 0;
for($i=0;$i<$count;$i++)
{
$lambdaMax += $total[$i]*$pv[$i];
}
$tab_rand_index = [1 => 0.0, 2 => 0.0, 3 => 0.58, 4 => 0.90, 5 => 1.12, 6 => 1.24, 7 => 1.32, 8 => 1.41, 9 => 1.45, 10 => 1.49];
$ci = ($lambdaMax - $count) / ($count - 1);
$cr = $ci / $tab_rand_index[$count];
return view('perbandingan', compact('no', 'kriteria', 'perbandingan', 'id_kriteria', 'nilai', 'lambdaMax', 'ci', 'cr'));
}
return view('perbandingan', compact('no', 'kriteria', 'perbandingan', 'id_kriteria'));
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$kriteria = Kriteria::where('status', 'Aktif')->get();
$id_kriteria = Kriteria::where('status', 'Aktif')->select('id')->get();
$arr_idk = array();
foreach ($id_kriteria as $idk) {
array_push($arr_idk, $idk->id);
}
$count = Kriteria::count();
$total = array_fill(0, $count, 0);
for ($i = 0; $i < $count; $i++) {
$temp = 0;
for ($j = 0; $j < $count; $j++) {
$p = Perbandingan::where('id_kriteria_1', '=', $arr_idk[$i])->where('id_kriteria_2', '=', $arr_idk[$j])->get()->first();
$p->nilai = $request[$arr_idk[$i] . ":" . $arr_idk[$j]];
$temp += $request[$arr_idk[$j] . ":" . $arr_idk[$i]];
$p->save();
}
$total[$i] = $temp;
}
$tabel_nilai = array_fill(0, $count, array_fill(0, $count, 0));
for ($i = 0; $i < $count; $i++) {
for ($j = 0; $j < $count; $j++) {
$tabel_nilai[$j][$i] = $request[$arr_idk[$i] . ":" . $arr_idk[$j]] / $total[$j];
}
}
$tabel_nilai_total = array_fill(0, $count, 0);
$pv = array_fill(0, $count, 0);
for ($i = 0; $i < $count; $i++) {
$tt = 0;
for ($j = 0; $j < $count; $j++) {
$tt += $tabel_nilai[$j][$i];
}
$tabel_nilai_total[$i] = $tt;
$pv[$i] = $tt / $count;
}
foreach ($kriteria as $index => $k) {
$nilai = nilai::where('id_kriteria', '=', $k->id)->first();
if ($nilai == null) {
nilai::create([
'id_kriteria' => $k->id,
'nilai' => $pv[$index],
]);
} else {
$nilai->nilai = $pv[$index];
$nilai->save();
}
}
return redirect('perbandingan');
}
/**
* Display the specified resource.
*/
public function show($id)
{
$nilai = Nilai::where('id_kriteria', $id)->first();
return $nilai->nilai;
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}