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

#110 - Add support for offline tests #128

Merged
merged 10 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions app/Actions/SetQuizLocalAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\Actions;

use App\Models\Quiz;

class SetQuizLocalAction
{
public function execute(Quiz $quiz): void
{
$quiz->is_local = true;
$quiz->save();
}
}
16 changes: 16 additions & 0 deletions app/Actions/SetQuizOnlineAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\Actions;

use App\Models\Quiz;

class SetQuizOnlineAction
{
public function execute(Quiz $quiz): void
{
$quiz->is_local = false;
$quiz->save();
}
}
20 changes: 20 additions & 0 deletions app/Http/Controllers/QuizController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use App\Actions\AssignToQuizAction;
use App\Actions\CreateUserQuizAction;
use App\Actions\LockQuizAction;
use App\Actions\SetQuizLocalAction;
use App\Actions\SetQuizOnlineAction;
use App\Actions\UnlockQuizAction;
use App\Helpers\SortHelper;
use App\Http\Requests\QuizRequest;
Expand Down Expand Up @@ -109,6 +111,24 @@ public function assign(AssignToQuizAction $action, Request $request, Quiz $quiz)
->with("status", "Przypisano do testu");
}

public function makeLocal(SetQuizLocalAction $action, Quiz $quiz): RedirectResponse
{
$action->execute($quiz);

return redirect()
->back()
->with("status", "Tryb testu został zmieniony na stacjonarny.");
}

public function makeOnline(SetQuizOnlineAction $action, Quiz $quiz): RedirectResponse
{
$action->execute($quiz);

return redirect()
->back()
->with("status", "Tryb testu został zmieniony na zdalny.");
}

private function filterArchivedQuizzes(Builder $query, Request $request): Builder
{
$showArchived = $request->query("archived", "false") === "true";
Expand Down
1 change: 1 addition & 0 deletions app/Http/Requests/QuizRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function rules(): array
"title" => ["required", "string", "max:255"],
"scheduled_at" => ["date", "after:now"],
"duration" => ["numeric", "min:1", "max:2147483647"],
"description" => ["string", "nullable"],
];
}
}
13 changes: 7 additions & 6 deletions app/Http/Requests/UpdateQuizRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ public function rules(): array
"title" => ["required", "string", "max:255"],
"scheduled_at" => ["date", "after:now"],
"duration" => ["integer", "min:1", "max:2147483647"],
"description" => ["string", "nullable"],
"questions" => ["array"],
"questions.*.id" => "integer|min:0",
"questions.*.text" => "required|string",
"questions.*.answers" => "array",
"questions.*.answers.*.id" => "integer|min:0",
"questions.*.answers.*.text" => "nullable|string",
"questions.*.answers.*.correct" => "boolean",
"questions.*.id" => ["integer", "min:0"],
"questions.*.text" => ["required", "string"],
"questions.*.answers" => ["array"],
"questions.*.answers.*.id" => ["integer", "min:0"],
"questions.*.answers.*.text" => ["nullable", "string"],
"questions.*.answers.*.correct" => ["boolean"],
];
}

Expand Down
4 changes: 3 additions & 1 deletion app/Http/Resources/QuizResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ public function toArray($request): array
return [
"id" => $this->id,
"title" => $this->title,
"description" => $this->description,
"createdAt" => $this->created_at,
"updatedAt" => $this->updated_at,
"scheduledAt" => $this->scheduled_at,
"duration" => $this->duration,
"state" => $this->state,
"canBeLocked" => $this->canBeLocked,
"canBeUnlocked" => $this->canBeUnlocked,
"questions" => QuestionResource::collection($this->questions),
"questions" => $this->is_local ? [] : QuestionResource::collection($this->questions),
"isUserAssigned" => $this->isUserAssigned($request->user()),
"isRankingPublished" => $this->isRankingPublished,
"isLocal" => $this->is_local,
];
}
}
12 changes: 10 additions & 2 deletions app/Models/Quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
* @property ?Carbon $scheduled_at
* @property ?Carbon $ranking_published_at
* @property ?Carbon $locked_at
* @property bool $is_local
* @property ?int $duration
* @property bool $isLocked
* @property bool $isPublished
* @property bool $canBeLocked
* @property bool $canBeUnlocked
* @property string $state
* @property ?string $description
* @property bool $isRankingPublished
* @property ?Carbon $closeAt
* @property Collection<Question> $questions
Expand All @@ -43,6 +45,7 @@ class Quiz extends Model
"scheduled_at",
"duration",
"ranking_published_at",
"description",
];
protected $guarded = [];

