Completed
Push — master ( 2f17a8...b73b0e )
by Matt
14:14
created

QuizController::activate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\Models\Quiz;
7
use App\Models\User;
8
use Illuminate\Database\Eloquent\ModelNotFoundException;
9
use Illuminate\Support\Facades\DB;
10
use Illuminate\Support\Facades\Auth;
11
use Validator;
12
use App\Repositories\Repository;
13
14
/**
15
 * @TODO separate controllers for host and user. 
16
 * implement repositories.
17
 */
18
19
class QuizController extends Controller
20
{
21
22
    protected $model;
23
24
    public function __construct(Quiz $quiz)
25
    {
26
        $this->model = new Repository($quiz);
27
    }
28
29
    public function showIndexWelcome()
30
    {
31
        return view('index.welcome');
32
    }
33
34
    public function showHostDashboard()
35
    {
36
        // Move to trait/facade/logic class @TODO
37
        $quizCount = DB::table('user_quizzes')->where('user_id', Auth::user()->id)->count();
0 ignored issues
show
Bug introduced by
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...
38
        return view('quiz_host.dashboard.index')->with('quizCount', $quizCount);
39
    }
40
41
    public function showHostManageQuizzes()
42
    {
43
        $quizzes = Quiz::leftJoin('user_quizzes', 'user_quizzes.quiz_id', 'quizzes.id')
44
                        ->join('users', 'user_quizzes.user_id', '=', 'users.id')
45
                        ->where('users.id', Auth::user()->id)
0 ignored issues
show
Bug introduced by
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...
46
                        ->get();
47
48
        return view('quiz_host.dashboard.manage-quizzes')->with('quizzes', $quizzes);
49
    }
50
51
    public function create()
52
    {
53
        return view('quiz_host.dashboard.quiz.create');
54
    }
55
56
    public function store(Request $request)
57
    {
58
        // @TODO use validator facade over $request->validate();
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59
        $validator = $request->validate([
0 ignored issues
show
Unused Code introduced by
The assignment to $validator is dead and can be removed.
Loading history...
60
            'quiz_name'        => 'required|max:30',
61
            'quiz_description' => 'required|max:500'
62
        ]);
63
64
        $quiz = new Quiz;
65
        $quiz->quiz_name = $request->get('quiz_name');
66
        $quiz->quiz_description = $request->get('quiz_description');
67
        $quiz->active = '0';
68
        $quiz->quiz_pin = '5555';
69
        $quiz->save();
70
71
        $user = Auth::user();
72
        $quiz->user()->attach($user);
73
74
        return redirect()->route('quiz_host.dashboard.manage-quizzes')->with('quizCreated', 'Whoa ' . Auth::user()->username . ', you have created a quiz! Now it\'s time to add some questions');
0 ignored issues
show
Bug introduced by
Accessing username on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
75
76
    }
77
78
    public function edit($id) 
79
    {
80
        try {
81
            $quiz = Quiz::findOrFail($id);
82
        } catch (ModelNotFoundException $e) {
83
            return redirect()->route('quiz_host.dashboard.manage-quizzes');
84
        }
85
86
        return view('quiz_host.dashboard.quiz.edit')->with('quiz', $quiz);
87
    }
88
89
    public function activate($id) 
90
    {
91
        try {
92
            $quiz = Quiz::findOrFail($id);
0 ignored issues
show
Unused Code introduced by
The assignment to $quiz is dead and can be removed.
Loading history...
93
        } catch (ModelNotFoundException $e) {
94
            return redirect()->route('quiz_host.dashboard.manage-quizzes');
95
        }
96
    }
97
98
    public function destroy($id) 
99
    {
100
        try {
101
            $quiz = Quiz::findOrFail($id);
102
        } catch (ModelNotFoundException $e) {
103
            return redirect()->route('quiz_host.dashboard.manage-quizzes');
104
        }
105
106
        $quiz->delete();
107
        $quiz->user()->detach(Auth::user());
108
        return redirect()->route('quiz_host.dashboard.manage-quizzes')->with('quizDeleted', 'Quiz succesfully deleted.');
109
110
    }
111
112
    public function showQuestion()
113
    {
114
        return view('quiz_user.question');
115
    }
116
117
    public function showPin()
118
    {
119
        return view('quiz_user.pin');
120
    }
121
122
    public function leaderboards()
123
    {
124
        return view('quiz_user.leaderboard');
125
    }
126
127
128
    public function showSplash()
129
    {
130
        return view('quiz_user.splash');
131
    }
132
133
    public function enterQuizPin(Request $request)
134
    {
135
        // @TODO - PIN max length needs to be dynamic with trait
136
        // will sort out soon - Matt.
137
        $validatedData = $request->validate([
0 ignored issues
show
Unused Code introduced by
The assignment to $validatedData is dead and can be removed.
Loading history...
138
            'pin'      => 'required|string'
139
        ]);
140
141
        try {
142
            $quiz = Quiz::where('quiz_pin', $request->get('pin'))
143
                    ->where('active', '1')
144
                    ->firstOrFail();
145
        } catch (ModelNotFoundException $e) {
146
            return back()->with('pin_error', 'Either the quiz doesn\'t exist, or it hasn\'t been activated');
147
        }
148
149
        //Add user to the socket.io stuff!
150
        event(new \App\Events\addUser($request->get('username'))); //Adds the user to the socket stuff
151
        return redirect()->route('quiz_user.showSplash')->with('quizData', $quiz);
152
    }
153
}
154