1
0

add tests

This commit is contained in:
Rokas Puzonas 2023-10-29 21:41:27 +02:00
parent f24bb757bd
commit e5ef214753
17 changed files with 498 additions and 54 deletions

View File

@ -0,0 +1,91 @@
<?php
namespace App\Http\Controllers;
use App\Models\Question;
use App\Models\Test;
use Illuminate\Http\Request;
class QuestionController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validated = $request->validate([
"question" => "required|string",
"answer" => "required|string",
"points" => "required|integer|min:1",
"test_id" => "required|integer",
]);
Question::create($validated);
return redirect(route("tests.edit", [
"test" => Test::where("id", $validated["test_id"])->first()
]));
}
/**
* Display the specified resource.
*/
public function show(Question $question)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Question $question)
{
return view("questions.edit", [
"question" => $question,
]);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Question $question)
{
$validated = $request->validate([
"question" => "required|string",
"answer" => "required|string",
"points" => "required|integer|min:1"
]);
$question->update($validated);
return view("tests.edit", [
"test" => $validated["test_id"]
]);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Question $question)
{
$question->delete();
return redirect(route("tests.edit", $question->test_id));
}
}

View File

@ -0,0 +1,83 @@
<?php
namespace App\Http\Controllers;
use App\Models\Test;
use Illuminate\Http\Request;
class TestController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
return view("tests.index", [
"tests" => $request->user()->tests()->get()
]);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validated = $request->validate([
"name" => "required|string"
]);
$request->user()->tests()->create($validated);
return redirect(route("tests.index"));
}
/**
* Display the specified resource.
*/
public function show(Test $test)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Test $test)
{
return view("tests.edit", [
"test" => $test,
]);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Test $test)
{
$validated = $request->validate([
"name" => "required|string"
]);
$test->update($validated);
return redirect(route("tests.index"));
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Test $test)
{
$test->delete();
return redirect(route("tests.index"));
}
}

24
app/Models/Question.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Question extends Model
{
use HasFactory;
protected $fillable = [
"test_id",
'question',
'answer',
'points',
];
public function test(): BelongsTo
{
return $this->belongsTo(Test::class);
}
}

27
app/Models/Test.php Normal file
View File

@ -0,0 +1,27 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Test extends Model
{
use HasFactory;
protected $fillable = [
"name"
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function questions(): HasMany
{
return $this->hasMany(Question::class);
}
}

View File

@ -49,4 +49,9 @@ class User extends Authenticatable
{
return $this->hasMany(Chirp::class);
}
public function tests(): HasMany
{
return $this->hasMany(Test::class);
}
}

View File

@ -108,44 +108,4 @@ return [
'migrations' => 'migrations',
/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer body of commands than a typical key-value system
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
],
'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
],
'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_CACHE_DB', '1'),
],
],
];

View File

@ -0,0 +1,29 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('tests', function (Blueprint $table) {
$table->id();
$table->foreignId("user_id")->references("id")->on("users")->constrained()->cascadeOnDelete();
$table->string("name");
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('tests');
}
};

View File

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('questions', function (Blueprint $table) {
$table->id();
$table->foreignId("test_id")->references("id")->on("tests")->constrained()->cascadeOnDelete();
$table->string("question");
$table->string("answer");
$table->unsignedInteger("points")->default(1);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('questions');
}
};

View File

@ -3,7 +3,10 @@
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
class DatabaseSeeder extends Seeder
{
@ -12,11 +15,23 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
// \App\Models\User::factory(10)->create();
// \App\Models\User::factory()->create([
// 'name' => 'Test User',
// 'email' => 'test@example.com',
// ]);
User::create([
'name' => "admin",
'email' => "admin@admin",
'password' => Hash::make("admin"),
'role' => "admin"
]);
User::create([
'name' => "Bob teacher",
'email' => "bob@teacher",
'password' => Hash::make("bob"),
'role' => "teacher"
]);
User::create([
'name' => "Alice student",
'email' => "alice@student",
'password' => Hash::make("alice"),
'role' => "student"
]);
}
}

View File

@ -13,5 +13,7 @@
"Edit": "Atnaujinti",
"Delete": "Ištrinti",
"Save": "Saugoti",
"Cancel": "Atšaukti"
"Cancel": "Atšaukti",
"Create test": "Sukurti testą",
"Tests": "Testai"
}

View File

