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
resources/views/tests
|
@ -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'));
|
||||
}
|
||||
}
|
||||
|
|
34
app/User.php
34
app/User.php
|
@ -41,4 +41,38 @@ class User extends Authenticatable
|
|||
{
|
||||
return $this->belongsTo(Company::class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function updateInfo($requestAll)
|
||||
{
|
||||
$this->name = $requestAll["name"];
|
||||
$this->email = $requestAll["email"];
|
||||
if (!empty($requestAll["password"])) {
|
||||
$this->password = bcrypt($requestAll["password"]);
|
||||
}
|
||||
$this->update();
|
||||
return true;
|
||||
}
|
||||
|
||||
public function deleteUser()
|
||||
{
|
||||
if ($this->id == \Auth::user()->id) {
|
||||
$this->delete();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (\Auth::user()->company_id == $this->company_id && \Auth::user()->access_level >= 2) {
|
||||
$this->delete();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (\Auth::user()->access_level >= 3) {
|
||||
$this->delete();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
25
resources/views/tests/answer.blade.php
Normal file
25
resources/views/tests/answer.blade.php
Normal file
|
@ -0,0 +1,25 @@
|
|||
@extends('layouts.test')
|
||||
|
||||
@section('content')
|
||||
|
||||
<div class="container-fluid">
|
||||
<div class="col-md-6 col-md-offset-3">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">Answer</div>
|
||||
<div class="panel-body">
|
||||
<h4><strong>{{ $question->title }}</strong></h4>
|
||||
<p>{{ $question->question }}</p>
|
||||
<form method="post" action="/test/answer">
|
||||
{{ csrf_field() }}
|
||||
@foreach ($shuffled_options as $option)
|
||||
<input type="radio" name="answer" value="{{ $option->id }}"> {{ $option->option }}
|
||||
</br>
|
||||
@endforeach
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@stop
|
|
@ -1,20 +0,0 @@
|
|||
@extends('layouts.test')
|
||||
|
||||
@section('content')
|
||||
|
||||
<div class="container-fluid">
|
||||
<div class="col-md-6 col-md-offset-3">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">Question</div>
|
||||
<div class="panel-body">
|
||||
<h4><strong>WRONG</strong></h4>
|
||||
<form method="get" action="/test/question">
|
||||
{{ csrf_field() }}
|
||||
<input type="submit" value="Next">
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@stop
|
|
@ -5,7 +5,7 @@
|
|||
<div class="container-fluid">
|
||||
<div class="col-md-6 col-md-offset-3">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">Question Number</div>
|
||||
<div class="panel-heading">Question</div>
|
||||
<div class="panel-body">
|
||||
<h4><strong>{{ $question->title }}</strong></h4>
|
||||
<p>{{ $question->question }}</p>
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
@extends('layouts.test')
|
||||
|
||||
@section('content')
|
||||
|
||||
<div class="container-fluid">
|
||||
<div class="col-md-6 col-md-offset-3">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">Question</div>
|
||||
<div class="panel-body">
|
||||
<h4><strong>TRUE</strong></h4>
|
||||
<form method="get" action="/test/question">
|
||||
{{ csrf_field() }}
|
||||
<input type="submit" value="Next">
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@stop
|
Reference in a new issue