Skip to content

Commit

Permalink
Merge pull request #30 from rizkyilhampra/version-0.2
Browse files Browse the repository at this point in the history
Version 0.2
  • Loading branch information
rizkyilhampra authored Jun 8, 2023
2 parents 5a014fd + 4168a30 commit 751cbaa
Show file tree
Hide file tree
Showing 10 changed files with 492 additions and 293 deletions.
19 changes: 7 additions & 12 deletions app/Http/Controllers/DiagnosisController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class DiagnosisController extends Controller
{
public function Diagnosis(Request $request)
public function diagnosis(Request $request)
{
$allGejala = Gejala::get('id')->count();

Expand Down Expand Up @@ -69,26 +69,21 @@ public function Diagnosis(Request $request)
$modelDiagnosis->penyakit_id = $penyakitId;
$modelDiagnosis->save();
}
$penyakit = Penyakit::where('id', $modelDiagnosis->penyakit_id)->first(['name', 'reason', 'solution', 'image']);
$penyakit = Penyakit::where('id', $modelDiagnosis->penyakit_id)->first('id');
$terdeteksi = true;
}
}


// Tidak ada penyakit yang terdeteksi
if (!$terdeteksi && $request->idgejala == $allGejala) {
return response()->json(['penyakitUndentified' => 'Tidak ada penyakit yang cocok dengan gejala yang anda masukkan.']);
return response()->json([
'penyakitUndentified' => true
]);
}

return response()->json([
$modelDiagnosis->answer_log,
$penyakit ?? null
'idDiagnosis' => $modelDiagnosis->id,
'idPenyakit' => $penyakit ?? null
]);
}

public function getGejala(Request $request)
{
$gejala = Gejala::get(['id', 'name']);
return response()->json($gejala);
}
}
75 changes: 43 additions & 32 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Models\Diagnosis;
use App\Models\Gejala;
use App\Models\Penyakit;
use GdImage;
use Illuminate\Http\Request;

class UserController extends Controller
Expand All @@ -30,9 +29,6 @@ public function historiDiagnosis(Request $request)

$user = auth()->user();

$start = $request->input('start', 0);
$length = $request->input('length', 5);

$query = Diagnosis::with(['penyakit' => function ($query) {
$query->select('id', 'name');
}])->where('user_id', $user->id ?? null);
Expand All @@ -50,33 +46,36 @@ public function historiDiagnosis(Request $request)

$totalData = $query->count();

$historiDiagnosis = $query->offset($start)
$start = $request->input('start', 0);
$length = $request->input('length', 5);

$orderColumn = $request->input('order.0.column', 0);
$orderDirection = $request->input('order.0.dir', 'asc');

$orderColumns = [
0 => 'id',
1 => 'created_at',
];

if (array_key_exists($orderColumn, $orderColumns)) {
$orderBy = $orderColumns[$orderColumn];
$query->orderBy($orderBy, $orderDirection);

$no = ($orderDirection == 'asc') ? $start + 1 : $totalData - $start;
}

$historiDiagnosis = $query
->offset($start)
->limit($length)
->get(['id', 'created_at', 'penyakit_id']);