@ -23,6 +23,11 @@
{{ __('Users') }}
</x-nav-link>
@endif
@if (Auth::user()->role == "teacher")
<x-nav-link :href="route('tests.index')" :active="request()->routeIs('tests.index')">
{{ __('Tests') }}
</x-nav-link>
@endif
</div>
</div>
@ -86,6 +91,11 @@
{{ __('Users') }}
</x-responsive-nav-link>
@endif
@if (Auth::user()->role == "teacher")
<x-responsive-nav-link :href="route('tests.index')" :active="request()->routeIs('tests.index')">
{{ __('Tests') }}
</x-responsive-nav-link>
@endif
</div>
<!-- Responsive Settings Options -->

View File

@ -19,11 +19,11 @@
</div>
</div>
<div class="p-4 sm:p-8 bg-white shadow sm:rounded-lg">
<div class="max-w-xl">
@include('profile.partials.delete-user-form')
</div>
</div>
{{-- <div class="p-4 sm:p-8 bg-white shadow sm:rounded-lg"> --}}
{{-- <div class="max-w-xl"> --}}
{{-- @include('profile.partials.delete-user-form') --}}
{{-- </div> --}}
{{-- </div> --}}
</div>
</div>
</x-app-layout>

View File

@ -0,0 +1,26 @@
<x-app-layout>
<div class="max-w-2xl mx-auto p-4 sm:p-6 lg:p-8">
<form method="POST" action="{{ route('questions.update', $question) }}">
@csrf
@method("patch")
<div class="flex flex-col gap-1">
<x-input-label>{{ __("Question") }}</x-input-label>
<x-text-input name="question" placeholder="{{ __('Question') }}" value="{{ old('question', $question->question) }}" />
<x-input-error :messages="$errors->get('question')" class="mt-2" />
<x-input-label>{{ __("Answer") }}</x-input-label>
<x-text-input name="answer" placeholder="{{ __('Answer') }}" value="{{ old('answer', $question->answer) }}" />
<x-input-error :messages="$errors->get('answer')" class="mt-2" />
<x-input-label>{{ __("Points") }}</x-input-label>
<x-text-input name="points" placeholder="{{ __('Points') }}" value="{{ old('points', $question->points) }}" />
<x-input-error :messages="$errors->get('points')" class="mt-2" />
</div>
<div class="mt-4 space-x-2">
<x-primary-button>{{ __("Save") }}</x-primary-button>
<a href="{{ route('tests.edit', $question->test_id) }}">{{ __("Cancel") }}</a>
</div>
</form>
</div>
</x-app-layout>

View File

@ -0,0 +1,83 @@
<x-app-layout>
<div class="max-w-2xl mx-auto p-4 sm:p-6 lg:p-8">
<form method="POST" action="{{ route('tests.update', $test) }}">
@csrf
@method("patch")
<div class="flex flex-col gap-1">
<x-input-label>{{ __("Name") }}</x-input-label>
<x-text-input name="name" placeholder="{{ __('Name') }}" value="{{ old('name', $test->name) }}" />
<x-input-error :messages="$errors->get('name')" class="mt-2" />
</div>
<div class="mt-4 space-x-2">
<x-primary-button>{{ __("Save") }}</x-primary-button>
<a href="{{ route('tests.index') }}">{{ __("Cancel") }}</a>
</div>
</form>
<hr class="mt-6 mb-6">
<form method="POST" action="{{ route('questions.store') }}">
@csrf
<div class="flex flex-col gap-1">
<input type="hidden" value="{{ $test->id }}" name="test_id">
<x-input-label>{{ __("Question") }}</x-input-label>
<x-text-input name="question" placeholder="{{ __('Question') }}" value="{{ old('question') }}" />
<x-input-error :messages="$errors->get('question')" class="mt-2" />
<x-input-label>{{ __("Answer") }}</x-input-label>
<x-text-input name="answer" placeholder="{{ __('Answer') }}" value="{{ old('answer') }}" />
<x-input-error :messages="$errors->get('answer')" class="mt-2" />
<x-input-label>{{ __("Points") }}</x-input-label>
<x-text-input name="points" placeholder="{{ __('Points') }}" value="{{ old('points', 1) }}" />
<x-input-error :messages="$errors->get('points')" class="mt-2" />
</div>
<div class="mt-4 space-x-2">
<x-primary-button>{{ __("Add question") }}</x-primary-button>
</div>
</form>
<table class="mt-6 border border-gray-300 w-full text-left">
<tr>
<th class="border border-gray-300 p-1">{{ __("Question") }}</th>
<th class="border border-gray-300 p-1">{{ __("Answer") }}</th>
<th class="border border-gray-300 p-1">{{ __("Points") }}</th>
<th class="border border-gray-300 p-1"></th>
</tr>
@foreach ($test->questions()->get() as $question)
<tr>
<td class="border border-gray-300 p-1">{{ $question->question }}</td>
<td class="border border-gray-300 p-1">{{ $question->answer }}</td>
<td class="border border-gray-300 p-1">{{ $question->points }}</td>
<td class="border border-gray-300 p-1">
<x-dropdown>
<x-slot name="trigger">
<button>
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 text-gray-400" viewBox="0 0 20 20" fill="currentColor">
<path d="M6 10a2 2 0 11-4 0 2 2 0 014 0zM12 10a2 2 0 11-4 0 2 2 0 014 0zM16 12a2 2 0 100-4 2 2 0 000 4z" />
</svg>
</button>
</x-slot>
<x-slot name="content">
<x-dropdown-link :href="route('questions.edit', $question)">
{{ __("Edit") }}
</x-dropdown-link>
<form method="POST" action="{{ route("questions.destroy", $question) }}">
@csrf
@method("delete")
<x-dropdown-link :href="route('questions.destroy', $question)" onclick="event.preventDefault(); this.closest('form').submit();">
{{ __("Delete") }}
</x-dropdown-link>
</form>
</x-slot>
</x-dropdown>
</td>
</tr>
@endforeach
</table>
</div>
</x-app-layout>

