added functionality for multi answer questions and 2 different question types
This commit is contained in:
parent
a1af585133
commit
d4d634363d
9 changed files with 122 additions and 16 deletions
app/Http/Controllers
database
factories
migrations
seeds
resources/views/tests
|
@ -34,14 +34,10 @@ class TestController extends Controller
|
|||
|
||||
public function answerQuestion()
|
||||
{
|
||||
dd(request()->all());
|
||||
$question = session('questions')->get(session('question_counter')-1);
|
||||
$question_id = $question->id;
|
||||
$answer_id = request()->get('answer');
|
||||
if (!empty(session('answers'))) {
|
||||
$answers = session('answers');
|
||||
}
|
||||
$answers["$question_id"] = $answer_id;
|
||||
session(['answers' => $answers]);
|
||||
|
||||
if (!$question->isCorrect(session('answers')["$question->id"])) {
|
||||
session(['wrong_answers' => session('wrong_answers')+1]);
|
||||
|
@ -165,20 +161,55 @@ class TestController extends Controller
|
|||
$question = new Question;
|
||||
$question->title = request()->title;
|
||||
$question->question = request()->question;
|
||||
if (empty(Option::all()->last()->id)) {
|
||||
$question->answer_id = 1;
|
||||
$question->question_type = request()->question_type;
|
||||
if (request()->multiple_answers_question == null) {
|
||||
$question->multiple_answers_question = 0;
|
||||
} else {
|
||||
$question->answer_id = Option::all()->last()->id+1;
|
||||
$question->multiple_answers_question = request()->multiple_answers_question;
|
||||
}
|
||||
$correct_answers = 0;
|
||||
if (request()->correct_answer1 == 1) {
|
||||
$correct_answers++;
|
||||
}
|
||||
if (request()->correct_answer2 == 1) {
|
||||
$correct_answers++;
|
||||
}
|
||||
if (request()->correct_answer3 == 1) {
|
||||
$correct_answers++;
|
||||
}
|
||||
if (request()->correct_answer4 == 1) {
|
||||
$correct_answers++;
|
||||
}
|
||||
$question->correct_answers = $correct_answers;
|
||||
$test->questions()->save($question);
|
||||
$option1 = new Option;
|
||||
$option1->option = request()->option1;
|
||||
if (request()->correct_answer1 == null) {
|
||||
$option1->correct_answer = 0;
|
||||
} else {
|
||||
$option1->correct_answer = request()->correct_answer1;
|
||||
}
|
||||
$option2 = new Option;
|
||||
$option2->option = request()->option2;
|
||||
if (request()->correct_answer2 == null) {
|
||||
$option2->correct_answer = 0;
|
||||
} else {
|
||||
$option2->correct_answer = request()->correct_answer2;
|
||||
}
|
||||
$option3 = new Option;
|
||||
$option3->option = request()->option3;
|
||||
if (request()->correct_answer3 == null) {
|
||||
$option3->correct_answer = 0;
|
||||
} else {
|
||||
$option3->correct_answer = request()->correct_answer3;
|
||||
}
|
||||
$option4 = new Option;
|
||||
$option4->option = request()->option4;
|
||||
if (request()->correct_answer4 == null) {
|
||||
$option4->correct_answer = 0;
|
||||
} else {
|
||||
$option4->correct_answer = request()->correct_answer4;
|
||||
}
|
||||
$question->options()->save($option1);
|
||||
$question->options()->save($option2);
|
||||
$question->options()->save($option3);
|
||||
|
@ -189,12 +220,53 @@ class TestController extends Controller
|
|||
public function updateQuestion(Question $question)
|
||||
{
|
||||
$test = $question->test;
|
||||
$question->title = request()->title;
|
||||
$question->question = request()->question;
|
||||
$question->question_type = request()->question_type;
|
||||
if (request()->multiple_answers_question == null) {
|
||||
$question->multiple_answers_question = 0;
|
||||
} else {
|
||||
$question->multiple_answers_question = request()->multiple_answers_question;
|
||||
}
|
||||
$correct_answers = 0;
|
||||
if (request()->correct_answer1 == 1) {
|
||||
$correct_answers++;
|
||||
}
|
||||
if (request()->correct_answer2 == 1) {
|
||||
$correct_answers++;
|
||||
}
|
||||
if (request()->correct_answer3 == 1) {
|
||||
$correct_answers++;
|
||||
}
|
||||
if (request()->correct_answer4 == 1) {
|
||||
$correct_answers++;
|
||||
}
|
||||
$question->correct_answers = $correct_answers;
|
||||
$options = $question->options;
|
||||
$options[0]->option = request()->option1;
|
||||
if (request()->correct_answer1 == null) {
|
||||
$options[0]->correct_answer = 0;
|
||||
} else {
|
||||
$options[0]->correct_answer = request()->correct_answer1;
|
||||
}
|
||||
$options[1]->option = request()->option2;
|
||||
if (request()->correct_answer2 == null) {
|
||||
$options[1]->correct_answer = 0;
|
||||
} else {
|
||||
$options[1]->correct_answer = request()->correct_answer2;
|
||||
}
|
||||
$options[2]->option = request()->option3;
|
||||
if (request()->correct_answer3 == null) {
|
||||
$options[2]->correct_answer = 0;
|
||||
} else {
|
||||
$options[2]->correct_answer = request()->correct_answer3;
|
||||
}
|
||||
$options[3]->option = request()->option4;
|
||||
if (request()->correct_answer4 == null) {
|
||||
$options[3]->correct_answer = 0;
|
||||
} else {
|
||||
$options[3]->correct_answer = request()->correct_answer4;
|
||||
}
|
||||
$question->update();
|
||||
$options[0]->update();
|
||||
$options[1]->update();
|
||||
|
|
|
@ -44,7 +44,9 @@ $factory->define(App\Question::class, function (Faker\Generator $faker) {
|
|||
return [
|
||||
'title' => $faker->sentence(6, true),
|
||||
'question' => $faker->paragraph(3, true),
|
||||
'question_type' => "radio"
|
||||
'question_type' => "radio",
|
||||
'correct_answers' => 1,
|
||||
'multiple_answers_question' => 0
|
||||
];
|
||||
});
|
||||
|
||||
|
|
|
@ -19,6 +19,8 @@ class CreateQuestionsTable extends Migration
|
|||
$table->string('title');
|
||||
$table->text('question');
|
||||
$table->string('question_type');
|
||||
$table->integer('correct_answers')->unsigned();
|
||||
$table->integer('multiple_answers_question')->unsigned();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ class CreateOptionsTable extends Migration
|
|||
$table->increments('id');
|
||||
$table->integer('question_id')->unsigned()->index();
|
||||
$table->string('option');
|
||||
$table->integer('correct_answer');
|
||||
$table->integer('correct_answer')->unsigned();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
|
|
@ -18,12 +18,14 @@ class DatabaseSeeder extends Seeder
|
|||
'name' => 'Magnus Walbeck',
|
||||
'email' => 'admin@walbeck.dk',
|
||||
'password' => bcrypt('magnus'),
|
||||
'group_id' => 1,
|
||||
'access_level' => 3,
|
||||
]));
|
||||
$g->users()->save(factory(App\User::class)->create([
|
||||
'name' => 'Mod',
|
||||
'email' => 'mod@walbeck.dk',
|
||||
'password' => bcrypt('magnus'),
|
||||
'group_id' => 1,
|
||||
'access_level' => 2,
|
||||
]));
|
||||
$g->tests()->save(factory(App\Test::class)->make());
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
<form method="post" action="/test/question">
|
||||
{{ csrf_field() }}
|
||||
@foreach ($options as $option)
|
||||
<input type="radio" name="answer" value="{{ $option->id }}"> {{ $option->option }}
|
||||
<input type="{{ $question->question_type }}" name="answer{{ $option->id }}" value="{{ $option->id }}"> {{ $option->option }}
|
||||
</br>
|
||||
@endforeach
|
||||
<button type="submit" class="btn btn-default">Submit</button>
|
||||
|
|
|
@ -4,30 +4,46 @@
|
|||
|
||||
<div class="container-fluid">
|
||||
<div class="col-md-4 col-md-offset-4">
|
||||
<h1>Edit User</h1>
|
||||
<h1>Edit Question</h1>
|
||||
</br>
|
||||
<form method="POST" action="/{{ Auth::user()->getAdminPath() }}/questions/{{ $question->id }}">
|
||||
{{ csrf_field() }}
|
||||
{{ method_field('PATCH') }}
|
||||
<div class="form-group">
|
||||
<label>Title</label>
|
||||
<input type="text" class="form-control" name="title" value="{{ $question->title }}">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Question Type</label>
|
||||
<select name="question_type" class="form-control">
|
||||
<option value="radio">Radio Buttons</option>
|
||||
<option value="checkbox">Checkboxes</option>
|
||||
</select>
|
||||
<input type="checkbox" name="multiple_answers_question" value="1" @if ($question->correct_answer == 1) checked @endif> Multiple Correct Answers
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Question</label>
|
||||
<input type="text" class="form-control" name="question" value="{{ $question->question }}">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Right Answer</label>
|
||||
<label>Option 1</label>
|
||||
<input type="text" class="form-control" name="option1" value="{{ $options[0]->option }}">
|
||||
<input type="checkbox" name="correct_answer1" value="1" @if ($options[0]->correct_answer == 1) checked @endif> Correct Answer
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Option 2</label>
|
||||
<input type="text" class="form-control" name="option2" value="{{ $options[1]->option }}">
|
||||
<input type="checkbox" name="correct_answer2" value="1" @if ($options[1]->correct_answer == 1) checked @endif> Correct Answer
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Option 3</label>
|
||||
<input type="text" class="form-control" name="option3" value="{{ $options[2]->option }}">
|
||||
<input type="checkbox" name="correct_answer3" value="1" @if ($options[2]->correct_answer == 1) checked @endif> Correct Answer
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Option 4</label>
|
||||
<input type="text" class="form-control" name="option4" value="{{ $options[3]->option }}">
|
||||
<input type="checkbox" name="correct_answer4" value="1" @if ($options[3]->correct_answer == 1) checked @endif> Correct Answer
|
||||
</div>
|
||||
<button type="submit" class="btn btn-default">Submit</button>
|
||||
</form>
|
||||
|
|
|
@ -11,25 +11,37 @@
|
|||
<label>Title</label>
|
||||
<input type="text" class="form-control" name="title">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Question Type</label>
|
||||
<select name="question_type" class="form-control">
|
||||
<option value="radio">Radio Buttons</option>
|
||||
<option value="checkbox">Checkboxes</option>
|
||||
</select>
|
||||
<input type="checkbox" name="multiple_answers_question" value="1"> Multiple Correct Answers
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Question</label>
|
||||
<input type="text" class="form-control" name="question">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Right Answer</label>
|
||||
<input type="text" class="form-control" name="option1">
|
||||
<label>Option 1</label>
|
||||
<input type="text" class="form-control" name="option1">
|
||||
<input type="checkbox" name="correct_answer1" value="1"> Correct Answer
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Option 2</label>
|
||||
<input type="text" class="form-control" name="option2">
|
||||
<input type="checkbox" name="correct_answer2" value="1"> Correct Answer
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Option 3</label>
|
||||
<input type="text" class="form-control" name="option3">
|
||||
<input type="checkbox" name="correct_answer3" value="1"> Correct Answer
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Option 4</label>
|
||||
<input type="text" class="form-control" name="option4">
|
||||
<input type="checkbox" name="correct_answer4" value="1"> Correct Answer
|
||||
</div>
|
||||
<button type="submit" class="btn btn-default">Submit</button>
|
||||
</form>
|
||||
|
|
|
@ -43,7 +43,7 @@
|
|||
<td>{{ $question->question }}</td>
|
||||
<td>
|
||||
<form method="get" class="pull-right">
|
||||
<button class="btn btn-sm btn-default pull-left" formaction="/{{ Auth::user()->getAdminPath() }}{{ Auth::user()->getAdminPath() }}/questions/{{ $question->id }}/edit">Edit</button>
|
||||
<button class="btn btn-sm btn-default pull-left" formaction="/{{ Auth::user()->getAdminPath() }}/questions/{{ $question->id }}/edit">Edit</button>
|
||||
<button class="btn btn-sm btn-default pull-left" formaction="/{{ Auth::user()->getAdminPath() }}/questions/{{ $question->id }}/delete">Delete</button>
|
||||
</form>
|
||||
</td>
|
||||
|
|
Reference in a new issue