Expand Down Expand Up @@ -110,7 +113,7 @@ public function closeAt(): Attribute

public function isReadyToBePublished(): bool
{
return $this->scheduled_at !== null && $this->duration !== null && $this->questions->count() > 0 && $this->allQuestionsHaveCorrectAnswer();
return $this->scheduled_at !== null && $this->duration !== null && $this->hasValidQuestionsForOnline();
}

public function hasUserQuizzesFrom(User $user): bool
Expand All @@ -120,7 +123,12 @@ public function hasUserQuizzesFrom(User $user): bool

public function isClosingToday(): bool
{
return $this->isLocked && $this->closeAt->isFuture() && $this->closeAt->isToday();
return $this->isLocked && $this->closeAt !== null && $this->closeAt->isFuture() && $this->closeAt->isToday();
}

protected function hasValidQuestionsForOnline(): bool
{
return $this->is_local || $this->questions->count() > 0 && $this->allQuestionsHaveCorrectAnswer();
}

protected function allQuestionsHaveCorrectAnswer(): bool
Expand Down
2 changes: 1 addition & 1 deletion app/Policies/QuizPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function delete(User $user, Quiz $quiz): bool

public function submit(User $user, Quiz $quiz): bool
{
return $quiz->isLocked;
return $quiz->isLocked && !$quiz->is_local;
}

public function lock(User $user, Quiz $quiz): bool
Expand Down
2 changes: 1 addition & 1 deletion app/Services/QuizUpdateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function update(Quiz $quiz, array $data): void
{
$quiz->fill($data);

if (array_key_exists("questions", $data)) {
if (array_key_exists("questions", $data) && !$quiz->is_local) {
$questions = collect($data["questions"]);
$quiz->questions()->whereNotIn("id", $questions->pluck("id")->whereNotNull())->delete();

Expand Down
8 changes: 8 additions & 0 deletions database/factories/QuizFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ public function withRanking(): static
"ranking_published_at" => Carbon::now(),
]);
}

public function local(): static
{
return $this->state(fn(array $attributes): array => [
"is_local" => true,
"description" => fake()->text(100),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function up(): void
$table->timestamps();
$table->timestamp("locked_at")->nullable();
$table->string("title");
$table->text("description")->nullable();
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
public function up(): void
{
Schema::table("quizzes", function (Blueprint $table): void {
$table->boolean("is_local")->default(false);
});
}

public function down(): void
{
Schema::table("quizzes", function (Blueprint $table): void {
$table->dropColumn("is_local");
});
}
};
2 changes: 2 additions & 0 deletions resources/js/Types/Quiz.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ interface Quiz {
createdAt: string
updatedAt: string
state:'published' | 'locked' | 'unlocked'
isLocal: boolean
description?: string
isUserAssigned: boolean
isRankingPublished: boolean
questions: Question[]
Expand Down
4 changes: 3 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@
Route::get("/quizzes", [QuizController::class, "index"])->name("admin.quizzes.index");
Route::get("/quizzes/{quiz}", [QuizController::class, "show"])->name("admin.quizzes.demo");
Route::post("/quizzes", [QuizController::class, "store"])->name("admin.quizzes.store");
Route::post("/quizzes/{quiz}/local", [QuizController::class, "makeLocal"])->can("update,quiz,update")->name("admin.quizzes.make.local");
Route::post("/quizzes/{quiz}/online", [QuizController::class, "makeOnline"])->can("update,quiz")->name("admin.quizzes.make.online");
Route::patch("/quizzes/{quiz}", [QuizController::class, "update"])->can("update,quiz")->name("admin.quizzes.update");
Route::delete("/quizzes/{quiz}", [QuizController::class, "destroy"])->can("delete,quiz")->name("admin.quizzes.destroy");
Route::post("/quizzes/{quiz}/clone", [QuizController::class, "clone"])->name("admin.quizzes.clone");
Route::post("/quizzes/{quiz}/lock", [QuizController::class, "lock"])->name("admin.quizzes.lock");
Route::post("/quizzes/{quiz}/lock", [QuizController::class, "lock"])->can("lock,quiz")->name("admin.quizzes.lock");
Route::post("/quizzes/{quiz}/unlock", [QuizController::class, "unlock"])->can("unlock,quiz")->name("admin.quizzes.unlock");

Route::get("/quizzes/{quiz}/invite", [InviteController::class, "index"])->name("admin.quizzes.invite.index");
Expand Down
Loading
Loading