Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5420c83b30 | |||
| d28fbc7160 |
+2
-2
@@ -82,8 +82,8 @@
|
|||||||
<div class="modal-content modal-kahoot text-center p-3">
|
<div class="modal-content modal-kahoot text-center p-3">
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
<div class="mb-3 fs-1">⚠️</div>
|
<div class="mb-3 fs-1">⚠️</div>
|
||||||
<h4 class="modal-title mb-2 fw-bold" id="modalAbandonarSalaLabel">¿Abandonar la sala?</h4>
|
<h4 class="modal-title mb-2 fw-bold text-white" id="modalAbandonarSalaLabel">¿Abandonar la sala?</h4>
|
||||||
<p class="text-dark-50">Si sales de la sala, perderás tu lugar en el equipo.</p>
|
<p class="text-white-50">Si sales de la sala, perderás tu lugar en el equipo.</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer justify-content-center border-0 gap-2">
|
<div class="modal-footer justify-content-center border-0 gap-2">
|
||||||
<button type="button" class="btn btn-secondary px-4 py-2" data-bs-dismiss="modal">
|
<button type="button" class="btn btn-secondary px-4 py-2" data-bs-dismiss="modal">
|
||||||
|
|||||||
@@ -12,6 +12,40 @@ class Categoria{
|
|||||||
$this->nombreCategoria = $nombreCategoria;
|
$this->nombreCategoria = $nombreCategoria;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function crear(){
|
||||||
|
$conexion = conexionBD();
|
||||||
|
|
||||||
|
if(is_array($conexion)){
|
||||||
|
return $conexion;
|
||||||
|
}else{
|
||||||
|
try{
|
||||||
|
$consulta = $conexion->prepare("INSERT INTO categoria (nombreCategoria) VALUES (:nombreCategoria)");
|
||||||
|
|
||||||
|
$consulta->bindParam(':nombreCategoria', $this->nombreCategoria);
|
||||||
|
|
||||||
|
$consulta->execute();
|
||||||
|
|
||||||
|
$respuesta = [
|
||||||
|
'exito' => 'Categoría creada correctamente',
|
||||||
|
'exito_tecnico' => $this->nombreCategoria
|
||||||
|
];
|
||||||
|
|
||||||
|
return $respuesta;
|
||||||
|
|
||||||
|
}catch(PDOException $error){
|
||||||
|
$respuesta = [
|
||||||
|
'error' => 'Ocurrió un error al guardar la categoría.',
|
||||||
|
'error_tecnico' => $error->getMessage()
|
||||||
|
];
|
||||||
|
|
||||||
|
$conexion = null;
|
||||||
|
return $respuesta;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function listar(){
|
function listar(){
|
||||||
$conexion = conexionBD();
|
$conexion = conexionBD();
|
||||||
|
|
||||||
@@ -39,6 +73,77 @@ class Categoria{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function buscar(){
|
||||||
|
$conexion = conexionBD();
|
||||||
|
|
||||||
|
if(is_array($conexion)){
|
||||||
|
return $conexion;
|
||||||
|
}else{
|
||||||
|
try{
|
||||||
|
$consulta = $conexion->prepare("SELECT * FROM categoria WHERE idCategoria = :idCategoria");
|
||||||
|
|
||||||
|
$consulta->bindParam(':idCategoria', $this->idCategoria);
|
||||||
|
|
||||||
|
$consulta->execute();
|
||||||
|
|
||||||
|
$resultado = $consulta->fetch(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
|
return $resultado;
|
||||||
|
|
||||||
|
}catch(PDOException $error){
|
||||||
|
$respuesta = [
|
||||||
|
'error' => 'Ocurrió un error al buscar la categoría.',
|
||||||
|
'error_tecnico' => $error->getMessage()
|
||||||
|
];
|
||||||
|
|
||||||
|
$conexion = null;
|
||||||
|
return $respuesta;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function borrar(){
|
||||||
|
$conexion = conexionBD();
|
||||||
|
|
||||||
|
if(is_array($conexion)){
|
||||||
|
return $conexion;
|
||||||
|
}else{
|
||||||
|
try{
|
||||||
|
$consulta = $conexion->prepare("DELETE FROM categoria WHERE idCategoria = :idCategoria");
|
||||||
|
|
||||||
|
$consulta->bindParam(':idCategoria', $this->idCategoria);
|
||||||
|
|
||||||
|
$consulta->execute();
|
||||||
|
|
||||||
|
if($consulta->rowCount() > 0){
|
||||||
|
$respuesta = [
|
||||||
|
'exito' => 'Se eliminó correctamente la categoría',
|
||||||
|
'exito_tecnico' => $this->idCategoria
|
||||||
|
];
|
||||||
|
}else{
|
||||||
|
$respuesta = [
|
||||||
|
'error' => 'No existe ninguna categoría con ese ID.',
|
||||||
|
'error_tecnico' => $this->idCategoria
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $respuesta;
|
||||||
|
|
||||||
|
}catch(PDOException $error){
|
||||||
|
$respuesta = [
|
||||||
|
'error' => 'Ocurrió un error al eliminar la categoría.',
|
||||||
|
'error_tecnico' => $error->getMessage()
|
||||||
|
];
|
||||||
|
|
||||||
|
$conexion = null;
|
||||||
|
return $respuesta;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
+158
-186
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
<?php
|
<?php
|
||||||
require_once __DIR__ . '/../includes/conexionBD.php';
|
require_once __DIR__ . '/../includes/conexionBD.php';
|
||||||
|
|
||||||
@@ -8,17 +7,27 @@ class Partida
|
|||||||
public $idPartida;
|
public $idPartida;
|
||||||
public $nombrePartida;
|
public $nombrePartida;
|
||||||
public $idUsuario;
|
public $idUsuario;
|
||||||
|
public $fechaCreacion;
|
||||||
|
public $fechaInicio;
|
||||||
|
public $fechaFin;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
$idPartida = null,
|
$idPartida = null,
|
||||||
$nombrePartida = null,
|
$nombrePartida = null,
|
||||||
$idUsuario = null
|
$idUsuario = null,
|
||||||
|
$fechaCreacion = null,
|
||||||
|
$fechaInicio = null,
|
||||||
|
$fechaFin = null
|
||||||
) {
|
) {
|
||||||
$this->idPartida = $idPartida;
|
$this->idPartida = $idPartida;
|
||||||
$this->nombrePartida = $nombrePartida;
|
$this->nombrePartida = $nombrePartida;
|
||||||
$this->idUsuario = $idUsuario;
|
$this->idUsuario = $idUsuario;
|
||||||
|
$this->fechaCreacion = $fechaCreacion;
|
||||||
|
$this->fechaInicio = $fechaInicio;
|
||||||
|
$this->fechaFin = $fechaFin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function insertar()
|
public function insertar()
|
||||||
{
|
{
|
||||||
$conexion = conexionBD();
|
$conexion = conexionBD();
|
||||||
@@ -26,12 +35,7 @@ class Partida
|
|||||||
return $conexion;
|
return $conexion;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$consulta = $conexion->prepare("
|
$consulta = $conexion->prepare("INSERT INTO partida (nombrePartida,idUsuario) VALUES (:nombrePartida,:idUsuario)");
|
||||||
INSERT INTO partida
|
|
||||||
(nombrePartida,idUsuario)
|
|
||||||
VALUES
|
|
||||||
(:nombrePartida,:idUsuario)
|
|
||||||
");
|
|
||||||
$consulta->bindParam(":nombrePartida", $this->nombrePartida);
|
$consulta->bindParam(":nombrePartida", $this->nombrePartida);
|
||||||
$consulta->bindParam(":idUsuario", $this->idUsuario);
|
$consulta->bindParam(":idUsuario", $this->idUsuario);
|
||||||
$consulta->execute();
|
$consulta->execute();
|
||||||
@@ -47,8 +51,7 @@ class Partida
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function listar()
|
public function listar(){
|
||||||
{
|
|
||||||
$conexion = conexionBD();
|
$conexion = conexionBD();
|
||||||
if (is_array($conexion)) {
|
if (is_array($conexion)) {
|
||||||
return $conexion;
|
return $conexion;
|
||||||
@@ -65,18 +68,13 @@ class Partida
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buscar()
|
public function buscar(){
|
||||||
{
|
|
||||||
$conexion = conexionBD();
|
$conexion = conexionBD();
|
||||||
if (is_array($conexion)) {
|
if (is_array($conexion)) {
|
||||||
return $conexion;
|
return $conexion;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$consulta = $conexion->prepare("
|
$consulta = $conexion->prepare("SELECT * FROM partida WHERE idPartida=:idPartida");
|
||||||
SELECT *
|
|
||||||
FROM partida
|
|
||||||
WHERE idPartida=:idPartida
|
|
||||||
");
|
|
||||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||||
$consulta->execute();
|
$consulta->execute();
|
||||||
return $consulta->fetch(PDO::FETCH_ASSOC);
|
return $consulta->fetch(PDO::FETCH_ASSOC);
|
||||||
@@ -88,20 +86,14 @@ class Partida
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function editar()
|
|
||||||
{
|
public function editar(){
|
||||||
$conexion = conexionBD();
|
$conexion = conexionBD();
|
||||||
if (is_array($conexion)) {
|
if (is_array($conexion)) {
|
||||||
return $conexion;
|
return $conexion;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$consulta = $conexion->prepare("
|
$consulta = $conexion->prepare("UPDATE partida SET nombrePartida=:nombrePartida, idUsuario=:idUsuario WHERE idPartida=:idPartida");
|
||||||
UPDATE partida
|
|
||||||
SET
|
|
||||||
nombrePartida=:nombrePartida,
|
|
||||||
idUsuario=:idUsuario
|
|
||||||
WHERE idPartida=:idPartida
|
|
||||||
");
|
|
||||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||||
$consulta->bindParam(":nombrePartida", $this->nombrePartida);
|
$consulta->bindParam(":nombrePartida", $this->nombrePartida);
|
||||||
$consulta->bindParam(":idUsuario", $this->idUsuario);
|
$consulta->bindParam(":idUsuario", $this->idUsuario);
|
||||||
@@ -118,17 +110,13 @@ class Partida
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function eliminar()
|
public function eliminar(){
|
||||||
{
|
|
||||||
$conexion = conexionBD();
|
$conexion = conexionBD();
|
||||||
if (is_array($conexion)) {
|
if (is_array($conexion)) {
|
||||||
return $conexion;
|
return $conexion;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$consulta = $conexion->prepare("
|
$consulta = $conexion->prepare("DELETE FROM partida WHERE idPartida=:idPartida");
|
||||||
DELETE FROM partida
|
|
||||||
WHERE idPartida=:idPartida
|
|
||||||
");
|
|
||||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||||
$consulta->execute();
|
$consulta->execute();
|
||||||
return [
|
return [
|
||||||
@@ -143,27 +131,121 @@ class Partida
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Devuelve las preguntas asignadas a la partida, junto con el
|
public function iniciarPartida(){
|
||||||
* idPartidaxPregunta (necesario para guardar respuestas después).
|
$conexion = conexionBD();
|
||||||
*/
|
if (is_array($conexion)){
|
||||||
public function getPreguntas()
|
return $conexion;
|
||||||
{
|
}
|
||||||
|
try {
|
||||||
|
$consulta = $conexion->prepare("UPDATE partida SET fechaInicio=NOW() WHERE idPartida=:idPartida AND fechaInicio IS NULL");
|
||||||
|
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||||
|
$consulta->execute();
|
||||||
|
return [
|
||||||
|
"exito" => "La partida ha comenzado.",
|
||||||
|
"exito_tecnico" => $this->idPartida
|
||||||
|
];
|
||||||
|
} catch (PDOException $error) {
|
||||||
|
return [
|
||||||
|
"error" => "Ocurrió un error al iniciar la partida.",
|
||||||
|
'error_tecnico' => $error->getMessage()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function finalizar(){
|
||||||
$conexion = conexionBD();
|
$conexion = conexionBD();
|
||||||
if (is_array($conexion)) {
|
if (is_array($conexion)) {
|
||||||
return $conexion;
|
return $conexion;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$consulta = $conexion->prepare("
|
$consulta = $conexion->prepare("UPDATE partida SET fechaFin=NOW() WHERE idPartida=:idPartida AND fechaFin IS NULL");
|
||||||
SELECT
|
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||||
pxp.idPartidaxPregunta,
|
$consulta->execute();
|
||||||
p.*
|
return [
|
||||||
FROM partidaxpregunta pxp
|
"exito" => "La partida se finalizó correctamente.",
|
||||||
INNER JOIN pregunta p
|
"exito_tecnico" => $this->idPartida
|
||||||
ON pxp.idPregunta=p.idPregunta
|
];
|
||||||
WHERE pxp.idPartida=:idPartida
|
} catch (PDOException $error) {
|
||||||
ORDER BY pxp.idPartidaxPregunta
|
return [
|
||||||
");
|
"error" => "Ocurrió un error al finalizar la partida.",
|
||||||
|
'error_tecnico' => $error->getMessage()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//true si la partida ya tiene fecha de fin (fechaFin no es NULL)
|
||||||
|
//Devuelve false si la partida no existe
|
||||||
|
|
||||||
|
public function estaFinalizada() {
|
||||||
|
$datos = $this->buscar();
|
||||||
|
if (is_array($datos) && isset($datos["error"])) {
|
||||||
|
return $datos;
|
||||||
|
}
|
||||||
|
if (!$datos) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return $datos["fechaFin"] !== null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Devuelve la partida más reciente del usuario que aún no ha
|
||||||
|
//terminado (fechaFin NULL), o false si no tiene ninguna.
|
||||||
|
//Incluye también las salas que todavía no han empezado.
|
||||||
|
|
||||||
|
public function partidaEnCursoDeUsuario(){
|
||||||
|
$conexion = conexionBD();
|
||||||
|
if (is_array($conexion)) {
|
||||||
|
return $conexion;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$consulta = $conexion->prepare("SELECT * FROM partida WHERE idUsuario=:idUsuario AND fechaFin IS NULL ORDER BY idPartida DESC LIMIT 1");
|
||||||
|
$consulta->bindParam(":idUsuario", $this->idUsuario);
|
||||||
|
$consulta->execute();
|
||||||
|
return $consulta->fetch(PDO::FETCH_ASSOC);
|
||||||
|
} catch (PDOException $error) {
|
||||||
|
return [
|
||||||
|
"error" => "Ocurrió un error al buscar la partida en curso.",
|
||||||
|
'error_tecnico' => $error->getMessage()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function listarSalasAbiertas() {
|
||||||
|
$conexion = conexionBD();
|
||||||
|
if (is_array($conexion)) {
|
||||||
|
return $conexion;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$consulta = $conexion->prepare("SELECT p.idPartida, p.nombrePartida, e.idEquipo, e.nombreEquipo, u.idUsuario, u.nombreUsuario
|
||||||
|
FROM partida p INNER JOIN equipoxpartida exp ON exp.idPartida=p.idPartida INNER JOIN equipo e ON e.idEquipo=exp.idEquipo
|
||||||
|
LEFT JOIN equipoxusuario exu ON exu.idEquipo=e.idEquipo LEFT JOIN usuario u ON u.idUsuario=exu.idUsuario WHERE p.fechaInicio IS NULL
|
||||||
|
AND p.fechaFin IS NULL ORDER BY p.idPartida DESC, e.idEquipo ASC");
|
||||||
|
$consulta->execute();
|
||||||
|
return $consulta->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
} catch (PDOException $error) {
|
||||||
|
return [
|
||||||
|
"error" => "Ocurrió un error al recuperar las salas abiertas.",
|
||||||
|
'error_tecnico' => $error->getMessage()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Devuelve las preguntas asignadas a la partida, junto con el
|
||||||
|
* idPartidaxPregunta (necesario para guardar respuestas después).
|
||||||
|
*/
|
||||||
|
public function getPreguntas(){
|
||||||
|
$conexion = conexionBD();
|
||||||
|
if (is_array($conexion)) {
|
||||||
|
return $conexion;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$consulta = $conexion->prepare("SELECT pxp.idPartidaxPregunta, p.* FROM partidaxpregunta pxp INNER JOIN pregunta p ON pxp.idPregunta=p.idPregunta
|
||||||
|
WHERE pxp.idPartida=:idPartida ORDER BY pxp.idPartidaxPregunta");
|
||||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||||
$consulta->execute();
|
$consulta->execute();
|
||||||
return $consulta->fetchAll(PDO::FETCH_ASSOC);
|
return $consulta->fetchAll(PDO::FETCH_ASSOC);
|
||||||
@@ -175,22 +257,14 @@ class Partida
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Asocia una pregunta ya existente a esta partida.
|
public function asignarPregunta($idPregunta) {
|
||||||
*/
|
|
||||||
public function asignarPregunta($idPregunta)
|
|
||||||
{
|
|
||||||
$conexion = conexionBD();
|
$conexion = conexionBD();
|
||||||
if (is_array($conexion)) {
|
if (is_array($conexion)) {
|
||||||
return $conexion;
|
return $conexion;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$consulta = $conexion->prepare("
|
$consulta = $conexion->prepare("INSERT INTO partidaxpregunta (idPartida,idPregunta) VALUES (:idPartida,:idPregunta)");
|
||||||
INSERT INTO partidaxpregunta
|
|
||||||
(idPartida,idPregunta)
|
|
||||||
VALUES
|
|
||||||
(:idPartida,:idPregunta)
|
|
||||||
");
|
|
||||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||||
$consulta->bindParam(":idPregunta", $idPregunta);
|
$consulta->bindParam(":idPregunta", $idPregunta);
|
||||||
$consulta->execute();
|
$consulta->execute();
|
||||||
@@ -206,18 +280,13 @@ class Partida
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Cuenta cuántas preguntas tiene la partida en total. Es la primera mitad de la comprobación "¿ha terminado el usuario de jugar?".
|
//Cuenta cuántas preguntas tiene la partida en total. Es la primera mitad de la comprobación "¿ha terminado el usuario de jugar?".
|
||||||
public function contarPreguntasAsignadas()
|
public function contarPreguntasAsignadas() {
|
||||||
{
|
|
||||||
$conexion = conexionBD();
|
$conexion = conexionBD();
|
||||||
if (is_array($conexion)) {
|
if (is_array($conexion)) {
|
||||||
return $conexion;
|
return $conexion;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$consulta = $conexion->prepare("
|
$consulta = $conexion->prepare("SELECT COUNT(*) AS total FROM partidaxpregunta WHERE idPartida=:idPartida");
|
||||||
SELECT COUNT(*) AS total
|
|
||||||
FROM partidaxpregunta
|
|
||||||
WHERE idPartida=:idPartida
|
|
||||||
");
|
|
||||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||||
$consulta->execute();
|
$consulta->execute();
|
||||||
$fila = $consulta->fetch(PDO::FETCH_ASSOC);
|
$fila = $consulta->fetch(PDO::FETCH_ASSOC);
|
||||||
@@ -230,21 +299,14 @@ class Partida
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function contarRespuestasUsuario($idUsuario)
|
public function contarRespuestasUsuario($idUsuario){
|
||||||
{
|
|
||||||
$conexion = conexionBD();
|
$conexion = conexionBD();
|
||||||
if (is_array($conexion)) {
|
if (is_array($conexion)) {
|
||||||
return $conexion;
|
return $conexion;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$consulta = $conexion->prepare("
|
$consulta = $conexion->prepare("SELECT COUNT(*) AS total FROM respuestaalmacenada ra INNER JOIN partidaxpregunta pxp
|
||||||
SELECT COUNT(*) AS total
|
ON ra.idPartidaxPregunta=pxp.idPartidaxPregunta WHERE pxp.idPartida=:idPartida AND ra.idUsuario=:idUsuario");
|
||||||
FROM respuestaalmacenada ra
|
|
||||||
INNER JOIN partidaxpregunta pxp
|
|
||||||
ON ra.idPartidaxPregunta=pxp.idPartidaxPregunta
|
|
||||||
WHERE pxp.idPartida=:idPartida
|
|
||||||
AND ra.idUsuario=:idUsuario
|
|
||||||
");
|
|
||||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||||
$consulta->bindParam(":idUsuario", $idUsuario);
|
$consulta->bindParam(":idUsuario", $idUsuario);
|
||||||
$consulta->execute();
|
$consulta->execute();
|
||||||
@@ -259,11 +321,11 @@ class Partida
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sustituye al antiguo campo `terminada`: compara cuántas preguntas
|
* Indica si ESTE usuario ya ha respondido todas las preguntas:
|
||||||
* tiene la partida contra cuántas ha respondido este usuario.
|
* compara cuántas preguntas tiene la partida contra cuántas ha
|
||||||
|
* respondido él. (La partida completa se marca con finalizar().)
|
||||||
*/
|
*/
|
||||||
public function estaFinalizadaParaUsuario($idUsuario)
|
public function estaFinalizadaParaUsuario($idUsuario){
|
||||||
{
|
|
||||||
$totalPreguntas = $this->contarPreguntasAsignadas();
|
$totalPreguntas = $this->contarPreguntasAsignadas();
|
||||||
if (is_array($totalPreguntas)) {
|
if (is_array($totalPreguntas)) {
|
||||||
return $totalPreguntas;
|
return $totalPreguntas;
|
||||||
@@ -278,19 +340,13 @@ class Partida
|
|||||||
/**
|
/**
|
||||||
* Partidas creadas por un usuario, de más reciente a más antigua.
|
* Partidas creadas por un usuario, de más reciente a más antigua.
|
||||||
*/
|
*/
|
||||||
public function listarDelUsuario()
|
public function listarDelUsuario(){
|
||||||
{
|
|
||||||
$conexion = conexionBD();
|
$conexion = conexionBD();
|
||||||
if (is_array($conexion)) {
|
if (is_array($conexion)) {
|
||||||
return $conexion;
|
return $conexion;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$consulta = $conexion->prepare("
|
$consulta = $conexion->prepare("SELECT * FROM partida WHERE idUsuario=:idUsuario ORDER BY idPartida DESC");
|
||||||
SELECT *
|
|
||||||
FROM partida
|
|
||||||
WHERE idUsuario=:idUsuario
|
|
||||||
ORDER BY idPartida DESC
|
|
||||||
");
|
|
||||||
$consulta->bindParam(":idUsuario", $this->idUsuario);
|
$consulta->bindParam(":idUsuario", $this->idUsuario);
|
||||||
$consulta->execute();
|
$consulta->execute();
|
||||||
return $consulta->fetchAll(PDO::FETCH_ASSOC);
|
return $consulta->fetchAll(PDO::FETCH_ASSOC);
|
||||||
@@ -302,22 +358,13 @@ class Partida
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function preguntaAsignada($idPregunta)
|
public function preguntaAsignada($idPregunta) {
|
||||||
{
|
|
||||||
$conexion = conexionBD();
|
$conexion = conexionBD();
|
||||||
|
|
||||||
if (is_array($conexion)) {
|
if (is_array($conexion)) {
|
||||||
return $conexion;
|
return $conexion;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$consulta = $conexion->prepare("
|
$consulta = $conexion->prepare("SELECT COUNT(*) AS total FROM partidaxpregunta WHERE idPartida=:idPartida AND idPregunta=:idPregunta");
|
||||||
SELECT COUNT(*) AS total
|
|
||||||
FROM partidaxpregunta
|
|
||||||
WHERE idPartida=:idPartida
|
|
||||||
AND idPregunta=:idPregunta
|
|
||||||
");
|
|
||||||
|
|
||||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||||
$consulta->bindParam(":idPregunta", $idPregunta);
|
$consulta->bindParam(":idPregunta", $idPregunta);
|
||||||
$consulta->execute();
|
$consulta->execute();
|
||||||
@@ -337,24 +384,13 @@ class Partida
|
|||||||
* Vincula un equipo a esta partida en la tabla intermedia 'equipoxpartida'.
|
* Vincula un equipo a esta partida en la tabla intermedia 'equipoxpartida'.
|
||||||
* Permite registrar qué grupos están participando en este torneo.
|
* Permite registrar qué grupos están participando en este torneo.
|
||||||
*/
|
*/
|
||||||
public function registrarEquipoEnPartida($idEquipo)
|
public function registrarEquipoEnPartida($idEquipo){
|
||||||
{
|
$conexion = conexionBD();
|
||||||
$conexion = conexionBD(); // Obtenemos la conexión modular del proyecto
|
|
||||||
|
|
||||||
if (is_array($conexion)) {
|
if (is_array($conexion)) {
|
||||||
return $conexion; // Si hay error en la conexión, lo devuelve
|
return $conexion;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Preparamos la consulta para insertar la relación en la tabla intermedia real
|
$consulta = $conexion->prepare("INSERT INTO equipoxpartida (idPartida, idEquipo) VALUES (:idPartida, :idEquipo)");
|
||||||
$consulta = $conexion->prepare("
|
|
||||||
INSERT INTO equipoxpartida
|
|
||||||
(idPartida, idEquipo)
|
|
||||||
VALUES
|
|
||||||
(:idPartida, :idEquipo)
|
|
||||||
");
|
|
||||||
|
|
||||||
// Vinculamos los parámetros con el objeto de la partida actual y el equipo recibido
|
|
||||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||||
$consulta->bindParam(":idEquipo", $idEquipo);
|
$consulta->bindParam(":idEquipo", $idEquipo);
|
||||||
$consulta->execute();
|
$consulta->execute();
|
||||||
@@ -364,7 +400,6 @@ class Partida
|
|||||||
"exito_tecnico" => $conexion->lastInsertId()
|
"exito_tecnico" => $conexion->lastInsertId()
|
||||||
];
|
];
|
||||||
} catch (PDOException $error) {
|
} catch (PDOException $error) {
|
||||||
// Capturamos el error extrayendo solo el mensaje de texto limpio
|
|
||||||
return [
|
return [
|
||||||
"error" => "Ocurrió un error al asignar el equipo a la partida.",
|
"error" => "Ocurrió un error al asignar el equipo a la partida.",
|
||||||
"error_tecnico" => $error->getMessage()
|
"error_tecnico" => $error->getMessage()
|
||||||
@@ -376,28 +411,14 @@ class Partida
|
|||||||
* Obtiene todos los equipos que están compitiendo en esta partida.
|
* Obtiene todos los equipos que están compitiendo en esta partida.
|
||||||
* Es útil para mostrar la lista de rivales antes de empezar a responder.
|
* Es útil para mostrar la lista de rivales antes de empezar a responder.
|
||||||
*/
|
*/
|
||||||
public function getEquiposParticipantes()
|
public function getEquiposParticipantes(){
|
||||||
{
|
|
||||||
$conexion = conexionBD();
|
$conexion = conexionBD();
|
||||||
|
|
||||||
if (is_array($conexion)) {
|
if (is_array($conexion)) {
|
||||||
return $conexion;
|
return $conexion;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Hacemos un INNER JOIN para traer los nombres de los equipos desde la tabla intermedia
|
$consulta = $conexion->prepare("SELECT exp.idEquipoxPartida, e.idEquipo, e.nombreEquipo FROM equipoxpartida exp
|
||||||
$consulta = $conexion->prepare("
|
INNER JOIN equipo e ON exp.idEquipo = e.idEquipo WHERE exp.idPartida = :idPartida ORDER BY e.nombreEquipo ASC");
|
||||||
SELECT
|
|
||||||
exp.idEquipoxPartida,
|
|
||||||
e.idEquipo,
|
|
||||||
e.nombreEquipo
|
|
||||||
FROM equipoxpartida exp
|
|
||||||
INNER JOIN equipo e
|
|
||||||
ON exp.idEquipo = e.idEquipo
|
|
||||||
WHERE exp.idPartida = :idPartida
|
|
||||||
ORDER BY e.nombreEquipo ASC
|
|
||||||
");
|
|
||||||
|
|
||||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||||
$consulta->execute();
|
$consulta->execute();
|
||||||
|
|
||||||
@@ -414,78 +435,29 @@ class Partida
|
|||||||
* Obtiene todos los jugadores reales (Usuarios) que están jugando en esta partida.
|
* Obtiene todos los jugadores reales (Usuarios) que están jugando en esta partida.
|
||||||
* Utiliza la tabla 'equipoxusuario' en SQL para cruzar los datos.
|
* Utiliza la tabla 'equipoxusuario' en SQL para cruzar los datos.
|
||||||
*/
|
*/
|
||||||
public function getJugadoresDeLaPartida()
|
public function getJugadoresDeLaPartida(){
|
||||||
{
|
|
||||||
$conexion = conexionBD();
|
$conexion = conexionBD();
|
||||||
|
|
||||||
if (is_array($conexion)) {
|
if (is_array($conexion)) {
|
||||||
return $conexion;
|
return $conexion;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
$consulta = $conexion->prepare("SELECT DISTINCT u.idUsuario, u.nombreUsuario, u.email, e.idEquipo, e.nombreEquipo
|
||||||
$consulta = $conexion->prepare("
|
FROM equipoxpartida exp INNER JOIN equipoxusuario exu ON exp.idEquipo = exu.idEquipo INNER JOIN usuario u
|
||||||
SELECT DISTINCT
|
ON exu.idUsuario = u.idUsuario INNER JOIN equipo e ON exu.idEquipo = e.idEquipo WHERE exp.idPartida = :idPartida
|
||||||
u.idUsuario,
|
ORDER BY e.nombreEquipo ASC, u.nombreUsuario ASC");
|
||||||
u.nombreUsuario,
|
|
||||||
u.email,
|
|
||||||
e.idEquipo,
|
|
||||||
e.nombreEquipo
|
|
||||||
FROM equipoxpartida exp
|
|
||||||
|
|
||||||
INNER JOIN equipoxusuario exu
|
|
||||||
ON exp.idEquipo = exu.idEquipo
|
|
||||||
|
|
||||||
INNER JOIN usuario u
|
|
||||||
ON exu.idUsuario = u.idUsuario
|
|
||||||
|
|
||||||
INNER JOIN equipo e
|
|
||||||
ON exu.idEquipo = e.idEquipo
|
|
||||||
|
|
||||||
WHERE exp.idPartida = :idPartida
|
|
||||||
|
|
||||||
ORDER BY e.nombreEquipo ASC,
|
|
||||||
u.nombreUsuario ASC
|
|
||||||
");
|
|
||||||
|
|
||||||
$consulta->bindParam(
|
$consulta->bindParam(
|
||||||
":idPartida",
|
":idPartida",
|
||||||
$this->idPartida
|
$this->idPartida
|
||||||
);
|
);
|
||||||
|
|
||||||
$consulta->execute();
|
$consulta->execute();
|
||||||
|
|
||||||
return $consulta->fetchAll(PDO::FETCH_ASSOC);
|
return $consulta->fetchAll(PDO::FETCH_ASSOC);
|
||||||
} catch (PDOException $error) {
|
} catch (PDOException $error) {
|
||||||
|
|
||||||
return [
|
return [
|
||||||
"error" =>
|
"error" =>
|
||||||
"Ocurrió un error al obtener la lista de jugadores.",
|
"Ocurrió un error al obtener la lista de jugadores.",
|
||||||
|
|
||||||
"error_tecnico" =>
|
"error_tecnico" =>
|
||||||
$error->getMessage()
|
$error->getMessage()
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function contar()
|
|
||||||
{
|
|
||||||
$conexion = conexionBD();
|
|
||||||
if (is_array($conexion)) {
|
|
||||||
return $conexion;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
$sql="SELECT COUNT(*) AS total FROM partida";
|
|
||||||
$consulta = $conexion->prepare($sql);
|
|
||||||
$consulta->execute();
|
|
||||||
return (int) $consulta->fetchColumn();
|
|
||||||
} catch (PDOException $error) {
|
|
||||||
return [
|
|
||||||
"error" => "Ocurrió un error al obtener el listado.",
|
|
||||||
'error_tecnico' => $error->getMessage()
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
+42
-10
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
<?php
|
<?php
|
||||||
require_once __DIR__ . '/../includes/conexionBD.php';
|
require_once __DIR__ . '/../includes/conexionBD.php';
|
||||||
|
|
||||||
@@ -57,23 +56,56 @@ class Pregunta {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function cargarRespuestas() {
|
||||||
|
if ($this->idPregunta === null) return;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static function getTodosIds()//devuelve solamente los id de todas las preguntas
|
|
||||||
{
|
|
||||||
$conexion = conexionBD();
|
$conexion = conexionBD();
|
||||||
|
|
||||||
if (is_array($conexion)) {
|
if (is_array($conexion)) {
|
||||||
return $conexion;
|
return $conexion;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$consulta = $conexion->prepare("SELECT idPregunta FROM pregunta");
|
$sql = "SELECT idRespuesta, nombreRespuesta, correcta FROM respuesta WHERE idPregunta = :idPreg";
|
||||||
$consulta->execute();
|
$stmt = $conexion->prepare($sql);
|
||||||
return $consulta->fetchAll(PDO::FETCH_COLUMN);
|
$stmt->execute([':idPreg' => $this->idPregunta]);
|
||||||
|
|
||||||
|
$this->respuestas = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
} catch (PDOException $error) {
|
} catch (PDOException $error) {
|
||||||
return [
|
return [
|
||||||
"error" => "Ocurrió un error al obtener el listado.",
|
'error' => 'Ocurrió un error al cargar las respuestas.',
|
||||||
|
'error_tecnico' => $error->getMessage()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function obtenerPorCategoria(int $idCategoria, int $limite = 10) {
|
||||||
|
$conexion = conexionBD();
|
||||||
|
|
||||||
|
if (is_array($conexion)) {
|
||||||
|
return $conexion;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$sql = "SELECT * FROM pregunta WHERE idCategoria = :idCat ORDER BY RAND() LIMIT :limite";
|
||||||
|
$stmt = $conexion->prepare($sql);
|
||||||
|
|
||||||
|
$stmt->bindValue(':idCat', $idCategoria, PDO::PARAM_INT);
|
||||||
|
$stmt->bindValue(':limite', $limite, PDO::PARAM_INT);
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
|
$preguntas = [];
|
||||||
|
while ($datos = $stmt->fetch(PDO::FETCH_ASSOC)) {
|
||||||
|
$preg = new Pregunta((int)$datos['idCategoria'], $datos['nombrePregunta'], (int)$datos['idPregunta']);
|
||||||
|
$preg->cargarRespuestas();
|
||||||
|
$preguntas[] = $preg;
|
||||||
|
}
|
||||||
|
return $preguntas;
|
||||||
|
|
||||||
|
} catch (PDOException $error) {
|
||||||
|
return [
|
||||||
|
'error' => 'Ocurrió un error al obtener las preguntas aleatorias.',
|
||||||
'error_tecnico' => $error->getMessage()
|
'error_tecnico' => $error->getMessage()
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,130 @@ class Respuesta{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}//fin método insertar
|
}//fin método insertar
|
||||||
|
|
||||||
|
public function listar(){
|
||||||
|
$conexion = conexionBD();
|
||||||
|
if(is_array($conexion)){
|
||||||
|
return $conexion;
|
||||||
|
}else{
|
||||||
|
try{
|
||||||
|
$consulta = $conexion->prepare("SELECT * FROM respuesta");
|
||||||
|
$consulta->execute();
|
||||||
|
return $consulta->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
}catch(PDOException $error){
|
||||||
|
$respuesta = [
|
||||||
|
'error' => 'Ocurrió un error al obtener el listado.',
|
||||||
|
'error_tecnico' => $error->getMessage()
|
||||||
|
];
|
||||||
|
$conexion = null;
|
||||||
|
return $respuesta;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}//fin método listar
|
||||||
|
|
||||||
|
public function eliminar(){
|
||||||
|
$conexion = conexionBD();
|
||||||
|
if(is_array($conexion)){
|
||||||
|
return $conexion;
|
||||||
|
}else{
|
||||||
|
try{
|
||||||
|
$consulta = $conexion->prepare("DELETE FROM respuesta WHERE idRespuesta=:idRespuesta");
|
||||||
|
$consulta->bindParam(':idRespuesta', $this->idRespuesta);
|
||||||
|
$consulta->execute();
|
||||||
|
$respuesta = [
|
||||||
|
'exito' => 'La respuesta se eliminó correctamente.',
|
||||||
|
'exito_tecnico' => $this->idRespuesta
|
||||||
|
];
|
||||||
|
return $respuesta;
|
||||||
|
}catch(PDOException $error){
|
||||||
|
$respuesta = [
|
||||||
|
'error' => 'No se pudo eliminar la respuesta.',
|
||||||
|
'error_tecnico' => $error->getMessage()
|
||||||
|
];
|
||||||
|
$conexion = null;
|
||||||
|
return $respuesta;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}//fin método eliminar
|
||||||
|
|
||||||
|
public function editar(){
|
||||||
|
$conexion = conexionBD();
|
||||||
|
if(is_array($conexion)){
|
||||||
|
return $conexion;
|
||||||
|
}else{
|
||||||
|
try{
|
||||||
|
$consulta = $conexion->prepare("UPDATE respuesta SET idPregunta=:idPregunta, nombreRespuesta=:nombreRespuesta, correcta=:correcta WHERE idRespuesta=:idRespuesta");
|
||||||
|
$consulta->bindParam(':idRespuesta', $this->idRespuesta);
|
||||||
|
$consulta->bindParam(':idPregunta', $this->idPregunta);
|
||||||
|
$consulta->bindParam(':nombreRespuesta', $this->nombreRespuesta);
|
||||||
|
$consulta->bindParam(':correcta', $this->correcta);
|
||||||
|
$consulta->execute();
|
||||||
|
$respuesta = [
|
||||||
|
'exito' => 'La respuesta se editó correctamente.',
|
||||||
|
'exito_tecnico' => $this->idRespuesta
|
||||||
|
];
|
||||||
|
return $respuesta;
|
||||||
|
}catch(PDOException $error){
|
||||||
|
$respuesta = [
|
||||||
|
'error' => 'Ocurrió un error al editar la respuesta.',
|
||||||
|
'error_tecnico' => $error->getMessage()
|
||||||
|
];
|
||||||
|
$conexion = null;
|
||||||
|
return $respuesta;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}//fin método editar
|
||||||
|
|
||||||
|
public function buscar(){
|
||||||
|
$conexion = conexionBD();
|
||||||
|
if(is_array($conexion)){
|
||||||
|
return $conexion;
|
||||||
|
}else{
|
||||||
|
try{
|
||||||
|
$consulta = $conexion->prepare("SELECT * FROM respuesta WHERE idRespuesta=:idRespuesta");
|
||||||
|
$consulta->bindParam(':idRespuesta', $this->idRespuesta);
|
||||||
|
$consulta->execute();
|
||||||
|
return $consulta->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
}catch(PDOException $error){
|
||||||
|
$respuesta = [
|
||||||
|
'error' => 'Ocurrió un error al buscar la respuesta.',
|
||||||
|
'error_tecnico' => $error->getMessage()
|
||||||
|
];
|
||||||
|
$conexion = null;
|
||||||
|
return $respuesta;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}//fin método buscar
|
||||||
|
|
||||||
|
public function listarPorPregunta(){//trae todas las respuestas posibles de una pregunta
|
||||||
|
$conexion = conexionBD();
|
||||||
|
if(is_array($conexion)){
|
||||||
|
return $conexion;
|
||||||
|
}else{
|
||||||
|
try{
|
||||||
|
$consulta = $conexion->prepare("SELECT * FROM respuesta WHERE idPregunta=:idPregunta");
|
||||||
|
$consulta->bindParam(':idPregunta', $this->idPregunta);
|
||||||
|
$consulta->execute();
|
||||||
|
$resultado = $consulta->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
if(!isset($resultado[0])){
|
||||||
|
$respuesta = [
|
||||||
|
'error' => 'La pregunta no tiene respuestas.'
|
||||||
|
];
|
||||||
|
return $respuesta;
|
||||||
|
}else{
|
||||||
|
return $resultado;
|
||||||
|
}
|
||||||
|
}catch(PDOException $error){
|
||||||
|
$respuesta = [
|
||||||
|
'error' => 'Ocurrió un error al obtener las respuestas de la pregunta.',
|
||||||
|
'error_tecnico' => $error->getMessage()
|
||||||
|
];
|
||||||
|
$conexion = null;
|
||||||
|
return $respuesta;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}//fin método listarPorPregunta
|
||||||
|
|
||||||
}//fin clase
|
}//fin clase
|
||||||
|
|
||||||
?>
|
?>
|
||||||
@@ -6,6 +6,22 @@ require_once '../clases/ClaseCategoria.php';
|
|||||||
|
|
||||||
switch ($_POST['orden']) {
|
switch ($_POST['orden']) {
|
||||||
|
|
||||||
|
case 'crearCategoria':
|
||||||
|
|
||||||
|
$nombreCategoria = trim($_POST['nombreCategoria'] ?? '');
|
||||||
|
|
||||||
|
if ($nombreCategoria == '') {
|
||||||
|
echo json_encode(['error' => 'El nombre de la categoría es obligatorio']);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$categoria = new Categoria(null, $nombreCategoria);
|
||||||
|
$resultado = $categoria->crear();
|
||||||
|
|
||||||
|
echo json_encode($resultado);
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
case 'listarCategorias':
|
case 'listarCategorias':
|
||||||
|
|
||||||
$categoria = new Categoria();
|
$categoria = new Categoria();
|
||||||
@@ -13,4 +29,27 @@ switch ($_POST['orden']) {
|
|||||||
|
|
||||||
echo json_encode($resultado);
|
echo json_encode($resultado);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
case 'buscarCategoria':
|
||||||
|
|
||||||
|
$idCategoria = intval($_POST['idCategoria'] ?? 0);
|
||||||
|
|
||||||
|
$categoria = new Categoria($idCategoria);
|
||||||
|
$resultado = $categoria->buscar();
|
||||||
|
|
||||||
|
echo json_encode($resultado);
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
case 'borrarCategoria':
|
||||||
|
|
||||||
|
$idCategoria = intval($_POST['idCategoria'] ?? 0);
|
||||||
|
|
||||||
|
$categoria = new Categoria($idCategoria);
|
||||||
|
$resultado = $categoria->borrar();
|
||||||
|
|
||||||
|
echo json_encode($resultado);
|
||||||
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
session_start();
|
||||||
|
|
||||||
|
require_once '../clases/ClaseEquipo.php';
|
||||||
|
|
||||||
|
switch ($_POST['orden']) {
|
||||||
|
|
||||||
|
case 'crearEquipo':
|
||||||
|
|
||||||
|
$nombreEquipo = trim($_POST['nombreEquipo'] ?? '');
|
||||||
|
|
||||||
|
if ($nombreEquipo == '') {
|
||||||
|
echo json_encode(['error' => 'El nombre del equipo es obligatorio']);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$equipo = new Equipo(null, $nombreEquipo);
|
||||||
|
$resultado = $equipo->crear();
|
||||||
|
|
||||||
|
echo json_encode($resultado);
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
case 'listarEquipos':
|
||||||
|
|
||||||
|
$equipo = new Equipo();
|
||||||
|
$resultado = $equipo->listar();
|
||||||
|
|
||||||
|
echo json_encode($resultado);
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
case 'buscarEquipo':
|
||||||
|
|
||||||
|
$idEquipo = intval($_POST['idEquipo'] ?? 0);
|
||||||
|
|
||||||
|
$equipo = new Equipo($idEquipo);
|
||||||
|
$resultado = $equipo->buscar();
|
||||||
|
|
||||||
|
echo json_encode($resultado);
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
case 'borrarEquipo':
|
||||||
|
|
||||||
|
$idEquipo = intval($_POST['idEquipo'] ?? 0);
|
||||||
|
|
||||||
|
$equipo = new Equipo($idEquipo);
|
||||||
|
$resultado = $equipo->borrar();
|
||||||
|
|
||||||
|
echo json_encode($resultado);
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,7 +6,6 @@ require_once '../clases/ClasePartida.php';
|
|||||||
require_once '../clases/ClaseEquipo.php';
|
require_once '../clases/ClaseEquipo.php';
|
||||||
require_once '../clases/ClaseEquipoxUsuario.php';
|
require_once '../clases/ClaseEquipoxUsuario.php';
|
||||||
require_once '../clases/ClaseEquipoxPartida.php';
|
require_once '../clases/ClaseEquipoxPartida.php';
|
||||||
require_once '../clases/ClasePregunta.php';
|
|
||||||
|
|
||||||
|
|
||||||
switch ($_POST['orden']) {
|
switch ($_POST['orden']) {
|
||||||
@@ -120,7 +119,6 @@ switch ($_POST['orden']) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$idPartida = (int)$resultadoPartida['exito_tecnico'];
|
$idPartida = (int)$resultadoPartida['exito_tecnico'];
|
||||||
$partida->idPartida = $idPartida;
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -213,42 +211,6 @@ switch ($_POST['orden']) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* ==========================================
|
|
||||||
* 5. ASIGNAMOS 10 PREGUNTAS ALEATORIAS A LA PARTIDA
|
|
||||||
* ==========================================
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Obtenemos todos los IDs de las preguntas
|
|
||||||
|
|
||||||
$listaPreguntas = Pregunta::getTodosIds();
|
|
||||||
|
|
||||||
$totalPreguntas = count($listaPreguntas);
|
|
||||||
|
|
||||||
// Guardamos las preguntas que ya hemos asignado
|
|
||||||
$preguntasAsignadas = [];
|
|
||||||
|
|
||||||
// Generamos 10 posiciones aleatorias sin repetir
|
|
||||||
while (count($preguntasAsignadas) < 10) {
|
|
||||||
|
|
||||||
// Generamos una posición aleatoria del array
|
|
||||||
$posicionAleatoria = rand(0, $totalPreguntas - 1);
|
|
||||||
|
|
||||||
// Obtenemos el ID real de la pregunta
|
|
||||||
$idPreguntaAleatoria = $listaPreguntas[$posicionAleatoria];
|
|
||||||
|
|
||||||
// Comprobamos que esa pregunta no haya sido asignada ya
|
|
||||||
if (!in_array($idPreguntaAleatoria, $preguntasAsignadas)) {
|
|
||||||
$preguntasAsignadas[] = $idPreguntaAleatoria;
|
|
||||||
|
|
||||||
// Asignamos el ID real de la pregunta a la partida
|
|
||||||
$partida->asignarPregunta($idPreguntaAleatoria);
|
|
||||||
}
|
|
||||||
}//fin shile
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ==========================================
|
* ==========================================
|
||||||
* TODO CORRECTO
|
* TODO CORRECTO
|
||||||
@@ -256,7 +218,7 @@ switch ($_POST['orden']) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
'exito' => 'Sala creada correctamente. Preguntas preparadas.',
|
'exito' => 'Sala creada correctamente.',
|
||||||
'idPartida' => $resultadoPartida['exito_tecnico'],
|
'idPartida' => $resultadoPartida['exito_tecnico'],
|
||||||
'idEquipo1' => $equipo1->idEquipo,
|
'idEquipo1' => $equipo1->idEquipo,
|
||||||
'idEquipo2' => $equipo2->idEquipo
|
'idEquipo2' => $equipo2->idEquipo
|
||||||
|
|||||||
@@ -121,4 +121,33 @@ switch ($_POST['orden']) {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
case 'obtenerPreguntasPorCategoria':
|
||||||
|
|
||||||
|
$idCategoria = intval($_POST['idCategoria'] ?? 0);
|
||||||
|
$limite = intval($_POST['limite'] ?? 10);
|
||||||
|
|
||||||
|
$preguntas = Pregunta::obtenerPorCategoria($idCategoria, $limite);
|
||||||
|
|
||||||
|
// Si ha devuelto un array de errores (formato ['error'=>...]) lo mandamos tal cual
|
||||||
|
if (isset($preguntas['error'])) {
|
||||||
|
echo json_encode($preguntas);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convertimos cada objeto Pregunta en un array simple para poder mandarlo en JSON
|
||||||
|
$listado = [];
|
||||||
|
foreach ($preguntas as $p) {
|
||||||
|
$listado[] = [
|
||||||
|
'idPregunta' => $p->getIdPregunta(),
|
||||||
|
'idCategoria' => $p->getIdCategoria(),
|
||||||
|
'nombrePregunta' => $p->getNombrePregunta(),
|
||||||
|
'respuestas' => $p->getRespuestas()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
echo json_encode($listado);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
session_start();
|
||||||
|
|
||||||
|
require_once '../clases/ClaseRespuesta.php';
|
||||||
|
|
||||||
|
switch ($_POST['orden']) {
|
||||||
|
|
||||||
|
case 'crearRespuesta':
|
||||||
|
|
||||||
|
$idPregunta = intval($_POST['idPregunta'] ?? 0);
|
||||||
|
$nombreRespuesta = trim($_POST['nombreRespuesta'] ?? '');
|
||||||
|
$correcta = intval($_POST['correcta'] ?? 0);
|
||||||
|
|
||||||
|
if ($idPregunta <= 0 || $nombreRespuesta == '') {
|
||||||
|
echo json_encode(['error' => 'La pregunta y el texto de la respuesta son obligatorios']);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$respuesta = new Respuesta(null, $idPregunta, $nombreRespuesta, $correcta);
|
||||||
|
$resultado = $respuesta->insertar();
|
||||||
|
|
||||||
|
echo json_encode($resultado);
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
case 'listarRespuestas':
|
||||||
|
|
||||||
|
$respuesta = new Respuesta();
|
||||||
|
$resultado = $respuesta->listar();
|
||||||
|
|
||||||
|
echo json_encode($resultado);
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
case 'listarRespuestasPorPregunta':
|
||||||
|
|
||||||
|
$idPregunta = intval($_POST['idPregunta'] ?? 0);
|
||||||
|
|
||||||
|
$respuesta = new Respuesta(null, $idPregunta);
|
||||||
|
$resultado = $respuesta->listarPorPregunta();
|
||||||
|
|
||||||
|
echo json_encode($resultado);
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
case 'editarRespuesta':
|
||||||
|
|
||||||
|
$idRespuesta = intval($_POST['idRespuesta'] ?? 0);
|
||||||
|
$idPregunta = intval($_POST['idPregunta'] ?? 0);
|
||||||
|
$nombreRespuesta = trim($_POST['nombreRespuesta'] ?? '');
|
||||||
|
$correcta = intval($_POST['correcta'] ?? 0);
|
||||||
|
|
||||||
|
$respuesta = new Respuesta($idRespuesta, $idPregunta, $nombreRespuesta, $correcta);
|
||||||
|
$resultado = $respuesta->editar();
|
||||||
|
|
||||||
|
echo json_encode($resultado);
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
case 'borrarRespuesta':
|
||||||
|
|
||||||
|
$idRespuesta = intval($_POST['idRespuesta'] ?? 0);
|
||||||
|
|
||||||
|
$respuesta = new Respuesta($idRespuesta);
|
||||||
|
$resultado = $respuesta->eliminar();
|
||||||
|
|
||||||
|
echo json_encode($resultado);
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -55,11 +55,10 @@ switch ($_POST['orden']) {
|
|||||||
|
|
||||||
if ($resultado instanceof Usuario) {
|
if ($resultado instanceof Usuario) {
|
||||||
|
|
||||||
// === GUARDAR EN SESIÓN (incluido avatar)===
|
// === AÑADIR ESTAS LÍNEAS PARA GUARDAR EN SESIÓN ===
|
||||||
$_SESSION['idUsuario'] = $resultado->getIdUsuario();
|
$_SESSION['idUsuario'] = $resultado->getIdUsuario();
|
||||||
$_SESSION['nombreUsuario'] = $resultado->getNombreUsuario();
|
$_SESSION['nombreUsuario'] = $resultado->getNombreUsuario();
|
||||||
$_SESSION['email'] = $resultado->getEmail();
|
$_SESSION['email'] = $resultado->getEmail();
|
||||||
$_SESSION['avatar'] = $resultado->getAvatar();
|
|
||||||
// =================================================
|
// =================================================
|
||||||
|
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
@@ -84,7 +83,6 @@ switch ($_POST['orden']) {
|
|||||||
unset($_SESSION['idUsuario']);
|
unset($_SESSION['idUsuario']);
|
||||||
unset($_SESSION['email']);
|
unset($_SESSION['email']);
|
||||||
unset($_SESSION['nombreUsuario']);
|
unset($_SESSION['nombreUsuario']);
|
||||||
unset($_SESSION['avatar']);
|
|
||||||
|
|
||||||
// Destruimos la sesión
|
// Destruimos la sesión
|
||||||
session_destroy();
|
session_destroy();
|
||||||
@@ -94,6 +92,7 @@ switch ($_POST['orden']) {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'mostrarEstadisticas':
|
case 'mostrarEstadisticas':
|
||||||
|
|
||||||
$usuario = Usuario::buscarPorId($_SESSION['idUsuario']);
|
$usuario = Usuario::buscarPorId($_SESSION['idUsuario']);
|
||||||
@@ -238,17 +237,16 @@ switch ($_POST['orden']) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'comprobarSesion':
|
case 'comprobarSesion':
|
||||||
if (isset($_SESSION['idUsuario']) && !empty($_SESSION['idUsuario'])) {
|
if (isset($_SESSION['idUsuario']) && !empty($_SESSION['idUsuario'])) {
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
'activa' => true,
|
'activa' => true,
|
||||||
'usuario' => [
|
'usuario' => [
|
||||||
'idUsuario' => $_SESSION['idUsuario'],
|
'idUsuario' => $_SESSION['idUsuario'],
|
||||||
'nombreUsuario' => $_SESSION['nombreUsuario'] ?? '',
|
'nombreUsuario' => $_SESSION['nombreUsuario'] ?? ''
|
||||||
'avatar' => $_SESSION['avatar'] ?? ''
|
]
|
||||||
]
|
]);
|
||||||
]);
|
} else {
|
||||||
} else {
|
echo json_encode(['activa' => false]);
|
||||||
echo json_encode(['activa' => false]);
|
}
|
||||||
}
|
break;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-10
@@ -68,8 +68,8 @@
|
|||||||
|
|
||||||
.btn-unirse {
|
.btn-unirse {
|
||||||
border: none;
|
border: none;
|
||||||
border-bottom: 3px solid #1f6f09;
|
border-bottom: 3px solid #0c4891;
|
||||||
background-color: #26890c;
|
background-color: var(--kahoot-blue);
|
||||||
color: white;
|
color: white;
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
@@ -105,11 +105,3 @@
|
|||||||
border-color: var(--kahoot-purple);
|
border-color: var(--kahoot-purple);
|
||||||
box-shadow: 0 0 0 3px rgba(70, 23, 143, 0.15);
|
box-shadow: 0 0 0 3px rgba(70, 23, 143, 0.15);
|
||||||
}
|
}
|
||||||
|
|
||||||
.jugador-host {
|
|
||||||
background-color: #fff3cd;
|
|
||||||
border: 1px solid #ffc107;
|
|
||||||
border-radius: 8px;
|
|
||||||
padding: 6px 10px;
|
|
||||||
font-weight: 600;
|
|
||||||
}
|
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 6.9 KiB |
+19
-50
@@ -123,24 +123,12 @@ function mostrarSala(partida, equipos, jugadores) {
|
|||||||
let jugadoresHTML = "";
|
let jugadoresHTML = "";
|
||||||
if (jugadoresEquipo.length > 0) {
|
if (jugadoresEquipo.length > 0) {
|
||||||
jugadoresEquipo.forEach(function (jugador) {
|
jugadoresEquipo.forEach(function (jugador) {
|
||||||
const esHost = jugador.idUsuario == partida.idUsuario;
|
|
||||||
|
|
||||||
jugadoresHTML += `
|
jugadoresHTML += `
|
||||||
<div class="jugador border border-2 d-flex align-items-center justify-content-between ${
|
<div class="jugador">
|
||||||
esHost ? "jugador-host" : ""
|
<span class="avatar">👤</span>
|
||||||
}">
|
${escapeHTML(jugador.nombreUsuario)}
|
||||||
<div>
|
</div>
|
||||||
<span class="avatar">${esHost ? "👑" : "👤"}</span>
|
`;
|
||||||
${escapeHTML(jugador.nombreUsuario)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
${
|
|
||||||
esHost
|
|
||||||
? `<span class="badge bg-warning text-dark ms-2">HOST</span>`
|
|
||||||
: ""
|
|
||||||
}
|
|
||||||
</div>
|
|
||||||
`;
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
jugadoresHTML = `<div class="text-muted small">Sin jugadores</div>`;
|
jugadoresHTML = `<div class="text-muted small">Sin jugadores</div>`;
|
||||||
@@ -150,7 +138,7 @@ function mostrarSala(partida, equipos, jugadores) {
|
|||||||
let botonUnirseHTML = "";
|
let botonUnirseHTML = "";
|
||||||
if (!usuarioYaEstaEnEquipo) {
|
if (!usuarioYaEstaEnEquipo) {
|
||||||
botonUnirseHTML = `
|
botonUnirseHTML = `
|
||||||
<button type="button" class="btn-unirse w-100 btn-unirse-equipo mt-2"
|
<button type="button" class="btn btn-unirse w-100 btn-unirse-equipo mt-2"
|
||||||
data-id-partida="${partida.idPartida}"
|
data-id-partida="${partida.idPartida}"
|
||||||
data-id-equipo="${equipo.idEquipo}">
|
data-id-equipo="${equipo.idEquipo}">
|
||||||
Unirse
|
Unirse
|
||||||
@@ -169,42 +157,25 @@ function mostrarSala(partida, equipos, jugadores) {
|
|||||||
`;
|
`;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Acciones disponibles si el usuario YA ESTÁ dentro de esta sala
|
|
||||||
// Acciones disponibles si el usuario YA ESTÁ dentro de esta sala
|
// Acciones disponibles si el usuario YA ESTÁ dentro de esta sala
|
||||||
let accionesSalaHTML = "";
|
let accionesSalaHTML = "";
|
||||||
|
|
||||||
if (usuarioYaEstaEnEquipo) {
|
if (usuarioYaEstaEnEquipo) {
|
||||||
let botonIniciarHTML = "";
|
|
||||||
|
|
||||||
// Solo el host puede iniciar la partida
|
|
||||||
if (partida.idUsuario == idUsuarioSesion) {
|
|
||||||
botonIniciarHTML = `
|
|
||||||
<a href="juego.php?idPartida=${partida.idPartida}"
|
|
||||||
class="btn btn-success btn-lg px-4">
|
|
||||||
🎮 ¡Iniciar Partida!
|
|
||||||
</a>
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
|
|
||||||
accionesSalaHTML = `
|
accionesSalaHTML = `
|
||||||
<div class="sala-acciones text-center mt-4 pt-3 border-top d-flex justify-content-center gap-3">
|
<div class="sala-acciones text-center mt-4 pt-3 border-top d-flex justify-content-center gap-3">
|
||||||
|
<button type="button" class="btn btn-outline-danger btn-abandonar-sala" data-id-partida="${partida.idPartida}">
|
||||||
<button type="button"
|
Abandonar sala
|
||||||
class="btn btn-outline-danger btn-abandonar-sala"
|
</button>
|
||||||
data-id-partida="${partida.idPartida}">
|
<a href="juego.php?idPartida=${partida.idPartida}" class="btn btn-success btn-lg px-4">
|
||||||
Abandonar sala
|
🎮 ¡Iniciar Partida!
|
||||||
</button>
|
</a>
|
||||||
|
</div>
|
||||||
${botonIniciarHTML}
|
`;
|
||||||
|
|
||||||
</div>
|
|
||||||
`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const salaHTML = `
|
const salaHTML = `
|
||||||
<div class="col-12 col-md-10 col-lg-8">
|
<div class="col-12 col-md-10 col-lg-8">
|
||||||
<div class="sala card p-3 shadow-sm mb-4">
|
<div class="sala card p-3 shadow-sm mb-4">
|
||||||
<div class="sala-header h4 text-center mb-3 text-white">
|
<div class="sala-header h4 text-center mb-3">
|
||||||
${escapeHTML(partida.nombrePartida)}
|
${escapeHTML(partida.nombrePartida)}
|
||||||
</div>
|
</div>
|
||||||
<div class="sala-body">
|
<div class="sala-body">
|
||||||
@@ -263,15 +234,13 @@ let botonAbandonarReferencia = null;
|
|||||||
// A. Al hacer clic en el botón "Abandonar sala" de la tarjeta, abrimos el modal
|
// A. Al hacer clic en el botón "Abandonar sala" de la tarjeta, abrimos el modal
|
||||||
$(document).on("click", ".btn-abandonar-sala", function () {
|
$(document).on("click", ".btn-abandonar-sala", function () {
|
||||||
botonAbandonarReferencia = $(this);
|
botonAbandonarReferencia = $(this);
|
||||||
const modal = new bootstrap.Modal(
|
const modal = new bootstrap.Modal(document.getElementById('modalAbandonarSala'));
|
||||||
document.getElementById("modalAbandonarSala"),
|
|
||||||
);
|
|
||||||
modal.show();
|
modal.show();
|
||||||
});
|
});
|
||||||
|
|
||||||
// B. Al hacer clic en "Sí, salir" dentro del modal Kahoot
|
// B. Al hacer clic en "Sí, salir" dentro del modal Kahoot
|
||||||
$(document).on("click", "#btnConfirmarAbandonar", function () {
|
$(document).on("click", "#btnConfirmarAbandonar", function () {
|
||||||
const modalEl = document.getElementById("modalAbandonarSala");
|
const modalEl = document.getElementById('modalAbandonarSala');
|
||||||
const modalInstance = bootstrap.Modal.getInstance(modalEl);
|
const modalInstance = bootstrap.Modal.getInstance(modalEl);
|
||||||
|
|
||||||
const btnConfirmar = $(this);
|
const btnConfirmar = $(this);
|
||||||
@@ -306,7 +275,7 @@ $(document).on("click", "#btnConfirmarAbandonar", function () {
|
|||||||
if (modalInstance) {
|
if (modalInstance) {
|
||||||
modalInstance.hide();
|
modalInstance.hide();
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
// 8. Evento: Crear Sala
|
// 8. Evento: Crear Sala
|
||||||
|
|||||||
+17
-18
@@ -1,5 +1,3 @@
|
|||||||
let idUsuarioSesion = null;
|
|
||||||
|
|
||||||
$(document).ready(function () {
|
$(document).ready(function () {
|
||||||
// Comprobar la sesión activa al cargar la página
|
// Comprobar la sesión activa al cargar la página
|
||||||
comprobarEstadoSesion();
|
comprobarEstadoSesion();
|
||||||
@@ -28,7 +26,7 @@ $(document).ready(function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Comprueba con el servidor si existe una sesión PHP
|
* Comprueba con el servidor si existe una sesión PHP activa.
|
||||||
*/
|
*/
|
||||||
function comprobarEstadoSesion() {
|
function comprobarEstadoSesion() {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
@@ -38,8 +36,8 @@ function comprobarEstadoSesion() {
|
|||||||
data: { orden: 'comprobarSesion' },
|
data: { orden: 'comprobarSesion' },
|
||||||
success: function (respuesta) {
|
success: function (respuesta) {
|
||||||
if (respuesta.activa) {
|
if (respuesta.activa) {
|
||||||
idUsuarioSesion = respuesta.usuario.idUsuario;
|
|
||||||
renderizarCabeceraConSesion(respuesta.usuario);
|
renderizarCabeceraConSesion(respuesta.usuario);
|
||||||
|
// Si existe el contenedor del botón Jugar en index, lo mostramos
|
||||||
$('#colBotonJugar').removeClass('d-none');
|
$('#colBotonJugar').removeClass('d-none');
|
||||||
} else {
|
} else {
|
||||||
renderizarCabeceraSinSesion();
|
renderizarCabeceraSinSesion();
|
||||||
@@ -81,8 +79,13 @@ function ejecutarLogin() {
|
|||||||
$('#modalLogin').modal('hide');
|
$('#modalLogin').modal('hide');
|
||||||
$('#formLogin')[0].reset();
|
$('#formLogin')[0].reset();
|
||||||
|
|
||||||
// Cargar datos de la sesión actualizados
|
renderizarCabeceraConSesion({
|
||||||
comprobarEstadoSesion();
|
idUsuario: respuesta.idUsuario,
|
||||||
|
nombreUsuario: respuesta.nombreUsuario,
|
||||||
|
avatar: respuesta.avatar
|
||||||
|
});
|
||||||
|
|
||||||
|
$('#colBotonJugar').removeClass('d-none');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
error: function () {
|
error: function () {
|
||||||
@@ -129,7 +132,7 @@ function ejecutarRegistro() {
|
|||||||
$('#modalRegistro').modal('hide');
|
$('#modalRegistro').modal('hide');
|
||||||
$('#formRegistro')[0].reset();
|
$('#formRegistro')[0].reset();
|
||||||
|
|
||||||
// Autologin tras registrarse
|
// Autologin
|
||||||
$('#loginEmail').val(email);
|
$('#loginEmail').val(email);
|
||||||
$('#loginPassword').val(pass);
|
$('#loginPassword').val(pass);
|
||||||
ejecutarLogin();
|
ejecutarLogin();
|
||||||
@@ -162,21 +165,15 @@ function cerrarSesion() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renderiza la cabecera cuando HAY sesión iniciada
|
* Renderiza la cabecera cuando HAY sesión iniciada (Menú Perfil + Avatar)
|
||||||
*/
|
*/
|
||||||
function renderizarCabeceraConSesion(usuario) {
|
function renderizarCabeceraConSesion(usuario) {
|
||||||
//Mete el avatar en cabecera
|
let avatarImg = usuario.avatar ? usuario.avatar : 'img/avatars/defaultProfile.jpeg';
|
||||||
let avatarImg = (usuario.avatar && usuario.avatar !== '')
|
|
||||||
? usuario.avatar
|
|
||||||
: 'img/avatars/defaultProfile.jpeg';
|
|
||||||
|
|
||||||
// Refrescar avatar si se cambia
|
|
||||||
let avatarConCacheBuster = avatarImg + '?v=' + new Date().getTime();
|
|
||||||
|
|
||||||
let html = `
|
let html = `
|
||||||
<div class="dropdown">
|
<div class="dropdown">
|
||||||
<button class="btn btn-outline-light dropdown-toggle d-flex align-items-center gap-2 rounded-pill px-3 py-1 fw-bold" type="button" id="userMenu" data-bs-toggle="dropdown" aria-expanded="false">
|
<button class="btn btn-outline-light dropdown-toggle d-flex align-items-center gap-2 rounded-pill px-3 py-1 fw-bold" type="button" id="userMenu" data-bs-toggle="dropdown" aria-expanded="false">
|
||||||
<img src="${avatarConCacheBuster}" alt="Avatar" class="rounded-circle border" style="width: 32px; height: 32px; object-fit: cover;">
|
<img src="${avatarImg}" alt="Avatar" class="rounded-circle border" style="width: 32px; height: 32px; object-fit: cover;">
|
||||||
<span>${escapeHtml(usuario.nombreUsuario)}</span>
|
<span>${escapeHtml(usuario.nombreUsuario)}</span>
|
||||||
</button>
|
</button>
|
||||||
<ul class="dropdown-menu dropdown-menu-end shadow border-0 mt-2" aria-labelledby="userMenu">
|
<ul class="dropdown-menu dropdown-menu-end shadow border-0 mt-2" aria-labelledby="userMenu">
|
||||||
@@ -198,11 +195,13 @@ function renderizarCabeceraConSesion(usuario) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renderiza los botones en la cabecera cuando NO hay sesión (Iniciar sesión azul)
|
* Renderiza los botones originales:
|
||||||
|
* - "Iniciar Sesión" (Rojo -> btn-danger)
|
||||||
|
* - "Registrarse" (Verde -> btn-success)
|
||||||
*/
|
*/
|
||||||
function renderizarCabeceraSinSesion() {
|
function renderizarCabeceraSinSesion() {
|
||||||
let html = `
|
let html = `
|
||||||
<button class="btn btn-primary rounded-pill px-4 fw-bold shadow-sm" data-bs-toggle="modal" data-bs-target="#modalLogin">
|
<button class="btn btn-danger rounded-pill px-4 fw-bold shadow-sm" data-bs-toggle="modal" data-bs-target="#modalLogin">
|
||||||
<i class="fa-solid fa-right-to-bracket me-1"></i> Iniciar sesión
|
<i class="fa-solid fa-right-to-bracket me-1"></i> Iniciar sesión
|
||||||
</button>
|
</button>
|
||||||
<button class="btn btn-success rounded-pill px-4 fw-bold shadow-sm" data-bs-toggle="modal" data-bs-target="#modalRegistro">
|
<button class="btn btn-success rounded-pill px-4 fw-bold shadow-sm" data-bs-toggle="modal" data-bs-target="#modalRegistro">
|
||||||
|
|||||||
@@ -0,0 +1,210 @@
|
|||||||
|
// ============================================
|
||||||
|
// DATOS DE PRUEBA
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
const salas = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
nombre: "Sala Principal",
|
||||||
|
equipos: [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
nombre: "Equipo 1",
|
||||||
|
jugadores: [
|
||||||
|
"Carlos",
|
||||||
|
"María"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 2,
|
||||||
|
nombre: "Equipo 2",
|
||||||
|
jugadores: [
|
||||||
|
"Jorge"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
id: 2,
|
||||||
|
nombre: "Los Trivialeros",
|
||||||
|
equipos: [
|
||||||
|
{
|
||||||
|
id: 3,
|
||||||
|
nombre: "Equipo Rojo",
|
||||||
|
jugadores: [
|
||||||
|
"Ana",
|
||||||
|
"Pedro",
|
||||||
|
"Lucía"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 4,
|
||||||
|
nombre: "Equipo Azul",
|
||||||
|
jugadores: [
|
||||||
|
"David",
|
||||||
|
"Laura"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
id: 3,
|
||||||
|
nombre: "Partida rápida",
|
||||||
|
equipos: [
|
||||||
|
{
|
||||||
|
id: 5,
|
||||||
|
nombre: "Equipo 1",
|
||||||
|
jugadores: [
|
||||||
|
"Miguel"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 6,
|
||||||
|
nombre: "Equipo 2",
|
||||||
|
jugadores: []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
id: 4,
|
||||||
|
nombre: "Noche de Trivial",
|
||||||
|
equipos: [
|
||||||
|
{
|
||||||
|
id: 7,
|
||||||
|
nombre: "Los quesitos",
|
||||||
|
jugadores: [
|
||||||
|
"Pablo",
|
||||||
|
"Sara"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 8,
|
||||||
|
nombre: "Los cerebritos",
|
||||||
|
jugadores: [
|
||||||
|
"Mario",
|
||||||
|
"Elena",
|
||||||
|
"Álvaro"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// MOSTRAR SALAS
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
function mostrarSalas(salas) {
|
||||||
|
|
||||||
|
const contenedor = document.getElementById("contenedorSalas");
|
||||||
|
|
||||||
|
contenedor.innerHTML = "";
|
||||||
|
|
||||||
|
salas.forEach(sala => {
|
||||||
|
|
||||||
|
const columna = document.createElement("div");
|
||||||
|
|
||||||
|
columna.className = "col-12 col-md-6 col-xl-5";
|
||||||
|
|
||||||
|
|
||||||
|
columna.innerHTML = `
|
||||||
|
|
||||||
|
<div class="sala">
|
||||||
|
|
||||||
|
<div class="sala-header">
|
||||||
|
${sala.nombre}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
<div class="sala-body">
|
||||||
|
|
||||||
|
<div class="equipos">
|
||||||
|
|
||||||
|
${crearEquipo(sala, sala.equipos[0])}
|
||||||
|
|
||||||
|
${crearEquipo(sala, sala.equipos[1])}
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
`;
|
||||||
|
|
||||||
|
contenedor.appendChild(columna);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// CREAR EQUIPO
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
function crearEquipo(sala, equipo) {
|
||||||
|
|
||||||
|
let jugadoresHTML = "";
|
||||||
|
|
||||||
|
equipo.jugadores.forEach(jugador => {
|
||||||
|
|
||||||
|
jugadoresHTML += `
|
||||||
|
<div class="jugador">
|
||||||
|
<span class="avatar">👤</span>
|
||||||
|
${jugador}
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
return `
|
||||||
|
|
||||||
|
<div class="equipo">
|
||||||
|
|
||||||
|
<h6>
|
||||||
|
${equipo.nombre}
|
||||||
|
</h6>
|
||||||
|
|
||||||
|
<div class="jugadores">
|
||||||
|
|
||||||
|
${jugadoresHTML}
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="btn btn-unirse"
|
||||||
|
onclick="unirseEquipo(${sala.id}, ${equipo.id})">
|
||||||
|
|
||||||
|
Unirse
|
||||||
|
|
||||||
|
</button>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function unirseEquipo(idSala, idEquipo) {
|
||||||
|
|
||||||
|
console.log("Sala:", idSala);
|
||||||
|
console.log("Equipo:", idEquipo);
|
||||||
|
|
||||||
|
alert(
|
||||||
|
"Has pulsado unirse al equipo " +
|
||||||
|
idEquipo +
|
||||||
|
" de la sala " +
|
||||||
|
idSala
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// CARGAR DATOS DE PRUEBA
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
mostrarSalas(salas);
|
||||||
+27
-20
@@ -26,10 +26,6 @@ $.ajax({
|
|||||||
function cambiarNombre() {
|
function cambiarNombre() {
|
||||||
let nuevoNombre = $("#nuevoNombre").val().trim();
|
let nuevoNombre = $("#nuevoNombre").val().trim();
|
||||||
|
|
||||||
if (nuevoNombre === "") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
method: "POST",
|
method: "POST",
|
||||||
url: "controladores/controladorUsuario.php",
|
url: "controladores/controladorUsuario.php",
|
||||||
@@ -46,13 +42,8 @@ function cambiarNombre() {
|
|||||||
// Actualizamos el nombre mostrado en la página
|
// Actualizamos el nombre mostrado en la página
|
||||||
$("#nombre").text(respuesta.nombreUsuario);
|
$("#nombre").text(respuesta.nombreUsuario);
|
||||||
|
|
||||||
|
// Limpiamos el input
|
||||||
$("#nuevoNombre").val("");
|
$("#nuevoNombre").val("");
|
||||||
|
|
||||||
|
|
||||||
if (typeof comprobarEstadoSesion === "function") {
|
|
||||||
comprobarEstadoSesion();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -83,11 +74,14 @@ $("#inputFoto").change(function () {
|
|||||||
// Comprobamos que sea una imagen
|
// Comprobamos que sea una imagen
|
||||||
if (!archivo.type.startsWith("image/")) {
|
if (!archivo.type.startsWith("image/")) {
|
||||||
alert("El archivo seleccionado no es una imagen.");
|
alert("El archivo seleccionado no es una imagen.");
|
||||||
|
|
||||||
$("#inputFoto").val("");
|
$("#inputFoto").val("");
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let datos = new FormData();
|
let datos = new FormData();
|
||||||
|
|
||||||
datos.append("avatar", archivo);
|
datos.append("avatar", archivo);
|
||||||
datos.append("orden", "cambiarAvatar");
|
datos.append("orden", "cambiarAvatar");
|
||||||
|
|
||||||
@@ -103,23 +97,18 @@ $("#inputFoto").change(function () {
|
|||||||
if (respuesta.error) {
|
if (respuesta.error) {
|
||||||
alert(respuesta.error);
|
alert(respuesta.error);
|
||||||
} else {
|
} else {
|
||||||
// Actualizamos la imagen en la página de perfil sin recargar
|
// Actualizamos la imagen sin recargar la página
|
||||||
$("#fotoPerfil").attr(
|
$("#fotoPerfil").attr(
|
||||||
"src",
|
"src",
|
||||||
respuesta.avatar + "?t=" + new Date().getTime()
|
respuesta.avatar + "?t=" + new Date().getTime(),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Actualizamos también el avatar superior en la cabecera
|
|
||||||
if (typeof comprobarEstadoSesion === "function") {
|
|
||||||
comprobarEstadoSesion();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// =========================
|
// =========================
|
||||||
// RECOGER CATEGORIAS
|
// RECOGER CATEGORIAS
|
||||||
// =========================
|
// ====
|
||||||
|
|
||||||
$("#modalCrearPregunta").on("show.bs.modal", function () {
|
$("#modalCrearPregunta").on("show.bs.modal", function () {
|
||||||
const selectCategoria = $("#idCategoria");
|
const selectCategoria = $("#idCategoria");
|
||||||
@@ -152,14 +141,16 @@ $("#modalCrearPregunta").on("show.bs.modal", function () {
|
|||||||
$("<option>", {
|
$("<option>", {
|
||||||
value: categoria.idCategoria,
|
value: categoria.idCategoria,
|
||||||
text: categoria.nombreCategoria,
|
text: categoria.nombreCategoria,
|
||||||
})
|
}),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
error: function (xhr, status, error) {
|
error: function (xhr, status, error) {
|
||||||
console.error("Error al cargar las categorías:", error);
|
console.error("Error al cargar las categorías:", error);
|
||||||
|
|
||||||
console.error("Respuesta del servidor:", xhr.responseText);
|
console.error("Respuesta del servidor:", xhr.responseText);
|
||||||
|
|
||||||
alert("No se han podido cargar las categorías.");
|
alert("No se han podido cargar las categorías.");
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -167,7 +158,7 @@ $("#modalCrearPregunta").on("show.bs.modal", function () {
|
|||||||
|
|
||||||
// =========================
|
// =========================
|
||||||
// CREAR PREGUNTA
|
// CREAR PREGUNTA
|
||||||
// =========================
|
// ====
|
||||||
|
|
||||||
$("#formCrearPregunta").on("submit", function (e) {
|
$("#formCrearPregunta").on("submit", function (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@@ -197,15 +188,24 @@ $("#formCrearPregunta").on("submit", function (e) {
|
|||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "controladores/controladorPregunta.php",
|
url: "controladores/controladorPregunta.php",
|
||||||
|
|
||||||
type: "POST",
|
type: "POST",
|
||||||
|
|
||||||
dataType: "json",
|
dataType: "json",
|
||||||
|
|
||||||
data: {
|
data: {
|
||||||
orden: "crearPregunta",
|
orden: "crearPregunta",
|
||||||
|
|
||||||
idCategoria: $("#idCategoria").val(),
|
idCategoria: $("#idCategoria").val(),
|
||||||
|
|
||||||
nombrePregunta: pregunta,
|
nombrePregunta: pregunta,
|
||||||
|
|
||||||
respuestaCorrecta: respuestaCorrecta,
|
respuestaCorrecta: respuestaCorrecta,
|
||||||
|
|
||||||
respuestaIncorrecta1: respuestaIncorrecta1,
|
respuestaIncorrecta1: respuestaIncorrecta1,
|
||||||
|
|
||||||
respuestaIncorrecta2: respuestaIncorrecta2,
|
respuestaIncorrecta2: respuestaIncorrecta2,
|
||||||
|
|
||||||
respuestaIncorrecta3: respuestaIncorrecta3,
|
respuestaIncorrecta3: respuestaIncorrecta3,
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -214,8 +214,10 @@ $("#formCrearPregunta").on("submit", function (e) {
|
|||||||
|
|
||||||
if (respuesta.error) {
|
if (respuesta.error) {
|
||||||
alert(respuesta.error);
|
alert(respuesta.error);
|
||||||
|
|
||||||
boton.prop("disabled", false);
|
boton.prop("disabled", false);
|
||||||
boton.text("Crear pregunta");
|
boton.text("Crear pregunta");
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,12 +233,17 @@ $("#formCrearPregunta").on("submit", function (e) {
|
|||||||
// Volvemos a activar el botón
|
// Volvemos a activar el botón
|
||||||
boton.prop("disabled", false);
|
boton.prop("disabled", false);
|
||||||
boton.text("Crear pregunta");
|
boton.text("Crear pregunta");
|
||||||
|
|
||||||
|
// Si tienes que actualizar la lista de preguntas:
|
||||||
|
// cargarPreguntas();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
error: function (xhr, status, error) {
|
error: function (xhr, status, error) {
|
||||||
console.error("Error al crear la pregunta:", error);
|
console.error("Error al crear la pregunta:", error);
|
||||||
|
|
||||||
console.error("Respuesta del servidor:", xhr.responseText);
|
console.error("Respuesta del servidor:", xhr.responseText);
|
||||||
|
|
||||||
alert("No se ha podido crear la pregunta.");
|
alert("No se ha podido crear la pregunta.");
|
||||||
|
|
||||||
boton.prop("disabled", false);
|
boton.prop("disabled", false);
|
||||||
|
|||||||
@@ -166,6 +166,9 @@ CREATE TABLE `partida` (
|
|||||||
`idPartida` int(11) NOT NULL AUTO_INCREMENT,
|
`idPartida` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
`nombrePartida` varchar(100) NOT NULL,
|
`nombrePartida` varchar(100) NOT NULL,
|
||||||
`idUsuario` int(11) NOT NULL,
|
`idUsuario` int(11) NOT NULL,
|
||||||
|
`fechaCreacion` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
`fechaInicio` DATETIME NULL,
|
||||||
|
`fechaFin` DATETIME NULL,
|
||||||
PRIMARY KEY (`idPartida`),
|
PRIMARY KEY (`idPartida`),
|
||||||
KEY `FK idUsuario tabla usuario` (`idUsuario`),
|
KEY `FK idUsuario tabla usuario` (`idUsuario`),
|
||||||
CONSTRAINT `FK idUsuario tabla usuario` FOREIGN KEY (`idUsuario`) REFERENCES `usuario` (`idUsuario`)
|
CONSTRAINT `FK idUsuario tabla usuario` FOREIGN KEY (`idUsuario`) REFERENCES `usuario` (`idUsuario`)
|
||||||
@@ -178,10 +181,10 @@ CREATE TABLE `partida` (
|
|||||||
|
|
||||||
LOCK TABLES `partida` WRITE;
|
LOCK TABLES `partida` WRITE;
|
||||||
/*!40000 ALTER TABLE `partida` DISABLE KEYS */;
|
/*!40000 ALTER TABLE `partida` DISABLE KEYS */;
|
||||||
INSERT INTO `partida` VALUES
|
INSERT INTO `partida` (idPartida, nombrePartida, idUsuario, fechaInicio, fechaFin) VALUES
|
||||||
(1,'Partida de prueba 1',1),
|
(1,'Partida de prueba 1',1, NULL, NULL),
|
||||||
(2,'Partida de prueba 2',4),
|
(2,'Partida de prueba 2',4, NULL, NULL),
|
||||||
(3,'Partida de prueba 3',9);
|
(3,'Partida de prueba 3',9, NULL, NULL);
|
||||||
/*!40000 ALTER TABLE `partida` ENABLE KEYS */;
|
/*!40000 ALTER TABLE `partida` ENABLE KEYS */;
|
||||||
UNLOCK TABLES;
|
UNLOCK TABLES;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user