228 lines
7.6 KiB
PHP
228 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\User\Http\Controllers;
|
|
|
|
use Illuminate\Contracts\Support\Renderable;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
use App\Models\Gejala;
|
|
use App\Models\Penyakit;
|
|
use App\Models\Riwayat;
|
|
use App\Models\Rule;
|
|
use App\Models\User;
|
|
use App\Models\Kondisi;
|
|
|
|
use DataTables;
|
|
use GuzzleHttp\Client;
|
|
use Carbon\Carbon;
|
|
|
|
class UserController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$data = [
|
|
'subtitle' => 'Diagnosa',
|
|
];
|
|
|
|
$gejala = Gejala::all();
|
|
return view('user::index', compact('data', 'gejala'));
|
|
}
|
|
|
|
public function profile()
|
|
{
|
|
$data = [
|
|
'subtitle' => 'Akun anda',
|
|
];
|
|
|
|
return view('user::setting', compact('data'));
|
|
}
|
|
|
|
public function riwayat(Request $request)
|
|
{
|
|
if ($request->ajax()) {
|
|
$data = Riwayat::where('id_user', user()->id)->get();
|
|
// Convert the Eloquent Collection to a regular PHP array
|
|
$data->each(function ($item, $key) {
|
|
$item->rowIndex = $key + 1;
|
|
});
|
|
|
|
return Datatables::of($data)
|
|
->addIndexColumn()
|
|
->addColumn('title-post', function($row){
|
|
$text = '
|
|
<p class="mb-0">' . $row->gejala . '</p>
|
|
';
|
|
return $text;
|
|
})
|
|
->addColumn('penyakit', function($row){
|
|
$getPenyakit = unserialize($row->penyakit);
|
|
$namePenyakit = [];
|
|
$i = 0;
|
|
foreach ($getPenyakit as $key1 => $value1) {
|
|
$i++;
|
|
$namePenyakit[$i] = $key1;
|
|
}
|
|
|
|
return Penyakit::where('kode_penyakit', $namePenyakit[1])->first()->nama_penyakit;
|
|
})
|
|
->addColumn('tingkat', function($row){
|
|
$persen = round($row->persen *100, 2);
|
|
return "<span class='text-success'>{$persen}%</span>";
|
|
})
|
|
->addColumn('penanganan', function($row){
|
|
$getPenyakit = unserialize($row->penyakit);
|
|
$namePenyakit = [];
|
|
$i = 0;
|
|
foreach ($getPenyakit as $key1 => $value1) {
|
|
$i++;
|
|
$namePenyakit[$i] = $key1;
|
|
}
|
|
|
|
return Penyakit::where('kode_penyakit', $namePenyakit[1])->first()->penanganan;
|
|
})
|
|
->addColumn('penjelasan', function($row){
|
|
return '1';
|
|
})
|
|
->addColumn('waktu_diagnosa', function($row){
|
|
return Carbon::parse($row->waktu)->format('j F Y H:i:s');
|
|
})
|
|
->rawColumns(['title-post', 'jenis_gejala', 'tingkat', 'waktu_diagnosa', 'penjelasan'])
|
|
->make(true);
|
|
}
|
|
|
|
$data = [
|
|
'subtitle' => 'Riwayat Diagnosa'
|
|
];
|
|
|
|
return view('user::riwayat.index', compact('data'));
|
|
}
|
|
|
|
public function update(Request $request)
|
|
{
|
|
$id = user()->id;
|
|
$validator = Validator::make($request->all(), [
|
|
'name' => 'required',
|
|
'email' => 'required',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return redirect()->back()->with('error', $validator->errors()->first())->withInput();
|
|
}
|
|
|
|
$input = $request->all();
|
|
$findUser = User::find($id);
|
|
|
|
if($findUser) {
|
|
// update data
|
|
$findUser->name = $input['name'];
|
|
$findUser->email = $input['email'];
|
|
if(!empty($input['password'])) {
|
|
$findUser->password = bcrypt($input['password']);
|
|
}
|
|
|
|
// save data
|
|
$findUser->save();
|
|
return redirect()->back()->with('success', 'Successfully update data');
|
|
} else {
|
|
return redirect()->back()->with('error', 'Unexpected error');
|
|
}
|
|
}
|
|
|
|
public function diagnosa(Request $request)
|
|
{
|
|
$arcolor = array('#ffffff', '#cc66ff', '#019AFF', '#00CBFD', '#00FEFE', '#A4F804', '#FFFC00', '#FDCD01', '#FD9A01', '#FB6700');
|
|
$inptanggal = date('Y-m-d H:i:s');
|
|
|
|
$arbobot = array('1', '0.8', '0.6', '0.4', '0.2', '0');
|
|
$argejala = array();
|
|
$arkondisitext = array();
|
|
|
|
for ($i = 0; $i < count($_POST['kondisi']); $i++) {
|
|
$arkondisi = explode("_", $_POST['kondisi'][$i]);
|
|
if (strlen($_POST['kondisi'][$i]) > 1) {
|
|
$argejala += array($arkondisi[0] => $arkondisi[1]);
|
|
}
|
|
}
|
|
|
|
foreach(Kondisi::all() as $kondisi) {
|
|
$arkondisitext[$kondisi->id] = $kondisi->name;
|
|
}
|
|
|
|
//print_r($arkondisitext);
|
|
// -------- perhitungan certainty factor (CF) ---------
|
|
// --------------------- START ------------------------
|
|
$arpenyakit = array();
|
|
foreach(Penyakit::all() as $rpenyakit) {
|
|
|
|
$cftotal_temp = 0;
|
|
$cf = 0;
|
|
$cflama = 0;
|
|
$urutancf = 1;
|
|
|
|
foreach(Rule::where('kode_penyakit', $rpenyakit->kode_penyakit)->get() as $rgejala) {
|
|
for ($i = 0; $i < count($_POST['kondisi']); $i++) {
|
|
$arkondisi = explode("_", $_POST['kondisi'][$i]);
|
|
$gejala = $arkondisi[0];
|
|
|
|
// echo $arkondisi[1] . '<br>';
|
|
|
|
if ($rgejala->kode_gejala === $gejala) {
|
|
// kondisi terpilih
|
|
$bobotNilai = Kondisi::where('id', $arkondisi[1])->first()->nilai;
|
|
// CF[H]
|
|
$cf = ($rgejala->mb - $rgejala->md) * $bobotNilai;
|
|
|
|
// echo 'CF GEJALA SAAT INI ( ' . $rpenyakit->nama_penyakit . ' - ' . $rgejala->kode_gejala . ' ) : CF PAKAR * CF KONDISI DIPILIH USER <br>';
|
|
// echo 'CF GEJALA SAAT INI:' . $cf . ' * ' . $bobotNilai . '<br>';
|
|
// echo 'CF GEJALA SAAT ini: ' . $cf * $bobotNilai . '<br><br>';
|
|
|
|
if (($cf >= 0) && ($cf * $cflama >= 0)) {
|
|
$cflama = $cflama + ($cf * (1 - $cflama));
|
|
}
|
|
if ($cf * $cflama < 0) {
|
|
$cflama = ($cflama + $cf) / (1 - min(abs($cflama), abs($cf)));
|
|
}
|
|
if (($cf < 0) && ($cf * $cflama >= 0)) {
|
|
$cflama = $cflama + ($cf * (1 + $cflama));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($cflama > 0) {
|
|
$arpenyakit += array($rpenyakit->kode_penyakit => number_format($cflama, 4));
|
|
}
|
|
|
|
}
|
|
|
|
arsort($arpenyakit);
|
|
|
|
$inpgejala = serialize($argejala);
|
|
$inppenyakit = serialize($arpenyakit);
|
|
|
|
$simpan = Riwayat::create([
|
|
'id_user' => user()->id,
|
|
'jawaban' => $inpgejala,
|
|
'penyakit' => $inppenyakit,
|
|
'persen' => reset($arpenyakit)
|
|
]);
|
|
|
|
$data['arpenyakit'] = $arpenyakit;
|
|
$data['arcolor'] = $arcolor;
|
|
$data['list_gejala'] = $argejala;
|
|
$data['arkondisitext'] = $arkondisitext;
|
|
|
|
if($simpan) {
|
|
$data['subtitle'] = 'Hasil Diagnosa';
|
|
return view('user::diagnosa', compact('data'));
|
|
} else {
|
|
return redirect()->back()->with('error', 'Unexpected error in diagnosa process');
|
|
}
|
|
}
|
|
}
|