Files
Sistema-Educativo-Laravel/app/Livewire/GrupoDocente.php
T

151 lines
5.1 KiB
PHP

<?php
namespace App\Livewire;
use App\Models\Docente;
use App\Models\Entrega;
use App\Models\Material;
use App\Models\Tarea;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Livewire\Component;
class GrupoDocente extends Component
{
public $grupo;
public $docentes;
public $materias;
public $docente_id;
public $materia_id;
public $materiasSelect;
// Para reemplazo de docente
public $reemplazoDocenteActualId;
public $reemplazoMateriaId;
public $reemplazoDocenteId = '';
public $reemplazoMateriaNombre = '';
public function mount($grupo)
{
$this->grupo = $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();
$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)
->when(!empty($materiasAsignadas), fn($q) => $q->whereNotIn('id', $materiasAsignadas))
->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)
{
// Borrar archivos físicos de las entregas antes de eliminar en cascada
$tareaIds = Tarea::where('grupo_id', $this->grupo->id)
->where('material_id', $materiaId)
->where('docente_id', $docenteId)
->pluck('id');
Entrega::whereIn('tarea_id', $tareaIds)
->whereNotNull('archivo')
->pluck('archivo')
->each(fn($path) => Storage::delete($path));
Tarea::whereIn('id', $tareaIds)->delete();
$this->grupo->docentes()->wherePivot('materia_id', $materiaId)->detach($docenteId);
$this->dispatch('swal',
icon: 'success', title: 'Eliminado',
text: 'Docente y sus tareas removidos correctamente',
timer: 2000, confirm: false, closeModal: true,
);
}
public function abrirReemplazo($docenteId, $materiaId, $materiaNombre)
{
$this->reemplazoDocenteActualId = $docenteId;
$this->reemplazoMateriaId = $materiaId;
$this->reemplazoMateriaNombre = $materiaNombre;
$this->reemplazoDocenteId = '';
$this->dispatch('open-modal-reemplazo');
}
public function reemplazar()
{
$this->validate([
'reemplazoDocenteId' => 'required|exists:docentes,id|different:reemplazoDocenteActualId',
], [
'reemplazoDocenteId.required' => 'Selecciona un docente.',
'reemplazoDocenteId.different' => 'Debes elegir un docente diferente al actual.',
]);
// Actualizar el registro en docente_grupo
DB::table('docente_grupo')
->where('grupo_id', $this->grupo->id)
->where('materia_id', $this->reemplazoMateriaId)
->where('docente_id', $this->reemplazoDocenteActualId)
->update(['docente_id' => $this->reemplazoDocenteId]);
// Reasignar las tareas al nuevo docente (conserva entregas intactas)
Tarea::where('grupo_id', $this->grupo->id)
->where('material_id', $this->reemplazoMateriaId)
->where('docente_id', $this->reemplazoDocenteActualId)
->update(['docente_id' => $this->reemplazoDocenteId]);
$this->reset(['reemplazoDocenteId', 'reemplazoMateriaId', 'reemplazoDocenteActualId', 'reemplazoMateriaNombre']);
$this->dispatch('close-modal-reemplazo');
$this->dispatch('swal',
icon: 'success', title: '¡Docente reemplazado!',
text: 'El nuevo docente fue asignado y las tareas previas se conservaron.',
timer: 3500, confirm: false, closeModal: true,
);
}
public function render()
{
return view('livewire.grupo-docente');
}
}