Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#19 - Tests should be scheduled #22

Merged
merged 18 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions app/Http/Controllers/QuizController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use App\Http\Requests\QuizRequest;
use App\Http\Resources\QuizResource;
use App\Models\Quiz;
use Carbon\Carbon;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

Expand Down Expand Up @@ -50,16 +50,6 @@ public function update(QuizRequest $request, Quiz $quiz): RedirectResponse
->with("success", "Quiz updated");
}

public function lock(Quiz $quiz): RedirectResponse
{
$quiz->locked_at = Carbon::now();
$quiz->save();

return redirect()
->back()
->with("success", "Quiz locked");
}

public function destroy(Quiz $quiz): RedirectResponse
{
$quiz->delete();
Expand All @@ -77,4 +67,12 @@ public function clone(Quiz $quiz): RedirectResponse
->back()
->with("success", "Quiz cloned");
}

public function createSubmission(Request $request, Quiz $quiz): RedirectResponse
{
$user = $request->user();
$submission = $quiz->createSubmission($user);

return redirect("/submissions/{$submission->id}/");
}
}
20 changes: 20 additions & 0 deletions app/Http/Controllers/QuizSubmissionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Http\Resources\QuizSubmissionResource;
use App\Models\QuizSubmission;
use Inertia\Inertia;
use Inertia\Response;

class QuizSubmissionController extends Controller
{
public function show(QuizSubmission $quizSubmission): Response
{
$quizSubmission->load(["answerRecords.question.answers", "quiz"]);

return Inertia::render("Submission/Show", ["submission" => QuizSubmissionResource::make($quizSubmission)]);
}
}
28 changes: 28 additions & 0 deletions app/Http/Middleware/EnsureQuizIsNotAlreadyStarted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use App\Models\QuizSubmission;
use Closure;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class EnsureQuizIsNotAlreadyStarted
{
public function handle(Request $request, Closure $next): Response|RedirectResponse
{
$user = $request->user();
$quiz = $request->route()->parameter("quiz");

$submission = QuizSubmission::query()->where(["quiz_id" => $quiz->id, "user_id" => $user->id])->first();

if ($submission) {
return redirect("/submissions/{$submission->id}/");
}
kamilpiech97 marked this conversation as resolved.
Show resolved Hide resolved

return $next($request);
}
}
2 changes: 2 additions & 0 deletions app/Http/Requests/QuizRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public function rules(): array
{
return [
"name" => ["required", "string"],
"scheduled_at" => ["date", "date_format:Y-m-d H:i:s", "after:now"],
kamilpiech97 marked this conversation as resolved.
Show resolved Hide resolved
"duration" => ["integer", "min:1"],
];
}
}
33 changes: 33 additions & 0 deletions app/Http/Resources/AnswerRecordResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class AnswerRecordResource extends JsonResource
{
public function toArray(Request $request): array
{
$answers = collect();

foreach ($this->question->answers as $answer) {
$answers->add([
"id" => $answer->id,
"text" => $answer->text,
]);
}

return [
"id" => $this->id,
"question" => $this->question->text,
"createdAt" => $this->created_at,
"updatedAt" => $this->updated_at,
"closed" => $this->isClosed,
"answers" => $answers->shuffle(),
"selected" => $this->answer_id,
];
}
}
1 change: 1 addition & 0 deletions app/Http/Resources/QuizResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function toArray($request): array
"name" => $this->name,
"createdAt" => $this->created_at,
"updatedAt" => $this->updated_at,
"duration" => $this->duration,
"locked" => $this->isLocked,
"questions" => QuestionResource::collection($this->questions),
];
Expand Down
24 changes: 24 additions & 0 deletions app/Http/Resources/QuizSubmissionResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class QuizSubmissionResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
"id" => $this->id,
"name" => $this->quiz->name,
"createdAt" => $this->created_at,
"updatedAt" => $this->updated_at,
"closedAt" => $this->closed_at,
"closed" => $this->isClosed,
"answers" => AnswerRecordResource::collection($this->answerRecords->shuffle()),
];
}
}
48 changes: 48 additions & 0 deletions app/Models/AnswerRecord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* @property int $id
* @property Carbon $created_at
* @property Carbon $updated_at
* @property int $quiz_submission_id
* @property int $question_id
* @property int $answer_id
* @property bool $isClosed
* @property QuizSubmission $quizSubmission
* @property Question $question
* @property Answer $answer
*/
class AnswerRecord extends Model
{
use HasFactory;

public function quizSubmission(): BelongsTo
{
return $this->belongsTo(QuizSubmission::class);
}

public function answer(): BelongsTo
{
return $this->belongsTo(Answer::class);
}

public function question(): BelongsTo
{
return $this->belongsTo(Question::class);
}

public function isClosed(): Attribute
{
return Attribute::get(fn(): bool => $this->quizSubmission->isClosed);
}
}
38 changes: 35 additions & 3 deletions app/Models/Quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
* @property string $name
* @property Carbon $created_at
* @property Carbon $updated_at
* @property Carbon $locked_at
* @property Carbon $scheduled_at
* @property ?int $duration
* @property bool $isLocked
* @property ?Carbon $closeAt
* @property Collection<Question> $questions
* @property Collection<Answer> $answers
*/
Expand All @@ -28,6 +30,8 @@ class Quiz extends Model

