# Tài liệu Mối quan hệ các bảng LCMS (Migration System)

Tài liệu này mô tả cấu trúc và mối quan hệ giữa các bảng có tiền tố `lcms_` (hệ thống lưu trữ mới) và các bảng dữ liệu gốc tương ứng.

---

## 1. Danh sách ánh xạ các bảng

### 1. Bảng Loại khóa học
**Table `lcms_course_types`** (Dữ liệu gốc: `course_type`)
| Cột (lcms_) | Cột (Gốc) | Ghi chú |
| :--- | :--- | :--- |
| `coutyp_id` | `id` | PK (bigint unsigned) |
| `coutyp_name` | `course_type_name` | |
| `coutyp_is_sync` | - | Trạng thái đồng bộ |
| `coutyp_created_at` | `created_at` | |
| `coutyp_updated_at` | `updated_at` | |

### 2. Bảng Khóa học & Học liệu
**Table `lcms_courses`** (Dữ liệu gốc: `api_moodle`)
| Cột (lcms_) | Cột (Gốc) | Ghi chú |
| :--- | :--- | :--- |
| `cou_id` | `id` | PK (bigint unsigned) |
| `cou_moodle_id` | `moodle_id` | Moodle ID gốc — dùng để join với zeus_session_completions |
| `cou_name` | `moodle_name` | |
| `cou_type` | `moodle_type` | 'course', 'section', 'quiz' |
| `cou_parent_id` | `parent_id` | Quan hệ đa cấp (Nối với `cou_id`) |
| `cou_total_round` | `total_rounds` | |
| `cou_last_round` | `last_round` | |
| `cou_section_type` | `section_type` | |
| `cou_quiz_setting_type` | `quiz_settings_type` | |
| `cou_type_id` | `course_type_id` | Nối với `lcms_course_types.coutyp_id` |
| `cou_is_sync` | - | Trạng thái đồng bộ |
| `cou_created_at` | `created_at` | |
| `cou_updated_at` | `updated_at` | |

### 3. Bảng Giao bài tập (Completion)
**Table `lcms_user_assignments`** (Dữ liệu gốc: `zeus_session_completions`)
| Cột (lcms_) | Cột (Gốc) | Ghi chú |
| :--- | :--- | :--- |
| `usrasi_id` | `id` | PK (bigint unsigned) |
| `usrasi_course_id` | `course_id` | Nối với `lcms_courses.cou_id` (Course) |
| `usrasi_section_id` | `section_id` | Nối với `lcms_courses.cou_id` (Section) |
| `usrasi_zeus_id` | `zeus_id` | |
| `usrasi_student_id` | `student_id` | Nối với `lcms_students.stu_id` |
| `usrasi_completion_time`| `completion_time`| |
| `usrasi_completion_state`| `completion_state`| |
| `usrasi_payload_data` | `payload_data` | |
| `usrasi_response_data` | `response_data` | |
| `usrasi_is_cron` | `is_cron` | |
| `usrasi_is_sync` | - | |
| `usrasi_created_at` | `created_at` | |
| `usrasi_updated_at` | `updated_at` | |

### 4. Bảng Điểm số học sinh
**Table `lcms_student_scores`** (Dữ liệu gốc: `student_score`)
| Cột (lcms_) | Cột (Gốc) | Ghi chú |
| :--- | :--- | :--- |
| `stusco_id` | `id` | PK (bigint unsigned) |
| `stusco_student_id` | `student_id` | Nối với `lcms_students.stu_id` |
| `stusco_course_id` | `quiz_id` | Nối với `lcms_courses.cou_id` (Quiz) |
| `stusco_mock_contest_id`| `idMockContest` | |
| `stusco_score_status` | `score_status` | |
| `stusco_history_contest_id`| `idHistoryContest`| |
| `stusco_overall_score` | `overrall_score` | |
| `stusco_is_sync` | - | |
| `stusco_created_at` | `created_at` | |
| `stusco_updated_at` | `updated_at` | |

