405fcc2b69
- Modal: "Ab wann" (Sofort/Zeitpunkt) + "Bis wann" (Presets + date/time statt datetime-local), Live-Zusammenfassung; zukuenftiger Start => vorbereitet - assignments.php: startsAt-Param (Default NOW()), T->Space - modules.php: neue Klasse ohne Eintrag -> nur Weltkueche frei (_defaultMode), Rest gesperrt; Freigabe-Matrix bleibt (getrennt von der Aufgabe) - schueler.html: Hinweis "Deine Lehrperson kann weitere Simulationen freischalten" Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
96 lines
3.9 KiB
PHP
96 lines
3.9 KiB
PHP
<?php
|
|
/**
|
|
* API: Klassen-Simulationen (Aufgaben/Hausübungen)
|
|
* GET /api/assignments?class_id=X → Aktive Aufgaben einer Klasse
|
|
* GET /api/assignments?student=1 → Aufgaben für eingeloggten Schüler*in
|
|
* POST /api/assignments {action} → Aufgabe erstellen/beenden
|
|
*/
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
$db = Database::get();
|
|
|
|
if ($method === 'GET') {
|
|
// Schüler*innen-Ansicht
|
|
if (isset($_GET['student']) && $_GET['student'] === '1') {
|
|
$sessionId = Session::requireStudent();
|
|
$session = $db->fetchOne('SELECT class_id FROM student_sessions WHERE id = ?', [$sessionId]);
|
|
if (!$session || !$session['class_id']) Response::ok([]);
|
|
|
|
$assignments = $db->fetchAll(
|
|
'SELECT a.id, a.module_id, a.level, a.starts_at, a.ends_at, a.created_at
|
|
FROM class_assignments a
|
|
WHERE a.class_id = ? AND a.active = 1 AND a.starts_at <= NOW() AND (a.ends_at IS NULL OR a.ends_at > NOW())
|
|
ORDER BY a.created_at DESC',
|
|
[$session['class_id']]
|
|
);
|
|
Response::ok($assignments);
|
|
}
|
|
|
|
// Lehrperson-Ansicht
|
|
$teacherId = Session::requireTeacher();
|
|
$classId = (int)($_GET['class_id'] ?? 0);
|
|
if (!$classId) Response::error('class_id erforderlich');
|
|
|
|
$class = $db->fetchOne('SELECT id FROM classes WHERE id = ? AND teacher_id = ?', [$classId, $teacherId]);
|
|
if (!$class) Response::error('Klasse nicht gefunden', 404);
|
|
|
|
$assignments = $db->fetchAll(
|
|
'SELECT id, module_id, level, starts_at, ends_at, active, created_at FROM class_assignments WHERE class_id = ? ORDER BY created_at DESC',
|
|
[$classId]
|
|
);
|
|
Response::ok($assignments);
|
|
}
|
|
|
|
if ($method === 'POST') {
|
|
$teacherId = Session::requireTeacher();
|
|
$body = json_decode(file_get_contents('php://input'), true);
|
|
$action = $body['action'] ?? '';
|
|
|
|
if ($action === 'create') {
|
|
$classId = (int)($body['classId'] ?? 0);
|
|
$moduleId = $body['moduleId'] ?? '';
|
|
$level = max(1, min(5, (int)($body['level'] ?? 1)));
|
|
// datetime-local liefert 'YYYY-MM-DDTHH:MM' → 'T' für MySQL zu Leerzeichen.
|
|
$endsAt = !empty($body['endsAt']) ? str_replace('T', ' ', $body['endsAt']) : null;
|
|
$startsAt = !empty($body['startsAt']) ? str_replace('T', ' ', $body['startsAt']) : null;
|
|
|
|
if (!$classId || !$moduleId) Response::error('classId und moduleId erforderlich');
|
|
|
|
$class = $db->fetchOne('SELECT id FROM classes WHERE id = ? AND teacher_id = ?', [$classId, $teacherId]);
|
|
if (!$class) Response::error('Klasse nicht gefunden', 404);
|
|
|
|
// Vorherige aktive Aufgabe für dieses Modul deaktivieren
|
|
$db->execute(
|
|
'UPDATE class_assignments SET active = 0 WHERE class_id = ? AND module_id = ? AND active = 1',
|
|
[$classId, $moduleId]
|
|
);
|
|
|
|
if ($startsAt) {
|
|
$db->execute(
|
|
'INSERT INTO class_assignments (class_id, teacher_id, module_id, level, starts_at, ends_at) VALUES (?, ?, ?, ?, ?, ?)',
|
|
[$classId, $teacherId, $moduleId, $level, $startsAt, $endsAt]
|
|
);
|
|
} else {
|
|
$db->execute(
|
|
'INSERT INTO class_assignments (class_id, teacher_id, module_id, level, starts_at, ends_at) VALUES (?, ?, ?, ?, NOW(), ?)',
|
|
[$classId, $teacherId, $moduleId, $level, $endsAt]
|
|
);
|
|
}
|
|
Response::ok(['id' => (int)$db->lastInsertId()]);
|
|
}
|
|
|
|
if ($action === 'stop') {
|
|
$assignmentId = (int)($body['assignmentId'] ?? 0);
|
|
if (!$assignmentId) Response::error('assignmentId erforderlich');
|
|
$db->execute(
|
|
'UPDATE class_assignments SET active = 0, ends_at = NOW() WHERE id = ? AND teacher_id = ?',
|
|
[$assignmentId, $teacherId]
|
|
);
|
|
Response::ok();
|
|
}
|
|
|
|
Response::error('Unbekannte Aktion');
|
|
}
|
|
|
|
Response::error('Methode nicht erlaubt', 405);
|