From 59359d33d32e584b1fa0a72e0158bee956eb3504 Mon Sep 17 00:00:00 2001 From: Curry141 <135717077+Daniongithub@users.noreply.github.com> Date: Fri, 31 Oct 2025 16:51:30 +0100 Subject: [PATCH] Piccolo fix + importatore da ITIS --- htdocs/admin/importer.php | 334 ++++++++++++++++++++++++++++++++++++++ htdocs/admin/index.php | 4 +- htdocs/laboratori.php | 20 ++- 3 files changed, 350 insertions(+), 8 deletions(-) create mode 100644 htdocs/admin/importer.php diff --git a/htdocs/admin/importer.php b/htdocs/admin/importer.php new file mode 100644 index 0000000..095aa2f --- /dev/null +++ b/htdocs/admin/importer.php @@ -0,0 +1,334 @@ +prepare("DELETE FROM timetable WHERE class_id = ?"); + $stmt->bind_param("i", $classe_id); + $stmt->execute(); + $stmt->close(); + + $inserimenti = 0; + $materie_create = []; + + // Processa ogni giorno + foreach ($data['giorni'] as $giorno => $ore) { + foreach ($ore as $oraData) { + // Salta ore vuote + if ($oraData['materia'] === null) { + continue; + } + + $ora = $oraData['ora']; + $materia = $oraData['materia']; + $materiaCompleta = $oraData['materiaCompleta']; + $docenti = $oraData['docenti']; + $laboratori = $oraData['laboratori']; // Ora è un array + + // Se non ci sono docenti, salta (situazione anomala) + if (count($docenti) === 0) { + continue; + } + + // Caso 1: Stesso numero di docenti e laboratori → associazione 1:1 + if (count($docenti) === count($laboratori) && count($laboratori) > 0) { + foreach ($docenti as $idx => $docente) { + $laboratorio = $laboratori[$idx]; + + // Cerca/crea materia + $stmt = $conn->prepare("SELECT id FROM subjects WHERE name = ? AND teacher = ? AND room = ?"); + $stmt->bind_param("sss", $materiaCompleta, $docente, $laboratorio); + $stmt->execute(); + $result = $stmt->get_result(); + + if ($result->num_rows > 0) { + $subject_id = $result->fetch_assoc()['id']; + } else { + $stmt2 = $conn->prepare("INSERT INTO subjects (name, teacher, room) VALUES (?, ?, ?)"); + $stmt2->bind_param("sss", $materiaCompleta, $docente, $laboratorio); + $stmt2->execute(); + $subject_id = $conn->insert_id; + $stmt2->close(); + $materie_create[] = "$materiaCompleta ($docente - $laboratorio)"; + } + $stmt->close(); + + // Inserisci in timetable + $stmt3 = $conn->prepare("INSERT INTO timetable (class_id, day, hour, subject_id) VALUES (?, ?, ?, ?)"); + $stmt3->bind_param("isii", $classe_id, $giorno, $ora, $subject_id); + $stmt3->execute(); + $stmt3->close(); + $inserimenti++; + } + } + // Caso 2: Più docenti, un laboratorio (o nessuno) → stesso laboratorio per tutti + else if (count($laboratori) <= 1) { + $laboratorio = count($laboratori) > 0 ? $laboratori[0] : null; + + foreach ($docenti as $docente) { + // Cerca/crea materia + if ($laboratorio) { + $stmt = $conn->prepare("SELECT id FROM subjects WHERE name = ? AND teacher = ? AND room = ?"); + $stmt->bind_param("sss", $materiaCompleta, $docente, $laboratorio); + } else { + $stmt = $conn->prepare("SELECT id FROM subjects WHERE name = ? AND teacher = ? AND (room IS NULL OR room = '')"); + $stmt->bind_param("ss", $materiaCompleta, $docente); + } + + $stmt->execute(); + $result = $stmt->get_result(); + + if ($result->num_rows > 0) { + $subject_id = $result->fetch_assoc()['id']; + } else { + $stmt2 = $conn->prepare("INSERT INTO subjects (name, teacher, room) VALUES (?, ?, ?)"); + $stmt2->bind_param("sss", $materiaCompleta, $docente, $laboratorio); + $stmt2->execute(); + $subject_id = $conn->insert_id; + $stmt2->close(); + $materie_create[] = "$materiaCompleta ($docente" . ($laboratorio ? " - $laboratorio" : "") . ")"; + } + $stmt->close(); + + // Inserisci in timetable + $stmt3 = $conn->prepare("INSERT INTO timetable (class_id, day, hour, subject_id) VALUES (?, ?, ?, ?)"); + $stmt3->bind_param("isii", $classe_id, $giorno, $ora, $subject_id); + $stmt3->execute(); + $stmt3->close(); + $inserimenti++; + } + } + // Caso 3: Più laboratori che docenti → usa il primo laboratorio per tutti + else { + $laboratorio = $laboratori[0]; + + foreach ($docenti as $docente) { + $stmt = $conn->prepare("SELECT id FROM subjects WHERE name = ? AND teacher = ? AND room = ?"); + $stmt->bind_param("sss", $materiaCompleta, $docente, $laboratorio); + $stmt->execute(); + $result = $stmt->get_result(); + + if ($result->num_rows > 0) { + $subject_id = $result->fetch_assoc()['id']; + } else { + $stmt2 = $conn->prepare("INSERT INTO subjects (name, teacher, room) VALUES (?, ?, ?)"); + $stmt2->bind_param("sss", $materiaCompleta, $docente, $laboratorio); + $stmt2->execute(); + $subject_id = $conn->insert_id; + $stmt2->close(); + $materie_create[] = "$materiaCompleta ($docente - $laboratorio)"; + } + $stmt->close(); + + $stmt3 = $conn->prepare("INSERT INTO timetable (class_id, day, hour, subject_id) VALUES (?, ?, ?, ?)"); + $stmt3->bind_param("isii", $classe_id, $giorno, $ora, $subject_id); + $stmt3->execute(); + $stmt3->close(); + $inserimenti++; + } + } + } + } + + $message = "Importazione completata con successo!
"; + $message .= "- Inserite $inserimenti ore di lezione
"; + if (count($materie_create) > 0) { + $message .= "- Create " . count($materie_create) . " nuove materie"; + } + $messageType = "success"; + + } catch (Exception $e) { + $message = "Errore durante l'importazione: " . htmlspecialchars($e->getMessage()); + $messageType = "error"; + } + } +} +?> + + + + Importa Orario + + + + + + + + +
+

