Passed
Pull Request — develop (#11)
by
unknown
06:16
created

InvitationController::index()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 1
dl 0
loc 21
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 Illuminate\Http\Request;
7
use App\Judite\Models\Invitation;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Database\QueryException;
10
11
class InvitationController extends Controller
12
{
13
    /**
14
     * Create a new controller instance.
15
     */
16
    public function __construct()
17
    {
18
        $this->middleware('auth');
19
        $this->middleware('can.student');
20
        $this->middleware('student.verified');
21
        $this->middleware('can.group');
22
    }
23
24
    /**
25
     * Display a listing of the resource.
26
     *
27
     * @return \Illuminate\Http\Response
28
     */
29
    public function index($courseId)
30
    {
31
        $invitations = Invitation::where([
32
            ['student_number', '=', Auth::student()->student_number],
33
            ['course_id', '=', $courseId],
34
        ])->get();
35
36
        foreach ($invitations as $invitation) {
37
            $group = Group::whereId($invitation->group_id)->first();
38
            $memberships = $group->memberships()->get();
39
40
            $students = [];
41
            foreach ($memberships as $membershipKey => $membership) {
42
                $student = $membership->student()->first();
43
                $student->name = $student->user()->first()->name;
44
                array_push($students, $student);
45
            }
46
            $invitation->students = $students;
0 ignored issues
show
Bug introduced by
The property students does not exist on App\Judite\Models\Invitation. Did you mean student?
Loading history...
47
        }
48
49
        return view('invitations.index', compact('invitations'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('invitations...compact('invitations')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
50
    }
51
52
    /**
53
     * Store a newly created resource in storage.
54
     *
55
     * @param \Illuminate\Http\Request $request
56
     * @param $groupId
57
     * @param $courseId
58
     *
59
     * @return \Illuminate\Http\Response
60
     */
61
    public function store(Request $request, $groupId, $courseId)
62
    {
63
        $studentNumber = $request->input('student_number');
64
65
        if ($studentNumber == Auth::student()->student_number) {
66
            flash('You can not invite yourself.')
67
                ->error();
68
69
            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...
70
        }
71
72
        $invitation = new Invitation();
73
        $invitation->student_number = $studentNumber;
74
        $invitation->group_id = $groupId;
75
        $invitation->course_id = $courseId;
76
77
        try {
78
            $invitation->save();
79
            flash('Invitation successfully sent.')
80
                ->success();
81
        } catch (QueryException $e) {
82
            flash('User already invited.')
83
                ->error();
84
        }
85
86
        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...
87
    }
88
89
    /**
90
     * Remove the specified resource from storage.
91
     *
92
     * @param int $id
93
     */
94
    public function destroy($id)
95
    {
96
        $invitation = Invitation::whereId($id)->first();
97
98
        $courseId = $invitation->course_id;
99
100
        $invitation->delete();
101
102
        return redirect()->route('groups.show', compact('courseId'));
103
    }
104
}
105