more logic for test taking and started moving logic from controllers to models
This commit is contained in:
parent
3a8ec97891
commit
bc6183991d
7 changed files with 99 additions and 61 deletions
app/Http/Controllers
|
@ -98,12 +98,7 @@ class AdminController extends Controller
|
|||
|
||||
public function updateUser(User $user)
|
||||
{
|
||||
$user->name = request()->name;
|
||||
$user->email = request()->email;
|
||||
if (!empty(request()->password)) {
|
||||
$user->password = bcrypt(request()->password);
|
||||
}
|
||||
$user->update();
|
||||
$user->updateInfo(request()->all());
|
||||
return redirect('/admin/users');
|
||||
}
|
||||
|
||||
|
@ -114,7 +109,7 @@ class AdminController extends Controller
|
|||
|
||||
public function deleteUser(User $user)
|
||||
{
|
||||
$user->delete();
|
||||
$user->deleteUser();
|
||||
return redirect('/admin/users');
|
||||
}
|
||||
|
||||
|
|
|
@ -11,40 +11,64 @@ class TestController extends Controller
|
|||
public function startTest(Test $test)
|
||||
{
|
||||
$test_id = $test->id;
|
||||
$question_count = 1;
|
||||
$question_counter = 1;
|
||||
$wrong_answers = 0;
|
||||
$questions = $test->questions;
|
||||
$rand_questions = $questions->random($test->question_count);
|
||||
$shuffled_questions = $rand_questions->shuffle();
|
||||
session(['questions' => $shuffled_questions, 'question_count' => $question_count, 'test_id' => $test_id]);
|
||||
session(['questions' => $shuffled_questions, 'question_counter' => $question_counter, 'test_id' => $test_id, 'wrong_answers' => $wrong_answers]);
|
||||
return redirect()->action('TestController@showQuestion');
|
||||
}
|
||||
|
||||
public function showQuestion()
|
||||
{
|
||||
$question_count = session('question_count');
|
||||
$question_counter = session('question_counter');
|
||||
$questions = session('questions');
|
||||
$question = $questions->get($question_count-1);
|
||||
$question = $questions->get($question_counter-1);
|
||||
$options = $question->options;
|
||||
$shuffled_options = $options->shuffle();
|
||||
session(['options' => $shuffled_options]);
|
||||
return view('tests.index', compact('question'), compact('shuffled_options'));
|
||||
}
|
||||
|
||||
public function answerQuestion ()
|
||||
public function answerQuestion()
|
||||
{
|
||||
$question_count = session('question_count');
|
||||
$questions = session('questions');
|
||||
$question = $questions->get($question_count-1);
|
||||
$question_count++;
|
||||
session(['question_count' => $question_count]);
|
||||
if (request()->get('answer') != $question->answer_id) {
|
||||
return view('tests.false');
|
||||
$question_counter = session('question_counter');
|
||||
$question = $questions->get($question_counter-1);
|
||||
$question_id = $question->id;
|
||||
$answer_id = request()->get('answer');
|
||||
if (!empty(session('answers'))) {
|
||||
$answers = session('answers');
|
||||
}
|
||||
return view('tests.true');
|
||||
$answers["$question_id"] = $answer_id;
|
||||
session(['answers' => $answers]);
|
||||
return redirect()->action('TestController@showAnswer');
|
||||
}
|
||||
|
||||
public function hasFailed()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function lastQuestion()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function showAnswer()
|
||||
{
|
||||
|
||||
$question_counter = session('question_counter');
|
||||
$questions = session('questions');
|
||||
$question = $questions->get($question_counter-1);
|
||||
$question_answer = $question->answer_id;
|
||||
$question_counter++;
|
||||
if (request()->get('answer') == $question_answer) {
|
||||
$question_status = "GZ, that was the correct answer!";
|
||||
} else {
|
||||
$question_status = "Sorry, wrong answer, pal!";
|
||||
}
|
||||
session(['question_counter' => $question_counter]);
|
||||
return view('tests.answer', compact('question_status'), compact('question_answer'));
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue