started on logic for taking tests

This commit is contained in:
mwalbeck 2016-09-07 18:26:58 +02:00
parent 74cbeb2eb8
commit 3a8ec97891
7 changed files with 84 additions and 11 deletions

View file

@ -8,15 +8,43 @@ use App\Test;
class TestController extends Controller
{
public function test(Test $test)
public function startTest(Test $test)
{
$test_id = $test->id;
$question_count = 1;
$questions = $test->questions;
$options = $questions[0]->options;
return view('tests.index', compact('questions'), compact('options'));
$rand_questions = $questions->random($test->question_count);
$shuffled_questions = $rand_questions->shuffle();
session(['questions' => $shuffled_questions, 'question_count' => $question_count, 'test_id' => $test_id]);
return redirect()->action('TestController@showQuestion');
}
public function answer()
public function showQuestion()
{
echo "Hello";
$question_count = session('question_count');
$questions = session('questions');
$question = $questions->get($question_count-1);
$options = $question->options;
$shuffled_options = $options->shuffle();
session(['options' => $shuffled_options]);
return view('tests.index', compact('question'), compact('shuffled_options'));
}
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');
}
return view('tests.true');
}
public function showAnswer()
{
}
}

View file

@ -16,6 +16,7 @@ class CreateQuestionsTable extends Migration
Schema::create('questions', function (Blueprint $table) {
$table->increments('id');
$table->integer('test_id')->unsigned()->index();
$table->string('title');
$table->text('question');
$table->integer('answer_id')->unsigned()->index();
$table->timestamps();

View file

@ -19,7 +19,7 @@
<td>{{ $test->title }}</td>
<td>
<form method="get" class="pull-right">
<button class="btn btn-xs btn-default pull-left" formaction="/admin/questions/{{ $test->id }}/edit">Start</button>
<button class="btn btn-xs btn-default pull-left" formaction="/test/{{ $test->id }}">Start</button>
</form>
</td>
</tr>

View file

@ -0,0 +1,20 @@
@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

View file

@ -7,9 +7,11 @@
<div class="panel panel-default">
<div class="panel-heading">Question Number</div>
<div class="panel-body">
<h4><strong>{{ $questions[1-1]->title }}</strong></h4>
<form method="post" action="/test/answer">
@foreach ($options as $option)
<h4><strong>{{ $question->title }}</strong></h4>
<p>{{ $question->question }}</p>
<form method="post" action="/test/question">
{{ csrf_field() }}
@foreach ($shuffled_options as $option)
<input type="radio" name="answer" value="{{ $option->id }}"> {{ $option->option }}
</br>
@endforeach

View file

@ -0,0 +1,20 @@
@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

View file

@ -20,8 +20,10 @@ Route::get('/stats', 'HomeController@stats');
Route::get('/settings', 'HomeController@settings');
Route::patch('/settings/update', 'HomeController@updateSettings');
Route::get('/test/{test}', 'TestController@test');
Route::get('/test/answer', 'TestController@answer');
Route::get('/test/question', 'TestController@showQuestion');
Route::post('/test/question', 'TestController@answerQuestion');
Route::get('/test/answer', 'TestController@showAnswer');
Route::get('/test/{test}', 'TestController@startTest');
Route::get('/admin', 'AdminController@index');