$data = $historiDiagnosis->map(function ($item) {
$item->penyakit = Penyakit::find($item->penyakit_id, ['name']) ?? ['name' => 'Tidak Diketahui'];
$data = $historiDiagnosis->map(function ($item) use (&$no, $orderDirection) {
$penyakit = Penyakit::find($item->penyakit_id, ['name']);
$item->penyakit = $penyakit ? $penyakit->name : 'Tidak Diketahui';
$item->no = ($orderDirection == 'asc') ? $no++ : $no--;
return $item;
});

$no = $start + 1;
$data = $data->map(function ($item) use (&$no) {
$item->no = $no++;
return $item;
});

if ($request->has('order.0.column') && $request->has('order.0.dir')) {
$orderColumn = $request->input('order.0.column');
$orderDirection = $request->input('order.0.dir');
if ($orderColumn == 0) {
$data = $data->sortBy('no', SORT_REGULAR, $orderDirection == 'desc')->values();
} else if ($orderColumn == 1) {
$data = $data->sortBy('created_at', SORT_REGULAR, $orderDirection == 'desc')->values();
} else if ($orderColumn == 2) {
$data = $data->sortBy('penyakit.name', SORT_REGULAR, $orderDirection == 'desc')->values();
}
}

return response()->json([
'draw' => $request->input('draw'),
'recordsTotal' => $totalData,
Expand All @@ -85,29 +84,41 @@ public function historiDiagnosis(Request $request)
]);
}

public function historiDiagnosisDetail(Request $request)
public function detailDiagnosis(Request $request)
{
$diagnosis = Diagnosis::find($request->id, ['answer_log']);
$penyakit = Penyakit::find(
Diagnosis::find($request->id_diagnosis, ['penyakit_id'])->penyakit_id,
['name', 'reason', 'solution', 'image']
);

$diagnosis = Diagnosis::find($request->id_diagnosis, ['answer_log']);
$answerLog = json_decode($diagnosis->answer_log, true);
foreach ($answerLog as $key => $value) {
$answerLog[$key] = $value == 1 ? 'Ya' : 'Tidak';
}
$gejala = Gejala::whereIn('id', array_keys($answerLog))->get(['id', 'name']);
foreach ($gejala as $item) {

$item->answer = $answerLog[$item->id];
}
$answerLog = $gejala->map(function ($item) use ($request) {
return [
'no' => $request->no,
'id' => $item->id,
'name' => $item->name,
'answer' => $item->answer,
];
});

return response()->json([
'answerLog' => $answerLog,
]);
return response()->json(
[
'penyakit' => $penyakit,
'answerLog' => $answerLog,
]
);
}

public function getGejala()
{
$gejala = Gejala::get(['id', 'name']);
return response()->json($gejala);
}
}
131 changes: 131 additions & 0 deletions public/spesified-assets/user/detail-diagnosis-modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
const detailDiagnosisModal = document.querySelector('#detailDiagnosisModal');
const titleDetailDiagnosisModal = detailDiagnosisModal.querySelector('.modal-title');
const instanceDetailDiagnosisModal = bootstrap.Modal.getOrCreateInstance(detailDiagnosisModal);
const headerDetailDiagnosis = document.getElementById('headerDetailDiagnosis');
const subheaderDetailDiagnosis = document.getElementById('subheaderDetailDiagnosis');
const containerImagePenyakitDetailDiagnosisModal = document.getElementById('containerImagePenyakitDetailDiagnosisModal');
const headerPenyakitSolution = document.getElementById('headerPenyakitSolution');
const rowDetailPenyakit = document.getElementById('rowDetailPenyakit');
const detailJawabanDiagnosisTable = document.getElementById('detailJawabanDiagnosisTable');
const tableBody = detailJawabanDiagnosisTable.querySelector('tbody');

let idPenyakit = null;
let idDiagnosis = null;
let noHistoriDiagnosis = null;
let diagnosed = false;
let penyakitUndentified = false;

function getPenyakitIdFromHistori(data, no) {
idDiagnosis = data;
noHistoriDiagnosis = no;
diagnosed = false;
instanceDetailDiagnosisModal.show();
}

function getPenyakitFromDiagnose(data, wasDiagnosed) {
idPenyakit = data.idPenyakit;
idDiagnosis = data.idDiagnosis;
diagnosed = wasDiagnosed;
instanceDetailDiagnosisModal.show();
}

function getUndentifiedPenyakit(data) {
penyakitUndentified = data;
instanceDetailDiagnosisModal.show();
}

