cambios nombres controladores
This commit is contained in:
@@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
session_start();
|
||||||
|
|
||||||
|
require_once '../clases/ClasePregunta.php';
|
||||||
|
|
||||||
|
switch ($_POST['orden']) {
|
||||||
|
|
||||||
|
case 'crearPregunta':
|
||||||
|
|
||||||
|
$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']);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$pregunta = new Pregunta($idCategoria, $nombrePregunta);
|
||||||
|
$resultado = $pregunta->guardar();
|
||||||
|
|
||||||
|
echo json_encode($resultado);
|
||||||
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user