SuperCafe: use Alumnos for persons, fix sysadmin add-user form

Co-authored-by: naielv <109038805+naielv@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-02-21 21:07:46 +00:00
parent 69d7e46dc8
commit 6e4496b050
4 changed files with 170 additions and 42 deletions

View File

@@ -17,8 +17,7 @@ DATA/
│ │ ├── aulario_abc123.json │ │ ├── aulario_abc123.json
│ │ └── aulario_xyz456.json │ │ └── aulario_xyz456.json
│ └── SuperCafe/ # SuperCafe data (per centro) │ └── SuperCafe/ # SuperCafe data (per centro)
│ ├── Personas.json # People directory │ ├── Menu.json # Menu items / categories (optional)
│ ├── Menu.json # Menu items / categories
│ └── Comandas/ # One JSON file per order │ └── Comandas/ # One JSON file per order
│ └── sc<id>.json │ └── sc<id>.json
└── centro2/ └── centro2/
@@ -28,14 +27,10 @@ DATA/
## File Examples ## File Examples
### SuperCafe Persons (DATA/entreaulas/Centros/{centro_id}/SuperCafe/Personas.json) ### SuperCafe (persons come from the existing Alumnos system)
```json Persons are loaded automatically from the aulario Alumnos directories — no separate configuration file is needed.
{ See the **Aulario Student Names** section for the alumnos data format.
"persona1": { "Nombre": "Ana García", "Region": "Clase A" },
"persona2": { "Nombre": "Luis Martínez", "Region": "Clase B" }
}
```
### SuperCafe Menu (DATA/entreaulas/Centros/{centro_id}/SuperCafe/Menu.json) ### SuperCafe Menu (DATA/entreaulas/Centros/{centro_id}/SuperCafe/Menu.json)
@@ -57,13 +52,14 @@ DATA/
```json ```json
{ {
"Fecha": "2024-01-15", "Fecha": "2024-01-15",
"Persona": "persona1", "Persona": "aulario_abc123:Juan",
"Comanda": "1x Café, 1x Bocadillo", "Comanda": "1x Café, 1x Bocadillo",
"Notas": "Sin azúcar", "Notas": "Sin azúcar",
"Estado": "Pedido" "Estado": "Pedido"
} }
``` ```
`Persona` is stored as `{aulario_id}:{alumno_name}` and resolved to a display name at render time.
Order statuses: `Pedido`, `En preparación`, `Listo`, `Entregado`, `Deuda`. Order statuses: `Pedido`, `En preparación`, `Listo`, `Entregado`, `Deuda`.
A person with 3 or more orders in `Deuda` status cannot place new orders. A person with 3 or more orders in `Deuda` status cannot place new orders.

View File