function ajaxRequestDetailDiagnosis() {
return $.ajax({
url: '/detail-diagnosis',
method: 'GET',
data: {
id_penyakit: idPenyakit,
id_diagnosis: idDiagnosis,
},
});
}

detailDiagnosisModal.addEventListener('show.bs.modal', async () => {
try {
let response = await ajaxRequestDetailDiagnosis();
drawDetailDiagnosis(response, diagnosed);
drawDetailJawabanDiagnosis(response.answerLog);
} catch (error) {
swalError(error.responseJSON);
}
});


function drawDetailDiagnosis(response, wasDiagnosed) {
if (wasDiagnosed) {
titleDetailDiagnosisModal.innerText = 'Detail Diagnosis';
headerDetailDiagnosis.innerText = "Penyakit Ditemukan!";
subheaderDetailDiagnosis.innerHTML = "Penyakit yang diderita adalah " + `<u>${response.penyakit.name}</u>`;
headerDetailDiagnosis.classList.remove('d-none');
subheaderDetailDiagnosis.classList.remove('d-none');
}
titleDetailDiagnosisModal.innerText = 'Detail Diagnosis No. ' + noHistoriDiagnosis;
if (response.penyakit == null || penyakitUndentified) {
headerDetailDiagnosis.innerText = "Penyakit Tidak Ditemukan!";
subheaderDetailDiagnosis.innerHTML = 'Tidak ada penyakit yang cocok dengan gejala yang anda masukkan.';
headerDetailDiagnosis.classList.remove('d-none');
subheaderDetailDiagnosis.classList.remove('d-none');
rowDetailPenyakit.classList.add('d-none');
} else {
if (rowDetailPenyakit.classList.contains('d-none')) {
rowDetailPenyakit.classList.remove('d-none');
}
const penyakitName = document.getElementById('penyakitName');
const penyakitReason = document.getElementById('penyakitReason');
penyakitName.innerHTML = response.penyakit.name;
penyakitReason.innerHTML = response.penyakit.reason;

let penyakitSolution = response.penyakit.solution;
let regex = /(\d+\.)\s*(.*?)(?=(\d+\.|$))/gs;
let matches = [...penyakitSolution.matchAll(regex)];
let nomorAsOlTag = '<ol>';
for (let i = 0; i < matches.length; i++) {
nomorAsOlTag += '<li>' + matches[i][2] + '</li>';
}
nomorAsOlTag += '</ol>';
headerPenyakitSolution.insertAdjacentHTML('afterend', nomorAsOlTag);

const imagePenyakit = new Image();
imagePenyakit.src = assetImageUrl + '/' + response.penyakit.image;
imagePenyakit.alt = response.penyakit.name;
imagePenyakit.classList.add('img-fluid');
containerImagePenyakitDetailDiagnosisModal.appendChild(imagePenyakit);
}
}

function drawDetailJawabanDiagnosis(data) {
const response = data;
response.forEach((item, index) => {
const tableRow = document.createElement('tr');
const tableData = document.createElement('td');
const tableData2 = document.createElement('td');
const tableData3 = document.createElement('td');
let number = index + 1;
tableData.innerHTML = number;
tableData2.innerHTML = item.name;
tableData3.innerHTML = item.answer;
tableRow.appendChild(tableData);
tableRow.appendChild(tableData2);
tableRow.appendChild(tableData3);
tableBody.appendChild(tableRow);
});
}

detailDiagnosisModal.addEventListener('hide.bs.modal', function (event) {
containerImagePenyakitDetailDiagnosisModal.innerHTML = '';
if (headerPenyakitSolution.nextElementSibling) {
headerPenyakitSolution.nextElementSibling.remove();
}
headerDetailDiagnosis.classList.add('d-none');
subheaderDetailDiagnosis.classList.add('d-none');

//remove all child element in table body
while (tableBody.firstChild) {
tableBody.removeChild(tableBody.firstChild);
}
});
Loading

0 comments on commit 751cbaa

Please sign in to comment.