From de8fbdfed8ea833241afd42b9dbc0c85fa0a394b Mon Sep 17 00:00:00 2001 From: whf <2399365302@qq.com> Date: Wed, 6 May 2026 02:29:14 +0800 Subject: [PATCH] =?UTF-8?q?Block=205:=20Add=20LearningService=20=E2=80=94?= =?UTF-8?q?=20business=20logic=20layer=20for=20progress,=20modules,=20quiz?= =?UTF-8?q?=20recording,=20mastery=20tracking?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deeptutor/learning/service.py | 66 +++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 deeptutor/learning/service.py diff --git a/deeptutor/learning/service.py b/deeptutor/learning/service.py new file mode 100644 index 000000000..9926d45af --- /dev/null +++ b/deeptutor/learning/service.py @@ -0,0 +1,66 @@ +from __future__ import annotations + +import time +import uuid + +from deeptutor.learning.models import ( + ErrorRecord, + LearningModule, + LearningProgress, + LearningStage, + QuizAttempt, +) +from deeptutor.learning.storage import LearningStore + + +class LearningService: + def __init__(self, store: LearningStore | None = None) -> None: + self._store = store or LearningStore() + + def get_or_create(self, book_id: str) -> LearningProgress: + existing = self._store.load(book_id) + if existing is not None: + return existing + return LearningProgress(book_id=book_id) + + def init_modules( + self, progress: LearningProgress, modules: list[LearningModule] + ) -> None: + progress.modules = modules + for mod in modules: + for kp in mod.knowledge_points: + progress.knowledge_types[kp.id] = kp.type + + def advance_stage( + self, progress: LearningProgress, next_stage: LearningStage + ) -> None: + progress.current_stage = next_stage + progress.updated_at = time.time() + + def record_quiz_attempt( + self, progress: LearningProgress, attempt: QuizAttempt + ) -> None: + if not attempt.is_correct and attempt.error_type is not None: + record = ErrorRecord( + id=uuid.uuid4().hex, + question_id=attempt.question_id, + knowledge_point_id=attempt.knowledge_point_id, + module_id=attempt.module_id, + error_type=attempt.error_type, + self_attribution=attempt.self_attribution, + status="active", + ) + progress.error_records.append(record) + progress.updated_at = time.time() + + def update_mastery( + self, progress: LearningProgress, kp_id: str, level: float + ) -> None: + progress.mastery_levels[kp_id] = level + progress.updated_at = time.time() + + def save(self, progress: LearningProgress) -> None: + self._store.save(progress) + + +__all__ = ["LearningService"]