|
|
|
|
@ -57,209 +57,185 @@ class PreguntaController extends Controller
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getPregunta($id)
|
|
|
|
|
{
|
|
|
|
|
$pregunta = Pregunta::find($id);
|
|
|
|
|
|
|
|
|
|
if (!$pregunta) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'message' => 'Pregunta no encontrada'
|
|
|
|
|
], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getPregunta($id)
|
|
|
|
|
{
|
|
|
|
|
$pregunta = Pregunta::find($id);
|
|
|
|
|
|
|
|
|
|
if (!$pregunta) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'data' => $pregunta
|
|
|
|
|
]);
|
|
|
|
|
'success' => false,
|
|
|
|
|
'message' => 'Pregunta no encontrada'
|
|
|
|
|
], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function agregarPreguntaCurso(Request $request)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$user = auth()->user();
|
|
|
|
|
$pregunta->imagenes = collect($pregunta->imagenes ?? [])->map(fn($path) => $path ? url(Storage::url($path)) : null);
|
|
|
|
|
$pregunta->imagenes_explicacion = collect($pregunta->imagenes_explicacion ?? [])->map(fn($path) => $path ? url(Storage::url($path)) : null);
|
|
|
|
|
|
|
|
|
|
if (!$user->hasRole('Admin')) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'message' => 'No autorizado'
|
|
|
|
|
], 403);
|
|
|
|
|
}
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'data' => $pregunta
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
|
|
|
'curso_id' => 'required|exists:cursos,id',
|
|
|
|
|
'enunciado' => 'required|string',
|
|
|
|
|
'enunciado_adicional' => 'nullable|string',
|
|
|
|
|
'opciones' => 'required|array|min:2',
|
|
|
|
|
'opciones.*' => 'required|string',
|
|
|
|
|
'respuesta_correcta' => 'required|string',
|
|
|
|
|
'explicacion' => 'nullable|string',
|
|
|
|
|
'nivel_dificultad' => 'required|in:facil,medio,dificil',
|
|
|
|
|
'activo' => 'boolean',
|
|
|
|
|
'imagenes' => 'nullable|array',
|
|
|
|
|
'imagenes.*' => 'image|mimes:jpg,jpeg,png,gif,webp|max:2048',
|
|
|
|
|
'imagenes_explicacion' => 'nullable|array',
|
|
|
|
|
'imagenes_explicacion.*' => 'image|mimes:jpg,jpeg,png,gif,webp|max:2048',
|
|
|
|
|
], [
|
|
|
|
|
'opciones.required' => 'Debe agregar al menos 2 opciones',
|
|
|
|
|
'opciones.min' => 'Debe agregar al menos 2 opciones',
|
|
|
|
|
'respuesta_correcta.required' => 'Debe seleccionar una respuesta correcta',
|
|
|
|
|
]);
|
|
|
|
|
public function agregarPreguntaCurso(Request $request)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$user = auth()->user();
|
|
|
|
|
if (!$user->hasRole('administrador')) {
|
|
|
|
|
return response()->json(['success' => false, 'message' => 'No autorizado'], 403);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'errors' => $validator->errors()
|
|
|
|
|
], 422);
|
|
|
|
|
}
|
|
|
|
|
// Validación (igual que antes)
|
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
|
|
|
'curso_id' => 'required|exists:cursos,id',
|
|
|
|
|
'enunciado' => 'required|string',
|
|
|
|
|
'enunciado_adicional' => 'nullable|string',
|
|
|
|
|
'opciones' => 'required',
|
|
|
|
|
'respuesta_correcta' => 'required|string',
|
|
|
|
|
'explicacion' => 'nullable|string',
|
|
|
|
|
'nivel_dificultad' => 'required|in:facil,medio,dificil',
|
|
|
|
|
'activo' => 'boolean',
|
|
|
|
|
'imagenes' => 'nullable|array',
|
|
|
|
|
'imagenes.*' => 'image|mimes:jpg,jpeg,png,gif,webp|max:2048',
|
|
|
|
|
'imagenes_explicacion' => 'nullable|array',
|
|
|
|
|
'imagenes_explicacion.*' => 'image|mimes:jpg,jpeg,png,gif,webp|max:2048',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Validar que la respuesta correcta esté en las opciones
|
|
|
|
|
if (!in_array($request->respuesta_correcta, $request->opciones)) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'errors' => ['respuesta_correcta' => ['La respuesta correcta debe estar entre las opciones']]
|
|
|
|
|
], 422);
|
|
|
|
|
}
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
|
return response()->json(['success' => false, 'errors' => $validator->errors()], 422);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Opciones
|
|
|
|
|
$opciones = is_string($request->opciones) ? json_decode($request->opciones, true) : $request->opciones;
|
|
|
|
|
$opcionesValidas = array_map('trim', $opciones);
|
|
|
|
|
|
|
|
|
|
// Procesar imágenes del enunciado
|
|
|
|
|
$imagenesPaths = [];
|
|
|
|
|
if ($request->hasFile('imagenes')) {
|
|
|
|
|
foreach ($request->file('imagenes') as $imagen) {
|
|
|
|
|
$path = $imagen->store('preguntas/enunciados', 'public');
|
|
|
|
|
$imagenesPaths[] = $path;
|
|
|
|
|
}
|
|
|
|
|
// Validar respuesta correcta
|
|
|
|
|
if (!in_array($request->respuesta_correcta, $opcionesValidas)) {
|
|
|
|
|
return response()->json(['success' => false, 'errors' => ['respuesta_correcta' => ['La respuesta correcta debe estar entre las opciones']]] ,422);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Procesar imágenes del enunciado y devolver URLs completas
|
|
|
|
|
$imagenesUrls = [];
|
|
|
|
|
if ($request->hasFile('imagenes')) {
|
|
|
|
|
foreach ($request->file('imagenes') as $imagen) {
|
|
|
|
|
$path = $imagen->store('preguntas/enunciados', 'public');
|
|
|
|
|
$imagenesUrls[] = url(Storage::url($path)); // URL completa
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Procesar imágenes de la explicación
|
|
|
|
|
$imagenesExplicacionPaths = [];
|
|
|
|
|
if ($request->hasFile('imagenes_explicacion')) {
|
|
|
|
|
foreach ($request->file('imagenes_explicacion') as $imagen) {
|
|
|
|
|
$path = $imagen->store('preguntas/explicaciones', 'public');
|
|
|
|
|
$imagenesExplicacionPaths[] = $path;
|
|
|
|
|
}
|
|
|
|
|
// Procesar imágenes de la explicación
|
|
|
|
|
$imagenesExplicacionUrls = [];
|
|
|
|
|
if ($request->hasFile('imagenes_explicacion')) {
|
|
|
|
|
foreach ($request->file('imagenes_explicacion') as $imagen) {
|
|
|
|
|
$path = $imagen->store('preguntas/explicaciones', 'public');
|
|
|
|
|
$imagenesExplicacionUrls[] = url(Storage::url($path)); // URL completa
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$pregunta = Pregunta::create([
|
|
|
|
|
'curso_id' => $request->curso_id,
|
|
|
|
|
'enunciado' => $request->enunciado,
|
|
|
|
|
'enunciado_adicional' => $request->enunciado_adicional,
|
|
|
|
|
'opciones' => $request->opciones,
|
|
|
|
|
'respuesta_correcta' => $request->respuesta_correcta,
|
|
|
|
|
'explicacion' => $request->explicacion,
|
|
|
|
|
'nivel_dificultad' => $request->nivel_dificultad,
|
|
|
|
|
'activo' => $request->boolean('activo'),
|
|
|
|
|
'imagenes' => $imagenesPaths,
|
|
|
|
|
'imagenes_explicacion' => $imagenesExplicacionPaths,
|
|
|
|
|
]);
|
|
|
|
|
// Crear pregunta
|
|
|
|
|
$pregunta = Pregunta::create([
|
|
|
|
|
'curso_id' => $request->curso_id,
|
|
|
|
|
'enunciado' => $request->enunciado,
|
|
|
|
|
'enunciado_adicional' => $request->enunciado_adicional,
|
|
|
|
|
'opciones' => $opcionesValidas,
|
|
|
|
|
'respuesta_correcta' => $request->respuesta_correcta,
|
|
|
|
|
'explicacion' => $request->explicacion,
|
|
|
|
|
'nivel_dificultad' => $request->nivel_dificultad,
|
|
|
|
|
'activo' => $request->boolean('activo'),
|
|
|
|
|
'imagenes' => $imagenesUrls,
|
|
|
|
|
'imagenes_explicacion' => $imagenesExplicacionUrls,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
Log::info('Pregunta creada', [
|
|
|
|
|
'pregunta_id' => $pregunta->id,
|
|
|
|
|
'curso_id' => $request->curso_id,
|
|
|
|
|
'user_id' => $user->id
|
|
|
|
|
]);
|
|
|
|
|
return response()->json(['success' => true, 'message' => 'Pregunta creada correctamente', 'data' => $pregunta], 201);
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => 'Pregunta creada correctamente',
|
|
|
|
|
'data' => $pregunta
|
|
|
|
|
], 201);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::error('Error creando pregunta', ['error' => $e->getMessage()]);
|
|
|
|
|
return response()->json(['success' => false, 'message' => 'Error al crear la pregunta'], 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::error('Error creando pregunta', ['error' => $e->getMessage()]);
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'message' => 'Error al crear la pregunta'
|
|
|
|
|
], 500);
|
|
|
|
|
public function actualizarPregunta(Request $request, $id)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$user = auth()->user();
|
|
|
|
|
if (!$user->hasRole('administrador')) {
|
|
|
|
|
return response()->json(['success' => false, 'message' => 'No autorizado'], 403);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function actualizarPregunta(Request $request, $id)
|
|
|
|
|
{
|
|
|
|
|
$pregunta = Pregunta::find($id);
|
|
|
|
|
|
|
|
|
|
if (!$pregunta) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'message' => 'Pregunta no encontrada'
|
|
|
|
|
], 404);
|
|
|
|
|
return response()->json(['success' => false, 'message' => 'Pregunta no encontrada'], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validación (igual que antes)
|
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
|
|
|
'enunciado' => 'required|string',
|
|
|
|
|
'curso_id' => 'required|exists:cursos,id',
|
|
|
|
|
'enunciado' => 'required|string',
|
|
|
|
|
'enunciado_adicional' => 'nullable|string',
|
|
|
|
|
'opciones' => 'required|array|min:2',
|
|
|
|
|
'opciones.*' => 'required|string',
|
|
|
|
|
'respuesta_correcta' => 'required|string',
|
|
|
|
|
'explicacion' => 'nullable|string',
|
|
|
|
|
'nivel_dificultad' => 'required|in:facil,medio,dificil',
|
|
|
|
|
'activo' => 'boolean',
|
|
|
|
|
'imagenes' => 'nullable|array',
|
|
|
|
|
'imagenes.*' => 'image|mimes:jpg,jpeg,png,gif,webp|max:2048',
|
|
|
|
|
'imagenes_explicacion' => 'nullable|array',
|
|
|
|
|
'opciones' => 'required',
|
|
|
|
|
'respuesta_correcta' => 'required|string',
|
|
|
|
|
'explicacion' => 'nullable|string',
|
|
|
|
|
'nivel_dificultad' => 'required|in:facil,medio,dificil',
|
|
|
|
|
'activo' => 'boolean',
|
|
|
|
|
'imagenes' => 'nullable|array',
|
|
|
|
|
'imagenes.*' => 'image|mimes:jpg,jpeg,png,gif,webp|max:2048',
|
|
|
|
|
'imagenes_explicacion' => 'nullable|array',
|
|
|
|
|
'imagenes_explicacion.*' => 'image|mimes:jpg,jpeg,png,gif,webp|max:2048',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'errors' => $validator->errors()
|
|
|
|
|
], 422);
|
|
|
|
|
return response()->json(['success' => false, 'errors' => $validator->errors()], 422);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validar que la respuesta correcta esté en las opciones
|
|
|
|
|
if (!in_array($request->respuesta_correcta, $request->opciones)) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'errors' => ['respuesta_correcta' => ['La respuesta correcta debe estar entre las opciones']]
|
|
|
|
|
], 422);
|
|
|
|
|
// Opciones
|
|
|
|
|
$opciones = is_string($request->opciones) ? json_decode($request->opciones, true) : $request->opciones;
|
|
|
|
|
$opcionesValidas = array_map('trim', $opciones);
|
|
|
|
|
|
|
|
|
|
if (!in_array($request->respuesta_correcta, $opcionesValidas)) {
|
|
|
|
|
return response()->json(['success' => false, 'errors' => ['respuesta_correcta' => ['La respuesta correcta debe estar entre las opciones']]], 422);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Procesar nuevas imágenes del enunciado
|
|
|
|
|
$imagenesActuales = $pregunta->imagenes ?? [];
|
|
|
|
|
// Imágenes del enunciado
|
|
|
|
|
$imagenesActuales = $request->imagenes_existentes ?? $pregunta->imagenes ?? [];
|
|
|
|
|
if ($request->hasFile('imagenes')) {
|
|
|
|
|
foreach ($request->file('imagenes') as $imagen) {
|
|
|
|
|
$path = $imagen->store('preguntas/enunciados', 'public');
|
|
|
|
|
$imagenesActuales[] = $path;
|
|
|
|
|
$imagenesActuales[] = url(Storage::url($path));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Procesar nuevas imágenes de la explicación
|
|
|
|
|
$imagenesExplicacionActuales = $pregunta->imagenes_explicacion ?? [];
|
|
|
|
|
// Imágenes de la explicación
|
|
|
|
|
$imagenesExplicacionActuales = $request->imagenes_explicacion_existentes ?? $pregunta->imagenes_explicacion ?? [];
|
|
|
|
|
if ($request->hasFile('imagenes_explicacion')) {
|
|
|
|
|
foreach ($request->file('imagenes_explicacion') as $imagen) {
|
|
|
|
|
$path = $imagen->store('preguntas/explicaciones', 'public');
|
|
|
|
|
$imagenesExplicacionActuales[] = $path;
|
|
|
|
|
$imagenesExplicacionActuales[] = url(Storage::url($path));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Si se enviaron imágenes existentes en edición
|
|
|
|
|
if ($request->has('imagenes_existentes')) {
|
|
|
|
|
$imagenesActuales = $request->imagenes_existentes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($request->has('imagenes_explicacion_existentes')) {
|
|
|
|
|
$imagenesExplicacionActuales = $request->imagenes_explicacion_existentes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$pregunta->update([
|
|
|
|
|
'enunciado' => $request->enunciado,
|
|
|
|
|
'curso_id' => $request->curso_id,
|
|
|
|
|
'enunciado' => $request->enunciado,
|
|
|
|
|
'enunciado_adicional' => $request->enunciado_adicional,
|
|
|
|
|
'opciones' => $request->opciones,
|
|
|
|
|
'respuesta_correcta' => $request->respuesta_correcta,
|
|
|
|
|
'explicacion' => $request->explicacion,
|
|
|
|
|
'nivel_dificultad' => $request->nivel_dificultad,
|
|
|
|
|
'activo' => $request->boolean('activo'),
|
|
|
|
|
'imagenes' => $imagenesActuales,
|
|
|
|
|
'opciones' => $opcionesValidas,
|
|
|
|
|
'respuesta_correcta' => $request->respuesta_correcta,
|
|
|
|
|
'explicacion' => $request->explicacion,
|
|
|
|
|
'nivel_dificultad' => $request->nivel_dificultad,
|
|
|
|
|
'activo' => $request->boolean('activo'),
|
|
|
|
|
'imagenes' => $imagenesActuales,
|
|
|
|
|
'imagenes_explicacion' => $imagenesExplicacionActuales,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => 'Pregunta actualizada correctamente',
|
|
|
|
|
'data' => $pregunta
|
|
|
|
|
]);
|
|
|
|
|
return response()->json(['success' => true, 'message' => 'Pregunta actualizada correctamente', 'data' => $pregunta]);
|
|
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::error('Error actualizando pregunta', ['error' => $e->getMessage()]);
|
|
|
|
|
return response()->json(['success' => false, 'message' => 'Error al actualizar la pregunta'], 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function eliminarPregunta($id)
|
|
|
|
|
{
|
|
|
|
|
|