finished basic test taking functionality
This commit is contained in:
parent
7987e74a6e
commit
75253b0b64
7 changed files with 40 additions and 45 deletions
app
resources/views
routes
|
@ -3,6 +3,7 @@
|
|||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use App\Http\Requests;
|
||||
use App\Test;
|
||||
|
||||
|
@ -10,10 +11,11 @@ class TestController extends Controller
|
|||
{
|
||||
public function startTest(Test $test)
|
||||
{
|
||||
if (!$test->startTest(\Auth::user())) {
|
||||
//session flash
|
||||
if (Auth::user()->testTaken(2)) {
|
||||
//insert session flash
|
||||
return redirect('/home');
|
||||
}
|
||||
session(['questions' => $test->randomizeQuestions(), 'question_counter' => 1, 'test' => $test, 'wrong_answers' => 0, 'is_correct' => false, 'has_failed' => false, 'last_question' => false]);
|
||||
return redirect()->action('TestController@showQuestion');
|
||||
}
|
||||
|
||||
|
@ -35,47 +37,29 @@ class TestController extends Controller
|
|||
}
|
||||
$answers["$question_id"] = $answer_id;
|
||||
session(['answers' => $answers]);
|
||||
return redirect()->action('TestController@isCorrect');
|
||||
}
|
||||
|
||||
public function isCorrect()
|
||||
{
|
||||
$question = session('questions')->get(session('question_counter')-1);
|
||||
if (!$question->isCorrect(session('answers')["$question->id"])) {
|
||||
session(['wrong_answers' => session('wrong_answers')+1]);
|
||||
session(['is_correct' => false]);
|
||||
} else {
|
||||
session(['is_correct' => true]);
|
||||
}
|
||||
return redirect()->action('TestController@hasFailed');
|
||||
}
|
||||
|
||||
public function hasFailed()
|
||||
{
|
||||
if (session('test')->hasFailed(session('wrong_answers'))) {
|
||||
//Abort logic
|
||||
return redirect('/home');
|
||||
session(['has_failed' => true]);
|
||||
}
|
||||
return redirect()->action('TestController@lastQuestion');
|
||||
}
|
||||
|
||||
public function lastQuestion()
|
||||
{
|
||||
/*if (session('test')->lastQuestion(session('question_counter'))) {
|
||||
|
||||
}*/
|
||||
if (session('test')->lastQuestion(session('question_counter'))) {
|
||||
session(['last_question' => true]);
|
||||
}
|
||||
return redirect()->action('TestController@showAnswer');
|
||||
}
|
||||
|
||||
public function showAnswer()
|
||||
{
|
||||
$question = session('questions')->get(session('question_counter')-1);
|
||||
if (session('answers')["$question->id"] == $question->answer_id) {
|
||||
$question_status = "GZ, that was the correct answer!";
|
||||
} else {
|
||||
$question_status = "Sorry, wrong answer, pal!";
|
||||
}
|
||||
$options = session('options');
|
||||
session(['question_counter' => session('question_counter')+1]);
|
||||
return view('tests.answer');
|
||||
return view('tests.answer', compact("question"), compact("options"));
|
||||
}
|
||||
}
|
||||
|
|
17
app/Test.php
17
app/Test.php
|
@ -18,15 +18,6 @@ class Test extends Model
|
|||
return $this->belongsTo(Company::class);
|
||||
}
|
||||
|
||||
public function startTest(User $user)
|
||||
{
|
||||
if ($user->testTaken($this->id)) {
|
||||
return false;
|
||||
}
|
||||
session(['questions' => $this->randomizeQuestions(), 'question_counter' => 1, 'test' => $this, 'wrong_answers' => 0]);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function randomizeQuestions()
|
||||
{
|
||||
return $this->questions->random($this->question_count)->shuffle();
|
||||
|
@ -39,4 +30,12 @@ class Test extends Model
|
|||
}
|
||||
return $this->question_count_to_fail <= $wrong_answers;
|
||||
}
|
||||
|
||||
public function lastQuestion($question_counter)
|
||||
{
|
||||
if ($this->question_count == $question_counter) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,7 +23,6 @@
|
|||
<td>{{ $user->name }} ({{ $user->email }})</td>
|
||||
<td>
|
||||
<form method="get" class="pull-right">
|
||||
<button class="btn btn-sm btn-default pull-left" formaction="/admin/users/{{ $user->id }}/edit">Edit</button>
|
||||
<button class="btn btn-sm btn-default pull-left" formaction="/admin/users/{{ $user->id }}/delete">Delete</button>
|
||||
</form>
|
||||
</td>
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
</button>
|
||||
|
||||
<!-- Branding Image -->
|
||||
<a class="navbar-brand" href="{{ url('/') }}">
|
||||
<a class="navbar-brand" href="{{ url('/home') }}">
|
||||
Elearning
|
||||
</a>
|
||||
</div>
|
||||
|
|
|
@ -5,13 +5,29 @@
|
|||
<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-heading">Question #{{ session("question_counter") }}</div>
|
||||
<div class="panel-body">
|
||||
<h4><strong>{{ "title" }}</strong></h4>
|
||||
<p>{{ "question" }}</p>
|
||||
<h4><strong>{{ $question->title }}</strong></h4>
|
||||
<p>{{ $question->question }}</p>
|
||||
<form method="get" action="/test/question">
|
||||
{{ csrf_field() }}
|
||||
<input type="submit" value="Submit">
|
||||
@foreach ($options as $option)
|
||||
@if ($option->id == $question->answer_id)
|
||||
<p>{{ $option->option }}/CORRECT</p>
|
||||
</br>
|
||||
@else
|
||||
<p>{{ $option->option }}</p>
|
||||
</br>
|
||||
@endif
|
||||
@endforeach
|
||||
@if (session("has_failed") == true)
|
||||
<button type="submit" class="btn btn-default" formaction="/test/{{ $question->test_id }}">Retry</button>
|
||||
<button type="submit" class="btn btn-default" formaction="/home">End</button>
|
||||
@elseif (session("last_question") == true)
|
||||
<button type="submit" class="btn btn-default" formaction="/home">End</button>
|
||||
@else
|
||||
<button type="submit" class="btn btn-default" formaction="/test/question">Next</button>
|
||||
@endif
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -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</div>
|
||||
<div class="panel-heading">Question #{{ session("question_counter") }}</div>
|
||||
<div class="panel-body">
|
||||
<h4><strong>{{ $question->title }}</strong></h4>
|
||||
<p>{{ $question->question }}</p>
|
||||
|
|
|
@ -21,9 +21,6 @@ Route::get('/settings', 'HomeController@settings');
|
|||
Route::patch('/settings/update/email', 'HomeController@updateEmail');
|
||||
Route::patch('/settings/update/password', 'HomeController@updatePassword');
|
||||
|
||||
Route::get('/test/question/iscorrect', 'TestController@isCorrect');
|
||||
Route::get('/test/question/hasfailed', 'TestController@hasFailed');
|
||||
Route::get('/test/question/lastquestion', 'TestController@lastQuestion');
|
||||
Route::get('/test/question', 'TestController@showQuestion');
|
||||
Route::post('/test/question', 'TestController@answerQuestion');
|
||||
Route::get('/test/answer', 'TestController@showAnswer');
|
||||
|
|
Reference in a new issue