Issues (350)

app/Http/Controllers/HomeController.php (9 issues)

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;
0 ignored issues
show
The trait App\Traits\PeriodSelection requires the property $id which is not provided by App\Http\Controllers\HomeController.
Loading history...
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'])) {
0 ignored issues
show
The method hasRole() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        if (backpack_user()->/** @scrutinizer ignore-call */ hasRole(['admin', 'secretary'])) {
Loading history...
32
            return redirect()->route('admin');
33
        } elseif (backpack_user()->isTeacher()) {
0 ignored issues
show
The method isTeacher() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        } elseif (backpack_user()->/** @scrutinizer ignore-call */ isTeacher()) {
Loading history...
34
            return redirect()->route('teacherDashboard');
35
        } elseif (backpack_user()->isStudent()) {
0 ignored issues
show
The method isStudent() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        } elseif (backpack_user()->/** @scrutinizer ignore-call */ isStudent()) {
Loading history...
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)');
0 ignored issues
show
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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();
0 ignored issues
show
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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();
0 ignored issues
show
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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');
0 ignored issues
show
Accessing lastname on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Accessing firstname on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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