60 lines
2.0 KiB
PHP
60 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Siswa;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Materi;
|
|
use App\Models\Tugas;
|
|
use App\Models\Mengajar;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Carbon\Carbon;
|
|
|
|
class NotifikasiController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$siswa = Auth::guard('siswa')->user();
|
|
$since = Carbon::now()->subDays(7);
|
|
|
|
// ID mengajar untuk kelas siswa ini
|
|
$idMengajars = Mengajar::where('id_kelas', $siswa->id_kelas)
|
|
->pluck('id_mengajar');
|
|
|
|
// Materi baru
|
|
$materiBaru = Materi::with(['mengajar.mapel', 'mengajar.kelas'])
|
|
->whereIn('id_mengajar', $idMengajars)
|
|
->where('created_at', '>=', $since)
|
|
->orderBy('created_at', 'desc')
|
|
->get()
|
|
->map(fn($m) => [
|
|
'type' => 'materi',
|
|
'title' => 'Materi Baru',
|
|
'message' => optional($m->mengajar->mapel)->nama_mapel . ': ' . $m->judul_materi,
|
|
'time' => $m->created_at->diffForHumans(),
|
|
'time_raw'=> $m->created_at->toIso8601String(),
|
|
]);
|
|
|
|
// Tugas baru
|
|
$tugasBaru = Tugas::with(['mengajar.mapel', 'mengajar.kelas'])
|
|
->whereIn('id_mengajar', $idMengajars)
|
|
->where('created_at', '>=', $since)
|
|
->orderBy('created_at', 'desc')
|
|
->get()
|
|
->map(fn($t) => [
|
|
'type' => 'tugas',
|
|
'title' => 'Tugas Baru',
|
|
'message' => optional($t->mengajar->mapel)->nama_mapel . ': ' . $t->judul_tugas,
|
|
'time' => $t->created_at->diffForHumans(),
|
|
'time_raw'=> $t->created_at->toIso8601String(),
|
|
]);
|
|
|
|
$notifications = $materiBaru->concat($tugasBaru)
|
|
->sortByDesc('time_raw')
|
|
->values();
|
|
|
|
return response()->json([
|
|
'count' => $notifications->count(),
|
|
'notifications' => $notifications,
|
|
]);
|
|
}
|
|
} |