|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\Event; |
|
6
|
|
|
use App\Models\LeadType; |
|
7
|
|
|
use App\Models\Period; |
|
8
|
|
|
use App\Models\Student; |
|
9
|
|
|
use App\Models\Teacher; |
|
10
|
|
|
use App\Traits\PeriodSelection; |
|
11
|
|
|
use Carbon\Carbon; |
|
12
|
|
|
use Illuminate\Http\Request; |
|
13
|
|
|
use Illuminate\Support\Facades\Log; |
|
14
|
|
|
|
|
15
|
|
|
class HomeController extends Controller |
|
16
|
|
|
{ |
|
17
|
|
|
use PeriodSelection; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
public function __construct() |
|
20
|
|
|
{ |
|
21
|
|
|
parent::__construct(); |
|
22
|
|
|
|
|
23
|
|
|
$this->middleware(backpack_middleware()); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* redirect the user according to their role. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function index() |
|
30
|
|
|
{ |
|
31
|
|
|
if (backpack_user()->hasRole(['admin', 'secretary'])) { |
|
|
|
|
|
|
32
|
|
|
return redirect()->route('admin'); |
|
33
|
|
|
} elseif (backpack_user()->isTeacher()) { |
|
|
|
|
|
|
34
|
|
|
return redirect()->route('teacherDashboard'); |
|
35
|
|
|
} elseif (backpack_user()->isStudent()) { |
|
|
|
|
|
|
36
|
|
|
return redirect()->route('studentDashboard'); |
|
37
|
|
|
} else { |
|
38
|
|
|
// this should never happen |
|
39
|
|
|
Log::warning(backpack_user()->id.' accessed the generic dashboard (no role identified)'); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
abort(403, 'User with no role'); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function teacher(Request $request) |
|
46
|
|
|
{ |
|
47
|
|
|
if (! backpack_user()->isTeacher()) { |
|
48
|
|
|
abort(403); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$period = $this->selectPeriod($request); |
|
52
|
|
|
|
|
53
|
|
|
$teacher = Teacher::where('id', backpack_user()->id)->first(); |
|
|
|
|
|
|
54
|
|
|
Log::info($teacher->name.' accessed the student dashboard'); |
|
55
|
|
|
|
|
56
|
|
|
$remoteVolume = $teacher->courses()->whereNull('parent_course_id')->where('period_id', $period->id)->sum('remote_volume'); |
|
57
|
|
|
$presentialVolume = $teacher->courses()->whereNull('parent_course_id')->where('period_id', $period->id)->sum('volume'); |
|
58
|
|
|
|
|
59
|
|
|
return view('teacher.dashboard', [ |
|
60
|
|
|
'teacher' => $teacher, |
|
61
|
|
|
'courses' => $teacher->period_courses($period), |
|
62
|
|
|
'pending_attendance' => $teacher->events_with_pending_attendance($period), |
|
63
|
|
|
'selected_period' => $period, |
|
64
|
|
|
'remoteVolume' => $remoteVolume, |
|
65
|
|
|
'volume' => $presentialVolume, |
|
66
|
|
|
'totalVolume' => $remoteVolume + $presentialVolume, |
|
67
|
|
|
]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function student() |
|
71
|
|
|
{ |
|
72
|
|
|
if (! backpack_user()->isStudent()) { |
|
73
|
|
|
abort(403); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$student = Student::where('id', backpack_user()->id)->first(); |
|
|
|
|
|
|
77
|
|
|
Log::info($student->name.' accessed the student dashboard'); |
|
78
|
|
|
|
|
79
|
|
|
return view('student.dashboard', [ |
|
80
|
|
|
'student' => $student, |
|
81
|
|
|
'enrollments' => $student->enrollments()->real(), |
|
82
|
|
|
]); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function admin() |
|
86
|
|
|
{ |
|
87
|
|
|
$currentPeriod = Period::get_default_period(); |
|
88
|
|
|
$enrollmentsPeriod = Period::get_enrollments_period(); |
|
89
|
|
|
|
|
90
|
|
|
if (! backpack_user()->hasRole(['admin', 'secretary'])) { |
|
91
|
|
|
abort(403); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
Log::info(backpack_user()->firstname.' '.backpack_user()->lastname.' accessed the admin dashboard'); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
// todo optimize this !! |
|
97
|
|
|
$events = Event::where('start', '>', Carbon::now()->subDays(15))->where('end', '<', Carbon::now()->addDays(15))->orderBy('id', 'desc') |
|
98
|
|
|
->get() |
|
99
|
|
|
->map(fn ($event) => [ |
|
100
|
|
|
'title' => $event['name'], |
|
101
|
|
|
'resourceId' => $event['teacher_id'], |
|
102
|
|
|
'start' => $event['start'], |
|
103
|
|
|
'end' => $event['end'], |
|
104
|
|
|
'backgroundColor' => $event['color'], |
|
105
|
|
|
'borderColor' => $event['color'], |
|
106
|
|
|
]) |
|
107
|
|
|
->toArray(); |
|
108
|
|
|
|
|
109
|
|
|
$teachers = Teacher::with('user')->get()->toArray(); |
|
110
|
|
|
|
|
111
|
|
|
$teachers = array_map(fn ($teacher) => [ |
|
112
|
|
|
'id' => $teacher['id'], |
|
113
|
|
|
'title' => $teacher['user']['firstname'], |
|
114
|
|
|
], $teachers); |
|
115
|
|
|
|
|
116
|
|
|
return view('admin.dashboard', [ |
|
117
|
|
|
'pending_enrollment_count' => $currentPeriod->pending_enrollments_count, |
|
118
|
|
|
'paid_enrollment_count' => $currentPeriod->paid_enrollments_count, |
|
119
|
|
|
'students_count' => $currentPeriod->studentCount(), |
|
120
|
|
|
'currentPeriod' => $currentPeriod, |
|
121
|
|
|
'enrollmentsPeriod' => $enrollmentsPeriod, |
|
122
|
|
|
'total_enrollment_count' => $currentPeriod->internal_enrollments_count, |
|
123
|
|
|
'resources' => $teachers, |
|
124
|
|
|
'events' => $events, |
|
125
|
|
|
'pending_attendance' => $currentPeriod->courses_with_pending_attendance, |
|
126
|
|
|
// todo: optimize |
|
127
|
|
|
'unassigned_events' => Event::unassigned()->count(), |
|
128
|
|
|
'pending_leads' => LeadType::find(4)->students()->count(), |
|
129
|
|
|
]); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|