### 5. Bảng Trung gian Đăng ký học (Enrollment)
**Table `lcms_course_student`** (Dữ liệu gốc: `course_student`)
| Cột (lcms_) | Cột (Gốc) | Ghi chú |
| :--- | :--- | :--- |
| `coustu_id` | `id` | PK |
| `coustu_student_id` | `student_id` | Nối với `lcms_students.stu_id` |
| `coustu_course_id` | `course_id` | Nối với `lcms_courses.cou_id` |
| `coustu_course_end` | `course_end` | |
| `coustu_is_sync` | - | |
| `coustu_created_at` | `created_at` | |
| `coustu_updated_at` | `updated_at` | |

### 6. Bảng Thông tin học sinh
**Table `lcms_students`** (Dữ liệu gốc: `students`)
| Cột (lcms_) | Cột (Gốc) | Ghi chú |
| :--- | :--- | :--- |
| `stu_id` | `id` | PK (bigint unsigned) |
| `stu_name` | `name` | |
| `stu_email` | `email` | |
| `stu_username` | `username` | |
| `stu_user_id` | `zeus_id` | Link Zeus ID |
| `stu_phone` | `phone` | |
| `stu_dob` | `dob` | |
| `stu_gender` | `gender` | |
| `stu_is_sync` | - | |
| `stu_created_at` | `created_at` | |
| `stu_updated_at` | `updated_at` | |

### 7. Bảng Điểm số Quiz Speakup (Mới)
**Table `lcms_user_quiz_grades`** (Dữ liệu gốc: `user_quiz_grade`)
| Cột (lcms_) | Cột (Gốc) | Ghi chú |
| :--- | :--- | :--- |
| `usrqgr_id` | `id` | PK |
| `usrqgr_user_moodle_id`| `user_moodle_id` | |
| `usrqgr_user_id` | `user_id` | **Nối với `lcms_students.stu_id`** (Đã mapping từ `user_id` sang student id) |
| `usrqgr_username` | `username` | |
| `usrqgr_email` | `email` | |
| `usrqgr_score` | `score` | |
| `usrqgr_spk_course_id` | `spk_course_id` | |
| `usrqgr_spk_quiz_id` | `spk_quiz_id` | |
| `usrqgr_spk_cmid` | `spk_cmid` | |
| `usrqgr_quiz_id` | `quiz_id` | Ánh xạ sang id gốc của quiz |
| `usrqgr_quiz_name` | `quiz_name` | |
| `usrqgr_status` | `status` | Trạng thái xử lý nội bộ |
| `usrqgr_is_sync` | - | Trạng thái đồng bộ |
| `usrqgr_created_at` | `created_at` | |
| `usrqgr_updated_at` | `updated_at` | |

---

## 2. Định nghĩa mối quan hệ (Diagram Reference)

### Mối quan hệ giữa các bảng LCMS:
```mermaid
erDiagram
    lcms_course_types ||--o{ lcms_courses : "coutyp_id = cou_type_id"
    lcms_courses ||--o{ lcms_courses : "cou_id = cou_parent_id (Child)"
    lcms_students ||--o{ lcms_user_assignments : "stu_id = usrasi_student_id"
    lcms_courses ||--o{ lcms_user_assignments : "cou_id = usrasi_course_id / usrasi_section_id"
    lcms_students ||--o{ lcms_student_scores : "stu_id = stusco_student_id"
    lcms_courses ||--o{ lcms_student_scores : "cou_id = stusco_course_id"
    lcms_students ||--o{ lcms_course_student : "stu_id = coustu_student_id"
    lcms_courses ||--o{ lcms_course_student : "cou_id = coustu_course_id"
    lcms_students ||--o{ lcms_user_quiz_grades : "stu_id = usrqgr_user_id"
    lcms_courses ||--o{ lcms_user_quiz_grades : "cou_id = usrqgr_quiz_id"
```

### Mối quan hệ gốc (Gợi ý truy vấn):
- **api_moodle.course_type_id** > **course_type.id**
- **zeus_session_completions.student_id** > **students.id**
- **zeus_session_completions.course_id** > **api_moodle.moodle_id** (với moodle_type = 'course')
- **zeus_session_completions.section_id** > **api_moodle.moodle_id** (với moodle_type = 'section')
- **student_score.student_id** > **students.id**
- **student_score.quiz_id** > **api_moodle.moodle_id** (với moodle_type = 'quiz')
- **user_quiz_grade.user_id** (mapping) > **students.user_id** -> **students.id**
