Passed
Pull Request — develop (#11)
by
unknown
02:46
created

GroupController::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 1
dl 0
loc 23
rs 9.7998
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\DB;
10
use Illuminate\Support\Facades\Auth;
11
use App\Exceptions\UserHasAlreadyGroupInCourseException;
12
13
class GroupController extends Controller
14
{
15
    /**
16
     * Create a new controller instance.
17
     */
18
    public function __construct()
19
    {
20
        $this->middleware('auth');
21
        $this->middleware('can.student');
22
        $this->middleware('student.verified');
23
        $this->middleware('can.group');
24
    }
25
26
    /**
27
     * Display a listing of the resource.
28
     *
29
     * @return \Illuminate\View\View
30
     */
31
    public function index()
32
    {
33
        $student = Auth::student();
34
35
        $enrollments = $student
36
            ->enrollments()
37
            ->orderByCourse()
38
            ->get();
39
40
        foreach ($enrollments as $enrollmentKey => $enrollment) {
41
            $course = DB::transaction(function () use ($enrollment) {
42
                return Course::whereId($enrollment->course_id)->first();
43
            });
44
45
            if ($course->group_max <= 0) {
46
                unset($enrollments[$enrollmentKey]);
47
                continue;
48
            }
49
50
            $membership = $student->findMembershipByCourse($course->id);
51
52
            if (is_null($membership)) {
53
                $enrollment->group_status = 0;
54
            } else {
55
                $group = DB::transaction(function () use ($membership) {
56
                    return Group::with('memberships')
57
                        ->whereId($membership->group_id)
58
                        ->first();
59
                });
60
61
                $enrollment->group_status = $group->memberships->count();
62
            }
63
64
            $enrollment->name = $course->name;
65
            $enrollment->group_min = $course->group_min;
66
            $enrollment->group_max = $course->group_max;
67
68
            $enrollment->number_invitations = Invitation::ofStudentInCourse(
69
                    $student->student_number,
70
                    $enrollment->course_id
71
                )->count();
72
        }
73
74
        return view('groups.index', compact('enrollments'));
75
    }
76
77
    /**
78
     * Store a newly created resource in storage.
79
     *
80
     * @param $courseId
81
     *
82
     * @return \Illuminate\Http\RedirectResponse
83
     */
84
    public function store($courseId)
85
    {
86
        $group = DB::transaction(function () use ($courseId) {
87
            if (! Course::find($courseId)->exists()) {
88
                return redirect()->back();
89
            }
90
91
            $group = new Group();
92
            $group->course_id = $courseId;
93
            $group->save();
94
95
            return $group;
96
        });
97
98
        try {
99
            Auth::student()->join($group);
100
101
            flash('You have successfully joined a group.')->success();
102
        } catch (UserHasAlreadyGroupInCourseException $e) {
103
            flash('You already have a group.')->error();
104
            $group->delete();
105
        }
106
107
        return redirect()->route('groups.show', compact('courseId'));
108
    }
109
110
    /**
111
     * Display the specified resource.
112
     *
113
     * @param int $courseId
114
     *
115
     * @return \Illuminate\View\View
116
     */
117
    public function show($courseId)
118
    {
119
        $student = Auth::student();
120
121
        $course = DB::transaction(function () use ($courseId, $student) {
122
            $course = Course::whereId($courseId)->first();
123
124
            $course->number_invitations = Invitation::ofStudentInCourse(
0 ignored issues
show
Bug introduced by
The property number_invitations 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...
125
                    $student->student_number,
126
                    $courseId
127
                )->count();
128
129
            return $course;
130
        });
131
132
        $membership = DB::transaction(function () use ($courseId, $student) {
133
            return $student->memberships()
134
                ->with('group.memberships.student.user')
135
                ->whereCourseId($courseId)
136
                ->first();
137
        });
138
139
        return view('groups.show', compact('course', 'membership'));
140
    }
141
142
    /**
143
     * Update the specified resource in storage.
144
     *
145
     * @param int $inviteId
146
     *
147
     * @return \Illuminate\Http\RedirectResponse
148
     */
149
    public function update($inviteId)
150
    {
151
        $invitation = DB::transaction(function () use ($inviteId) {
152
            return Invitation::with(['group.memberships', 'group.course'])
153
                ->whereId($inviteId)
154
                ->first();
155
        });
156
157
        $numberOfGroupMembers = $invitation->group->memberships->count();
158
        $groupMaxSize = $invitation->group->course->group_max;
159
160
        if ($numberOfGroupMembers >= $groupMaxSize) {
161
            flash('Course group limit exceeded')->error();
162
163
            return redirect()->back();
164
        }
165
166
        try {
167
            Auth::student()->join($invitation->group);
168
169
            flash('You have successfully joined the group.')
170
                ->success();
171
172
            return redirect()->route('invitations.destroy', compact('inviteId'));
173
        } catch (UserHasAlreadyGroupInCourseException $e) {
174
            flash('You already have a group.')
175
                ->error();
176
177
            $courseId = $invitation->course_id;
178
179
            return redirect()->route('groups.show', compact('courseId'));
180
        }
181
    }
182
183
    /**
184
     * Remove the specified resource from storage.
185
     *
186
     * @param int $courseId
187
     *
188
     * @return \Illuminate\Http\RedirectResponse
189
     */
190
    public function destroy($courseId)
191
    {
192
        $group = Auth::student()->memberships()->get()
193
            ->where('course_id', '=', $courseId)->first()
194
            ->group()->first();
195
196
        Auth::student()->leave($group);
197
198
        if (! $group->memberships()->count()) {
199
            $invitations = Invitation::whereGroupId($group->id)->get();
200
201
            foreach ($invitations as $invitation) {
202
                $invitation->delete();
203
            }
204
205
            $group->delete();
206
            flash('Group deleted.')->success();
207
        } else {
208
            flash('You have successfully left the group.')->success();
209
        }
210
211
        return redirect()->route('groups.show', compact('courseId'));
212
    }
213
}
214