Skip to content

Commit

Permalink
sorting topicos
Browse files Browse the repository at this point in the history
  • Loading branch information
Lobz committed Jun 24, 2024
1 parent 8a9248e commit 58dd93d
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 4 deletions.
4 changes: 3 additions & 1 deletion app/Http/Controllers/ExercicioController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public function index()
public function create()
{
$this->authorize('create', Exercicio::class);
return View('exercicio.create')->with('pacotesR', $this->getInstalledPackages());
return View('exercicio.create')
->with('pacotesR', $this->getInstalledPackages())
->with('topicos', Topico::orderBy('order'));
}

/**
Expand Down
31 changes: 31 additions & 0 deletions app/Http/Controllers/TopicoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,37 @@ public function destroy(Topico $topico)
$topico->exercicios()->detach();

$topico->delete();
return redirect()->action([get_class($this), 'index']);
}

/**
* Show form for reordering of Topico
*/
public function sort()
{
$this->authorize('sort', Topico::class);
$topicos = Topico::orderBy('order')->get();

return View('topico.sort')->with('topicos', $topicos);
}

/**
* Update order of topicos
*/
public function order(Request $request)
{
$this->authorize('sort', Topico::class);
$rules = [
'topico_id.*' => 'required|int',
];

$data = $request->validate($rules);
foreach($data['topico_id'] as $i => $id) {

}



return redirect()->action([get_class($this), 'index']);
}
}
2 changes: 1 addition & 1 deletion app/Policies/TopicoPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function delete(User $user, Topico $topico)
* @param \App\Models\User $user
* @return mixed
*/
public function order(User $user)
public function sort(User $user)
{
return $user->isAdmin();
}
Expand Down
13 changes: 13 additions & 0 deletions resources/views/exercicio/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@
@enderror
</div>

<div class="form-group">
<label for="topico"><h2>Tópico</h2></label>
@php($old_topico = old('topico_id'))
<select id="topico_id" name="topico_id" >
<option value="" {{$old_topico == null ? 'selected' : ''}}></option>
@foreach ($topicos as $value)
<option value="{{$value->id}}" {{$old_topico == $value->id ? 'selected' : ''}}>{{$value->name}}</option>
@endforeach
</select>
@error('topico_id')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="description"><h2>Enunciado</h2></label>
<textarea class="form-control @error('description') is-invalid @enderror" id="description" name="description" placeholder="Salve o resultado da estatística xyz na variável abc">{{ old('description') }}</textarea>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/topico/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
href="{{ URL::to('exercicio/create') }}">Cadastrar exercicio
</a>
@endcan
@can ('order', App\Models\Topico::class)
@can ('sort', App\Models\Topico::class)
<a class="btn btn-small btn-edit inline"
href="{{ URL::to('topico/order') }}">Reordenar topicos
href="{{ URL::to('topico/sort') }}">Reordenar topicos
</a>
@endcan
@can ('create', App\Models\Topico::class)
Expand Down
35 changes: 35 additions & 0 deletions resources/views/topico/sort.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@extends('layouts.base')
@section('content')

<div class="container">
<form action="{{URL::to('/topico/sort')}}" method="PUT">
@csrf
@method ('put')

<div class="alert alert-info">
Arraste para reordenar!
</div>
<table class="table table-striped table-bordered sortable">
<thead>
<tr>
<td>Tópico</td>
</tr>
</thead>
<tbody>
@foreach (old('topico_id', $topicos) as $value)
<tr>
<td>
<input type='hidden'
id="topico_id[]"
name="topico_id[]"
value="{{$value->id}}">
{{ $value->name }}
</td>
</tr>
@endforeach
</tbody>
</table>
<button type="submit" class="btn btn-primary">Salvar</button>
</form>
</div>
@endsection
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
Route::get('/exercicio/{exercicio}/export', [ExercicioController::class, 'export'])->name('exercicio.export');
Route::put('/exercicio/{exercicio}/import', [ExercicioController::class, 'importEdit']);

Route::get('/topico/sort', [TopicoController::class, 'sort'])->name('topico.sort');
Route::put('/topico/sort', [TopicoController::class, 'order'])->name('topico.order');

Route::resources([
'user' => UserController::class,
'turma' => TurmaController::class,
Expand Down

0 comments on commit 58dd93d

Please sign in to comment.