diff --git a/app/Http/Controllers/Admin/AddictionController.php b/app/Http/Controllers/Admin/AddictionController.php index 7ec5d92..6210672 100644 --- a/app/Http/Controllers/Admin/AddictionController.php +++ b/app/Http/Controllers/Admin/AddictionController.php @@ -58,6 +58,7 @@ public function store(Request $request) 'name' => $request->name, 'code' => $request->code, 'description' => $request->description, + 'solution' => $request->solution, 'min_percentage' => $request->min_percentage, 'max_percentage' => $request->max_percentage, 'status' => $request->status, @@ -117,6 +118,7 @@ public function update(Request $request, Addiction $addiction) 'name' => $request->name, 'code' => $request->code, 'description' => $request->description, + 'solution' => $request->solution, 'min_percentage' => $request->min_percentage, 'max_percentage' => $request->max_percentage, 'status' => $request->status, diff --git a/app/Http/Controllers/Admin/HistoryController.php b/app/Http/Controllers/Admin/HistoryController.php index 5205e4c..6864e46 100644 --- a/app/Http/Controllers/Admin/HistoryController.php +++ b/app/Http/Controllers/Admin/HistoryController.php @@ -54,7 +54,7 @@ public function store(Request $request) */ public function show(History $history) { - // + return view('admin.histories.show', compact('history')); } /** diff --git a/app/Http/Controllers/User/DashboardController.php b/app/Http/Controllers/User/DashboardController.php index df880af..ad96f31 100644 --- a/app/Http/Controllers/User/DashboardController.php +++ b/app/Http/Controllers/User/DashboardController.php @@ -35,9 +35,16 @@ public function history() return view('user.history', compact('histories')); } + public function detailHistory($id) + { + $history = History::with('user', 'addiction', 'details')->find($id); + + return view('user.history-detail', compact('history')); + } + public function addiction() { - $items = Item::where('status', 'active')->get(); + $items = Item::where('status', 'active')->inRandomOrder()->get(); $likerts = Likert::orderBy('score')->get(); return view('user.addiction', compact('items', 'likerts')); @@ -45,6 +52,7 @@ public function addiction() public function storeAddiction(Request $request) { + $items = $request->items; $values = $request->values; $answers = $request->answers; $maxLikert = Likert::orderBy('score', 'desc')->first()->score; @@ -53,7 +61,8 @@ public function storeAddiction(Request $request) $maxScores = []; foreach ($values as $key => $value) { - $userScores[] = $value * $answers[$key]; + $score = Likert::find($answers[$key])->score; + $userScores[] = $value * $score; $maxScores[] = $value * $maxLikert; } @@ -67,16 +76,23 @@ public function storeAddiction(Request $request) ->where('status', 'active') ->first(); - if ($addiction === null) { + if (is_null($addiction)) { return redirect()->back()->with('error', 'Terjadi kesalahan saat mencari jenis kecanduan'); } - History::create([ + $history = History::create([ 'user_id' => Auth::id(), 'addiction_id' => $addiction->id, - 'result' => $percentage.'%', + 'result' => $percentage, ]); + foreach ($items as $key => $item) { + $history->details()->create([ + 'item_id' => $item, + 'likert_id' => $answers[$key], + ]); + } + return redirect()->route('user.history')->with('success', 'Berhasil menambahkan riwayat'); } } diff --git a/app/Models/History.php b/app/Models/History.php index 726e720..176dbd8 100644 --- a/app/Models/History.php +++ b/app/Models/History.php @@ -20,4 +20,9 @@ public function addiction() { return $this->belongsTo(Addiction::class, 'addiction_id', 'id'); } + + public function details() + { + return $this->hasMany(HistoryDetail::class, 'history_id', 'id'); + } } diff --git a/app/Models/HistoryDetail.php b/app/Models/HistoryDetail.php new file mode 100644 index 0000000..41c22a0 --- /dev/null +++ b/app/Models/HistoryDetail.php @@ -0,0 +1,28 @@ +belongsTo(History::class, 'history_id', 'id'); + } + + public function item() + { + return $this->belongsTo(Item::class, 'item_id', 'id'); + } + + public function likert() + { + return $this->belongsTo(Likert::class, 'likert_id', 'id'); + } +} diff --git a/database/factories/HistoryDetailFactory.php b/database/factories/HistoryDetailFactory.php new file mode 100644 index 0000000..5f413ad --- /dev/null +++ b/database/factories/HistoryDetailFactory.php @@ -0,0 +1,23 @@ + + */ +class HistoryDetailFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition() + { + return [ + // + ]; + } +} diff --git a/database/migrations/2024_12_20_184758_create_history_details_table.php b/database/migrations/2024_12_20_184758_create_history_details_table.php new file mode 100644 index 0000000..385a83c --- /dev/null +++ b/database/migrations/2024_12_20_184758_create_history_details_table.php @@ -0,0 +1,34 @@ +id(); + $table->foreignId('history_id')->constrained()->onDelete('cascade'); + $table->foreignId('item_id')->constrained()->onDelete('cascade'); + $table->foreignId('likert_id')->constrained()->onDelete('cascade'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('history_details'); + } +}; diff --git a/database/migrations/2024_12_20_204733_add_solution_to_addictions_table.php b/database/migrations/2024_12_20_204733_add_solution_to_addictions_table.php new file mode 100644 index 0000000..15cd7b6 --- /dev/null +++ b/database/migrations/2024_12_20_204733_add_solution_to_addictions_table.php @@ -0,0 +1,32 @@ +text('solution')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('addictions', function (Blueprint $table) { + // + }); + } +}; diff --git a/database/seeders/HistoryDetailSeeder.php b/database/seeders/HistoryDetailSeeder.php new file mode 100644 index 0000000..aec74f1 --- /dev/null +++ b/database/seeders/HistoryDetailSeeder.php @@ -0,0 +1,19 @@ + +
+ + + @error('solution') + + {{ $message }} + + @enderror +
+
+ @error('solution') + + {{ $message }} + + @enderror +
+
+ @error('solution') + + {{ $message }} + + @enderror +
+
+
+
+ + +
+ +
+
+ + +
+
+ + +
+
+ +
+
+

Addiction Test Result

+ + + + + + + + + + @foreach ($history->details as $detail) + + + + + + @endforeach + +
#Item ContentAnswer
{{ $loop->iteration }}{{ $detail->item->content }}{{ $detail->likert->name }}
+
+
+ + + + + +@endsection diff --git a/resources/views/admin/items/index.blade.php b/resources/views/admin/items/index.blade.php index 4e1d6e0..21927df 100644 --- a/resources/views/admin/items/index.blade.php +++ b/resources/views/admin/items/index.blade.php @@ -23,7 +23,7 @@

Item Data

Create Item
- +
@@ -39,10 +39,10 @@ @foreach ($items as $factorName => $groupedItems) @foreach ($groupedItems as $index => $item) - + - + + @@ -37,6 +32,16 @@ + @endforeach diff --git a/routes/web.php b/routes/web.php index 7d0a8b2..92f7819 100644 --- a/routes/web.php +++ b/routes/web.php @@ -50,6 +50,7 @@ Route::get('/home', [HomeController::class, 'index'])->name('dashboard'); Route::get('dashboard', [App\Http\Controllers\User\DashboardController::class, 'index'])->name('dashboard'); Route::get('history', [App\Http\Controllers\User\DashboardController::class, 'history'])->name('history'); + Route::get('history/{id}', [App\Http\Controllers\User\DashboardController::class, 'detailHistory'])->name('history.show'); Route::get('addiction', [App\Http\Controllers\User\DashboardController::class, 'addiction'])->name('addiction'); Route::post('addiction', [App\Http\Controllers\User\DashboardController::class, 'storeAddiction'])->name('addiction.store'); });
#
{{ $loop->iteration }}{{ $loop->iteration }} {{ $factorName }} {{ $item->code }}{{ $item->content }}{{ $item->content }} {{ $item->value }} {{ ucwords($item->status) }} @@ -96,7 +96,8 @@ columnDefs: [ { targets: 1, visible: false } ], - responsive: true, + responsive: false, + scrollX: true, order: [[1, 'asc']], }); }); diff --git a/resources/views/user/addiction.blade.php b/resources/views/user/addiction.blade.php index e563278..14d3b7e 100644 --- a/resources/views/user/addiction.blade.php +++ b/resources/views/user/addiction.blade.php @@ -29,11 +29,12 @@
{{ $item->content }} + + +
+ + +
+ +
+
+ + +
+
+ + +
+
+ +
+
+

Addiction Test Result

+ + + + + + + + + + @foreach ($history->details as $detail) + + + + + + @endforeach + +
#Item ContentAnswer
{{ $loop->iteration }}{{ $detail->item->content }}{{ $detail->likert->name }}
+
+
+ + + + + +@endsection diff --git a/resources/views/user/history.blade.php b/resources/views/user/history.blade.php index daa1341..6bdb4be 100644 --- a/resources/views/user/history.blade.php +++ b/resources/views/user/history.blade.php @@ -9,12 +9,6 @@ @endsection @section('content')
-
@@ -28,6 +22,7 @@
Addiction Result Date Time#
{{ $history->addiction->name }} {{ $history->result }}% {{ $history->created_at->format('d-m-Y H:i:s') }} + +