From a069ea434877898c51dba77aad00be497101fd0d Mon Sep 17 00:00:00 2001 From: Mali Oz Date: Wed, 24 Jul 2024 13:45:02 -0300 Subject: [PATCH 01/27] remove unused function --- app/Models/User.php | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index 3098f10..56ebdc3 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -64,19 +64,6 @@ public function isAdmin() { return $this->is_admin; } - /** - * Nota final (atual) dentro do prazo (get from DB) - * - * @var Prazo $prazo - * @return float - */ - public function getNotaFinal(Prazo $prazo) { - return $this->notas()-> - where('exercicio_id',$prazo->exercicio_id)-> //exercicio correto - where('created_at','<',$prazo->prazo)-> //dentro do prazo - get()->max('nota'); //maior nota - } - /** * Nota final (atual) dentro do prazo * From 8675d2968529aff308cd6a3f04083c57551b9e4f Mon Sep 17 00:00:00 2001 From: Mali Oz Date: Wed, 24 Jul 2024 18:26:14 -0300 Subject: [PATCH 02/27] Isolated validations and bulk add users --- app/Http/Controllers/TurmaController.php | 116 +++++++++++++++-------- 1 file changed, 76 insertions(+), 40 deletions(-) diff --git a/app/Http/Controllers/TurmaController.php b/app/Http/Controllers/TurmaController.php index acfc1cc..b2abb27 100644 --- a/app/Http/Controllers/TurmaController.php +++ b/app/Http/Controllers/TurmaController.php @@ -14,6 +14,30 @@ class TurmaController extends Controller { + /** + * Validate model input + * + * @param \Illuminate\Http\Request $request + * @return mixed + */ + private function validateRequest(Request $request, Turma|null $model = null) + { + $rules = [ + 'name' => 'required|unique:turmas' . ($model ? ',name,' . $model->id : ''), + 'description'=> 'required', + 'maillist' => [ + 'file', + new CsvRule([ + 'name' => 'required', + 'email' => 'required|email' + ]) + ], + 'defaultpassword' => 'required_with:maillist' + ]; + + return $request->validate($rules); + } + /** * Display a listing of the resource. * @@ -28,13 +52,26 @@ public function index() /** * Show the form for creating a new resource. * - * @return \Illuminate\Http\Response + * @return View */ public function create() { + $this->authorize('create', Turma::class); return View('turma.create'); } + /** + * Show the form for creating a copy of a model + * + * @param \App\Models\Turma $turma + * @return \Illuminate\Http\RedirectResponse + */ + public function duplicate(Turma $turma) + { + $this->authorize('create', Turma::class); + + } + /** * Store a newly created resource in storage. * @@ -44,14 +81,18 @@ public function create() public function store(Request $request) { $this->authorize('create', Turma::class); - $rules = array( - 'name' => 'required', - 'description'=> 'required', - ); - $data = $request->validate($rules); + $data = $this->validateRequest($request); // store $turma = tap(new Turma($data))->save(); + + // bulk add users + if ($request->maillist ?? "") { + $this->bulkAddUsers($turma, + new Csv($request->maillist->get()), + $request->defaultpassword); + } + return redirect()->action([get_class($this),'show'], ['turma' => $turma]); } @@ -155,43 +196,16 @@ public function updateprazos(Request $request, Turma $turma) public function update(Request $request, Turma $turma) { $this->authorize('edit',$turma); - $rules = [ - 'name' => 'required', - 'description'=> 'required', - 'maillist' => [ - 'file', - new CsvRule([ - 'name' => 'required', - 'email' => 'required|email' - ]) - ], - 'defaultpassword' => 'required_with:maillist' - ]; - $data = $request->validate($rules); - $turma->update(['name' => $data['name'],'description' => $data['description']]); + $data = $this->validateRequest($request, $turma); + $turma->update( + ['name' => $data['name'],'description' => $data['description']] + ); // bulk add users if ($request->maillist ?? "") { - $csv = new Csv($request->maillist->get()); - $pssw = $request->defaultpassword; - foreach( $csv->getData() as $user ) { - $email = $user['email']; - $name = $user['name']; - $newmember = User::where('email', $email)->first(); - if($newmember) { - // if user not in turma, add - if ($turma->users()->find($newmember) == null) { - $turma->users()->save($newmember); - } - } else { - // if user doesn't exist, create new - $newmember = User::create([ - 'email' => $email, - 'name' => $name, - 'password' => $pssw]); - $turma->users()->save($newmember); - } - } + $this->bulkAddUsers($turma, + new Csv($request->maillist->get()), + $request->defaultpassword); } return redirect()->action([get_class($this),'show'], ['turma' => $turma]); @@ -229,4 +243,26 @@ public function destroy(Turma $turma) $turma->delete(); return redirect()->action([get_class($this),'index']); } + + protected function bulkAddUsers (Turma $turma, Csv $csv, String $psswd) + { + foreach( $csv->getData() as $user ) { + $email = $user['email']; + $name = $user['name']; + $newmember = User::where('email', $email)->first(); + if($newmember) { + // if user not in turma, add + if ($turma->users()->find($newmember) == null) { + $turma->users()->save($newmember); + } + } else { + // if user doesn't exist, create new + $newmember = User::create([ + 'email' => $email, + 'name' => $name, + 'password' => $psswd]); + $turma->users()->save($newmember); + } + } + } } From 97a586c9edba342fa9b6baaa56c0de5ac8589aac Mon Sep 17 00:00:00 2001 From: Mali Oz Date: Wed, 24 Jul 2024 18:28:19 -0300 Subject: [PATCH 03/27] small refactor on validate --- app/Http/Controllers/ExercicioController.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/ExercicioController.php b/app/Http/Controllers/ExercicioController.php index 57977c7..2407efe 100644 --- a/app/Http/Controllers/ExercicioController.php +++ b/app/Http/Controllers/ExercicioController.php @@ -59,12 +59,12 @@ public function create() } /** - * Store a newly created resource in storage. + * Validate model * * @param \Illuminate\Http\Request $request * @return mixed */ - private function validateExercicio(Request $request, Exercicio|null $exercicio) + private function validateRequest(Request $request, Exercicio|null $exercicio = null) { $rules = array( 'name' => 'required|string|unique:exercicios' . ($exercicio ? ',name,' . $exercicio->id : ''), @@ -98,7 +98,7 @@ public function store(Request $request) { $this->authorize('create', Exercicio::class); - $data = $this->validateExercicio($request, null); + $data = $this->validateRequest($request); // store $exercicio = new Exercicio($data); DB::transaction(function () use ($data, $exercicio) { @@ -393,7 +393,7 @@ public function edit(Exercicio $exercicio) public function update(Request $request, Exercicio $exercicio) { $this->authorize('edit', $exercicio); - $data = $this->validateExercicio($request, $exercicio); + $data = $this->validateRequest($request, $exercicio); // store DB::transaction(function () use ($data, $exercicio) { From 8436dce53e88c3bdfd9870e84f8dcf78258bf30f Mon Sep 17 00:00:00 2001 From: Mali Oz Date: Wed, 24 Jul 2024 19:14:02 -0300 Subject: [PATCH 04/27] copy prazos from --- app/Http/Controllers/TurmaController.php | 24 ++++++--- resources/views/turma/create.blade.php | 67 +++++++++++++++++------- resources/views/turma/edit.blade.php | 4 +- 3 files changed, 68 insertions(+), 27 deletions(-) diff --git a/app/Http/Controllers/TurmaController.php b/app/Http/Controllers/TurmaController.php index b2abb27..07339d4 100644 --- a/app/Http/Controllers/TurmaController.php +++ b/app/Http/Controllers/TurmaController.php @@ -11,6 +11,7 @@ use App\Models\Prazo; use App\Rules\CsvRule; use App\Utils\Csv; +use Illuminate\Support\Facades\DB; class TurmaController extends Controller { @@ -32,10 +33,12 @@ private function validateRequest(Request $request, Turma|null $model = null) 'email' => 'required|email' ]) ], - 'defaultpassword' => 'required_with:maillist' + 'defaultpassword' => 'required_with:maillist', + 'copyfrom' => 'int|exists:turmas,id|nullable' ]; - return $request->validate($rules); + $data = $request->validate($rules); + return ['name' => $data['name'], 'description' => $data['description']]; } /** @@ -57,7 +60,7 @@ public function index() public function create() { $this->authorize('create', Turma::class); - return View('turma.create'); + return View('turma.create')->with('turmas', Turma::all()); } /** @@ -83,6 +86,7 @@ public function store(Request $request) $this->authorize('create', Turma::class); $data = $this->validateRequest($request); + DB::beginTransaction(); // store $turma = tap(new Turma($data))->save(); @@ -93,6 +97,16 @@ public function store(Request $request) $request->defaultpassword); } + // copy prazos from another turma + if ($request->copyfrom ?? "") { + $original = Turma::find($request->copyfrom); + foreach ($original->prazos as $prazo) { + $newprazo = $prazo->replicate(); + $turma->prazos()->save($newprazo); + } + } + DB::commit(); + return redirect()->action([get_class($this),'show'], ['turma' => $turma]); } @@ -197,9 +211,7 @@ public function update(Request $request, Turma $turma) { $this->authorize('edit',$turma); $data = $this->validateRequest($request, $turma); - $turma->update( - ['name' => $data['name'],'description' => $data['description']] - ); + $turma->update($data); // bulk add users if ($request->maillist ?? "") { diff --git a/resources/views/turma/create.blade.php b/resources/views/turma/create.blade.php index 52d5dcf..d06b045 100644 --- a/resources/views/turma/create.blade.php +++ b/resources/views/turma/create.blade.php @@ -1,29 +1,56 @@ @extends('layouts.base') @section('content') -
+

Criar nova turma

- @csrf - @include ('includes.error_alert') -
- - - @error('name') -
{{ $message }}
- @enderror -
-
- - - @error('description') -
{{ $message }}
- @enderror -
- + @csrf + @include ('includes.error_alert') + +
+ + + @error('name') +
{{ $message }}
+ @enderror +
+ +
+ + + @error('description') +
{{ $message }}
+ @enderror +
+ +
+ + @php($original_turma = old('copyfrom')) + + @error('copyfrom') +
{{ $message }}
+ @enderror +
+ +
-
- @endsection +
+@endsection diff --git a/resources/views/turma/edit.blade.php b/resources/views/turma/edit.blade.php index b792e2f..f909eaf 100644 --- a/resources/views/turma/edit.blade.php +++ b/resources/views/turma/edit.blade.php @@ -25,7 +25,9 @@ @enderror
- + {{ old('maillist','') }} @error('maillist')
{{ $message }}
From 9ac00f63a980addcf6698d7a465a7be1f3350c47 Mon Sep 17 00:00:00 2001 From: Mali Oz Date: Wed, 24 Jul 2024 19:14:16 -0300 Subject: [PATCH 05/27] indent --- resources/views/exercicio/create.blade.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/resources/views/exercicio/create.blade.php b/resources/views/exercicio/create.blade.php index 0d18804..08bc52f 100644 --- a/resources/views/exercicio/create.blade.php +++ b/resources/views/exercicio/create.blade.php @@ -10,6 +10,7 @@
@csrf @method ('put') + @error('file') @@ -17,9 +18,11 @@ @enderror
+ @if (old('from_import',false))
Arquivo carregado. Verifique os campos antes de salvar!
@endif +
@csrf @@ -28,7 +31,10 @@
- + @error('name')
{{ $message }}
@enderror @@ -47,6 +53,7 @@
{{ $message }}
@enderror
+
From f143e2186f6964bcc1d513db7a1a0d44aeff2c9f Mon Sep 17 00:00:00 2001 From: Mali Oz Date: Fri, 26 Jul 2024 14:58:33 -0300 Subject: [PATCH 06/27] Curso model and migratiom --- app/Models/Curso.php | 22 ++++++++++++ app/Models/Topico.php | 1 - app/Models/Turma.php | 4 +++ .../2024_07_25_122648_create_cursos_table.php | 36 +++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 app/Models/Curso.php create mode 100644 database/migrations/2024_07_25_122648_create_cursos_table.php diff --git a/app/Models/Curso.php b/app/Models/Curso.php new file mode 100644 index 0000000..20a3df6 --- /dev/null +++ b/app/Models/Curso.php @@ -0,0 +1,22 @@ +hasMany(Turma::class); + } +} diff --git a/app/Models/Topico.php b/app/Models/Topico.php index 052ed15..f07088d 100644 --- a/app/Models/Topico.php +++ b/app/Models/Topico.php @@ -9,7 +9,6 @@ class Topico extends Model { use HasFactory; protected $table = 'topicos'; - protected $guarded = []; // relationships public function exercicios() diff --git a/app/Models/Turma.php b/app/Models/Turma.php index 6bdcfbb..b4a3c20 100644 --- a/app/Models/Turma.php +++ b/app/Models/Turma.php @@ -24,6 +24,10 @@ public function users() { return $this->belongsToMany(User::class); } + public function curso() + { + return $this->belongsTo(Curso::class); + } public function prazos() { return $this->hasMany(Prazo::class); diff --git a/database/migrations/2024_07_25_122648_create_cursos_table.php b/database/migrations/2024_07_25_122648_create_cursos_table.php new file mode 100644 index 0000000..2f24642 --- /dev/null +++ b/database/migrations/2024_07_25_122648_create_cursos_table.php @@ -0,0 +1,36 @@ +id(); + $table->string('name'); + $table->string('description'); + $table->timestamps(); + }); + Schema::table('turmas', function (Blueprint $table) { + $table->foreignId('curso_id')->nullable()->constrained(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('turmas', function (Blueprint $table) { + $table->dropForeign(['curso_id']); + $table->dropColumn('curso_id'); + }); + Schema::dropIfExists('cursos'); + } +}; From 315383f5a681e5a7fa6b686a60cce3b3ce4cebbe Mon Sep 17 00:00:00 2001 From: Mali Oz Date: Fri, 26 Jul 2024 16:28:05 -0300 Subject: [PATCH 07/27] curso factory and policy --- app/Policies/CursoPolicy.php | 47 +++++++++++++++++++++++++++++ database/factories/CursoFactory.php | 29 ++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 app/Policies/CursoPolicy.php create mode 100644 database/factories/CursoFactory.php diff --git a/app/Policies/CursoPolicy.php b/app/Policies/CursoPolicy.php new file mode 100644 index 0000000..92ad4ca --- /dev/null +++ b/app/Policies/CursoPolicy.php @@ -0,0 +1,47 @@ +isAdmin(); + } + + /** + * Determine whether the user can update the model. + * + * @param \App\Models\User $user + * @param \App\Models\Curso $curso + * @return mixed + */ + public function edit(User $user, Curso $curso) + { + return $user->isAdmin(); + } + + /** + * Determine whether the user can delete the model. + * + * @param \App\Models\User $user + * @param \App\Models\Curso $curso + * @return mixed + */ + public function delete(User $user, Curso $curso) + { + return $user->isAdmin(); + } +} diff --git a/database/factories/CursoFactory.php b/database/factories/CursoFactory.php new file mode 100644 index 0000000..76dc47d --- /dev/null +++ b/database/factories/CursoFactory.php @@ -0,0 +1,29 @@ + substr($this->faker->sentence(2), 0, -1), + 'description' => $this->faker->paragraph, + ]; + } +} From daa224d826e9404056b2579daaacb52250ed9ecc Mon Sep 17 00:00:00 2001 From: Mali Oz Date: Fri, 2 Aug 2024 15:37:09 -0300 Subject: [PATCH 08/27] add curso_id --- app/Http/Controllers/TurmaController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/TurmaController.php b/app/Http/Controllers/TurmaController.php index 07339d4..cd88d7d 100644 --- a/app/Http/Controllers/TurmaController.php +++ b/app/Http/Controllers/TurmaController.php @@ -26,6 +26,7 @@ private function validateRequest(Request $request, Turma|null $model = null) $rules = [ 'name' => 'required|unique:turmas' . ($model ? ',name,' . $model->id : ''), 'description'=> 'required', + 'curso_id' => 'sometimes|int|exists:cursos,id|nullable', 'maillist' => [ 'file', new CsvRule([ @@ -38,7 +39,7 @@ private function validateRequest(Request $request, Turma|null $model = null) ]; $data = $request->validate($rules); - return ['name' => $data['name'], 'description' => $data['description']]; + return ['name' => $data['name'], 'description' => $data['description'], 'curso_id' => $data['curso_id']]; } /** From dcecc09a7c966a4e31446ee7a58cf19b1ed771a2 Mon Sep 17 00:00:00 2001 From: Mali Oz Date: Fri, 2 Aug 2024 15:37:17 -0300 Subject: [PATCH 09/27] CursoController --- app/Http/Controllers/CursoController.php | 97 ++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 app/Http/Controllers/CursoController.php diff --git a/app/Http/Controllers/CursoController.php b/app/Http/Controllers/CursoController.php new file mode 100644 index 0000000..f10b4e3 --- /dev/null +++ b/app/Http/Controllers/CursoController.php @@ -0,0 +1,97 @@ +with('turmas'); + + return View('curso.index')->with('cursos', $cursos->get()) + ->with('semCurso', $semCurso->get()); + } + + /** + * Show the form for creating a new resource. + */ + public function create() + { + return View('curso.create'); + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + $this->authorize('create', Curso::class); + $rules = array( + 'name' => 'required', + ); + $data = $request->validate($rules); + + // store + $curso = tap(new Curso($data))->save(); + + // redirect to show + return redirect()->action([get_class($this), 'show'], ['curso' => $curso]); + } + + /** + * Display the specified resource. + */ + public function show(Curso $curso) + { + // index is the same for turma and curso + return redirect()->action([get_class($this), 'index']); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(Curso $curso) + { + $this->authorize('edit', $curso); + return View('curso.edit')->with('curso', $curso); + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, Curso $curso) + { + $this->authorize('edit', $curso); + + $rules = [ + 'name' => 'required', + ]; + $data = $request->validate($rules); + $curso->update(['name' => $data['name']]); + return redirect()->action([get_class($this), 'index']); + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(Curso $curso) + { + $this->authorize('delete', $curso); + // remove turmas + $curso->turmas()->detach(); + + $curso->delete(); + return redirect()->action([get_class($this), 'index']); + } + +} From 2b1c6a2321bd46fa3eb867d736516f65f2235070 Mon Sep 17 00:00:00 2001 From: Mali Oz Date: Fri, 2 Aug 2024 15:37:34 -0300 Subject: [PATCH 10/27] copy views from topico do curso --- resources/views/curso/create.blade.php | 21 ++++++++++++++ resources/views/curso/edit.blade.php | 38 ++++++++++++++++++++++++++ resources/views/curso/table.blade.php | 34 +++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 resources/views/curso/create.blade.php create mode 100644 resources/views/curso/edit.blade.php create mode 100644 resources/views/curso/table.blade.php diff --git a/resources/views/curso/create.blade.php b/resources/views/curso/create.blade.php new file mode 100644 index 0000000..6971e71 --- /dev/null +++ b/resources/views/curso/create.blade.php @@ -0,0 +1,21 @@ +@extends('layouts.base') +@section('content') +
+
+

Criar novo curso

+
+
+ + @csrf + @include ('includes.error_alert') +
+ + @error('name') +
{{ $message }}
+ @enderror +
+ + +
+
+ @endsection diff --git a/resources/views/curso/edit.blade.php b/resources/views/curso/edit.blade.php new file mode 100644 index 0000000..b7c38b7 --- /dev/null +++ b/resources/views/curso/edit.blade.php @@ -0,0 +1,38 @@ +@extends('layouts.base') +@section('content') + +
+
+

Editando {{ $curso->name }}

+
+
+
id)}} method="POST" > + @csrf + @method('PUT') + @include ('includes.error_alert') + +
+ + + @error('name') +
{{ $message }}
+ @enderror +
+ + +
+
+ + @can ('delete', $curso) +
id)}}"> + {{ csrf_field() }} + {{ method_field('DELETE') }} + +
+ +
+
+ @endcan + +
+@endsection diff --git a/resources/views/curso/table.blade.php b/resources/views/curso/table.blade.php new file mode 100644 index 0000000..0780598 --- /dev/null +++ b/resources/views/curso/table.blade.php @@ -0,0 +1,34 @@ +@foreach ($cursos as $value) + +

