backend-lehrer (10 files): - game/database.py (785 → 5), correction_api.py (683 → 4) - classroom_engine/antizipation.py (676 → 5) - llm_gateway schools/edu_search already done in prior batch klausur-service (12 files): - orientation_crop_api.py (694 → 5), pdf_export.py (677 → 4) - zeugnis_crawler.py (676 → 5), grid_editor_api.py (671 → 5) - eh_templates.py (658 → 5), mail/api.py (651 → 5) - qdrant_service.py (638 → 5), training_api.py (625 → 4) website (6 pages): - middleware (696 → 8), mail (733 → 6), consent (628 → 8) - compliance/risks (622 → 5), export (502 → 5), brandbook (629 → 7) studio-v2 (3 components): - B2BMigrationWizard (848 → 3), CleanupPanel (765 → 2) - dashboard-experimental (739 → 2) admin-lehrer (4 files): - uebersetzungen (769 → 4), manager (670 → 2) - ChunkBrowserQA (675 → 6), dsfa/page (674 → 5) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
157 lines
6.6 KiB
Python
157 lines
6.6 KiB
Python
# ==============================================
|
|
# Breakpilot Drive - Learning State DB Methods
|
|
# ==============================================
|
|
# Methods for reading/writing student learning state.
|
|
|
|
import logging
|
|
from typing import Optional
|
|
|
|
from .database_models import StudentLearningState
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class LearningStateMixin:
|
|
"""Mixin providing learning state database methods for GameDatabase."""
|
|
|
|
async def get_learning_state(self, student_id: str) -> Optional[StudentLearningState]:
|
|
"""Get learning state for a student."""
|
|
await self._ensure_connected()
|
|
if not self._pool:
|
|
return None
|
|
|
|
try:
|
|
async with self._pool.acquire() as conn:
|
|
row = await conn.fetchrow(
|
|
"""
|
|
SELECT id, student_id, overall_level, math_level, german_level,
|
|
english_level, total_play_time_minutes, total_sessions,
|
|
questions_answered, questions_correct, created_at, updated_at
|
|
FROM student_learning_state
|
|
WHERE student_id = $1
|
|
""",
|
|
student_id
|
|
)
|
|
|
|
if row:
|
|
return StudentLearningState(
|
|
id=str(row["id"]),
|
|
student_id=str(row["student_id"]),
|
|
overall_level=row["overall_level"],
|
|
math_level=float(row["math_level"]),
|
|
german_level=float(row["german_level"]),
|
|
english_level=float(row["english_level"]),
|
|
total_play_time_minutes=row["total_play_time_minutes"],
|
|
total_sessions=row["total_sessions"],
|
|
questions_answered=row["questions_answered"] or 0,
|
|
questions_correct=row["questions_correct"] or 0,
|
|
created_at=row["created_at"],
|
|
updated_at=row["updated_at"],
|
|
)
|
|
except Exception as e:
|
|
logger.error(f"Failed to get learning state: {e}")
|
|
|
|
return None
|
|
|
|
async def create_or_update_learning_state(
|
|
self,
|
|
student_id: str,
|
|
overall_level: int = 3,
|
|
math_level: float = 3.0,
|
|
german_level: float = 3.0,
|
|
english_level: float = 3.0,
|
|
) -> Optional[StudentLearningState]:
|
|
"""Create or update learning state for a student."""
|
|
await self._ensure_connected()
|
|
if not self._pool:
|
|
return None
|
|
|
|
try:
|
|
async with self._pool.acquire() as conn:
|
|
row = await conn.fetchrow(
|
|
"""
|
|
INSERT INTO student_learning_state (
|
|
student_id, overall_level, math_level, german_level, english_level
|
|
) VALUES ($1, $2, $3, $4, $5)
|
|
ON CONFLICT (student_id) DO UPDATE SET
|
|
overall_level = EXCLUDED.overall_level,
|
|
math_level = EXCLUDED.math_level,
|
|
german_level = EXCLUDED.german_level,
|
|
english_level = EXCLUDED.english_level,
|
|
updated_at = NOW()
|
|
RETURNING id, student_id, overall_level, math_level, german_level,
|
|
english_level, total_play_time_minutes, total_sessions,
|
|
questions_answered, questions_correct, created_at, updated_at
|
|
""",
|
|
student_id, overall_level, math_level, german_level, english_level
|
|
)
|
|
|
|
if row:
|
|
return StudentLearningState(
|
|
id=str(row["id"]),
|
|
student_id=str(row["student_id"]),
|
|
overall_level=row["overall_level"],
|
|
math_level=float(row["math_level"]),
|
|
german_level=float(row["german_level"]),
|
|
english_level=float(row["english_level"]),
|
|
total_play_time_minutes=row["total_play_time_minutes"],
|
|
total_sessions=row["total_sessions"],
|
|
questions_answered=row["questions_answered"] or 0,
|
|
questions_correct=row["questions_correct"] or 0,
|
|
created_at=row["created_at"],
|
|
updated_at=row["updated_at"],
|
|
)
|
|
except Exception as e:
|
|
logger.error(f"Failed to create/update learning state: {e}")
|
|
|
|
return None
|
|
|
|
async def update_learning_stats(
|
|
self,
|
|
student_id: str,
|
|
duration_minutes: int,
|
|
questions_answered: int,
|
|
questions_correct: int,
|
|
new_level: Optional[int] = None,
|
|
) -> bool:
|
|
"""Update learning stats after a game session."""
|
|
await self._ensure_connected()
|
|
if not self._pool:
|
|
return False
|
|
|
|
try:
|
|
async with self._pool.acquire() as conn:
|
|
if new_level is not None:
|
|
await conn.execute(
|
|
"""
|
|
UPDATE student_learning_state SET
|
|
total_play_time_minutes = total_play_time_minutes + $2,
|
|
total_sessions = total_sessions + 1,
|
|
questions_answered = COALESCE(questions_answered, 0) + $3,
|
|
questions_correct = COALESCE(questions_correct, 0) + $4,
|
|
overall_level = $5,
|
|
updated_at = NOW()
|
|
WHERE student_id = $1
|
|
""",
|
|
student_id, duration_minutes, questions_answered,
|
|
questions_correct, new_level
|
|
)
|
|
else:
|
|
await conn.execute(
|
|
"""
|
|
UPDATE student_learning_state SET
|
|
total_play_time_minutes = total_play_time_minutes + $2,
|
|
total_sessions = total_sessions + 1,
|
|
questions_answered = COALESCE(questions_answered, 0) + $3,
|
|
questions_correct = COALESCE(questions_correct, 0) + $4,
|
|
updated_at = NOW()
|
|
WHERE student_id = $1
|
|
""",
|
|
student_id, duration_minutes, questions_answered, questions_correct
|
|
)
|
|
return True
|
|
except Exception as e:
|
|
logger.error(f"Failed to update learning stats: {e}")
|
|
|
|
return False
|