99 lines
2.7 KiB
PHP
99 lines
2.7 KiB
PHP
<?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: 3000,
|
|
confirm: false,
|
|
closeModal: true,
|
|
);
|
|
}
|
|
|
|
public function eliminar($docenteId, $materiaId)
|
|
{
|
|
$this->grupo->docentes()->wherePivot('materia_id', $materiaId)->detach($docenteId);
|
|
|
|
$this->dispatch('swal',
|
|
icon: 'success',
|
|
title: 'Eliminado',
|
|
text: 'Docente removido de la materia correctamente',
|
|
timer: 2000,
|
|
confirm: false,
|
|
closeModal: true,
|
|
);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.grupo-docente');
|
|
}
|
|
}
|