Alta de modificaciones de grupo con alumbnos y docentes
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
namespace App\Livewire;
|
||||
|
||||
use App\Models\Docente;
|
||||
use App\Models\Material;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Livewire\Component;
|
||||
|
||||
class GrupoDocente extends Component
|
||||
{
|
||||
public $grupo;
|
||||
public $docentes;
|
||||
public $materias;
|
||||
public $docente_id;
|
||||
public $materia_id;
|
||||
public $materiasSelect;
|
||||
|
||||
public function mount($grupo)
|
||||
{
|
||||
$this->grupo = $grupo; // <- primero asignar el grupo
|
||||
|
||||
$user = Auth::user();
|
||||
$plantelesIds = $user->plantelUsuarios->pluck('id');
|
||||
|
||||
$this->docentes = Docente::with(['docentes.plantelUsuarios'])
|
||||
->whereHas('docentes', function($query) use ($plantelesIds) {
|
||||
$query->whereHas('plantelUsuarios', function($q) use ($plantelesIds) {
|
||||
$q->whereIn('plantels.id', $plantelesIds);
|
||||
});
|
||||
})
|
||||
->get();
|
||||
|
||||
// Todas las materias de la carrera sin filtro de ciclo
|
||||
$this->materias = Material::whereHas('carreraMaterial', function($query) use ($grupo) {
|
||||
$query->where('carreras.id', $grupo->plan->carrera);
|
||||
})
|
||||
->orderBy('ciclo')
|
||||
->get();
|
||||
|
||||
$materiasAsignadas = DB::table('docente_grupo')
|
||||
->where('grupo_id', $this->grupo->id)
|
||||
->pluck('materia_id')
|
||||
->toArray();
|
||||
|
||||
$this->materiasSelect = Material::whereHas('carreraMaterial', function($query) use ($grupo) {
|
||||
$query->where('carreras.id', $grupo->plan->carrera);
|
||||
})
|
||||
->where('ciclo', $this->grupo->ciclo) // solo ciclo actual
|
||||
->when(!empty($materiasAsignadas), function($query) use ($materiasAsignadas) {
|
||||
$query->whereNotIn('id', $materiasAsignadas); // excluir ya asignadas
|
||||
})
|
||||
->get();
|
||||
}
|
||||
|
||||
public function guardar()
|
||||
{
|
||||
$this->validate([
|
||||
'docente_id' => 'required|exists:docentes,id',
|
||||
'materia_id' => 'required|exists:materials,id',
|
||||
]);
|
||||
|
||||
$this->grupo->docentes()->attach($this->docente_id, [
|
||||
'materia_id' => $this->materia_id
|
||||
]);
|
||||
|
||||
$this->reset(['docente_id', 'materia_id']);
|
||||
$this->dispatch('close-modal-docente');
|
||||
|
||||
$this->dispatch('swal',
|
||||
icon: 'success',
|
||||
title: '¡Listo!',
|
||||
text: 'Docente asignado correctamente',
|
||||
timer: 2000,
|
||||
confirm: false,
|
||||
closeModal: true,
|
||||
reload: true
|
||||
);
|
||||
}
|
||||
|
||||
public function eliminar($docenteId)
|
||||
{
|
||||
$this->grupo->docentes()->detach($docenteId);
|
||||
|
||||
$this->dispatch('swal',
|
||||
icon: 'success',
|
||||
title: '¡Eliminado!',
|
||||
text: 'Docente removido correctamente',
|
||||
timer: 2000,
|
||||
confirm: false,
|
||||
closeModal: false,
|
||||
reload: true
|
||||
);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.grupo-docente');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user