Skip to content

Commit

Permalink
Merge branch '50-crud-for-schools-frontend' of https://github.com/blu…
Browse files Browse the repository at this point in the history
…milksoftware/interns2024b into 106-crud-for-users-frontend
  • Loading branch information
AmonDeShir committed Dec 3, 2024
2 parents e7174fc + 13a866c commit 5139716
Show file tree
Hide file tree
Showing 150 changed files with 2,475 additions and 1,400 deletions.
17 changes: 0 additions & 17 deletions app/Actions/CloseQuizSubmissionAction.php

This file was deleted.

17 changes: 17 additions & 0 deletions app/Actions/CloseUserQuizAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace App\Actions;

use App\Models\UserQuiz;
use Carbon\Carbon;

class CloseUserQuizAction
{
public function execute(UserQuiz $userQuiz): void
{
$userQuiz->closed_at = Carbon::now();
$userQuiz->save();
}
}
58 changes: 58 additions & 0 deletions app/Helpers/RegonHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace App\Helpers;

class RegonHelper
{
public const array WEIGHTS_SHORT = [8, 9, 2, 3, 4, 5, 6, 7];
public const array WEIGHTS_LONG = [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8];

public static function generateShortRegon()
{
$digits = "";

for ($i = 0; $i < 8; $i++) {
$digits .= rand(0, 9);
}

$checksum = self::calculateChecksum(str_split($digits), self::WEIGHTS_SHORT);

return $digits . $checksum;
}

public static function generateLongRegon()
{
$digits = "";

for ($i = 0; $i < 13; $i++) {
$digits .= rand(0, 9);
}

$checksum = self::calculateChecksum(str_split($digits), self::WEIGHTS_LONG);

return $digits . $checksum;
}

/***
* @param array<string> $number
* @param array<int> $wages
*/
public static function calculateChecksum(array $number, array $wages): int
{
$sum = 0;

for ($i = 0; $i < count($wages); $i++) {
$sum += $wages[$i] * intval($number[$i]);
}

$checksum = $sum % 11;

if ($checksum === 10) {
return 0;
}

return $checksum;
}
}
70 changes: 70 additions & 0 deletions app/Helpers/SortHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace App\Helpers;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Lang;
use Symfony\Component\HttpFoundation\Response as Status;

class SortHelper
{
public function __construct(
private Request $request,
) {}

/**
* @param array<string> $allowedFields
* @param array<string> $ignoredFields
*/
public function sort(Builder $query, array $allowedFields, array $ignoredFields): Builder
{
[$field, $order] = $this->getSortParameters();

if (!in_array($field, $allowedFields, true)) {
if (in_array($field, $ignoredFields, true)) {
return $query;
}

abort(Status::HTTP_BAD_REQUEST, Lang::get("validation.custom.sorting.unsupported_field", ["attribute" => $field]));
}

return $query->orderBy($field, $order);
}

/**
* @return array<string>
*/
public function getSortParameters(): array
{
$field = $this->request->query("sort", "id");
$ascending = $this->request->query("order", "desc") === "asc";

return [$field, $ascending ? "asc" : "desc"];
}

public function search(Builder $query, string $field): Builder
{
$searchText = $this->request->query("search");

if ($searchText) {
return $query->where($field, "ilike", "%$searchText%");
}

return $query;
}

public function paginate(Builder $query): LengthAwarePaginator
{
$limit = (int)$this->request->query("limit", "50");

if (!$limit || $limit < 0) {
$limit = 50;
}

return $query->paginate($limit);
}
}
4 changes: 1 addition & 3 deletions app/Http/Controllers/AuthenticateSessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public function authenticate(AuthenticateSessionRequest $request): RedirectRespo
if (auth()->attempt($credentials)) {
$request->session()->regenerate();

return $request->user()->hasRole(["admin", "super_admin"])
? Redirect::route("admin.quizzes.index")
: Redirect::route("dashboard");
return redirect()->route("home");
}

