Passed
Pull Request — develop (#11)
by
unknown
03:18
created

GroupController::update()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 5
nop 1
dl 0
loc 30
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Judite\Models\Group;
6
use App\Judite\Models\Course;
7
use App\Judite\Models\Student;
8
use App\Judite\Models\Invitation;
9
use Illuminate\Support\Facades\Auth;
10
use App\Exceptions\UserHasAlreadyGroupInCourseException;
11
12
class GroupController extends Controller
13
{
14
    /**
15
     * Create a new controller instance.
16
     */
17
    public function __construct()
18
    {
19
        $this->middleware('auth');
20
        $this->middleware('can.student');
21
        $this->middleware('student.verified');
22
        $this->middleware('can.group');
23
    }
24
25
    /**
26
     * Display a listing of the resource.
27
     *
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function index()
31
    {
32
        $student = Auth::student();
33
34
        $enrollments = $student
35
            ->enrollments()
36
            ->orderByCourse()
37
            ->get();
38
39
        foreach ($enrollments as $enrollmentKey => $enrollment) {
40
            $course = Course::whereId($enrollment->course_id)->first();
41
42
            if ($course->group_max <= 0) {
43
                unset($enrollments[$enrollmentKey]);
44
                continue;
45
            }
46
47
            $membership = $student->memberships()
48
                ->whereCourseId($course->id)
49
                ->first();
50
51
            if (is_null($membership)) {
52
                $enrollment->group_status = 0;
53
            } else {
54
                $group = Group::whereId($membership->group_id)->first();
55
                $enrollment->group_status =
56
                    $group->memberships()->count();
57
            }
58
59
            $enrollment->name = $course->name;
60
            $enrollment->group_min = $course->group_min;
61
            $enrollment->group_max = $course->group_max;
62
63
            $enrollment->number_invitations = $this->numberInvitations(
64
                $enrollment->course_id,
65
                $student->student_number
66
            );
67
        }
68
69
        return view('groups.index', compact('enrollments'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('groups.inde...compact('enrollments')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
70
    }
71
72
    /**
73
     * Store a newly created resource in storage.
74
     *
75
     * @param $courseId
76
     *
77
     * @return \Illuminate\Http\Response
78
     */
79
    public function store($courseId)
80
    {
81
        $group = new Group();
82
        $group->course_id = $courseId;
83
        $group->save();
84
85
        try {
86
            Auth::student()->join($group);
87
88
            flash('You have successfully joined a group.')
89
                ->success();
90
        } catch (UserHasAlreadyGroupInCourseException $e) {
91
            flash('You already have a group.')
92
                ->error();
93
            $group->delete();
94
        }
95
96
        return redirect()->route('groups.show', compact('courseId'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...', compact('courseId')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
97
    }
98
99
    /**
100
     * Display the specified resource.
101
     *
102
     * @param int $courseId
103
     *
104
     * @return \Illuminate\Http\Response
105
     */
106
    public function show($courseId)
107
    {
108
        $student = Auth::student();
109
110
        $course = Course::whereId($courseId)->first();
111
112
        $course->numberInvitations = $this->numberInvitations(
0 ignored issues
show
Bug introduced by
The property numberInvitations does not seem to exist on App\Judite\Models\Course. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
113
            $courseId,
114
            $student->student_number
115
        );
116
117
        $membership = $student
118
            ->memberships()
119
            ->whereCourseId($courseId)
120
            ->first();
121
122
        $students = [];
123
        $group = 0;
124
        if (! is_null($membership)) {
125
            $group = $membership->group()->first();
126
            $memberships = $group->memberships()->get();
127
128
            foreach ($memberships as $membership) {
129
                $student = $membership->student()->first();
130
131
                $student->name = $student->user()->first()->name;
132
133
                array_push($students, $student);
134
            }
135
        }
136
137
        return view('groups.show', compact('course', 'students', 'group'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('groups.show..., 'students', 'group')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
138
    }
139
140
    /**
141
     * Update the specified resource in storage.
142
     *
143
     * @param int $inviteId
144
     *
145
     * @return \Illuminate\Http\Response
146
     */
147
    public function update($inviteId)
148
    {
149
        $invitation = Invitation::whereId($inviteId)->first();
150
151
        $group = Group::whereId($invitation->group_id)->first();
152
153
        $courseMax = Course::whereId($invitation->course_id)
154
            ->first()
155
            ->group_max;
156
157
        if ($group->memberships()->count() >= $courseMax) {
158
            flash('Course group limit exceeded')->error();
159
160
            return redirect()->back();
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back() returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
161
        }
162
163
        try {
164
            Auth::student()->join($group);
165
166
            flash('You have successfully joined the group.')
167
                ->success();
168
169
            return redirect()->route('invitations.destroy', compact('inviteId'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...', compact('inviteId')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
170
        } catch (UserHasAlreadyGroupInCourseException $e) {
171
            flash('You already have a group.')
172
                ->error();
173
174
            $courseId = $invitation->course_id;
175
176
            return redirect()->route('groups.show', compact('courseId'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...', compact('courseId')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
177
        }
178
    }
179
180
    /**
181
     * Remove the specified resource from storage.
182
     *
183
     * @param int $id
184
     *
185
     * @return \Illuminate\Http\Response
186
     */
187
    public function destroy($courseId)
188
    {
189
        $group = Auth::student()->memberships()->get()
190
            ->where('course_id', '=', $courseId)->first()
191
            ->group()->first();
192
193
        Auth::student()->leave($group);
194
195
        if (! ($group->memberships()->count())) {
196
            $invitations = Invitation::whereGroupId($group->id)->get();
197
198
            foreach ($invitations as $invitation) {
199
                $invitation->delete();
200
            }
201
202
            $group->delete();
203
            flash('Group deleted.')->success();
204
        } else {
205
            flash('You have successfully left the group.')->success();
206
        }
207
208
        return redirect()->route('groups.show', compact('courseId'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...', compact('courseId')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
209
    }
210
211
    private function numberInvitations($courseId, $studentNumber)
212
    {
213
        return Invitation::where([
214
            ['course_id', '=', $courseId],
215
            ['student_number', '=', $studentNumber],
216
            ])
217
            ->count();
218
    }
219
}
220