Importa Orario da Sistema Esterno

+ ⬅ Torna al Dashboard + + +
+ +
+ + +
+ Attenzione: L'importazione cancellerà l'orario esistente della classe selezionata + e lo sostituirà con i dati importati dal sistema esterno. + Verranno create automaticamente le materie mancanti. +
+ +
+

Configura Importazione

+
+
+ + + Classe nel tuo database dove importare l'orario +
+ +
+ + + Codice della classe nel sistema esterno +
+ +
+ + + Endpoint dell'API Node.js per lo scraping +
+ + +
+
+ +
+

Come funziona l'importazione

+
    +
  1. Assicurati che il server Node.js sia avviato (node server.js)
  2. +
  3. Seleziona la classe di destinazione nel tuo database
  4. +
  5. Inserisci il codice della classe nel sistema esterno (es: 3BIN, 1A, 5AINF)
  6. +
  7. Clicca su "Importa Orario"
  8. +
  9. Il sistema cancellerà l'orario esistente e importerà i nuovi dati
  10. +
+ +

Gestione casi speciali

+ +
+ +

+ Copyright (C) 2025 EmmeV. - Released under GNU AGPL 3.0 License. +

+
+ + + diff --git a/htdocs/admin/index.php b/htdocs/admin/index.php index 8b4e1c4..d92825b 100644 --- a/htdocs/admin/index.php +++ b/htdocs/admin/index.php @@ -25,11 +25,12 @@ if (!isset($_SESSION['admin'])) {
-

Benvenuto, !

+

Benvenuto, !

Gestisci Classi Gestisci Materie Gestisci Orario + 🔄 Importa Orario Cambia Password'; @@ -48,4 +49,3 @@ if (!isset($_SESSION['admin'])) {

- diff --git a/htdocs/laboratori.php b/htdocs/laboratori.php index 38d8d7e..4f7e205 100644 --- a/htdocs/laboratori.php +++ b/htdocs/laboratori.php @@ -63,16 +63,21 @@ if ($res->num_rows === 0) { if($q->num_rows > 0){ $subject = null; - $entries = []; + // FIX: Uso array associativo per evitare duplicati classe+docente + $class_teacher_pairs = []; while($row = $q->fetch_assoc()){ if($subject === null) { $subject = $row['subject_name']; } - $entries[] = $row['class_name'] . " (" . $row['teacher'] . ")"; + // Creo una coppia unica classe-docente + $pair = $row['class_name'] . " (" . $row['teacher'] . ")"; + $class_teacher_pairs[$pair] = true; // Uso chiave per evitare duplicati } - // FIX: Gestione corretta di multiple classi + // Converto in array e unisco + $entries = array_keys($class_teacher_pairs); + if(count($entries) > 1){ $last = array_pop($entries); $entries_list = implode(", ", $entries) . " e " . $last; @@ -93,7 +98,7 @@ if ($res->num_rows === 0) { ?> - +
@@ -111,15 +116,18 @@ if ($res->num_rows === 0) { if($q->num_rows > 0): $subject = null; - $entries = []; + $class_teacher_pairs = []; while($row = $q->fetch_assoc()){ if($subject === null) { $subject = $row['subject_name']; } - $entries[] = $row['class_name'] . " (" . $row['teacher'] . ")"; + $pair = $row['class_name'] . " (" . $row['teacher'] . ")"; + $class_teacher_pairs[$pair] = true; } + $entries = array_keys($class_teacher_pairs); + if(count($entries) > 1){ $last = array_pop($entries); $entries_list = implode(", ", $entries) . " e " . $last;