@@ -17,6 +17,53 @@ function sc_safe_order_id($value)
return preg_replace('/[^a-zA-Z0-9_-]/', '', basename((string)$value)); return preg_replace('/[^a-zA-Z0-9_-]/', '', basename((string)$value));
} }
/**
* Load personas from the existing Alumnos system.
* Returns array keyed by "{aulario_id}:{alumno_name}" with
* ['Nombre', 'Region' (aulario display name), 'AularioID', 'HasPhoto'] entries.
*/
function sc_load_personas_from_alumnos($centro_id)
{
$aularios_path = "/DATA/entreaulas/Centros/$centro_id/Aularios";
$personas = [];
if (!is_dir($aularios_path)) {
return $personas;
}
foreach (glob("$aularios_path/*.json") ?: [] as $aulario_file) {
$aulario_id = basename($aulario_file, '.json');
$aulario_data = json_decode(file_get_contents($aulario_file), true);
$aulario_name = $aulario_data['name'] ?? $aulario_id;
$alumnos_path = "$aularios_path/$aulario_id/Alumnos";
if (!is_dir($alumnos_path)) {
continue;
}
foreach (glob("$alumnos_path/*/", GLOB_ONLYDIR) ?: [] as $alumno_dir) {
$alumno_name = basename($alumno_dir);
$key = $aulario_id . ':' . $alumno_name;
$personas[$key] = [
'Nombre' => $alumno_name,
'Region' => $aulario_name,
'AularioID' => $aulario_id,
'HasPhoto' => file_exists("$alumno_dir/photo.jpg"),
];
}
}
return $personas;
}
/**
* Return a human-readable label for a persona key.
* Falls back to showing the raw stored value for legacy orders.
*/
function sc_persona_label($persona_key, $personas)
{
if (isset($personas[$persona_key])) {
$p = $personas[$persona_key];
return $p['Nombre'] . ' (' . $p['Region'] . ')';
}
return $persona_key;
}
$centro_id = safe_centro_id_sc($_SESSION['auth_data']['entreaulas']['centro'] ?? ''); $centro_id = safe_centro_id_sc($_SESSION['auth_data']['entreaulas']['centro'] ?? '');
if ($centro_id === '') { if ($centro_id === '') {
require_once "_incl/pre-body.php"; require_once "_incl/pre-body.php";
@@ -37,6 +84,7 @@ $estados_colores = [
]; ];
$can_edit = in_array('supercafe:edit', $_SESSION['auth_data']['permissions'] ?? []); $can_edit = in_array('supercafe:edit', $_SESSION['auth_data']['permissions'] ?? []);
$personas = sc_load_personas_from_alumnos($centro_id);
// Handle POST actions (requires edit permission) // Handle POST actions (requires edit permission)
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $can_edit) { if ($_SERVER['REQUEST_METHOD'] === 'POST' && $can_edit) {
@@ -139,7 +187,7 @@ require_once "_incl/pre-body.php";
?> ?>
<tr style="background: <?= htmlspecialchars($bg) ?>;"> <tr style="background: <?= htmlspecialchars($bg) ?>;">
<td><?= htmlspecialchars($order['Fecha'] ?? '') ?></td> <td><?= htmlspecialchars($order['Fecha'] ?? '') ?></td>
<td><?= htmlspecialchars($order['Persona'] ?? '') ?></td> <td><?= htmlspecialchars(sc_persona_label($order['Persona'] ?? '', $personas)) ?></td>
<td style="white-space: pre-wrap; max-width: 250px;"><?= htmlspecialchars($order['Comanda'] ?? '') ?></td> <td style="white-space: pre-wrap; max-width: 250px;"><?= htmlspecialchars($order['Comanda'] ?? '') ?></td>
<td><?= htmlspecialchars($order['Notas'] ?? '') ?></td> <td><?= htmlspecialchars($order['Notas'] ?? '') ?></td>
<td><strong><?= htmlspecialchars($estado) ?></strong></td> <td><strong><?= htmlspecialchars($estado) ?></strong></td>
@@ -203,7 +251,7 @@ require_once "_incl/pre-body.php";
<?php foreach ($orders_deuda as $order): ?> <?php foreach ($orders_deuda as $order): ?>
<tr style="background: #f5d3ff;"> <tr style="background: #f5d3ff;">
<td><?= htmlspecialchars($order['Fecha'] ?? '') ?></td> <td><?= htmlspecialchars($order['Fecha'] ?? '') ?></td>
<td><?= htmlspecialchars($order['Persona'] ?? '') ?></td> <td><?= htmlspecialchars(sc_persona_label($order['Persona'] ?? '', $personas)) ?></td>
<td style="white-space: pre-wrap; max-width: 250px;"><?= htmlspecialchars($order['Comanda'] ?? '') ?></td> <td style="white-space: pre-wrap; max-width: 250px;"><?= htmlspecialchars($order['Comanda'] ?? '') ?></td>
<td><?= htmlspecialchars($order['Notas'] ?? '') ?></td> <td><?= htmlspecialchars($order['Notas'] ?? '') ?></td>
<?php if ($can_edit): ?> <?php if ($can_edit): ?>

View File

@@ -31,14 +31,45 @@ define('SC_MAX_DEBTS', 3);
$valid_statuses = ['Pedido', 'En preparación', 'Listo', 'Entregado', 'Deuda']; $valid_statuses = ['Pedido', 'En preparación', 'Listo', 'Entregado', 'Deuda'];
function sc_load_personas($sc_base) /**
* Load personas from the existing Alumnos system (alumnos.php).
* Returns array keyed by "{aulario_id}:{alumno_name}" with
* ['Nombre', 'Region' (aulario display name), 'AularioID'] entries.
* Groups are sorted by aulario name, alumnos sorted alphabetically.
*/
function sc_load_personas_from_alumnos($centro_id)
{ {
$path = "$sc_base/Personas.json"; $aularios_path = "/DATA/entreaulas/Centros/$centro_id/Aularios";
if (!file_exists($path)) { $personas = [];
return []; if (!is_dir($aularios_path)) {
return $personas;
} }
$data = json_decode(file_get_contents($path), true); $aulario_files = glob("$aularios_path/*.json") ?: [];
return is_array($data) ? $data : []; foreach ($aulario_files as $aulario_file) {
$aulario_id = basename($aulario_file, '.json');
$aulario_data = json_decode(file_get_contents($aulario_file), true);
$aulario_name = $aulario_data['name'] ?? $aulario_id;
$alumnos_path = "$aularios_path/$aulario_id/Alumnos";
if (!is_dir($alumnos_path)) {
continue;
}
$alumno_dirs = glob("$alumnos_path/*/", GLOB_ONLYDIR) ?: [];
usort($alumno_dirs, function ($a, $b) {
return strcasecmp(basename($a), basename($b));
});
foreach ($alumno_dirs as $alumno_dir) {
$alumno_name = basename($alumno_dir);
// Key uses ':' as separator; safe_id_segment chars [A-Za-z0-9_-] exclude ':'
$key = $aulario_id . ':' . $alumno_name;
$personas[$key] = [
'Nombre' => $alumno_name,
'Region' => $aulario_name,
'AularioID' => $aulario_id,
'HasPhoto' => file_exists("$alumno_dir/photo.jpg"),
];
}
}
return $personas;
} }
function sc_load_menu($sc_base) function sc_load_menu($sc_base)
@@ -51,7 +82,7 @@ function sc_load_menu($sc_base)
return is_array($data) ? $data : []; return is_array($data) ? $data : [];
} }
function sc_count_debts($persona_id) function sc_count_debts($persona_key)
{ {
if (!is_dir(SC_DATA_DIR)) { if (!is_dir(SC_DATA_DIR)) {
return 0; return 0;
@@ -60,7 +91,7 @@ function sc_count_debts($persona_id)
foreach (glob(SC_DATA_DIR . '/*.json') ?: [] as $file) { foreach (glob(SC_DATA_DIR . '/*.json') ?: [] as $file) {
$data = json_decode(file_get_contents($file), true); $data = json_decode(file_get_contents($file), true);
if (is_array($data) if (is_array($data)
&& ($data['Persona'] ?? '') === $persona_id && ($data['Persona'] ?? '') === $persona_key
&& ($data['Estado'] ?? '') === 'Deuda') { && ($data['Estado'] ?? '') === 'Deuda') {
$count++; $count++;
} }
@@ -92,22 +123,30 @@ if (!$is_new && is_readable($order_file)) {
} }
} }
$personas = sc_load_personas($sc_base); $personas = sc_load_personas_from_alumnos($centro_id);
$menu = sc_load_menu($sc_base); $menu = sc_load_menu($sc_base);
// Group personas by aulario for the optgroup picker
$personas_by_aulario = [];
foreach ($personas as $key => $pinfo) {
$personas_by_aulario[$pinfo['Region']][$key] = $pinfo;
}
$error = ''; $error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$persona_id = $_POST['Persona'] ?? ''; $persona_key = $_POST['Persona'] ?? '';
$notas = trim($_POST['Notas'] ?? ''); $notas = trim($_POST['Notas'] ?? '');
$estado = $_POST['Estado'] ?? 'Pedido'; $estado = $_POST['Estado'] ?? 'Pedido';
if (!in_array($estado, $valid_statuses, true)) { if (!in_array($estado, $valid_statuses, true)) {
$estado = 'Pedido'; $estado = 'Pedido';
} }
if ($persona_id === '') { // Validate that the submitted persona key exists in the loaded list.
$error = '¡Hay que elegir una persona!'; // When no alumnos are configured ($personas is empty), accept any non-empty free-text value.
if ($persona_key === '' || (!empty($personas) && !array_key_exists($persona_key, $personas))) {
$error = '¡Hay que elegir una persona válida!';
} else { } else {
// Build comanda string from selected menu items // Build comanda string from selected menu items
$comanda_parts = []; $comanda_parts = [];
@@ -122,7 +161,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
} }
} }
} else { } else {
// No menu configured: accept free-text input
$manual = trim($_POST['Comanda_manual'] ?? ''); $manual = trim($_POST['Comanda_manual'] ?? '');
if ($manual !== '') { if ($manual !== '') {
$comanda_parts[] = $manual; $comanda_parts[] = $manual;
@@ -130,10 +168,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
} }
$comanda_str = implode(', ', $comanda_parts); $comanda_str = implode(', ', $comanda_parts);
// Debt check: only for new orders or when changing the person // Debt check: only for new orders or when the person changes
$prev_persona = $order_data['Persona'] ?? ''; $prev_persona = $order_data['Persona'] ?? '';
if ($is_new || $prev_persona !== $persona_id) { if ($is_new || $prev_persona !== $persona_key) {
$debt_count = sc_count_debts($persona_id); $debt_count = sc_count_debts($persona_key);
if ($debt_count >= SC_MAX_DEBTS) { if ($debt_count >= SC_MAX_DEBTS) {
$error = 'Esta persona tiene ' . $debt_count . ' comandas en deuda. No se puede realizar el pedido.'; $error = 'Esta persona tiene ' . $debt_count . ' comandas en deuda. No se puede realizar el pedido.';
} }
@@ -142,7 +180,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($error === '') { if ($error === '') {
$new_data = [ $new_data = [
'Fecha' => date('Y-m-d'), 'Fecha' => date('Y-m-d'),
'Persona' => $persona_id, 'Persona' => $persona_key,
'Comanda' => $comanda_str, 'Comanda' => $comanda_str,
'Notas' => $notas, 'Notas' => $notas,
'Estado' => $is_new ? 'Pedido' : $estado, 'Estado' => $is_new ? 'Pedido' : $estado,
@@ -192,26 +230,44 @@ require_once "_incl/pre-body.php";
<div class="mb-3"> <div class="mb-3">
<label class="form-label"><strong>Persona</strong></label> <label class="form-label"><strong>Persona</strong></label>
<?php if (!empty($personas)): ?> <?php if (!empty($personas_by_aulario)): ?>
<select name="Persona" class="form-select" required> <select name="Persona" class="form-select" required>
<option value="">-- Selecciona una persona --</option> <option value="">-- Selecciona una persona --</option>
<?php foreach ($personas as $pid => $pinfo): ?> <?php foreach ($personas_by_aulario as $region_name => $group): ?>
<option value="<?= htmlspecialchars($pid) ?>" <optgroup label="<?= htmlspecialchars($region_name) ?>">
<?= ($order_data['Persona'] === (string)$pid) ? 'selected' : '' ?>> <?php foreach ($group as $pkey => $pinfo): ?>
<?= htmlspecialchars($pinfo['Nombre'] ?? $pid) ?> <option value="<?= htmlspecialchars($pkey) ?>"
<?php if (!empty($pinfo['Region'])): ?> <?= ($order_data['Persona'] === $pkey) ? 'selected' : '' ?>>
(<?= htmlspecialchars($pinfo['Region']) ?>) <?= htmlspecialchars($pinfo['Nombre']) ?>
<?php endif; ?> </option>
</option> <?php endforeach; ?>
</optgroup>
<?php endforeach; ?> <?php endforeach; ?>
</select> </select>
<?php
// Show photo of the currently selected person (if editing)
$sel_key = $order_data['Persona'];
$sel_info = $personas[$sel_key] ?? null;
if ($sel_info && $sel_info['HasPhoto']):
?>
<div id="sc-persona-photo" style="margin-top: 8px;">
<?php $photo_url = '/entreaulas/_filefetch.php?type=alumno_photo'
. '&centro=' . urlencode($centro_id)
. '&aulario=' . urlencode($sel_info['AularioID'])
. '&alumno=' . urlencode($sel_info['Nombre']); ?>
<img src="<?= htmlspecialchars($photo_url) ?>"
alt="Foto de <?= htmlspecialchars($sel_info['Nombre']) ?>"
style="height: 80px; border-radius: 8px; border: 2px solid #dee2e6;">
</div>
<?php endif; ?>
<?php else: ?> <?php else: ?>
<input type="text" name="Persona" class="form-control" <input type="text" name="Persona" class="form-control"
value="<?= htmlspecialchars($order_data['Persona']) ?>" value="<?= htmlspecialchars($order_data['Persona']) ?>"
placeholder="Nombre de la persona" required> placeholder="Nombre de la persona" required>
<small class="text-muted"> <small class="text-muted">
No hay personas configuradas en No hay alumnos registrados en los aularios de este centro.
<code>/DATA/entreaulas/Centros/<?= htmlspecialchars($centro_id) ?>/SuperCafe/Personas.json</code>. Añade alumnos desde
<a href="/entreaulas/">EntreAulas</a>.
</small> </small>
<?php endif; ?> <?php endif; ?>
</div> </div>
@@ -281,3 +337,4 @@ require_once "_incl/pre-body.php";
</form> </form>
<?php require_once "_incl/post-body.php"; ?> <?php require_once "_incl/post-body.php"; ?>

View File

@@ -192,6 +192,33 @@ switch ($_GET['action'] ?? '') {
<button type="submit" class="btn btn-primary mt-3">Crear Usuario</button> <button type="submit" class="btn btn-primary mt-3">Crear Usuario</button>
</div> </div>
</div> </div>
<div class="card pad">
<div>
<h2>EntreAulas: Configuración</h2>
<div class="mb-3">
<label for="centro" class="form-label">Centro asociado:</label>
<select id="centro" name="centro" class="form-select">
<option value="">-- Selecciona un centro --</option>
<?php
$centros_folders_add = glob("/DATA/entreaulas/Centros/*", GLOB_ONLYDIR) ?: [];
foreach ($centros_folders_add as $centro_folder) {
$centro_id_opt = basename($centro_folder);
echo '<option value="' . htmlspecialchars($centro_id_opt) . '">' . htmlspecialchars($centro_id_opt) . '</option>';
}
?>
</select>
</div>
<div class="mb-3">
<label for="role" class="form-label">Rol en EntreAulas:</label>
<select id="role" name="role" class="form-select">
<option value="">-- Selecciona un rol --</option>
<option value="teacher">Profesor</option>
<option value="student">Estudiante</option>
</select>
</div>
<p class="text-muted"><small>Las aulas podrán asignarse tras guardar el usuario.</small></p>
</div>
</div>
</form> </form>
<?php <?php
require_once "_incl/post-body.php"; require_once "_incl/post-body.php";