Editar + @endcan +

+ +
+ @include('turma.table', ['editButton' => true, 'turmas' => $value->turmas]) +
+ +@endforeach + +{{-- turmas sem curso --}} + +
+ @include('turma.table', ['editButton' => true, 'turmas' => $semcurso]) +
\ No newline at end of file From ad9bc47fd4c6b2cebe6c536531ebcca29d259835 Mon Sep 17 00:00:00 2001 From: Mali Oz Date: Fri, 2 Aug 2024 15:37:48 -0300 Subject: [PATCH 11/27] Curso link on sidebar --- resources/views/includes/sidebar.blade.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/resources/views/includes/sidebar.blade.php b/resources/views/includes/sidebar.blade.php index d221e1a..bca75ce 100644 --- a/resources/views/includes/sidebar.blade.php +++ b/resources/views/includes/sidebar.blade.php @@ -1,10 +1,8 @@
+
+ + + + + @error('{{"prazos[]"}}') +
{{ $message }}
+ @enderror +
+
From b05ffad3cc122adb860962ea8da87d15dab1ae0d Mon Sep 17 00:00:00 2001 From: Mali Oz Date: Fri, 2 Aug 2024 18:46:47 -0300 Subject: [PATCH 26/27] remove wrong type hint --- app/Http/Controllers/CursoController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Http/Controllers/CursoController.php b/app/Http/Controllers/CursoController.php index f10b4e3..fed819b 100644 --- a/app/Http/Controllers/CursoController.php +++ b/app/Http/Controllers/CursoController.php @@ -15,7 +15,6 @@ public function index() { $cursos = Curso::orderBy('name'); $semCurso = Turma::whereDoesntHave('curso'); - /** @var \App\Models\User */ $cursos = $cursos->with('turmas'); return View('curso.index')->with('cursos', $cursos->get()) From df8f7893494dbe80dc736d0a6f4d7ee2488ff524 Mon Sep 17 00:00:00 2001 From: Mali Oz Date: Fri, 2 Aug 2024 22:01:00 -0300 Subject: [PATCH 27/27] remove duplicate (abandoned feature) --- app/Http/Controllers/TurmaController.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/app/Http/Controllers/TurmaController.php b/app/Http/Controllers/TurmaController.php index 12ae745..f7f6160 100644 --- a/app/Http/Controllers/TurmaController.php +++ b/app/Http/Controllers/TurmaController.php @@ -68,18 +68,6 @@ public function create() ->with('cursos', Curso::all()); } - /** - * Show the form for creating a copy of a model - * - * @param \App\Models\Turma $turma - * @return \Illuminate\Http\RedirectResponse - */ - public function duplicate(Turma $turma) - { - $this->authorize('create', Turma::class); - - } - /** * Store a newly created resource in storage. *