refactored a bit of code used for create of questions and options
This commit is contained in:
parent
f010bbf128
commit
711dd87d32
4 changed files with 92 additions and 119 deletions
app
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Answer extends Model
|
||||
{
|
||||
//
|
||||
}
|
|
@ -177,127 +177,30 @@ class TestController extends Controller
|
|||
return view('tests.edit', compact('test'));
|
||||
}
|
||||
|
||||
/* REFACTOR */
|
||||
public function addQuestion(Test $test)
|
||||
public function addQuestion(Test $test, Request $request)
|
||||
{
|
||||
$question = new Question;
|
||||
$question->title = request()->title;
|
||||
$question->question = request()->question;
|
||||
if (request()->multiple_answers_question == null) {
|
||||
$question->multiple_answers_question = 0;
|
||||
$question->question_type = "radio";
|
||||
$question->correct_answers = 1;
|
||||
} else {
|
||||
$question->multiple_answers_question = request()->multiple_answers_question;
|
||||
$question->question_type = "checkbox";
|
||||
$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);
|
||||
$question->addQuestion($test, $request);
|
||||
$option1 = new Option;
|
||||
$option1->option = request()->option1;
|
||||
if (request()->correct_answer1 == null) {
|
||||
$option1->correct_answer = 0;
|
||||
} else {
|
||||
$option1->correct_answer = request()->correct_answer1;
|
||||
}
|
||||
$option1->addOption($question, $request, 1);
|
||||
$option2 = new Option;
|
||||
$option2->option = request()->option2;
|
||||
if (request()->correct_answer2 == null) {
|
||||
$option2->correct_answer = 0;
|
||||
} else {
|
||||
$option2->correct_answer = request()->correct_answer2;
|
||||
}
|
||||
$option2->addOption($question, $request, 2);
|
||||
$option3 = new Option;
|
||||
$option3->option = request()->option3;
|
||||
if (request()->correct_answer3 == null) {
|
||||
$option3->correct_answer = 0;
|
||||
} else {
|
||||
$option3->correct_answer = request()->correct_answer3;
|
||||
}
|
||||
$option3->addOption($question, $request, 3);
|
||||
$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);
|
||||
$question->options()->save($option4);
|
||||
$option4->addOption($question, $request, 4);
|
||||
return redirect("/admin/tests/$test->id");
|
||||
}
|
||||
|
||||
public function updateQuestion(Question $question)
|
||||
public function updateQuestion(Question $question, Request $request)
|
||||
{
|
||||
$test = $question->test;
|
||||
$question->title = request()->title;
|
||||
$question->question = request()->question;
|
||||
if (request()->multiple_answers_question == null) {
|
||||
$question->multiple_answers_question = 0;
|
||||
$question->question_type = "radio";
|
||||
$question->correct_answers = 1;
|
||||
} else {
|
||||
$question->multiple_answers_question = request()->multiple_answers_question;
|
||||
$question->question_type = "checkbox";
|
||||
$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;
|
||||
}
|
||||
$question->updateQuestion($request);
|
||||
$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();
|
||||
$options[2]->update();
|
||||
$options[3]->update();
|
||||
$options[0]->updateOption($request, 1);
|
||||
$options[1]->updateOption($request, 2);
|
||||
$options[2]->updateOption($request, 3);
|
||||
$options[3]->updateOption($request, 4);
|
||||
return redirect("/admin/tests/$test->id");
|
||||
}
|
||||
|
||||
|
|
|
@ -16,4 +16,26 @@ class Option extends Model
|
|||
$this->delete();
|
||||
return true;
|
||||
}
|
||||
|
||||
public function addOption($question, $request, $nr)
|
||||
{
|
||||
$this->option = $request->get("option{$nr}");
|
||||
if ($request->get("correct_answer{$nr}") == null) {
|
||||
$this->correct_answer = 0;
|
||||
} else {
|
||||
$this->correct_answer = $request->get("correct_answer{$nr}");
|
||||
}
|
||||
$question->options()->save($this);
|
||||
}
|
||||
|
||||
public function updateOption($request, $nr)
|
||||
{
|
||||
$this->option = $request->get("option{$nr}");
|
||||
if ($request->get("correct_answer{$nr}") == null) {
|
||||
$this->correct_answer = 0;
|
||||
} else {
|
||||
$this->correct_answer = $request->get("correct_answer{$nr}");
|
||||
}
|
||||
$this->update();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,4 +35,62 @@ class Question extends Model
|
|||
$this->delete();
|
||||
return true;
|
||||
}
|
||||
|
||||
public function addQuestion($test, $request)
|
||||
{
|
||||
$this->title = $request->title;
|
||||
$this->question = $request->question;
|
||||
if ($request->multiple_answers_question == null) {
|
||||
$this->multiple_answers_question = 0;
|
||||
$this->question_type = "radio";
|
||||
$this->correct_answers = 1;
|
||||
} else {
|
||||
$this->multiple_answers_question = $request->multiple_answers_question;
|
||||
$this->question_type = "checkbox";
|
||||
$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++;
|
||||
}
|
||||
$this->correct_answers = $correct_answers;
|
||||
}
|
||||
$test->questions()->save($this);
|
||||
}
|
||||
|
||||
public function updateQuestion($request)
|
||||
{
|
||||
$this->title = $request->title;
|
||||
$this->question = $request->question;
|
||||
if ($request->multiple_answers_question == null) {
|
||||
$this->multiple_answers_question = 0;
|
||||
$this->question_type = "radio";
|
||||
$this->correct_answers = 1;
|
||||
} else {
|
||||
$this->multiple_answers_question = $request->multiple_answers_question;
|
||||
$this->question_type = "checkbox";
|
||||
$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++;
|
||||
}
|
||||
$this->correct_answers = $correct_answers;
|
||||
}
|
||||
$this->update();
|
||||
}
|
||||
}
|
Reference in a new issue