259 lines
7.3 KiB
Go
259 lines
7.3 KiB
Go
package lmsprogress
|
|
|
|
import (
|
|
"context"
|
|
|
|
dbgen "Rezina-Backend/gen/db"
|
|
"Rezina-Backend/internal/domain"
|
|
)
|
|
|
|
// AdminUserExamPrepLearningActivityTree returns nested catalog course → unit → module → lesson → practice completions.
|
|
func (s *Service) AdminUserExamPrepLearningActivityTree(ctx context.Context, userID int64) (domain.AdminExamPrepUserLearningActivityTree, error) {
|
|
rows, err := s.store.ListUserExamPrepFlatLearningActivity(ctx, userID)
|
|
if err != nil {
|
|
return domain.AdminExamPrepUserLearningActivityTree{}, err
|
|
}
|
|
return buildAdminExamPrepLearningActivityTree(userID, rows), nil
|
|
}
|
|
|
|
type epLessonAccum struct {
|
|
id int64
|
|
title string
|
|
sortOrder int32
|
|
practices []domain.AdminExamPrepPracticeLearningEntry
|
|
seen map[int64]struct{}
|
|
}
|
|
|
|
type epModuleAccum struct {
|
|
id int64
|
|
name string
|
|
sortOrder int32
|
|
lessons map[int64]*epLessonAccum
|
|
order []int64
|
|
}
|
|
|
|
type epUnitAccum struct {
|
|
id int64
|
|
name string
|
|
sortOrder int32
|
|
practices []domain.AdminExamPrepPracticeLearningEntry
|
|
seen map[int64]struct{}
|
|
modules map[int64]*epModuleAccum
|
|
order []int64
|
|
}
|
|
|
|
type epCatalogAccum struct {
|
|
id int64
|
|
name string
|
|
sortOrder int32
|
|
practices []domain.AdminExamPrepPracticeLearningEntry
|
|
seen map[int64]struct{}
|
|
units map[int64]*epUnitAccum
|
|
order []int64
|
|
}
|
|
|
|
type examPrepActivityTreeBuilder struct {
|
|
catalogs map[int64]*epCatalogAccum
|
|
order []int64
|
|
}
|
|
|
|
func newExamPrepActivityTreeBuilder() *examPrepActivityTreeBuilder {
|
|
return &examPrepActivityTreeBuilder{catalogs: make(map[int64]*epCatalogAccum)}
|
|
}
|
|
|
|
func (b *examPrepActivityTreeBuilder) ensureCatalog(row dbgen.ListUserExamPrepFlatLearningActivityByUserRow) *epCatalogAccum {
|
|
if c, ok := b.catalogs[row.CatalogCourseID]; ok {
|
|
return c
|
|
}
|
|
c := &epCatalogAccum{
|
|
id: row.CatalogCourseID, name: row.CatalogCourseName, sortOrder: row.CatalogCourseSortOrder,
|
|
seen: make(map[int64]struct{}), units: make(map[int64]*epUnitAccum),
|
|
}
|
|
b.catalogs[row.CatalogCourseID] = c
|
|
b.order = append(b.order, row.CatalogCourseID)
|
|
return c
|
|
}
|
|
|
|
func appendExamPrepPractice(seen map[int64]struct{}, practices *[]domain.AdminExamPrepPracticeLearningEntry, row dbgen.ListUserExamPrepFlatLearningActivityByUserRow) {
|
|
if seen == nil {
|
|
return
|
|
}
|
|
if _, ok := seen[row.ExamPrepPracticeID]; ok {
|
|
return
|
|
}
|
|
seen[row.ExamPrepPracticeID] = struct{}{}
|
|
*practices = append(*practices, domain.AdminExamPrepPracticeLearningEntry{
|
|
ExamPrepPracticeID: row.ExamPrepPracticeID,
|
|
Title: row.PracticeTitle,
|
|
CompletedAt: row.ActivityAt.Time,
|
|
})
|
|
}
|
|
|
|
func examPrepActivityUnitID(row dbgen.ListUserExamPrepFlatLearningActivityByUserRow) (int64, bool) {
|
|
return pgInterfaceInt64(row.UnitID)
|
|
}
|
|
|
|
func examPrepActivityUnitName(row dbgen.ListUserExamPrepFlatLearningActivityByUserRow) string {
|
|
return pgInterfaceString(row.UnitName)
|
|
}
|
|
|
|
func examPrepActivityUnitSortOrder(row dbgen.ListUserExamPrepFlatLearningActivityByUserRow) int32 {
|
|
v, ok := pgInterfaceInt64(row.UnitSortOrder)
|
|
if !ok {
|
|
return 0
|
|
}
|
|
return int32(v)
|
|
}
|
|
|
|
func pgInterfaceInt64(v interface{}) (int64, bool) {
|
|
if v == nil {
|
|
return 0, false
|
|
}
|
|
switch n := v.(type) {
|
|
case int64:
|
|
return n, true
|
|
case int32:
|
|
return int64(n), true
|
|
case int:
|
|
return int64(n), true
|
|
case float64:
|
|
return int64(n), true
|
|
default:
|
|
return 0, false
|
|
}
|
|
}
|
|
|
|
func pgInterfaceString(v interface{}) string {
|
|
if v == nil {
|
|
return ""
|
|
}
|
|
if s, ok := v.(string); ok {
|
|
return s
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (ca *epCatalogAccum) ensureUnit(row dbgen.ListUserExamPrepFlatLearningActivityByUserRow) *epUnitAccum {
|
|
unitID, ok := examPrepActivityUnitID(row)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
if u, exists := ca.units[unitID]; exists {
|
|
return u
|
|
}
|
|
u := &epUnitAccum{
|
|
id: unitID, name: examPrepActivityUnitName(row), sortOrder: examPrepActivityUnitSortOrder(row),
|
|
seen: make(map[int64]struct{}), modules: make(map[int64]*epModuleAccum),
|
|
}
|
|
ca.units[unitID] = u
|
|
ca.order = append(ca.order, unitID)
|
|
return u
|
|
}
|
|
|
|
func (ua *epUnitAccum) ensureModule(row dbgen.ListUserExamPrepFlatLearningActivityByUserRow) *epModuleAccum {
|
|
if !row.ModuleID.Valid {
|
|
return nil
|
|
}
|
|
moduleID := row.ModuleID.Int64
|
|
if m, ok := ua.modules[moduleID]; ok {
|
|
return m
|
|
}
|
|
m := &epModuleAccum{
|
|
id: moduleID, name: row.ModuleName.String, sortOrder: row.ModuleSortOrder.Int32,
|
|
lessons: make(map[int64]*epLessonAccum),
|
|
}
|
|
ua.modules[moduleID] = m
|
|
ua.order = append(ua.order, moduleID)
|
|
return m
|
|
}
|
|
|
|
func (ma *epModuleAccum) ensureLesson(row dbgen.ListUserExamPrepFlatLearningActivityByUserRow) *epLessonAccum {
|
|
if !row.LessonID.Valid {
|
|
return nil
|
|
}
|
|
lessonID := row.LessonID.Int64
|
|
if l, ok := ma.lessons[lessonID]; ok {
|
|
return l
|
|
}
|
|
l := &epLessonAccum{
|
|
id: lessonID, title: row.LessonTitle.String, sortOrder: row.LessonSortOrder.Int32,
|
|
seen: make(map[int64]struct{}),
|
|
}
|
|
ma.lessons[lessonID] = l
|
|
ma.order = append(ma.order, lessonID)
|
|
return l
|
|
}
|
|
|
|
func (b *examPrepActivityTreeBuilder) ingest(row dbgen.ListUserExamPrepFlatLearningActivityByUserRow) {
|
|
c := b.ensureCatalog(row)
|
|
if _, hasUnit := examPrepActivityUnitID(row); !hasUnit {
|
|
appendExamPrepPractice(c.seen, &c.practices, row)
|
|
return
|
|
}
|
|
u := c.ensureUnit(row)
|
|
if u == nil {
|
|
return
|
|
}
|
|
if !row.ModuleID.Valid {
|
|
appendExamPrepPractice(u.seen, &u.practices, row)
|
|
return
|
|
}
|
|
m := u.ensureModule(row)
|
|
if m == nil {
|
|
return
|
|
}
|
|
if !row.LessonID.Valid {
|
|
return
|
|
}
|
|
l := m.ensureLesson(row)
|
|
if l == nil {
|
|
return
|
|
}
|
|
appendExamPrepPractice(l.seen, &l.practices, row)
|
|
}
|
|
|
|
func buildAdminExamPrepLearningActivityTree(userID int64, rows []dbgen.ListUserExamPrepFlatLearningActivityByUserRow) domain.AdminExamPrepUserLearningActivityTree {
|
|
b := newExamPrepActivityTreeBuilder()
|
|
for _, row := range rows {
|
|
b.ingest(row)
|
|
}
|
|
|
|
catalogs := make([]domain.AdminExamPrepCatalogCourseLearningEntry, 0, len(b.order))
|
|
for _, catalogID := range b.order {
|
|
ca := b.catalogs[catalogID]
|
|
units := make([]domain.AdminExamPrepUnitLearningEntry, 0, len(ca.order))
|
|
for _, unitID := range ca.order {
|
|
ua := ca.units[unitID]
|
|
modules := make([]domain.AdminExamPrepModuleLearningEntry, 0, len(ua.order))
|
|
for _, moduleID := range ua.order {
|
|
ma := ua.modules[moduleID]
|
|
lessons := make([]domain.AdminExamPrepLessonLearningEntry, 0, len(ma.order))
|
|
for _, lessonID := range ma.order {
|
|
la := ma.lessons[lessonID]
|
|
lessons = append(lessons, domain.AdminExamPrepLessonLearningEntry{
|
|
ID: la.id, Title: la.title, SortOrder: la.sortOrder, Practices: la.practices,
|
|
})
|
|
}
|
|
modules = append(modules, domain.AdminExamPrepModuleLearningEntry{
|
|
ID: ma.id, Name: ma.name, SortOrder: ma.sortOrder, Lessons: lessons,
|
|
})
|
|
}
|
|
chapterLessons := make([]domain.AdminExamPrepLessonLearningEntry, 0)
|
|
for _, module := range modules {
|
|
chapterLessons = append(chapterLessons, module.Lessons...)
|
|
}
|
|
units = append(units, domain.AdminExamPrepUnitLearningEntry{
|
|
ID: ua.id, Name: ua.name, SortOrder: ua.sortOrder, Practices: ua.practices, Modules: modules, Lessons: chapterLessons,
|
|
})
|
|
}
|
|
catalogs = append(catalogs, domain.AdminExamPrepCatalogCourseLearningEntry{
|
|
ID: ca.id, Name: ca.name, SortOrder: ca.sortOrder, Practices: ca.practices, Units: units,
|
|
})
|
|
}
|
|
|
|
if catalogs == nil {
|
|
catalogs = []domain.AdminExamPrepCatalogCourseLearningEntry{}
|
|
}
|
|
return domain.AdminExamPrepUserLearningActivityTree{UserID: userID, CatalogCourses: catalogs}
|
|
}
|