From fa6da11084f565aac2757f99ae07fe492389aa5c Mon Sep 17 00:00:00 2001 From: Krzysztof Rewak Date: Thu, 19 Sep 2024 20:42:29 +0200 Subject: [PATCH] #147 - Google CSV import to grades --- .phpstorm.meta.php | 15 +++ .../Controllers/Dashboard/GradeController.php | 12 +-- .../Dashboard/GradeImportController.php | 50 ++++++++++ ...olumn.php => UpdateGradeColumnRequest.php} | 2 +- ...UpdateGrade.php => UpdateGradeRequest.php} | 2 +- app/Models/Grade.php | 5 + app/Models/Group.php | 4 + app/Models/Student.php | 9 ++ app/Providers/BuilderServiceProvider.php | 14 ++- codestyle.php | 2 - database/factories/GradeColumnFactory.php | 2 +- .../Dashboard/CourseSemester/Grade/Import.vue | 96 +++++++++++++++++++ .../Dashboard/CourseSemester/Grade/Index.vue | 64 ++++++------- resources/js/Shared/Components/GradeCell.vue | 4 +- resources/tests/google/students001.txt | 7 ++ routes/web.php | 6 ++ 16 files changed, 245 insertions(+), 49 deletions(-) create mode 100644 .phpstorm.meta.php create mode 100644 app/Http/Controllers/Dashboard/GradeImportController.php rename app/Http/Requests/{UpdateGradeColumn.php => UpdateGradeColumnRequest.php} (88%) rename app/Http/Requests/{UpdateGrade.php => UpdateGradeRequest.php} (92%) create mode 100644 resources/js/Pages/Dashboard/CourseSemester/Grade/Import.vue create mode 100644 resources/tests/google/students001.txt diff --git a/.phpstorm.meta.php b/.phpstorm.meta.php new file mode 100644 index 00000000..fe6c2134 --- /dev/null +++ b/.phpstorm.meta.php @@ -0,0 +1,15 @@ +gradeColumns()->create($request->getData()); @@ -48,7 +48,7 @@ public function store(UpdateGradeColumn $request, CourseSemester $course, Group ->with("success", "Dodano kolumnę"); } - public function update(UpdateGradeColumn $request, CourseSemester $course, Group $group, GradeColumn $gradeColumn): RedirectResponse + public function update(UpdateGradeColumnRequest $request, CourseSemester $course, Group $group, GradeColumn $gradeColumn): RedirectResponse { $gradeColumn->update($request->getData()); @@ -56,7 +56,7 @@ public function update(UpdateGradeColumn $request, CourseSemester $course, Group ->with("success", "Zaktualizowano kolumnę"); } - public function storeGrade(UpdateGrade $request, CourseSemester $course, Group $group, GradeColumn $gradeColumn): RedirectResponse + public function storeGrade(UpdateGradeRequest $request, CourseSemester $course, Group $group, GradeColumn $gradeColumn): RedirectResponse { $gradeColumn->grades() ->create($request->getData()); @@ -64,7 +64,7 @@ public function storeGrade(UpdateGrade $request, CourseSemester $course, Group $ return redirect()->back(); } - public function updateGrade(UpdateGrade $request, CourseSemester $course, Group $group, GradeColumn $gradeColumn): RedirectResponse + public function updateGrade(UpdateGradeRequest $request, CourseSemester $course, Group $group, GradeColumn $gradeColumn): RedirectResponse { $grade = $gradeColumn->grades() ->where("student_id", $request->get("student_id")) diff --git a/app/Http/Controllers/Dashboard/GradeImportController.php b/app/Http/Controllers/Dashboard/GradeImportController.php new file mode 100644 index 00000000..7e022ef4 --- /dev/null +++ b/app/Http/Controllers/Dashboard/GradeImportController.php @@ -0,0 +1,50 @@ + CourseSemesterData::fromModel($course), + "group" => $group, + "column" => $column, + "csrfToken" => csrf_token(), + ]); + } + + public function prepare(Request $request, CourseSemester $course, Group $group, GradeColumn $column, ResponseFactory $response): JsonResponse + { + $students = $request; + + return $response->json([ + "students" => $column->refresh()->grades->map(fn(Grade $grade): array => [ + "id" => $grade->id, + "column" => [ + "id" => $column->id, + ], + "student" => [ + "id" => $grade->student->id, + "name" => $grade->student->fullName, + "indexNumber" => $grade->student->index_number, + ], + "status" => $grade->status, + "value" => $grade->value, + "imported" => rand(0, 1) > .5, + ]), + ]); + } +} diff --git a/app/Http/Requests/UpdateGradeColumn.php b/app/Http/Requests/UpdateGradeColumnRequest.php similarity index 88% rename from app/Http/Requests/UpdateGradeColumn.php rename to app/Http/Requests/UpdateGradeColumnRequest.php index 8cba0598..12262f55 100644 --- a/app/Http/Requests/UpdateGradeColumn.php +++ b/app/Http/Requests/UpdateGradeColumnRequest.php @@ -6,7 +6,7 @@ use Illuminate\Foundation\Http\FormRequest; -class UpdateGradeColumn extends FormRequest +class UpdateGradeColumnRequest extends FormRequest { public function rules(): array { diff --git a/app/Http/Requests/UpdateGrade.php b/app/Http/Requests/UpdateGradeRequest.php similarity index 92% rename from app/Http/Requests/UpdateGrade.php rename to app/Http/Requests/UpdateGradeRequest.php index 72a02e97..85377483 100644 --- a/app/Http/Requests/UpdateGrade.php +++ b/app/Http/Requests/UpdateGradeRequest.php @@ -6,7 +6,7 @@ use Illuminate\Foundation\Http\FormRequest; -class UpdateGrade extends FormRequest +class UpdateGradeRequest extends FormRequest { public function rules(): array { diff --git a/app/Models/Grade.php b/app/Models/Grade.php index 8f2da741..8bbb72ee 100644 --- a/app/Models/Grade.php +++ b/app/Models/Grade.php @@ -15,6 +15,10 @@ * @property string $id * @property ?boolean $status * @property ?string $value + * @property string $student_id + * @property Student $student + * @property string $grade_column_id + * @property GradeColumn $gradeColumn * @property Carbon $created_at * @property Carbon $updated_at */ @@ -27,6 +31,7 @@ class Grade extends Model "status", "value", "student_id", + "grade_column_id", ]; protected $casts = [ "status" => "boolean", diff --git a/app/Models/Group.php b/app/Models/Group.php index 5d7e747b..30b59952 100644 --- a/app/Models/Group.php +++ b/app/Models/Group.php @@ -5,6 +5,7 @@ namespace Keating\Models; use Carbon\Carbon; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Concerns\HasUlids; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -18,6 +19,9 @@ * @property string $name * @property string $course_semester_id * @property StudyForm $form + * @property CourseSemester course + * @property Collection students + * @property Collection gradeColumns * @property Carbon $created_at * @property Carbon $updated_at */ diff --git a/app/Models/Student.php b/app/Models/Student.php index a9a5a696..20ca690c 100644 --- a/app/Models/Student.php +++ b/app/Models/Student.php @@ -5,6 +5,7 @@ namespace Keating\Models; use Carbon\Carbon; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Concerns\HasUlids; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -16,6 +17,7 @@ * @property string $first_name * @property string $surname * @property string $index_number + * @property-read string $fullName * @property Carbon $created_at * @property Carbon $updated_at */ @@ -30,6 +32,13 @@ class Student extends Model "index_number", ]; + public function fullName(): Attribute + { + return new Attribute( + get: fn(): string => $this->first_name . " " . $this->surname, + ); + } + public function groups(): BelongsToMany { return $this->belongsToMany(Group::class, "student_group"); diff --git a/app/Providers/BuilderServiceProvider.php b/app/Providers/BuilderServiceProvider.php index 8c1eb274..d9f9e16a 100644 --- a/app/Providers/BuilderServiceProvider.php +++ b/app/Providers/BuilderServiceProvider.php @@ -9,9 +9,19 @@ class BuilderServiceProvider extends ServiceProvider { + /** + * all macros are also registered in .phpstorm.meta.php file + */ public function boot(): void { - Builder::macro("whereLikeUnaccentInsensitive", fn($column, $search): Builder => $this->whereRaw("unaccent($column) ILIKE unaccent(?)", ["%{$search}%"])); - Builder::macro("orWhereLikeUnaccentInsensitive", fn($column, $search): Builder => $this->orWhereRaw("unaccent($column) ILIKE unaccent(?)", ["%{$search}%"])); + Builder::macro( + "whereLikeUnaccentInsensitive", + fn($column, $search): Builder => $this->whereRaw("unaccent($column) ILIKE unaccent(?)", ["%{$search}%"]), + ); + + Builder::macro( + "orWhereLikeUnaccentInsensitive", + fn($column, $search): Builder => $this->orWhereRaw("unaccent($column) ILIKE unaccent(?)", ["%{$search}%"]), + ); } } diff --git a/codestyle.php b/codestyle.php index 3b73200a..e22e93d4 100644 --- a/codestyle.php +++ b/codestyle.php @@ -5,14 +5,12 @@ use Blumilk\Codestyle\Config; use Blumilk\Codestyle\Configuration\Defaults\CommonRules; use Blumilk\Codestyle\Configuration\Defaults\LaravelPaths; -use PhpCsFixer\Fixer\LanguageConstruct\ClassKeywordFixer; $paths = new LaravelPaths(); $rules = new CommonRules(); $config = new Config( paths: $paths->add("codestyle.php"), - rules: $rules->filter(ClassKeywordFixer::class), ); return $config->config(); diff --git a/database/factories/GradeColumnFactory.php b/database/factories/GradeColumnFactory.php index a37d4a58..b85a4edf 100644 --- a/database/factories/GradeColumnFactory.php +++ b/database/factories/GradeColumnFactory.php @@ -12,7 +12,7 @@ class GradeColumnFactory extends Factory public function definition(): array { return [ - "name" => fake()->asciify("***"), + "name" => fake()->numerify("W#"), "active" => fake()->boolean, "priority" => fake()->numberBetween(1, 100), "group_id" => Group::factory(), diff --git a/resources/js/Pages/Dashboard/CourseSemester/Grade/Import.vue b/resources/js/Pages/Dashboard/CourseSemester/Grade/Import.vue new file mode 100644 index 00000000..33c88daf --- /dev/null +++ b/resources/js/Pages/Dashboard/CourseSemester/Grade/Import.vue @@ -0,0 +1,96 @@ + + +