diff --git a/controladores/ControladorPregunta.php b/controladores/ControladorPregunta.php index d5c8395..09779fa 100644 --- a/controladores/ControladorPregunta.php +++ b/controladores/ControladorPregunta.php @@ -3,6 +3,7 @@ session_start(); require_once '../clases/ClasePregunta.php'; +require_once '../clases/ClaseRespuesta.php'; switch ($_POST['orden']) { @@ -11,18 +12,118 @@ switch ($_POST['orden']) { $idCategoria = intval($_POST['idCategoria'] ?? 0); $nombrePregunta = trim($_POST['nombrePregunta'] ?? ''); - if ($idCategoria <= 0 || $nombrePregunta == '') { - echo json_encode(['error' => 'La categoría y el texto de la pregunta son obligatorios']); + $respuestaCorrecta = trim($_POST['respuestaCorrecta'] ?? ''); + $respuestaIncorrecta1 = trim($_POST['respuestaIncorrecta1'] ?? ''); + $respuestaIncorrecta2 = trim($_POST['respuestaIncorrecta2'] ?? ''); + $respuestaIncorrecta3 = trim($_POST['respuestaIncorrecta3'] ?? ''); + + // Comprobamos los datos + if ( + $idCategoria <= 0 || + $nombrePregunta === '' || + $respuestaCorrecta === '' || + $respuestaIncorrecta1 === '' || + $respuestaIncorrecta2 === '' || + $respuestaIncorrecta3 === '' + ) { + + echo json_encode([ + 'error' => 'La categoría, la pregunta y todas las respuestas son obligatorias.' + ]); + break; } - $pregunta = new Pregunta($idCategoria, $nombrePregunta); - $resultado = $pregunta->guardar(); - echo json_encode($resultado); + /* + * ========================================== + * 1. CREAMOS LA PREGUNTA + * ========================================== + */ + + $pregunta = new Pregunta( + $idCategoria, + $nombrePregunta + ); + + $resultadoPregunta = $pregunta->guardar(); + + + if (isset($resultadoPregunta['error'])) { + + echo json_encode($resultadoPregunta); + + break; + } + + + // Obtenemos el ID de la pregunta creada + $idPregunta = (int)$resultadoPregunta['exito_tecnico']; + + + /* + * ========================================== + * 2. CREAMOS LAS RESPUESTAS + * ========================================== + */ + + $respuestas = [ + [ + 'texto' => $respuestaCorrecta, + 'correcta' => 1 + ], + [ + 'texto' => $respuestaIncorrecta1, + 'correcta' => 0 + ], + [ + 'texto' => $respuestaIncorrecta2, + 'correcta' => 0 + ], + [ + 'texto' => $respuestaIncorrecta3, + 'correcta' => 0 + ] + ]; + + + foreach ($respuestas as $respuesta) { + + $objRespuesta = new Respuesta( + null, + $idPregunta, + $respuesta['texto'], + $respuesta['correcta'] + ); + + + $resultadoRespuesta = $objRespuesta->insertar(); + + + if (isset($resultadoRespuesta['error'])) { + + echo json_encode($resultadoRespuesta); + + break 2; + } + } + + + /* + * ========================================== + * 3. TODO CORRECTO + * ========================================== + */ + + echo json_encode([ + 'exito' => 'Pregunta creada correctamente.', + 'idPregunta' => $idPregunta + ]); + break; + case 'obtenerPreguntasPorCategoria': $idCategoria = intval($_POST['idCategoria'] ?? 0); @@ -49,5 +150,4 @@ switch ($_POST['orden']) { echo json_encode($listado); break; - } diff --git a/js/buscadorSala.js b/js/buscadorSala.js index 0ee97f7..400ea11 100644 --- a/js/buscadorSala.js +++ b/js/buscadorSala.js @@ -1,128 +1,104 @@ let usuarioYaEstaEnEquipo = false; $(document).ready(function () { -comprobarSalaUsuario(); + comprobarSalaUsuario(); }); function comprobarSalaUsuario() { + $("#cargandoSalas").remove(); -$("#cargandoSalas").remove(); - -$.ajax({ + $.ajax({ url: "controladores/ControladorEquipoxUsuario.php", type: "POST", dataType: "json", data: { - orden: "obtenerSalaUsuario" + orden: "obtenerSalaUsuario", }, success: function (respuesta) { + console.log("Sala del usuario:", respuesta); - console.log("Sala del usuario:", respuesta); - - if (respuesta.error) { - - console.error(respuesta.error); - - usuarioYaEstaEnEquipo = false; - - cargarSalas(); - - return; - } - - if (respuesta.asociado) { - - usuarioYaEstaEnEquipo = true; - - $("#contenedorCrearSala").hide(); - - cargarSalaUsuario(respuesta.idPartida); - - } else { - - usuarioYaEstaEnEquipo = false; - - cargarSalas(); - } - }, - - error: function (xhr, status, error) { - - console.error( - "Error al comprobar la sala del usuario:", - error - ); + if (respuesta.error) { + console.error(respuesta.error); usuarioYaEstaEnEquipo = false; cargarSalas(); - } -}); + return; + } -} + if (respuesta.asociado) { + usuarioYaEstaEnEquipo = true; -function cargarSalaUsuario(idPartida) { + $("#contenedorCrearSala").hide(); -$.ajax({ - url: "controladores/controladorPartida.php", - type: "POST", - dataType: "json", + cargarSalaUsuario(respuesta.idPartida); + } else { + usuarioYaEstaEnEquipo = false; - data: { - orden: "buscar", - idPartida: idPartida - }, - - success: function (partida) { - - if (partida.error) { - - console.error(partida.error); - - return; - } - - $("#contenedorSalas").empty(); - - cargarInformacionSala(partida); + cargarSalas(); + } }, error: function (xhr, status, error) { + console.error("Error al comprobar la sala del usuario:", error); - console.error( - "Error al obtener la sala del usuario:", - error - ); - } -}); - + usuarioYaEstaEnEquipo = false; + cargarSalas(); + }, + }); } -function cargarSalas() { - -$.ajax({ +function cargarSalaUsuario(idPartida) { + $.ajax({ url: "controladores/controladorPartida.php", type: "POST", dataType: "json", data: { - orden: "listar" + orden: "buscar", + idPartida: idPartida, + }, + + success: function (partida) { + if (partida.error) { + console.error(partida.error); + + return; + } + + $("#contenedorSalas").empty(); + + cargarInformacionSala(partida); + }, + + error: function (xhr, status, error) { + console.error("Error al obtener la sala del usuario:", error); + }, + }); +} + +function cargarSalas() { + $.ajax({ + url: "controladores/controladorPartida.php", + type: "POST", + dataType: "json", + + data: { + orden: "listar", }, success: function (partidas) { + const contenedor = $("#contenedorSalas"); - const contenedor = $("#contenedorSalas"); + contenedor.empty(); + contenedor.empty(); - contenedor.empty(); - contenedor.empty(); - - if (!Array.isArray(partidas) || partidas.length === 0) { - - contenedor.html(` + if (!Array.isArray(partidas) || partidas.length === 0) { + contenedor.html(`
No hay salas disponibles. @@ -130,518 +106,383 @@ $.ajax({