package store import ( "context" "encoding/json" "errors" "fmt" "speech-learning-api/internal/domain" ) // Packs contain curriculum metadata and reviewed reserve content, not grammar // code. Another language is enabled only after a complete pack is installed. func EnglishCoursePack() domain.CoursePack { titles := []string{"Begrüßung & Vorstellung", "Familie & Personen", "Zahlen, Uhrzeit & Datum", "Alltag & Hobbys", "Essen & Einkaufen", "Zuhause & Stadt", "Wiederholung & A1-Test"} keys := []string{"introductions", "family", "numbers_time", "daily_life", "food_shopping", "home_city", "review_test"} goals := [][]string{ {"Greet people, introduce a name and origin; I am, my name is"}, {"Family members and people; personal pronouns and have got"}, {"Numbers, weekdays, dates and telling the time"}, {"Daily routines and hobbies; Simple Present"}, {"Order food, ask prices and quantities; polite requests"}, {"Rooms and places, directions and prepositions of place"}, {"Review all six modules and assess all six skills at A1"}, } // Each row: vocabulary/meaning, grammar/correct/distractors, reading/text // answer/options, listening/text answer/options, writing, speaking reference. type content struct{ word, meaning, grammar, correct, g1, g2, passage, readPrompt, readAnswer, r1, r2, audio, listenPrompt, listenAnswer, l1, l2, write, speak string } rows := []content{ {"name", "Name", "I ___ from Berlin.", "am", "is", "are", "Hello! My name is Mia. I am from Berlin.", "Wie heißt die Person?", "Mia", "Emma", "Ben", "My name is Anna. I am from London.", "Woher kommt Anna?", "London", "Berlin", "Paris", "Introduce yourself in two short sentences. You may invent a name and a city.", "Hello, my name is Anna."}, {"sister", "Schwester", "She ___ got a brother.", "has", "have", "am", "This is my sister Emma. She is ten years old.", "Wie heißt die Schwester?", "Emma", "Mia", "Anna", "My brother is ten years old.", "Wie alt ist der Bruder?", "ten", "two", "five", "Write two short sentences about a family. You may invent the details.", "I have got one brother."}, {"Monday", "Montag", "It ___ three o'clock.", "is", "are", "am", "The class is on Monday at nine o'clock.", "Wann beginnt der Unterricht?", "nine o'clock", "three o'clock", "six o'clock", "The meeting is on Friday at five o'clock.", "An welchem Tag ist das Treffen?", "Friday", "Monday", "Sunday", "Write a weekday and a time for a meeting in two short English sentences.", "It is three o'clock."}, {"read", "lesen", "She ___ tennis every day.", "plays", "play", "playing", "Tom gets up at seven. He plays football after school.", "Was macht Tom nach der Schule?", "plays football", "goes shopping", "cooks dinner", "I get up at eight and read a book in the evening.", "Was macht die Person am Abend?", "reads a book", "plays tennis", "goes shopping", "Write two short sentences about a daily routine and a hobby.", "I read a book every day."}, {"apple", "Apfel", "I would like ___ apple.", "an", "a", "two", "A sandwich costs three pounds. A bottle of water costs one pound.", "Wie viel kostet das Sandwich?", "three pounds", "one pound", "five pounds", "I would like two apples and a bottle of water, please.", "Wie viele Äpfel bestellt die Person?", "two", "one", "three", "Write a polite order for food and a drink in two short sentences.", "I would like some water, please."}, {"kitchen", "Küche", "The bank is ___ the station. (neben)", "next to", "under", "inside", "The library is next to the bank. The park is opposite the station.", "Was liegt neben der Bank?", "the library", "the park", "the station", "Turn left. The supermarket is next to the bank.", "In welche Richtung soll man abbiegen?", "left", "right", "straight on", "Describe two places in a home or a town using short English sentences.", "The park is next to the station."}, {"family", "Familie", "My name ___ Sam.", "is", "am", "are", "Sam lives in London. He has got a sister and likes football.", "Welches Hobby hat Sam?", "football", "tennis", "reading", "The shop opens on Monday at nine o'clock.", "An welchem Tag öffnet das Geschäft?", "Monday", "Friday", "Sunday", "Introduce a person and describe one daily activity in two short English sentences.", "My name is Sam. I am from London."}, } pack := domain.CoursePack{TargetLanguage: "en-US", CourseVersion: domain.CourseVersion} for i, c := range rows { id := "a1_" + keys[i] + "_v1" if i == 0 { id = "a1_introductions_v1" } pack.Modules = append(pack.Modules, domain.AdaptiveModule{ID: id, Key: keys[i], Title: titles[i], Description: goals[i][0], TargetLanguage: pack.TargetLanguage, CourseVersion: pack.CourseVersion, Goals: goals[i], Order: i + 1}) base := domain.AdaptiveTask{SchemaVersion: 2, ModuleID: id, OriginModuleKey: keys[i], TargetLanguage: pack.TargetLanguage, CourseVersion: pack.CourseVersion, CEFRLevel: "A1", Source: "seed"} choice := func(skill, prompt, passage, audio string, labels []string) { t := base t.ID = fmt.Sprintf("a2_reserve_en_%d_%s", i+1, skill) t.Skill = skill t.Kind = "choice" t.Prompt = prompt t.Passage = passage t.AudioText = audio for n, label := range labels { t.Options = append(t.Options, domain.TaskOption{ID: fmt.Sprintf("o%d", n+1), Label: label}) } t.AnswerKey = &domain.TaskKey{CorrectOptionID: "o1", ReferenceAnswer: labels[0], Rubric: []string{"Choose the option supported by the task."}, Explanation: "Die passende Antwort ist: " + labels[0] + "."} pack.Reserves = append(pack.Reserves, t) } choice("vocabulary", fmt.Sprintf("Was bedeutet „%s“?", c.word), "", "", []string{c.meaning, "Fenster", "Straße"}) choice("grammar", "Ergänze: "+c.grammar, "", "", []string{c.correct, c.g1, c.g2}) choice("reading", c.readPrompt, c.passage, "", []string{c.readAnswer, c.r1, c.r2}) choice("listening", c.listenPrompt, "", c.audio, []string{c.listenAnswer, c.l1, c.l2}) t := base t.ID = fmt.Sprintf("a2_reserve_en_%d_writing", i+1) t.Skill = "writing" t.Kind = "free_text" t.Prompt = c.write t.AnswerKey = &domain.TaskKey{ReferenceAnswer: c.speak, Rubric: []string{"Respond to the requested topic with understandable A1 English.", "Accept invented personal details and valid alternative wording.", "Give partial credit for understandable answers with minor language errors."}, Explanation: "Kurze, verständliche Sätze zum Thema sind ausreichend."} pack.Reserves = append(pack.Reserves, t) t = base t.ID = fmt.Sprintf("a2_reserve_en_%d_speaking", i+1) t.Skill = "speaking" t.Kind = "repeat_sentence" t.Prompt = "Sprich den englischen Satz nach." t.ReferenceText = c.speak if i%2 == 1 { t.Kind = "free_speech" t.Prompt = c.write + " Speak for no more than 30 seconds." t.ReferenceText = "" } t.AnswerKey = &domain.TaskKey{ReferenceAnswer: c.speak, Rubric: []string{"Use the actual Azure measurements when supplied; do not invent pronunciation errors.", "For free speech assess the response to the prompt, not exact wording of the example."}, Explanation: "Sprich kurze, verständliche Sätze."} pack.Reserves = append(pack.Reserves, t) } return pack } func (s *AdaptiveStore) InstallPack(ctx context.Context, p domain.CoursePack) error { if p.TargetLanguage == "" || p.CourseVersion != domain.CourseVersion || len(p.Modules) != 7 { return errors.New("a course pack needs a language, version and seven modules") } modules := map[string]bool{} ids := map[string]bool{} keys := map[string]bool{} orders := map[int]bool{} coverage := map[string]bool{} for _, m := range p.Modules { if m.ID == "" || m.Key == "" || m.TargetLanguage != p.TargetLanguage || m.CourseVersion != p.CourseVersion || modules[m.ID] || keys[m.Key] || orders[m.Order] || m.Order < 1 || m.Order > 7 { return errors.New("invalid course module") } modules[m.ID] = true keys[m.Key] = true orders[m.Order] = true } for _, t := range p.Reserves { if t.ID == "" || ids[t.ID] || t.CourseVersion != p.CourseVersion || !modules[t.ModuleID] || t.TargetLanguage != p.TargetLanguage || t.AnswerKey == nil || t.Source != "seed" { return errors.New("invalid reserve task") } ids[t.ID] = true coverage[t.ModuleID+":"+t.Skill] = true } for id := range modules { for _, skill := range domain.CourseSkills { if !coverage[id+":"+skill] { return errors.New("missing reviewed reserve for module and skill") } } } tx, err := s.db.BeginTx(ctx, nil) if err != nil { return err } defer tx.Rollback() for _, m := range p.Modules { data, err := json.Marshal(m) if err != nil { return err } _, err = tx.ExecContext(ctx, `INSERT INTO course_v2_modules(id,language,version,module_key,sort_order,data) VALUES($1,$2,$3,$4,$5,$6) ON CONFLICT(id,language,version) DO UPDATE SET data=EXCLUDED.data`, m.ID, p.TargetLanguage, p.CourseVersion, m.Key, m.Order, data) if err != nil { return err } } for _, t := range p.Reserves { var collision bool if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM course_v2_tasks WHERE id=$1 AND (language<>$2 OR version<>$3 OR module_id<>$4 OR source<>'seed' OR owner_id IS NOT NULL))`, t.ID, p.TargetLanguage, p.CourseVersion, t.ModuleID).Scan(&collision); err != nil { return err } if collision { return errors.New("reserve ID belongs to a different course context") } data, err := json.Marshal(t) if err != nil { return err } _, err = tx.ExecContext(ctx, `INSERT INTO course_v2_tasks(id,module_id,language,version,source,skill,kind,data,fingerprint) VALUES($1,$2,$3,$4,'seed',$5,$6,$7,$1) ON CONFLICT(id) DO NOTHING`, t.ID, t.ModuleID, p.TargetLanguage, p.CourseVersion, t.Skill, t.Kind, data) if err != nil { return err } } return tx.Commit() }