81 lines
2.5 KiB
PHP
81 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Alumno;
|
|
use App\Models\Entrega;
|
|
use App\Models\Tarea;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class EntregaController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$alumno = Alumno::whereHas('alumnos', fn($q) => $q->where('id', Auth::id()))->first();
|
|
|
|
if (!$alumno) {
|
|
return view('tarea.mis_tareas', ['tareas' => collect(), 'entregasMap' => collect()]);
|
|
}
|
|
|
|
$gruposIds = $alumno->grupos()->pluck('grupos.id');
|
|
|
|
$tareas = Tarea::whereIn('grupo_id', $gruposIds)
|
|
->with(['material', 'grupo'])
|
|
->orderByDesc('created_at')
|
|
->get();
|
|
|
|
$entregasMap = Entrega::where('alumno_id', $alumno->id)
|
|
->whereIn('tarea_id', $tareas->pluck('id'))
|
|
->get()
|
|
->keyBy('tarea_id');
|
|
|
|
return view('tarea.mis_tareas', compact('tareas', 'entregasMap', 'alumno'));
|
|
}
|
|
|
|
public function store(Request $request, Tarea $tarea)
|
|
{
|
|
$request->validate([
|
|
'archivo' => 'nullable|file|max:20480',
|
|
'texto' => 'nullable|string|max:5000',
|
|
]);
|
|
|
|
if (!$request->hasFile('archivo') && !filled($request->texto)) {
|
|
return back()->withErrors(['entrega' => 'Debes escribir un texto o adjuntar un archivo.']);
|
|
}
|
|
|
|
$alumno = Alumno::whereHas('alumnos', fn($q) => $q->where('id', Auth::id()))->firstOrFail();
|
|
|
|
$entrega = Entrega::where('tarea_id', $tarea->id)
|
|
->where('alumno_id', $alumno->id)
|
|
->first();
|
|
|
|
if (!$entrega) {
|
|
$entrega = Entrega::create([
|
|
'tarea_id' => $tarea->id,
|
|
'alumno_id' => $alumno->id,
|
|
'status' => 'sin_subir',
|
|
]);
|
|
}
|
|
|
|
if ($entrega->status !== 'sin_subir') {
|
|
return back()->with('error', 'Ya entregaste esta tarea y no puedes volver a subirla.');
|
|
}
|
|
|
|
$path = null;
|
|
if ($request->hasFile('archivo')) {
|
|
// Almacena en disco local (privado) igual que el FileController espera
|
|
$path = $request->file('archivo')->store("entregas/{$tarea->id}");
|
|
}
|
|
|
|
$entrega->update([
|
|
'archivo' => $path,
|
|
'texto' => $request->texto,
|
|
'status' => 'pendiente',
|
|
]);
|
|
|
|
return back()->with('info', 'Tarea enviada correctamente. Queda pendiente de calificación.');
|
|
}
|
|
}
|