View File

@ -0,0 +1,50 @@
<x-app-layout>
<div class="max-w-2xl mx-auto p-4 sm:p-6 lg:p-8">
<form method="POST" action="{{ route('tests.store') }}">
@csrf
<div class="flex flex-col gap-1">
<x-input-label>{{ __("Name") }}</x-input-label>
<x-text-input name="name" placeholder="{{ __('Name') }}" value="{{ old('name') }}" />
<x-input-error :messages="$errors->get('name')" class="mt-2" />
</div>
<x-primary-button class="mt-4">{{ __('Create test') }}</x-primary-button>
</form>
<table class="mt-6 border border-gray-300 w-full text-left">
<tr>
<th class="border border-gray-300 p-1">{{ __("Name") }}</th>
<th class="border border-gray-300 p-1"></th>
</tr>
@foreach ($tests as $test)
<tr>
<td class="border border-gray-300 p-1">{{ $test->name }}</td>
<td class="border border-gray-300 p-1">
<x-dropdown>
<x-slot name="trigger">
<button>
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 text-gray-400" viewBox="0 0 20 20" fill="currentColor">
<path d="M6 10a2 2 0 11-4 0 2 2 0 014 0zM12 10a2 2 0 11-4 0 2 2 0 014 0zM16 12a2 2 0 100-4 2 2 0 000 4z" />
</svg>
</button>
</x-slot>
<x-slot name="content">
<x-dropdown-link :href="route('tests.edit', $test)">
{{ __("Edit") }}
</x-dropdown-link>
<form method="POST" action="{{ route("tests.destroy", $test) }}">
@csrf
@method("delete")
<x-dropdown-link :href="route('tests.destroy', $test)" onclick="event.preventDefault(); this.closest('form').submit();">
{{ __("Delete") }}
</x-dropdown-link>
</form>
</x-slot>
</x-dropdown>
</td>
</tr>
@endforeach
</table>
</div>
</x-app-layout>

View File

@ -8,7 +8,6 @@
</form>
{{-- <div class="mt-6 bg-white shadown-sm rounded-lg divide-y"> --}}
<table class="mt-6 border border-gray-300 w-full text-left">
<tr>
<th class="border border-gray-300 p-1">{{ __("Name") }}</th>
@ -50,6 +49,5 @@
</tr>
@endforeach
</table>
{{-- </div> --}}
</div>
</x-app-layout>

View File

@ -2,6 +2,8 @@
use App\Http\Controllers\ChirpController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\QuestionController;
use App\Http\Controllers\TestController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;
@ -38,4 +40,12 @@ Route::resource("users", UserController::class)
->only(["index", "store", "edit", "update", "destroy"])
->middleware(["auth", "verified"]);
Route::resource("tests", TestController::class)
->only(["index", "store", "edit", "update", "destroy"])
->middleware(["auth", "verified"]);
Route::resource("questions", QuestionController::class)
->only(["index", "store", "edit", "update", "destroy"])
->middleware(["auth", "verified"]);
require __DIR__.'/auth.php';