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
resources/views/admin
delete
edit
new
show
routes
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
|
18
resources/views/admin/delete/question.blade.php
Normal file
18
resources/views/admin/delete/question.blade.php
Normal file
|
@ -0,0 +1,18 @@
|
|||
@extends('layouts.base')
|
||||
|
||||
@section('content')
|
||||
<div class="container-fluid">
|
||||
<div class="col-md-4 col-md-offset-4">
|
||||
<h1>Are you Sure!</h1>
|
||||
</br>
|
||||
<form method="POST" action="/admin/questions/{{ $question->id }}">
|
||||
{{ csrf_field() }}
|
||||
{{ method_field('DELETE') }}
|
||||
<button type="submit" class="btn btn-default">Yes</button>
|
||||
</form>
|
||||
<form method="GET" action="/admin/tests">
|
||||
<button type="submit" class="btn btn-default">No</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
@stop
|
37
resources/views/admin/edit/question.blade.php
Normal file
37
resources/views/admin/edit/question.blade.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
@extends('layouts.base')
|
||||
|
||||
@section('content')
|
||||
|
||||
<div class="container-fluid">
|
||||
<div class="col-md-4 col-md-offset-4">
|
||||
<h1>Edit User</h1>
|
||||
</br>
|
||||
<form method="POST" action="/admin/questions/{{ $question->id }}">
|
||||
{{ csrf_field() }}
|
||||
{{ method_field('PATCH') }}
|
||||
<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>
|
||||
<input type="text" class="form-control" name="option1" value="{{ $options[0]->option }}">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Option 2</label>
|
||||
<input type="text" class="form-control" name="option2" value="{{ $options[1]->option }}">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Option 3</label>
|
||||
<input type="text" class="form-control" name="option3" value="{{ $options[2]->option }}">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Option 4</label>
|
||||
<input type="text" class="form-control" name="option4" value="{{ $options[3]->option }}">
|
||||
</div>
|
||||
<button type="submit" class="btn btn-default">Submit</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@stop
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
<div class="container-fluid">
|
||||
<div class="col-md-4 col-md-offset-4">
|
||||
<h1>Create New Test</h1>
|
||||
<h1>Edit Test: {{ $test->title }}</h1>
|
||||
</br>
|
||||
<form method="POST" action="/admin/tests/{{ $test->id }}">
|
||||
{{ csrf_field() }}
|
||||
|
|
35
resources/views/admin/new/question.blade.php
Normal file
35
resources/views/admin/new/question.blade.php
Normal file
|
@ -0,0 +1,35 @@
|
|||
@extends('layouts.base')
|
||||
|
||||
@section('content')
|
||||
<div class="container-fluid">
|
||||
<div class="col-md-4 col-md-offset-4">
|
||||
<h1>Question {{ $question_number }}</h1>
|
||||
</br>
|
||||
<form method="POST" action="/admin/tests/{{ $test->id }}/question">
|
||||
{{ csrf_field() }}
|
||||
<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">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Option 2</label>
|
||||
<input type="text" class="form-control" name="option2">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Option 3</label>
|
||||
<input type="text" class="form-control" name="option3">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Option 4</label>
|
||||
<input type="text" class="form-control" name="option4">
|
||||
</div>
|
||||
<button type="submit" class="btn btn-default">Submit</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@stop
|
|
@ -1 +0,0 @@
|
|||
hello
|
|
@ -1,18 +1,55 @@
|
|||
@extends('layouts.base')
|
||||
|
||||
@section('content')
|
||||
<div class="container">
|
||||
<div class="container-fluid">
|
||||
<div class="row">
|
||||
<div class="col-md-6 col-md-offset-3">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">{{ $test->title }}</div>
|
||||
<div class="panel-heading">Test</div>
|
||||
<div class="panel-body">
|
||||
<button class="btn btn-default" formaction="/admin/tests/{{ $test->id }}/">Edit</button>
|
||||
<button class="btn btn-default" formaction="/admin/tests/{{ $test->id }}/">Add Question</button>
|
||||
<button class="btn btn-default" formaction="/admin/tests/{{ $test->id }}/delete">Delete</button>
|
||||
<form method="get">
|
||||
<button class="btn btn-default pull-right" formaction="/admin/tests/{{ $test->id }}/edit">Edit</button>
|
||||
</form>
|
||||
<strong>Title:</strong><br>
|
||||
{{ $test->title }}
|
||||
<br>
|
||||
<br>
|
||||
<strong>Question count:</strong><br>
|
||||
{{ $test->question_count }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">Questions</div>
|
||||
<div class="panel-body">
|
||||
<table class="table">
|
||||
<tr>
|
||||
<th>#</th>
|
||||
<th>Question</th>
|
||||
<th>
|
||||
<form method="get">
|
||||
<button class="btn btn-sm btn-default pull-right" formaction="/admin/tests/{{ $test->id }}/question">Add Question</button>
|
||||
</form>
|
||||
</th>
|
||||
</tr>
|
||||
@foreach ($questions as $question)
|
||||
<tr>
|
||||
<td>1</td>
|
||||
<td>{{ $question->question }}</td>
|
||||
<td>
|
||||
<form method="get" class="pull-right">
|
||||
<button class="btn btn-sm btn-default pull-left" formaction="/admin/questions/{{ $question->id }}/edit">Edit</button>
|
||||
<button class="btn btn-sm btn-default pull-left" formaction="/admin/questions/{{ $question->id }}/delete">Delete</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@stop
|
||||
@stop
|
||||
|
||||
<button class="btn btn-default" formaction="/admin/tests/{{ $test->id }}/delete">Delete</button>
|
|
@ -33,6 +33,13 @@ Route::patch('/admin/tests/{test}', 'AdminController@updateTest');
|
|||
Route::get('/admin/tests/{test}/delete', 'AdminController@delTest');
|
||||
Route::delete('/admin/tests/{test}', 'AdminController@deleteTest');
|
||||
|
||||
Route::get('/admin/tests/{test}/question', 'AdminController@question');
|
||||
Route::post('/admin/tests/{test}/question', 'AdminController@addQuestion');
|
||||
Route::get('/admin/questions/{question}/edit', 'AdminController@editQuestion');
|
||||
Route::patch('/admin/questions/{question}', 'AdminController@updateQuestion');
|
||||
Route::get('/admin/questions/{question}/delete', 'AdminController@delQuestion');
|
||||
Route::delete('/admin/questions/{question}/', 'AdminController@deleteQuestion');
|
||||
|
||||
Route::get('/admin/users', 'AdminController@showUser');
|
||||
Route::get('/admin/users/new', 'AdminController@user');
|
||||
Route::post('/admin/users/new', 'AdminController@addUser');
|
||||
|
|
Reference in a new issue