188 lines
4.7 KiB
PHP
188 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Requests;
|
|
use App\Test;
|
|
use App\Group;
|
|
use App\User;
|
|
use App\Question;
|
|
use App\Option;
|
|
|
|
class AdminController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
}
|
|
|
|
|
|
public function index()
|
|
{
|
|
return view('admin.admin');
|
|
}
|
|
|
|
public function showUser()
|
|
{
|
|
$users = User::all();
|
|
return view('admin.show.users', compact('users'));
|
|
}
|
|
|
|
public function delUser(User $user)
|
|
{
|
|
return view('admin.delete.user', compact('user'));
|
|
}
|
|
|
|
public function deleteUser(User $user)
|
|
{
|
|
$user->deleteUser();
|
|
return redirect('/admin/users');
|
|
}
|
|
|
|
public function user()
|
|
{
|
|
return view('admin.new.user');
|
|
}
|
|
|
|
public function addUser()
|
|
{
|
|
$user = new User;
|
|
$user->addUser(request()->all());
|
|
return redirect('/admin/users');
|
|
}
|
|
|
|
|
|
|
|
|
|
public function showTests()
|
|
{
|
|
$tests = Test::all();
|
|
return view('admin.show.tests', compact('tests'));
|
|
}
|
|
|
|
public function showTest(Test $test)
|
|
{
|
|
$questions = $test->questions;
|
|
return view('admin.show.test', compact('test'), compact('questions'));
|
|
}
|
|
|
|
public function test()
|
|
{
|
|
return view('admin.new.test');
|
|
}
|
|
|
|
public function addTest()
|
|
{
|
|
$user = \Auth::user();
|
|
$test = new Test(request()->all());
|
|
if ($user->company_id) {
|
|
$group = Group::find($user->company_id);
|
|
$group->tests()->save($test);
|
|
return redirect('/admin/tests');
|
|
}
|
|
$test->save();
|
|
return redirect('/admin/tests');
|
|
}
|
|
|
|
public function editTest(Test $test)
|
|
{
|
|
return view('admin.edit.test', compact('test'));
|
|
}
|
|
|
|
public function updateTest(Test $test)
|
|
{
|
|
$test->update(request()->all());
|
|
return redirect("/admin/tests/$test->id");
|
|
}
|
|
|
|
public function delTest(Test $test)
|
|
{
|
|
return view('admin.delete.test', compact('test'));
|
|
}
|
|
|
|
public function deleteTest(Test $test)
|
|
{
|
|
$questions = $test->questions;
|
|
foreach ($questions as $question) {
|
|
$options = $question->options;
|
|
foreach ($options as $option) {
|
|
$option->delete();
|
|
}
|
|
$question->delete();
|
|
}
|
|
$test->delete();
|
|
return redirect('/admin/tests');
|
|
}
|
|
|
|
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->title = request()->title;
|
|
$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/$test->id");
|
|
}
|
|
|
|
public function delQuestion(Question $question)
|
|
{
|
|
return view('admin.delete.question', compact('question'));
|
|
}
|
|
|
|
public function deleteQuestion(Question $question)
|
|
{
|
|
$test = $question->test;
|
|
$options = $question->options;
|
|
foreach ($options as $option) {
|
|
$option->delete();
|
|
}
|
|
$question->delete();
|
|
return redirect("/admin/tests/$test->id");
|
|
}
|
|
|
|
public function editQuestion(Question $question)
|
|
{
|
|
$options = $question->options;
|
|
return view('admin.edit.question', compact('question'), compact('options'));
|
|
}
|
|
|
|
public function updateQuestion(Question $question)
|
|
{
|
|
$test = $question->test;
|
|
$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/$test->id");
|
|
}
|
|
}
|