added new, edit and delete functionality for questions
This commit is contained in:
parent
f36e6a2c9d
commit
d50b6214ed
8 changed files with 214 additions and 10 deletions
app/Http/Controllers
|
@ -7,6 +7,8 @@ use App\Http\Requests;
|
|||
use App\Test;
|
||||
use App\Group;
|
||||
use App\User;
|
||||
use App\Question;
|
||||
use App\Option;
|
||||
|
||||
class AdminController extends Controller
|
||||
{
|
||||
|
@ -29,7 +31,8 @@ class AdminController extends Controller
|
|||
|
||||
public function showTest(Test $test)
|
||||
{
|
||||
return view('admin.show.test', compact('test'));
|
||||
$questions = $test->questions;
|
||||
return view('admin.show.test', compact('test'), compact('questions'));
|
||||
}
|
||||
|
||||
public function test()
|
||||
|
@ -58,7 +61,7 @@ class AdminController extends Controller
|
|||
public function updateTest(Test $test)
|
||||
{
|
||||
$test->update(request()->all());
|
||||
return redirect('/admin/tests');
|
||||
return back();
|
||||
}
|
||||
|
||||
public function delTest(Test $test)
|
||||
|
@ -130,4 +133,72 @@ class AdminController extends Controller
|
|||
$user->save();
|
||||
return redirect('/admin/users');
|
||||
}
|
||||
|
||||
public function question(Test $test)
|
||||
{
|
||||
$question_number = count($test->questions)+1;
|
||||
return view('admin.new.question', compact('test'), compact('question_number'));
|
||||
}
|
||||
|
||||
public function addQuestion(Test $test)
|
||||
{
|
||||
$question = new Question;
|
||||
$question->question = request()->question;
|
||||
if (empty(Option::all()->last()->id)) {
|
||||
$question->answer_id = 1;
|
||||
} else {
|
||||
$question->answer_id = Option::all()->last()->id+1;
|
||||
}
|
||||
$test->questions()->save($question);
|
||||
$option1 = new Option;
|
||||
$option1->option = request()->option1;
|
||||
$option2 = new Option;
|
||||
$option2->option = request()->option2;
|
||||
$option3 = new Option;
|
||||
$option3->option = request()->option3;
|
||||
$option4 = new Option;
|
||||
$option4->option = request()->option4;
|
||||
$question->options()->save($option1);
|
||||
$question->options()->save($option2);
|
||||
$question->options()->save($option3);
|
||||
$question->options()->save($option4);
|
||||
return redirect("/admin/tests/");
|
||||
}
|
||||
|
||||
public function delQuestion(Question $question)
|
||||
{
|
||||
return view('admin.delete.question', compact('question'));
|
||||
}
|
||||
|
||||
public function deleteQuestion(Question $question)
|
||||
{
|
||||
$options = $question->options;
|
||||
foreach ($options as $option) {
|
||||
$option->delete();
|
||||
}
|
||||
$question->delete();
|
||||
return redirect('/admin/tests');
|
||||
}
|
||||
|
||||
public function editQuestion(Question $question)
|
||||
{
|
||||
$options = $question->options;
|
||||
return view('admin.edit.question', compact('question'), compact('options'));
|
||||
}
|
||||
|
||||
public function updateQuestion(Question $question)
|
||||
{
|
||||
$question->question = request()->question;
|
||||
$options = $question->options;
|
||||
$options[0]->option = request()->option1;
|
||||
$options[1]->option = request()->option2;
|
||||
$options[2]->option = request()->option3;
|
||||
$options[3]->option = request()->option4;
|
||||
$question->update();
|
||||
$options[0]->update();
|
||||
$options[1]->update();
|
||||
$options[2]->update();
|
||||
$options[3]->update();
|
||||
return redirect('/admin/tests');
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue