-- Realtime leerlingactiviteit: een sessie start pas bij echte interactie met -- een toegewezen widget. Heartbeats houden de sessie live; afgesloten en -- weggevallen sessies blijven kort terug te zien in het klasdashboard. CREATE TABLE IF NOT EXISTS live_learning_sessions ( id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, pupil_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, teacher_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, class_id BIGINT NOT NULL REFERENCES classes(id) ON DELETE CASCADE, assignment_id BIGINT REFERENCES assignments(id) ON DELETE SET NULL, board_id TEXT NOT NULL, widget_id TEXT NOT NULL, widget_type TEXT NOT NULL, mode TEXT NOT NULL DEFAULT 'werken' CHECK (mode IN ('werken', 'kijken')), started_at TIMESTAMPTZ NOT NULL DEFAULT now(), last_seen_at TIMESTAMPTZ NOT NULL DEFAULT now(), ended_at TIMESTAMPTZ, progress_count INTEGER NOT NULL DEFAULT 0, attempts INTEGER NOT NULL DEFAULT 0, correct INTEGER NOT NULL DEFAULT 0 ); CREATE INDEX IF NOT EXISTS idx_live_learning_sessions_class_recent ON live_learning_sessions (class_id, started_at DESC); CREATE INDEX IF NOT EXISTS idx_live_learning_sessions_pupil_open ON live_learning_sessions (pupil_id, ended_at, last_seen_at DESC);