protected $fillable = [
"name",
"scheduled_at",
"duration",
];

public function questions(): HasMany
Expand All @@ -42,7 +46,12 @@ public function answers(): HasManyThrough

public function isLocked(): Attribute
{
return Attribute::get(fn(): bool => $this->locked_at !== null);
return Attribute::get(fn(): bool => $this->isReadyToBePublished() && $this->scheduled_at <= Carbon::now());
}

public function closeAt(): Attribute
{
return Attribute::get(fn(): ?Carbon => $this->isReadyToBePublished() ? $this->scheduled_at->copy()->addSeconds($this->duration) : null);
kamilpiech97 marked this conversation as resolved.
Show resolved Hide resolved
}

public function clone(): self
Expand All @@ -57,10 +66,33 @@ public function clone(): self
return $quizCopy;
}

public function createSubmission(User $user): QuizSubmission
{
$submission = new QuizSubmission();
$submission->closed_at = $this->closeAt;
$submission->quiz()->associate($this);
$submission->user()->associate($user);
$submission->save();

foreach ($this->questions as $question) {
$answerRecord = new AnswerRecord();
$answerRecord->quizSubmission()->associate($submission);
$answerRecord->question()->associate($question);
$answerRecord->save();
}

return $submission;
}

protected function isReadyToBePublished(): bool
{
return $this->scheduled_at !== null && $this->duration !== null;
}

protected function casts(): array
{
return [
"locked_at" => "datetime",
"scheduled_at" => "datetime",
];
}
}
57 changes: 57 additions & 0 deletions app/Models/QuizSubmission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;

/**
* @property int $id
* @property Carbon $created_at
* @property Carbon $updated_at
* @property Carbon $closed_at
* @property int $quiz_id
* @property int $user_id
* @property bool $isClosed
* @property Quiz $quiz
* @property User $user
* @property Collection<AnswerRecord> $answerRecords
*/
class QuizSubmission extends Model
{
use HasFactory;

public function quiz(): BelongsTo
{
return $this->belongsTo(Quiz::class);
}

public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

public function answerRecords(): HasMany
{
return $this->hasMany(AnswerRecord::class);
}

public function isClosed(): Attribute
{
return Attribute::get(fn(): bool => $this->closed_at !== null);
}

protected function casts(): array
{
return [
"closed_at" => "datetime",
];
}
}
5 changes: 5 additions & 0 deletions app/Policies/QuizPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public function delete(User $user, Quiz $quiz): bool
{
return !$quiz->isLocked;
}

public function submit(User $user, Quiz $quiz): bool
{
return $quiz->isLocked;
}
}
16 changes: 16 additions & 0 deletions app/Policies/QuizSubmissionPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\Policies;

use App\Models\QuizSubmission;
use App\Models\User;

class QuizSubmissionPolicy
{
public function view(User $user, QuizSubmission $quizSubmission): bool
{
return $user->id === $quizSubmission->user_id;
}
}
24 changes: 24 additions & 0 deletions database/factories/AnswerRecordFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\AnswerRecord;
use App\Models\Question;
use App\Models\QuizSubmission;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<AnswerRecord>
*/
class AnswerRecordFactory extends Factory
{
public function definition(): array
{
return [
"quiz_submission_id" => QuizSubmission::factory(),
"question_id" => Question::factory(),
];
}
}
Loading
Loading