throw ValidationException::withMessages([
Expand Down
16 changes: 11 additions & 5 deletions app/Http/Controllers/ContestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace App\Http\Controllers;

use App\Http\Resources\QuizResource;
use App\Http\Resources\QuizSubmissionResource;
use App\Http\Resources\SchoolResource;
use App\Http\Resources\UserQuizResource;
use App\Models\Quiz;
use App\Models\School;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
Expand All @@ -22,11 +23,16 @@ public function index(): Response
return Inertia::render("Home", ["schools" => SchoolResource::collection($schools)]);
}

public function create(Request $request): Response
public function create(Request $request): RedirectResponse|Response
{
$user = $request->user();
$submissions = $user->quizSubmissions()
->with(["answerRecords.question.answers", "quiz"])

if ($user->hasRole(["admin", "super_admin"])) {
return redirect()->route("admin.quizzes.index");
}

$userQuizzes = $user->userQuizzes()
->with(["userQuestions.question.answers", "quiz"])
->get();

$quizzes = Quiz::query()
Expand All @@ -35,7 +41,7 @@ public function create(Request $request): Response
->get();

return Inertia::render("User/Dashboard", [
"submissions" => QuizSubmissionResource::collection($submissions),
"userQuizzes" => UserQuizResource::collection($userQuizzes),
"quizzes" => QuizResource::collection($quizzes),
]);
}
Expand Down
36 changes: 29 additions & 7 deletions app/Http/Controllers/QuizController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace App\Http\Controllers;

use App\Helpers\SortHelper;
use App\Http\Requests\QuizRequest;
use App\Http\Requests\UpdateQuizRequest;
use App\Http\Resources\QuizResource;
use App\Models\Quiz;
use App\Services\QuizUpdateService;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
Expand All @@ -19,15 +21,24 @@

class QuizController extends Controller
{
public function index(): Response
public function index(Request $request, SortHelper $sorter): Response
{
$quizzes = Quiz::query()
->with("questions.answers")
->get();
$query = $sorter->sort(Quiz::query()->with("questions.answers"), ["id", "title", "updated_at", "created_at"], []);
$query = $this->filterArchivedQuizzes($query, $request);
$query = $sorter->search($query, "title");
$quizzes = $sorter->paginate($query);

return Inertia::render("Admin/Quizzes", ["quizzes" => QuizResource::collection($quizzes)]);
}

public function show(Quiz $quiz): Response
{
return Inertia::render(
"Admin/QuizDemo",
["quiz" => $quiz->load("questions.answers")],
);
}

public function store(QuizRequest $request): RedirectResponse
{
Quiz::query()->create($request->validated());
Expand Down Expand Up @@ -78,12 +89,12 @@ public function unlock(Quiz $quiz): RedirectResponse
->with("status", "Publikacja testu została wycofana");
}

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

return redirect("/submissions/{$submission->id}/");
return redirect("/quizzes/{$userQuiz->id}/");
}

public function assign(Request $request, Quiz $quiz): RedirectResponse
Expand All @@ -96,4 +107,15 @@ public function assign(Request $request, Quiz $quiz): RedirectResponse
->back()
->with("status", "Przypisano do testu");
}

private function filterArchivedQuizzes(Builder $query, Request $request): Builder
{
$showArchived = $request->query("archived", "false") === "true";

if (!$showArchived) {
return $query->orWhere(fn(Builder $query) => $query->whereNull("locked_at")->orWhereDate("scheduled_at", ">", Carbon::now()));
}

return $query;
}
}
39 changes: 0 additions & 39 deletions app/Http/Controllers/QuizSubmissionController.php

This file was deleted.

10 changes: 5 additions & 5 deletions app/Http/Controllers/RankingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use App\Http\Resources\QuizResource;
use App\Http\Resources\RankingResource;
use App\Models\Quiz;
use App\Models\QuizSubmission;
use App\Models\UserQuiz;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;
Expand All @@ -18,29 +18,29 @@ class RankingController extends Controller
{
public function index(Quiz $quiz): Response
{
$submissions = QuizSubmission::query()
$userQuizzes = UserQuiz::query()
->where("quiz_id", $quiz->id)
->with("user.school")
->get();

return Inertia::render("Admin/Ranking", [
"quiz" => QuizResource::make($quiz),
"rankings" => RankingResource::collection($submissions),
"rankings" => RankingResource::collection($userQuizzes),
]);
}

public function indexUser(Quiz $quiz): Response
{
$this->authorize("viewUserRanking", $quiz);

$submissions = QuizSubmission::query()
$userQuizzes = UserQuiz::query()
->where("quiz_id", $quiz->id)
->with("user.school")
->get();

return Inertia::render("User/Ranking", [
"quiz" => QuizResource::make($quiz),
"rankings" => RankingResource::collection($submissions),
"rankings" => RankingResource::collection($userQuizzes),
]);
}

Expand Down
Loading

0 comments on commit 5139716

Please sign in to comment.