Mehrsprachige Bedienung und versionierte Aufgabenvarianten einführen
Übersetzungskataloge, getrennte Sprachpräferenzen und einen versionierten Aufgabenpilot ergänzen. Historische Aufgaben und Bewertungen bleiben erhalten; äquivalente Ansichten teilen Nachweise. Lokal mit Race-Tests und PostgreSQL, Vet, Web-Build, Bank-Audits und Browser-Fixtures geprüft. Dieser Stand dient als Ausgangspunkt für die anschließend beauftragte Zusammenführung zur Muttersprache.
This commit is contained in:
@@ -89,7 +89,7 @@ func (s *AdaptiveStore) Task(ctx context.Context, user, language, id string) (do
|
||||
t.BriefID = brief.String
|
||||
t.ExamID = exam.String
|
||||
if t.Source == "curated" && t.ExamID == "" {
|
||||
t.Review = presentations > 1
|
||||
t.Review = t.Review || presentations > 1
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
@@ -141,6 +141,7 @@ func fingerprint(t domain.AdaptiveTask) string {
|
||||
return hex.EncodeToString(h[:])
|
||||
}
|
||||
func (s *AdaptiveStore) SaveTask(ctx context.Context, j domain.CourseJob, t domain.AdaptiveTask) (domain.AdaptiveTask, error) {
|
||||
t.InstructionLanguage = j.InstructionLanguage
|
||||
t.ID = NewID("a2")
|
||||
if t.Source != "ai" {
|
||||
t.Source = "flowise"
|
||||
@@ -266,11 +267,15 @@ func (s *AdaptiveStore) Snapshot(ctx context.Context, user, language, uiLanguage
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
if err = s.localizeModuleLabels(ctx, language, uiLanguage, out.Modules); err != nil {
|
||||
return out, err
|
||||
}
|
||||
|
||||
err = s.db.QueryRowContext(ctx, `SELECT COALESCE((SELECT facts_version FROM course_v2_versions WHERE user_id=$1 AND language=$2 AND version=$3),0)`, user, language, domain.CourseVersion).Scan(&out.FactsVersion)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT t.module_id,t.skill,count(*),count(DISTINCT t.id),avg((a.evaluation->>'score')::float8),(array_agg(a.id ORDER BY a.created_at DESC,a.id DESC))[1]
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT t.module_id,t.skill,count(*),count(DISTINCT COALESCE(NULLIF(t.data->>'evidence_task_id',''),t.id)),avg((a.evaluation->>'score')::float8),(array_agg(a.id ORDER BY a.created_at DESC,a.id DESC))[1]
|
||||
FROM course_v2_attempts a JOIN course_v2_tasks t ON t.id=a.task_id WHERE a.user_id=$1 AND t.language=$2 AND t.version=$3 AND a.status='graded' AND NOT a.demo AND (a.exam_id IS NULL OR EXISTS(SELECT 1 FROM course_v2_exams e WHERE e.id=a.exam_id AND e.status='completed')) GROUP BY t.module_id,t.skill`, user, language, domain.CourseVersion)
|
||||
if err != nil {
|
||||
return out, err
|
||||
@@ -422,7 +427,7 @@ func (s *AdaptiveStore) Submit(ctx context.Context, user, request string, t doma
|
||||
return domain.AdaptiveAttempt{}, err
|
||||
}
|
||||
if actualID == id {
|
||||
if err = recordPronunciation(ctx, tx, user, "course", actualID, "course:"+t.ID, t.TargetLanguage, t.ExamID, actualTime, *speech); err != nil {
|
||||
if err = recordPronunciation(ctx, tx, user, "course", actualID, "course:"+valueOr(t.EvidenceTaskID, t.ID), t.TargetLanguage, t.ExamID, actualTime, *speech); err != nil {
|
||||
return domain.AdaptiveAttempt{}, err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
func (s *AdaptiveStore) Enqueue(ctx context.Context, user, language, kind, module, skill, subject, request string) (domain.CourseJob, error) {
|
||||
id := NewID("job")
|
||||
_, err := s.db.ExecContext(ctx, `INSERT INTO course_v2_jobs(id,user_id,language,version,kind,module_id,skill,subject_id,request_id) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9) ON CONFLICT(user_id,kind,request_id) DO NOTHING`, id, user, language, domain.CourseVersion, kind, module, skill, subject, request)
|
||||
_, err := s.db.ExecContext(ctx, `INSERT INTO course_v2_jobs(id,user_id,language,version,kind,module_id,skill,subject_id,request_id,instruction_language) SELECT $1,$2,$3,$4,$5,$6,$7,$8,$9,COALESCE(NULLIF(u.instruction_language,''),NULLIF(u.ui_language,''),'de-DE') FROM users u WHERE u.id=$2 ON CONFLICT(user_id,kind,request_id) DO NOTHING`, id, user, language, domain.CourseVersion, kind, module, skill, subject, request)
|
||||
if err != nil {
|
||||
return domain.CourseJob{}, err
|
||||
}
|
||||
@@ -28,7 +28,7 @@ func (s *AdaptiveStore) Enqueue(ctx context.Context, user, language, kind, modul
|
||||
func scanCourseJob(row interface{ Scan(...any) error }) (domain.CourseJob, error) {
|
||||
var j domain.CourseJob
|
||||
var result []byte
|
||||
err := row.Scan(&j.ID, &j.UserID, &j.Language, &j.Kind, &j.ModuleID, &j.Skill, &j.SubjectID, &j.Status, &j.Stage, &j.Error, &j.Lease, &result, &j.AIRevision)
|
||||
err := row.Scan(&j.ID, &j.UserID, &j.Language, &j.Kind, &j.ModuleID, &j.Skill, &j.SubjectID, &j.Status, &j.Stage, &j.Error, &j.Lease, &result, &j.AIRevision, &j.InstructionLanguage)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return j, ErrNotFound
|
||||
}
|
||||
@@ -51,7 +51,7 @@ func scanCourseJob(row interface{ Scan(...any) error }) (domain.CourseJob, error
|
||||
return j, nil
|
||||
}
|
||||
|
||||
const jobColumns = `id,user_id,language,kind,module_id,skill,subject_id,status,stage,error,lease,result,ai_revision`
|
||||
const jobColumns = `id,user_id,language,kind,module_id,skill,subject_id,status,stage,error,lease,result,ai_revision,instruction_language`
|
||||
|
||||
func (s *AdaptiveStore) Job(ctx context.Context, user, language, id string) (domain.CourseJob, error) {
|
||||
return scanCourseJob(s.db.QueryRowContext(ctx, `SELECT `+jobColumns+` FROM course_v2_jobs WHERE id=$1 AND user_id=$2 AND language=$3 AND version=$4`, id, user, language, domain.CourseVersion))
|
||||
|
||||
@@ -57,6 +57,9 @@ CREATE TABLE IF NOT EXISTS course_v2_jobs (
|
||||
deliveries INT NOT NULL DEFAULT 0,created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
UNIQUE(user_id,kind,request_id)
|
||||
);
|
||||
ALTER TABLE course_v2_jobs ADD COLUMN IF NOT EXISTS instruction_language TEXT NOT NULL DEFAULT '';
|
||||
-- Freeze the previous effective preference once for pre-existing queued jobs.
|
||||
UPDATE course_v2_jobs j SET instruction_language=COALESCE(NULLIF(u.instruction_language,''),NULLIF(u.ui_language,''),'de-DE') FROM users u WHERE j.user_id=u.id AND j.instruction_language='';
|
||||
ALTER TABLE course_v2_jobs ADD COLUMN IF NOT EXISTS ai_revision INT NOT NULL DEFAULT 0;
|
||||
CREATE INDEX IF NOT EXISTS course_v2_queue ON course_v2_jobs(status,created_at);
|
||||
UPDATE users u SET active_profile_id=(SELECT p.id FROM learner_profiles p WHERE p.user_id=u.id ORDER BY p.created_at,p.id LIMIT 1) WHERE active_profile_id IS NULL;
|
||||
|
||||
@@ -29,7 +29,6 @@ ALTER TABLE course_v2_tasks ADD COLUMN IF NOT EXISTS bank_entry_id TEXT REFERENC
|
||||
ALTER TABLE course_v2_tasks ADD COLUMN IF NOT EXISTS last_presented_at TIMESTAMPTZ;
|
||||
ALTER TABLE course_v2_tasks ADD COLUMN IF NOT EXISTS presentation_count INT NOT NULL DEFAULT 0;
|
||||
UPDATE course_v2_tasks SET presentation_count=1 WHERE source='curated' AND bank_entry_id IS NOT NULL AND presentation_count=0;
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS course_v2_bank_assignment ON course_v2_tasks(owner_id,bank_entry_id) WHERE exam_id IS NULL AND bank_entry_id IS NOT NULL;
|
||||
ALTER TABLE course_v2_tasks DROP CONSTRAINT IF EXISTS course_v2_tasks_source_check;
|
||||
ALTER TABLE course_v2_tasks ADD CONSTRAINT course_v2_tasks_source_check CHECK(source IN ('seed','flowise','ai','curated'));
|
||||
ALTER TABLE course_v2_attempts DROP CONSTRAINT IF EXISTS course_v2_attempts_status_check;
|
||||
@@ -201,15 +200,21 @@ func (s *AdaptiveStore) InstallBank(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
if err = tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.installVariants(ctx, entries)
|
||||
}
|
||||
|
||||
type bankCandidate struct {
|
||||
entry domain.BankEntry
|
||||
taskID string
|
||||
score float64
|
||||
seen bool
|
||||
presented int64
|
||||
entry domain.BankEntry
|
||||
bankEntryID string
|
||||
variantID string
|
||||
previousID string
|
||||
taskID string
|
||||
score float64
|
||||
seen bool
|
||||
presented int64
|
||||
}
|
||||
|
||||
// AssignBankTask serializes selections per learner, including presentation of
|
||||
@@ -245,9 +250,14 @@ func (s *AdaptiveStore) AssignBankTask(ctx context.Context, j domain.CourseJob)
|
||||
return t, err
|
||||
}
|
||||
}
|
||||
rows, err := tx.QueryContext(ctx, `SELECT b.data,COALESCE(t.id,''),COALESCE((SELECT (a.evaluation->>'score')::float8 FROM course_v2_attempts a WHERE a.task_id=t.id AND a.user_id=$1 AND a.status='graded' AND NOT a.demo ORDER BY a.created_at DESC,a.id DESC LIMIT 1),-1),COALESCE(extract(epoch from t.last_presented_at)::bigint,0)
|
||||
FROM course_v2_bank b LEFT JOIN course_v2_tasks t ON t.bank_entry_id=b.id AND t.owner_id=$1 AND t.exam_id IS NULL
|
||||
WHERE b.active AND b.language=$2 AND b.version=$3 AND b.module_id=$4 AND ($5='' OR b.skill=$5) AND ($6=false OR b.data->'task'->>'kind'='choice')`, j.UserID, j.Language, domain.CourseVersion, j.ModuleID, j.Skill, j.Kind == "legacy_next")
|
||||
instructionLanguage := valueOr(j.InstructionLanguage, "de-DE")
|
||||
rows, err := tx.QueryContext(ctx, availableBank+`
|
||||
SELECT av.entry,COALESCE(t.id,''),COALESCE(graded.score,-1),COALESCE(extract(epoch from previous.last_presented_at)::bigint,0),av.base_id,av.variant_id,COALESCE(previous.id,'')
|
||||
FROM available av
|
||||
LEFT JOIN course_v2_tasks t ON t.bank_entry_id=av.base_id AND COALESCE(t.variant_id,'')=av.variant_id AND t.owner_id=$1 AND t.exam_id IS NULL
|
||||
LEFT JOIN LATERAL (SELECT r.id,r.last_presented_at FROM course_v2_tasks r WHERE r.owner_id=$1 AND r.language=$2 AND r.version=$3 AND r.exam_id IS NULL AND COALESCE(NULLIF(r.data->>'canonical_task_id',''),r.bank_entry_id)=av.canonical_id ORDER BY r.last_presented_at DESC NULLS LAST,r.id LIMIT 1) previous ON true
|
||||
LEFT JOIN LATERAL (SELECT (a.evaluation->>'score')::float8 AS score FROM course_v2_attempts a JOIN course_v2_tasks r ON r.id=a.task_id WHERE a.user_id=$1 AND r.owner_id=$1 AND r.language=$2 AND r.version=$3 AND r.exam_id IS NULL AND COALESCE(NULLIF(r.data->>'canonical_task_id',''),r.bank_entry_id)=av.canonical_id AND a.status='graded' AND NOT a.demo ORDER BY a.created_at DESC,a.id DESC LIMIT 1) graded ON true
|
||||
WHERE av.module_id=$5 AND ($6='' OR av.skill=$6) AND ($7=false OR av.entry->'task'->>'kind'='choice')`, j.UserID, j.Language, domain.CourseVersion, instructionLanguage, j.ModuleID, j.Skill, j.Kind == "legacy_next")
|
||||
if err != nil {
|
||||
return domain.AdaptiveTask{}, err
|
||||
}
|
||||
@@ -255,7 +265,7 @@ func (s *AdaptiveStore) AssignBankTask(ctx context.Context, j domain.CourseJob)
|
||||
for rows.Next() {
|
||||
var c bankCandidate
|
||||
var data []byte
|
||||
if err = rows.Scan(&data, &c.taskID, &c.score, &c.presented); err != nil {
|
||||
if err = rows.Scan(&data, &c.taskID, &c.score, &c.presented, &c.bankEntryID, &c.variantID, &c.previousID); err != nil {
|
||||
rows.Close()
|
||||
return domain.AdaptiveTask{}, err
|
||||
}
|
||||
@@ -263,7 +273,7 @@ func (s *AdaptiveStore) AssignBankTask(ctx context.Context, j domain.CourseJob)
|
||||
rows.Close()
|
||||
return domain.AdaptiveTask{}, err
|
||||
}
|
||||
c.seen = c.taskID != ""
|
||||
c.seen = c.previousID != ""
|
||||
candidates = append(candidates, c)
|
||||
}
|
||||
err = rows.Err()
|
||||
@@ -272,6 +282,9 @@ func (s *AdaptiveStore) AssignBankTask(ctx context.Context, j domain.CourseJob)
|
||||
return domain.AdaptiveTask{}, err
|
||||
}
|
||||
if len(candidates) == 0 {
|
||||
if instructionLanguage != "de-DE" {
|
||||
return domain.AdaptiveTask{}, ErrCourseVariantUnavailable
|
||||
}
|
||||
return domain.AdaptiveTask{}, ErrNotFound
|
||||
}
|
||||
type recent struct {
|
||||
@@ -281,7 +294,7 @@ func (s *AdaptiveStore) AssignBankTask(ctx context.Context, j domain.CourseJob)
|
||||
difficulty int
|
||||
}
|
||||
recentScores := []recent{}
|
||||
rows, err = tx.QueryContext(ctx, `SELECT b.goal,(a.evaluation->>'score')::float8,t.id,b.difficulty FROM course_v2_attempts a JOIN course_v2_tasks t ON t.id=a.task_id JOIN course_v2_bank b ON b.id=t.bank_entry_id WHERE a.user_id=$1 AND t.language=$2 AND t.version=$3 AND t.module_id=$4 AND ($5='' OR t.skill=$5) AND a.status='graded' AND NOT a.demo AND a.exam_id IS NULL ORDER BY a.created_at DESC,a.id DESC LIMIT 5`, j.UserID, j.Language, domain.CourseVersion, j.ModuleID, j.Skill)
|
||||
rows, err = tx.QueryContext(ctx, `SELECT b.goal,(a.evaluation->>'score')::float8,COALESCE(NULLIF(t.data->>'evidence_task_id',''),t.id),b.difficulty FROM course_v2_attempts a JOIN course_v2_tasks t ON t.id=a.task_id JOIN course_v2_bank b ON b.id=t.bank_entry_id WHERE a.user_id=$1 AND t.language=$2 AND t.version=$3 AND t.module_id=$4 AND ($5='' OR t.skill=$5) AND a.status='graded' AND NOT a.demo AND a.exam_id IS NULL ORDER BY a.created_at DESC,a.id DESC LIMIT 5`, j.UserID, j.Language, domain.CourseVersion, j.ModuleID, j.Skill)
|
||||
if err != nil {
|
||||
return domain.AdaptiveTask{}, err
|
||||
}
|
||||
@@ -334,15 +347,15 @@ func (s *AdaptiveStore) AssignBankTask(ctx context.Context, j domain.CourseJob)
|
||||
}
|
||||
sort.Slice(candidates, func(i, k int) bool {
|
||||
a, b := candidates[i], candidates[k]
|
||||
if a.seen && b.seen && (a.taskID == lastID) != (b.taskID == lastID) {
|
||||
return b.taskID == lastID
|
||||
if a.seen && b.seen && (a.previousID == lastID) != (b.previousID == lastID) {
|
||||
return b.previousID == lastID
|
||||
}
|
||||
if rank(a) != rank(b) {
|
||||
return rank(a) < rank(b)
|
||||
}
|
||||
if a.seen && b.seen {
|
||||
if (a.taskID == lastID) != (b.taskID == lastID) {
|
||||
return b.taskID == lastID
|
||||
if (a.previousID == lastID) != (b.previousID == lastID) {
|
||||
return b.previousID == lastID
|
||||
}
|
||||
if a.presented != b.presented {
|
||||
return a.presented < b.presented
|
||||
@@ -367,10 +380,23 @@ func (s *AdaptiveStore) AssignBankTask(ctx context.Context, j domain.CourseJob)
|
||||
t.Source = "curated"
|
||||
t.Review = chosen.seen
|
||||
t.ID = chosen.taskID
|
||||
if !chosen.seen {
|
||||
if chosen.taskID == "" {
|
||||
t.ID = NewID("a2")
|
||||
if t.InstructionLanguage == "" {
|
||||
t.InstructionLanguage = "de-DE"
|
||||
}
|
||||
if t.CanonicalTaskID == "" {
|
||||
t.CanonicalTaskID = chosen.bankEntryID
|
||||
}
|
||||
// Reuse the earliest evidence identity without editing any historic snapshot.
|
||||
err = tx.QueryRowContext(ctx, `SELECT COALESCE(NULLIF(data->>'evidence_task_id',''),id) FROM course_v2_tasks WHERE owner_id=$1 AND language=$2 AND version=$3 AND exam_id IS NULL AND COALESCE(NULLIF(data->>'canonical_task_id',''),bank_entry_id)=$4 ORDER BY created_at,id LIMIT 1`, j.UserID, j.Language, domain.CourseVersion, t.CanonicalTaskID).Scan(&t.EvidenceTaskID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
t.EvidenceTaskID = t.ID
|
||||
} else if err != nil {
|
||||
return t, err
|
||||
}
|
||||
data, _ := json.Marshal(t)
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO course_v2_tasks(id,module_id,language,version,owner_id,source,skill,kind,data,fingerprint,bank_entry_id,last_presented_at,presentation_count) VALUES($1,$2,$3,$4,$5,'curated',$6,$7,$8,$9,$10,now(),1)`, t.ID, t.ModuleID, t.TargetLanguage, t.CourseVersion, j.UserID, t.Skill, t.Kind, data, "bank:"+chosen.entry.ID, chosen.entry.ID)
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO course_v2_tasks(id,module_id,language,version,owner_id,source,skill,kind,data,fingerprint,bank_entry_id,variant_id,last_presented_at,presentation_count) VALUES($1,$2,$3,$4,$5,'curated',$6,$7,$8,$9,$10,NULLIF($11,''),now(),1)`, t.ID, t.ModuleID, t.TargetLanguage, t.CourseVersion, j.UserID, t.Skill, t.Kind, data, "bank:"+chosen.entry.ID, chosen.bankEntryID, chosen.variantID)
|
||||
} else {
|
||||
_, err = tx.ExecContext(ctx, `UPDATE course_v2_tasks SET last_presented_at=now(),presentation_count=presentation_count+1 WHERE id=$1`, t.ID)
|
||||
}
|
||||
@@ -434,7 +460,11 @@ type BankInventoryItem struct {
|
||||
}
|
||||
|
||||
func (s *AdaptiveStore) BankInventory(ctx context.Context, user, language string) ([]BankInventoryItem, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT b.module_id,b.skill,count(*),count(*) FILTER(WHERE t.id IS NULL) FROM course_v2_bank b LEFT JOIN course_v2_tasks t ON t.bank_entry_id=b.id AND t.owner_id=$1 AND t.exam_id IS NULL WHERE b.active AND b.language=$2 AND b.version=$3 GROUP BY b.module_id,b.skill ORDER BY b.module_id,b.skill`, user, language, domain.CourseVersion)
|
||||
var instructionLanguage string
|
||||
if err := s.db.QueryRowContext(ctx, `SELECT instruction_language FROM users WHERE id=$1`, user).Scan(&instructionLanguage); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := s.db.QueryContext(ctx, availableBank+` SELECT av.module_id,av.skill,count(*),count(*) FILTER(WHERE NOT EXISTS(SELECT 1 FROM course_v2_tasks t WHERE t.owner_id=$1 AND t.language=$2 AND t.version=$3 AND t.exam_id IS NULL AND COALESCE(NULLIF(t.data->>'canonical_task_id',''),t.bank_entry_id)=av.canonical_id)) FROM available av GROUP BY av.module_id,av.skill ORDER BY av.module_id,av.skill`, user, language, domain.CourseVersion, instructionLanguage)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,270 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"embed"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"speech-learning-api/internal/domain"
|
||||
)
|
||||
|
||||
//go:embed coursebank/variants/*.json
|
||||
var variantFiles embed.FS
|
||||
|
||||
var ErrCourseVariantUnavailable = errors.New("no reviewed exercises are available for this explanation language")
|
||||
|
||||
const variantSchema = `
|
||||
CREATE TABLE IF NOT EXISTS course_v2_bank_variants (
|
||||
id TEXT PRIMARY KEY,base_entry_id TEXT NOT NULL REFERENCES course_v2_bank(id),
|
||||
canonical_task_id TEXT NOT NULL,instruction_language TEXT NOT NULL,revision TEXT NOT NULL,
|
||||
data JSONB NOT NULL,checksum TEXT NOT NULL,active BOOLEAN NOT NULL DEFAULT true);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS course_v2_variant_active_presentation ON course_v2_bank_variants(base_entry_id,instruction_language) WHERE active;
|
||||
CREATE INDEX IF NOT EXISTS course_v2_variant_lookup ON course_v2_bank_variants(instruction_language,base_entry_id) WHERE active;
|
||||
ALTER TABLE course_v2_tasks ADD COLUMN IF NOT EXISTS variant_id TEXT REFERENCES course_v2_bank_variants(id);
|
||||
CREATE INDEX IF NOT EXISTS course_v2_task_canonical ON course_v2_tasks(owner_id,language,version,(COALESCE(NULLIF(data->>'canonical_task_id',''),bank_entry_id))) WHERE exam_id IS NULL;
|
||||
-- Separate frozen presentations are allowed; their evidence identity is shared
|
||||
-- only when the editorial review classifies them as equivalent translations.
|
||||
DROP INDEX IF EXISTS course_v2_bank_assignment;
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS course_v2_bank_variant_assignment ON course_v2_tasks(owner_id,bank_entry_id,COALESCE(variant_id,'')) WHERE exam_id IS NULL AND bank_entry_id IS NOT NULL;
|
||||
`
|
||||
|
||||
func BundledVariants(base []domain.BankEntry) ([]domain.BankVariant, error) {
|
||||
manifestData, err := variantFiles.ReadFile("coursebank/variants/manifest.json")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var manifest struct {
|
||||
Total int `json:"total"`
|
||||
Files map[string]string `json:"files"`
|
||||
}
|
||||
if err = json.Unmarshal(manifestData, &manifest); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
variants := []domain.BankVariant{}
|
||||
for name, digest := range manifest.Files {
|
||||
if strings.ContainsAny(name, "/\\") || name == "manifest.json" {
|
||||
return nil, errors.New("invalid variant manifest path")
|
||||
}
|
||||
data, err := variantFiles.ReadFile("coursebank/variants/" + name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sum := sha256.Sum256(data)
|
||||
if hex.EncodeToString(sum[:]) != digest {
|
||||
return nil, fmt.Errorf("variant checksum mismatch: %s", name)
|
||||
}
|
||||
var batch []domain.BankVariant
|
||||
dec := json.NewDecoder(strings.NewReader(string(data)))
|
||||
dec.DisallowUnknownFields()
|
||||
if err = dec.Decode(&batch); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
variants = append(variants, batch...)
|
||||
}
|
||||
if len(variants) != manifest.Total || manifest.Total == 0 {
|
||||
return nil, errors.New("invalid variant count")
|
||||
}
|
||||
return variants, ValidateVariants(base, variants)
|
||||
}
|
||||
|
||||
func ValidateVariants(base []domain.BankEntry, variants []domain.BankVariant) error {
|
||||
entries := map[string]domain.BankEntry{}
|
||||
for _, entry := range base {
|
||||
entries[entry.ID] = entry
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
presentations := map[string]bool{}
|
||||
moduleLabels := map[string]string{}
|
||||
for _, v := range variants {
|
||||
original, ok := entries[v.BaseEntryID]
|
||||
t := v.Task
|
||||
b := original.Task
|
||||
if !ok || v.ID == "" || seen[v.ID] || !v.Reviewed || v.Revision == "" || v.InstructionLanguage == "" || t.ID != "" || t.Source != "" || t.ExamID != "" || t.EvidenceTaskID != "" || t.VariantID != v.ID || t.ContentRevision != v.Revision || t.CanonicalTaskID != v.CanonicalTaskID || t.InstructionLanguage != v.InstructionLanguage || t.SchemaVersion != b.SchemaVersion || t.ModuleID != b.ModuleID || t.TargetLanguage != b.TargetLanguage || t.CourseVersion != b.CourseVersion || t.CEFRLevel != b.CEFRLevel || t.Skill != b.Skill || t.Kind != b.Kind || t.AnswerKey == nil || t.Instruction == "" || len([]rune(t.Prompt)) > 2000 || strings.Contains(t.Prompt, "<") {
|
||||
return fmt.Errorf("invalid task variant: %s", v.ID)
|
||||
}
|
||||
if _, exists := entries[v.ID]; exists {
|
||||
return fmt.Errorf("variant identity collides with a base entry: %s", v.ID)
|
||||
}
|
||||
if v.ModuleTitle == "" || v.ModuleDescription == "" {
|
||||
return fmt.Errorf("variant module labels missing: %s", v.ID)
|
||||
}
|
||||
labelKey := t.ModuleID + ":" + v.InstructionLanguage
|
||||
labelValue := v.ModuleTitle + "\n" + v.ModuleDescription
|
||||
if previous, exists := moduleLabels[labelKey]; exists && previous != labelValue {
|
||||
return fmt.Errorf("inconsistent module labels: %s", v.ID)
|
||||
}
|
||||
moduleLabels[labelKey] = labelValue
|
||||
seen[v.ID] = true
|
||||
presentation := v.BaseEntryID + ":" + v.InstructionLanguage
|
||||
if presentations[presentation] {
|
||||
return fmt.Errorf("duplicate active locale presentation: %s", v.ID)
|
||||
}
|
||||
presentations[presentation] = true
|
||||
referenceLanguage := t.TargetLanguage
|
||||
if t.Skill == "vocabulary" {
|
||||
referenceLanguage = t.InstructionLanguage
|
||||
}
|
||||
if t.AnswerKey.ReferenceAnswerLanguage != referenceLanguage {
|
||||
return fmt.Errorf("invalid reference language: %s", v.ID)
|
||||
}
|
||||
if (v.Equivalence != "translation" && v.Equivalence != "adaptation") || (v.Equivalence == "translation" && v.CanonicalTaskID != v.BaseEntryID) || (v.Equivalence == "adaptation" && v.CanonicalTaskID != v.ID) {
|
||||
return fmt.Errorf("invalid variant identity: %s", v.ID)
|
||||
}
|
||||
prompt := t.Instruction
|
||||
if t.Stimulus != nil {
|
||||
stimulus := t.Stimulus
|
||||
if stimulus.Text == "" || stimulus.Language == "" || (stimulus.Role == "target" && stimulus.Language != t.TargetLanguage) || !bankMember(stimulus.Role, []string{"target", "source", "context"}) || (stimulus.Role == "source" && stimulus.Language == t.TargetLanguage) || t.Skill == "listening" {
|
||||
return fmt.Errorf("invalid public stimulus: %s", v.ID)
|
||||
}
|
||||
prompt += " " + stimulus.Text
|
||||
}
|
||||
if t.Prompt != prompt || t.AudioText != b.AudioText || t.Passage != b.Passage || t.ReferenceText != b.ReferenceText || len(t.Options) != len(b.Options) || t.AnswerKey.CorrectOptionID != b.AnswerKey.CorrectOptionID || t.AnswerKey.Explanation == "" || len(t.AnswerKey.Rubric) == 0 || t.AnswerKey.ReferenceAnswerLanguage == "" {
|
||||
return fmt.Errorf("variant changes protected task content: %s", v.ID)
|
||||
}
|
||||
labels := map[string]bool{}
|
||||
for i, option := range t.Options {
|
||||
label := strings.ToLower(strings.TrimSpace(option.Label))
|
||||
expectedRole := "target"
|
||||
expectedLanguage := t.TargetLanguage
|
||||
if t.Skill == "vocabulary" {
|
||||
expectedRole = "meaning"
|
||||
expectedLanguage = t.InstructionLanguage
|
||||
}
|
||||
if option.Role != expectedRole || option.Language != expectedLanguage {
|
||||
return fmt.Errorf("invalid option language role: %s", v.ID)
|
||||
}
|
||||
if expectedRole == "meaning" && v.Equivalence == "translation" && t.InstructionLanguage == t.TargetLanguage {
|
||||
return fmt.Errorf("same-language meanings need a reviewed adaptation: %s", v.ID)
|
||||
}
|
||||
if option.ID != b.Options[i].ID || option.Language == "" || label == "" || labels[label] || (v.Equivalence == "translation" && option.Role == "target" && option.Label != b.Options[i].Label) {
|
||||
return fmt.Errorf("invalid variant options: %s", v.ID)
|
||||
}
|
||||
labels[label] = true
|
||||
if option.ID == t.AnswerKey.CorrectOptionID && option.Label != t.AnswerKey.ReferenceAnswer {
|
||||
return fmt.Errorf("variant reference/key mismatch: %s", v.ID)
|
||||
}
|
||||
}
|
||||
if v.Equivalence == "translation" && t.Skill != "vocabulary" && t.AnswerKey.ReferenceAnswer != b.AnswerKey.ReferenceAnswer {
|
||||
return fmt.Errorf("translation changes reference: %s", v.ID)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *AdaptiveStore) installVariants(ctx context.Context, base []domain.BankEntry) error {
|
||||
variants, err := BundledVariants(base)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = s.db.ExecContext(ctx, variantSchema); err != nil {
|
||||
return err
|
||||
}
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if _, err = tx.ExecContext(ctx, `UPDATE course_v2_bank_variants v SET active=false FROM course_v2_bank b WHERE v.base_entry_id=b.id AND b.language='en-US' AND b.version=$1`, domain.CourseVersion); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, v := range variants {
|
||||
data, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sum := sha256.Sum256(data)
|
||||
checksum := hex.EncodeToString(sum[:])
|
||||
var old string
|
||||
err = tx.QueryRowContext(ctx, `SELECT checksum FROM course_v2_bank_variants WHERE id=$1`, v.ID).Scan(&old)
|
||||
if err == nil && old != checksum {
|
||||
return fmt.Errorf("variant revision is immutable: %s", v.ID)
|
||||
}
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO course_v2_bank_variants(id,base_entry_id,canonical_task_id,instruction_language,revision,data,checksum) VALUES($1,$2,$3,$4,$5,$6,$7) ON CONFLICT(id) DO UPDATE SET active=true`, v.ID, v.BaseEntryID, v.CanonicalTaskID, v.InstructionLanguage, v.Revision, data, checksum); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// availableBank joins a locale presentation to its original curriculum entry.
|
||||
// Existing German entries are left byte-for-byte intact in the bank.
|
||||
const availableBank = `WITH available AS (
|
||||
SELECT b.id AS base_id,''::text AS variant_id,b.id AS canonical_id,b.data AS entry,b.module_id,b.skill
|
||||
FROM course_v2_bank b WHERE b.active AND b.language=$2 AND b.version=$3 AND $4='de-DE'
|
||||
UNION ALL
|
||||
SELECT b.id,v.id,v.canonical_task_id,jsonb_build_object('id',v.id,'revision',v.revision,'learning_goal_id',b.goal,'difficulty',b.difficulty,'reviewed',true,'task',v.data->'task'),b.module_id,b.skill
|
||||
FROM course_v2_bank_variants v JOIN course_v2_bank b ON b.id=v.base_entry_id
|
||||
WHERE v.active AND b.active AND b.language=$2 AND b.version=$3 AND v.instruction_language=$4
|
||||
)`
|
||||
|
||||
func (s *AdaptiveStore) InstructionLanguages(ctx context.Context, language string) ([]string, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT 'de-DE' WHERE EXISTS(SELECT 1 FROM course_v2_bank WHERE active AND language=$1 AND version=$2) UNION SELECT DISTINCT v.instruction_language FROM course_v2_bank_variants v JOIN course_v2_bank b ON b.id=v.base_entry_id WHERE v.active AND b.active AND b.language=$1 AND b.version=$2 ORDER BY 1`, language, domain.CourseVersion)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []string{}
|
||||
for rows.Next() {
|
||||
var value string
|
||||
if err = rows.Scan(&value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, value)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *AdaptiveStore) SetInstructionLanguage(ctx context.Context, user, language, instruction string) error {
|
||||
options, err := s.InstructionLanguages(ctx, language)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !bankMember(instruction, options) {
|
||||
return ErrCourseVariantUnavailable
|
||||
}
|
||||
_, err = s.db.ExecContext(ctx, `UPDATE users SET instruction_language=$2 WHERE id=$1`, user, instruction)
|
||||
return err
|
||||
}
|
||||
|
||||
// A request replay may resume its frozen German exam after the learner has
|
||||
// chosen another explanation language. Availability applies to new sessions.
|
||||
func (s *AdaptiveStore) HasExamRequest(ctx context.Context, user, language, request string) (bool, error) {
|
||||
var exists bool
|
||||
err := s.db.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM course_v2_exams WHERE user_id=$1 AND language=$2 AND version=$3 AND request_id=$4)`, user, language, domain.CourseVersion, request).Scan(&exists)
|
||||
return exists, err
|
||||
}
|
||||
|
||||
// Module labels are derived display content, never a rewrite of stored course
|
||||
// packs, task snapshots or learning-goal identifiers.
|
||||
func (s *AdaptiveStore) localizeModuleLabels(ctx context.Context, language, instruction string, modules []domain.AdaptiveModule) error {
|
||||
if instruction == "" || instruction == "de-DE" {
|
||||
return nil
|
||||
}
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT DISTINCT b.module_id,v.data->>'module_title',v.data->>'module_description' FROM course_v2_bank_variants v JOIN course_v2_bank b ON b.id=v.base_entry_id WHERE v.active AND b.active AND b.language=$1 AND b.version=$2 AND v.instruction_language=$3`, language, domain.CourseVersion, instruction)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var id, title, description string
|
||||
if err = rows.Scan(&id, &title, &description); err != nil {
|
||||
return err
|
||||
}
|
||||
for i := range modules {
|
||||
if modules[i].ID == id {
|
||||
modules[i].Title = title
|
||||
modules[i].Description = description
|
||||
}
|
||||
}
|
||||
}
|
||||
return rows.Err()
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"speech-learning-api/internal/domain"
|
||||
)
|
||||
|
||||
func TestReviewedVariantsRejectChangedIdentityAndLeakedMedia(t *testing.T) {
|
||||
base, err := BundledBank()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
variants, err := BundledVariants(base)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(variants) != 12 {
|
||||
t.Fatal("unexpected pilot count", len(variants))
|
||||
}
|
||||
copyVariants := func() []domain.BankVariant {
|
||||
raw, _ := json.Marshal(variants)
|
||||
var out []domain.BankVariant
|
||||
if err := json.Unmarshal(raw, &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return out
|
||||
}
|
||||
t.Run("changed requirement is not a translation", func(t *testing.T) {
|
||||
v := copyVariants()
|
||||
for i := range v {
|
||||
if v[i].Equivalence == "adaptation" {
|
||||
v[i].Equivalence = "translation"
|
||||
break
|
||||
}
|
||||
}
|
||||
if ValidateVariants(base, v) == nil {
|
||||
t.Fatal("adaptation with shared identity accepted")
|
||||
}
|
||||
})
|
||||
t.Run("ambiguous options", func(t *testing.T) {
|
||||
v := copyVariants()
|
||||
v[0].Task.Options[1].Label = v[0].Task.Options[0].Label
|
||||
if ValidateVariants(base, v) == nil {
|
||||
t.Fatal("ambiguous options accepted")
|
||||
}
|
||||
})
|
||||
t.Run("private listening content", func(t *testing.T) {
|
||||
v := copyVariants()
|
||||
for i := range v {
|
||||
if v[i].Task.Skill == "listening" {
|
||||
v[i].Task.Stimulus = &domain.TaskStimulus{Role: "target", Language: "en-US", Text: v[i].Task.AudioText}
|
||||
break
|
||||
}
|
||||
}
|
||||
if ValidateVariants(base, v) == nil {
|
||||
t.Fatal("public listening transcript accepted")
|
||||
}
|
||||
})
|
||||
t.Run("identity translation", func(t *testing.T) {
|
||||
v := copyVariants()
|
||||
v[0].Task.Stimulus.Role = "source"
|
||||
if ValidateVariants(base, v) == nil {
|
||||
t.Fatal("source and target identity translation accepted")
|
||||
}
|
||||
})
|
||||
for _, v := range variants {
|
||||
public := v.Task.Public(false)
|
||||
if public.AnswerKey != nil || public.AudioText != "" || public.EvidenceTaskID != "" {
|
||||
t.Fatal("private fields exposed", v.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"revision": "en-A1-bank-v2",
|
||||
"total": 840,
|
||||
"editorial_review_sha256": "4cd23522372690392e0d6eae90ede2c918ed129296464035bf32bed30b8a8b7b",
|
||||
"editorial_review_sha256": "a64cc0b862192e996265420c7e6d8ac82c3543449c97fbdd99dc816a270e141a",
|
||||
"files": {
|
||||
"introductions.grammar.json": "e166492fa3e9e13760620bda1af2c0bb6472d91216a4c46808360264450c5ebc",
|
||||
"introductions.vocabulary.json": "d72c534e3c7422d85b2aa03f2988b77da31163637c70ac511ca07f278d1fdd81",
|
||||
|
||||
@@ -0,0 +1,630 @@
|
||||
[
|
||||
{
|
||||
"id": "intro_en_explanations_v1_grammar_01",
|
||||
"revision": "introductions-en-explanations-v1",
|
||||
"base_entry_id": "a1bank_en_v1_introductions_grammar_01",
|
||||
"canonical_task_id": "a1bank_en_v1_introductions_grammar_01",
|
||||
"instruction_language": "en-US",
|
||||
"module_title": "Greetings & introductions",
|
||||
"module_description": "Greet people, introduce a name and origin; I am, my name is",
|
||||
"equivalence": "translation",
|
||||
"reviewed": true,
|
||||
"task": {
|
||||
"schema_version": 2,
|
||||
"module_id": "a1_introductions_v1",
|
||||
"origin_module_key": "introductions",
|
||||
"target_language": "en-US",
|
||||
"course_version": "a1-v2",
|
||||
"cefr_level": "A1",
|
||||
"skill": "grammar",
|
||||
"kind": "choice",
|
||||
"prompt": "Complete the sentence. I ___ Anna.",
|
||||
"options": [
|
||||
{
|
||||
"id": "o1",
|
||||
"label": "am",
|
||||
"role": "target",
|
||||
"language": "en-US"
|
||||
},
|
||||
{
|
||||
"id": "o2",
|
||||
"label": "is",
|
||||
"role": "target",
|
||||
"language": "en-US"
|
||||
},
|
||||
{
|
||||
"id": "o3",
|
||||
"label": "are",
|
||||
"role": "target",
|
||||
"language": "en-US"
|
||||
}
|
||||
],
|
||||
"answer_key": {
|
||||
"correct_option_id": "o1",
|
||||
"reference_answer": "am",
|
||||
"rubric": [
|
||||
"Choose the one answer supported by the sentence, word meaning or supplied text."
|
||||
],
|
||||
"explanation": "Use am after I.",
|
||||
"reference_answer_language": "en-US"
|
||||
},
|
||||
"instruction_language": "en-US",
|
||||
"variant_id": "intro_en_explanations_v1_grammar_01",
|
||||
"canonical_task_id": "a1bank_en_v1_introductions_grammar_01",
|
||||
"content_revision": "introductions-en-explanations-v1",
|
||||
"instruction": "Complete the sentence.",
|
||||
"stimulus": {
|
||||
"role": "target",
|
||||
"language": "en-US",
|
||||
"text": "I ___ Anna."
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "intro_en_explanations_v1_grammar_02",
|
||||
"revision": "introductions-en-explanations-v1",
|
||||
"base_entry_id": "a1bank_en_v1_introductions_grammar_02",
|
||||
"canonical_task_id": "a1bank_en_v1_introductions_grammar_02",
|
||||
"instruction_language": "en-US",
|
||||
"module_title": "Greetings & introductions",
|
||||
"module_description": "Greet people, introduce a name and origin; I am, my name is",
|
||||
"equivalence": "translation",
|
||||
"reviewed": true,
|
||||
"task": {
|
||||
"schema_version": 2,
|
||||
"module_id": "a1_introductions_v1",
|
||||
"origin_module_key": "introductions",
|
||||
"target_language": "en-US",
|
||||
"course_version": "a1-v2",
|
||||
"cefr_level": "A1",
|
||||
"skill": "grammar",
|
||||
"kind": "choice",
|
||||
"prompt": "Complete the sentence. You ___ my friend.",
|
||||
"options": [
|
||||
{
|
||||
"id": "o1",
|
||||
"label": "is",
|
||||
"role": "target",
|
||||
"language": "en-US"
|
||||
},
|
||||
{
|
||||
"id": "o2",
|
||||
"label": "are",
|
||||
"role": "target",
|
||||
"language": "en-US"
|
||||
},
|
||||
{
|
||||
"id": "o3",
|
||||
"label": "am",
|
||||
"role": "target",
|
||||
"language": "en-US"
|
||||
}
|
||||
],
|
||||
"answer_key": {
|
||||
"correct_option_id": "o2",
|
||||
"reference_answer": "are",
|
||||
"rubric": [
|
||||
"Choose the one answer supported by the sentence, word meaning or supplied text."
|
||||
],
|
||||
"explanation": "Use are after you.",
|
||||
"reference_answer_language": "en-US"
|
||||
},
|
||||
"instruction_language": "en-US",
|
||||
"variant_id": "intro_en_explanations_v1_grammar_02",
|
||||
"canonical_task_id": "a1bank_en_v1_introductions_grammar_02",
|
||||
"content_revision": "introductions-en-explanations-v1",
|
||||
"instruction": "Complete the sentence.",
|
||||
"stimulus": {
|
||||
"role": "target",
|
||||
"language": "en-US",
|
||||
"text": "You ___ my friend."
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "intro_en_explanations_v1_vocabulary_01",
|
||||
"revision": "introductions-en-explanations-v1",
|
||||
"base_entry_id": "a1bank_en_v1_introductions_vocabulary_01",
|
||||
"canonical_task_id": "intro_en_explanations_v1_vocabulary_01",
|
||||
"instruction_language": "en-US",
|
||||
"module_title": "Greetings & introductions",
|
||||
"module_description": "Greet people, introduce a name and origin; I am, my name is",
|
||||
"equivalence": "adaptation",
|
||||
"reviewed": true,
|
||||
"task": {
|
||||
"schema_version": 2,
|
||||
"module_id": "a1_introductions_v1",
|
||||
"origin_module_key": "introductions",
|
||||
"target_language": "en-US",
|
||||
"course_version": "a1-v2",
|
||||
"cefr_level": "A1",
|
||||
"skill": "vocabulary",
|
||||
"kind": "choice",
|
||||
"prompt": "What does hello mean in this sentence? Hello, I am Anna.",
|
||||
"options": [
|
||||
{
|
||||
"id": "o1",
|
||||
"label": "a greeting",
|
||||
"role": "meaning",
|
||||
"language": "en-US"
|
||||
},
|
||||
{
|
||||
"id": "o2",
|
||||
"label": "a farewell",
|
||||
"role": "meaning",
|
||||
"language": "en-US"
|
||||
},
|
||||
{
|
||||
"id": "o3",
|
||||
"label": "a polite request",
|
||||
"role": "meaning",
|
||||
"language": "en-US"
|
||||
}
|
||||
],
|
||||
"answer_key": {
|
||||
"correct_option_id": "o1",
|
||||
"reference_answer": "a greeting",
|
||||
"rubric": [
|
||||
"Choose the one answer supported by the sentence, word meaning or supplied text."
|
||||
],
|
||||
"explanation": "Hello is a greeting. We use it when we meet someone.",
|
||||
"reference_answer_language": "en-US"
|
||||
},
|
||||
"instruction_language": "en-US",
|
||||
"variant_id": "intro_en_explanations_v1_vocabulary_01",
|
||||
"canonical_task_id": "intro_en_explanations_v1_vocabulary_01",
|
||||
"content_revision": "introductions-en-explanations-v1",
|
||||
"instruction": "What does hello mean in this sentence?",
|
||||
"stimulus": {
|
||||
"role": "target",
|
||||
"language": "en-US",
|
||||
"text": "Hello, I am Anna."
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "intro_en_explanations_v1_vocabulary_02",
|
||||
"revision": "introductions-en-explanations-v1",
|
||||
"base_entry_id": "a1bank_en_v1_introductions_vocabulary_02",
|
||||
"canonical_task_id": "intro_en_explanations_v1_vocabulary_02",
|
||||
"instruction_language": "en-US",
|
||||
"module_title": "Greetings & introductions",
|
||||
"module_description": "Greet people, introduce a name and origin; I am, my name is",
|
||||
"equivalence": "adaptation",
|
||||
"reviewed": true,
|
||||
"task": {
|
||||
"schema_version": 2,
|
||||
"module_id": "a1_introductions_v1",
|
||||
"origin_module_key": "introductions",
|
||||
"target_language": "en-US",
|
||||
"course_version": "a1-v2",
|
||||
"cefr_level": "A1",
|
||||
"skill": "vocabulary",
|
||||
"kind": "choice",
|
||||
"prompt": "What does name mean in this sentence? My name is Ben.",
|
||||
"options": [
|
||||
{
|
||||
"id": "o1",
|
||||
"label": "where someone lives",
|
||||
"role": "meaning",
|
||||
"language": "en-US"
|
||||
},
|
||||
{
|
||||
"id": "o2",
|
||||
"label": "what a person is called",
|
||||
"role": "meaning",
|
||||
"language": "en-US"
|
||||
},
|
||||
{
|
||||
"id": "o3",
|
||||
"label": "how old someone is",
|
||||
"role": "meaning",
|
||||
"language": "en-US"
|
||||
}
|
||||
],
|
||||
"answer_key": {
|
||||
"correct_option_id": "o2",
|
||||
"reference_answer": "what a person is called",
|
||||
"rubric": [
|
||||
"Choose the one answer supported by the sentence, word meaning or supplied text."
|
||||
],
|
||||
"explanation": "A name is what a person is called. The person's name here is Ben.",
|
||||
"reference_answer_language": "en-US"
|
||||
},
|
||||
"instruction_language": "en-US",
|
||||
"variant_id": "intro_en_explanations_v1_vocabulary_02",
|
||||
"canonical_task_id": "intro_en_explanations_v1_vocabulary_02",
|
||||
"content_revision": "introductions-en-explanations-v1",
|
||||
"instruction": "What does name mean in this sentence?",
|
||||
"stimulus": {
|
||||
"role": "target",
|
||||
"language": "en-US",
|
||||
"text": "My name is Ben."
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "intro_en_explanations_v1_reading_01",
|
||||
"revision": "introductions-en-explanations-v1",
|
||||
"base_entry_id": "a1bank_en_v1_introductions_reading_01",
|
||||
"canonical_task_id": "a1bank_en_v1_introductions_reading_01",
|
||||
"instruction_language": "en-US",
|
||||
"module_title": "Greetings & introductions",
|
||||
"module_description": "Greet people, introduce a name and origin; I am, my name is",
|
||||
"equivalence": "translation",
|
||||
"reviewed": true,
|
||||
"task": {
|
||||
"schema_version": 2,
|
||||
"module_id": "a1_introductions_v1",
|
||||
"origin_module_key": "introductions",
|
||||
"target_language": "en-US",
|
||||
"course_version": "a1-v2",
|
||||
"cefr_level": "A1",
|
||||
"skill": "reading",
|
||||
"kind": "choice",
|
||||
"prompt": "What is the person's name?",
|
||||
"passage": "My name is Anna. I am from Spain.",
|
||||
"options": [
|
||||
{
|
||||
"id": "o1",
|
||||
"label": "Anna",
|
||||
"role": "target",
|
||||
"language": "en-US"
|
||||
},
|
||||
{
|
||||
"id": "o2",
|
||||
"label": "Mia",
|
||||
"role": "target",
|
||||
"language": "en-US"
|
||||
},
|
||||
{
|
||||
"id": "o3",
|
||||
"label": "Eva",
|
||||
"role": "target",
|
||||
"language": "en-US"
|
||||
}
|
||||
],
|
||||
"answer_key": {
|
||||
"correct_option_id": "o1",
|
||||
"reference_answer": "Anna",
|
||||
"rubric": [
|
||||
"Choose the one answer supported by the sentence, word meaning or supplied text."
|
||||
],
|
||||
"explanation": "The text says: My name is Anna. The correct answer is Anna.",
|
||||
"reference_answer_language": "en-US"
|
||||
},
|
||||
"instruction_language": "en-US",
|
||||
"variant_id": "intro_en_explanations_v1_reading_01",
|
||||
"canonical_task_id": "a1bank_en_v1_introductions_reading_01",
|
||||
"content_revision": "introductions-en-explanations-v1",
|
||||
"instruction": "What is the person's name?"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "intro_en_explanations_v1_reading_02",
|
||||
"revision": "introductions-en-explanations-v1",
|
||||
"base_entry_id": "a1bank_en_v1_introductions_reading_02",
|
||||
"canonical_task_id": "a1bank_en_v1_introductions_reading_02",
|
||||
"instruction_language": "en-US",
|
||||
"module_title": "Greetings & introductions",
|
||||
"module_description": "Greet people, introduce a name and origin; I am, my name is",
|
||||
"equivalence": "translation",
|
||||
"reviewed": true,
|
||||
"task": {
|
||||
"schema_version": 2,
|
||||
"module_id": "a1_introductions_v1",
|
||||
"origin_module_key": "introductions",
|
||||
"target_language": "en-US",
|
||||
"course_version": "a1-v2",
|
||||
"cefr_level": "A1",
|
||||
"skill": "reading",
|
||||
"kind": "choice",
|
||||
"prompt": "Which country is Ben from?",
|
||||
"passage": "Ben is from Canada. He lives in London.",
|
||||
"options": [
|
||||
{
|
||||
"id": "o1",
|
||||
"label": "Italy",
|
||||
"role": "target",
|
||||
"language": "en-US"
|
||||
},
|
||||
{
|
||||
"id": "o2",
|
||||
"label": "Canada",
|
||||
"role": "target",
|
||||
"language": "en-US"
|
||||
},
|
||||
{
|
||||
"id": "o3",
|
||||
"label": "Germany",
|
||||
"role": "target",
|
||||
"language": "en-US"
|
||||
}
|
||||
],
|
||||
"answer_key": {
|
||||
"correct_option_id": "o2",
|
||||
"reference_answer": "Canada",
|
||||
"rubric": [
|
||||
"Choose the one answer supported by the sentence, word meaning or supplied text."
|
||||
],
|
||||
"explanation": "The text says: Ben is from Canada. The correct answer is Canada.",
|
||||
"reference_answer_language": "en-US"
|
||||
},
|
||||
"instruction_language": "en-US",
|
||||
"variant_id": "intro_en_explanations_v1_reading_02",
|
||||
"canonical_task_id": "a1bank_en_v1_introductions_reading_02",
|
||||
"content_revision": "introductions-en-explanations-v1",
|
||||
"instruction": "Which country is Ben from?"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "intro_en_explanations_v1_listening_01",
|
||||
"revision": "introductions-en-explanations-v1",
|
||||
"base_entry_id": "a1bank_en_v1_introductions_listening_01",
|
||||
"canonical_task_id": "a1bank_en_v1_introductions_listening_01",
|
||||
"instruction_language": "en-US",
|
||||
"module_title": "Greetings & introductions",
|
||||
"module_description": "Greet people, introduce a name and origin; I am, my name is",
|
||||
"equivalence": "translation",
|
||||
"reviewed": true,
|
||||
"task": {
|
||||
"schema_version": 2,
|
||||
"module_id": "a1_introductions_v1",
|
||||
"origin_module_key": "introductions",
|
||||
"target_language": "en-US",
|
||||
"course_version": "a1-v2",
|
||||
"cefr_level": "A1",
|
||||
"skill": "listening",
|
||||
"kind": "choice",
|
||||
"prompt": "Which country is Anna from?",
|
||||
"audio_text": "My name is Anna. I am from Spain.",
|
||||
"options": [
|
||||
{
|
||||
"id": "o1",
|
||||
"label": "Spain",
|
||||
"role": "target",
|
||||
"language": "en-US"
|
||||
},
|
||||
{
|
||||
"id": "o2",
|
||||
"label": "France",
|
||||
"role": "target",
|
||||
"language": "en-US"
|
||||
},
|
||||
{
|
||||
"id": "o3",
|
||||
"label": "Italy",
|
||||
"role": "target",
|
||||
"language": "en-US"
|
||||
}
|
||||
],
|
||||
"answer_key": {
|
||||
"correct_option_id": "o1",
|
||||
"reference_answer": "Spain",
|
||||
"rubric": [
|
||||
"Choose the one answer supported by the sentence, word meaning or supplied text."
|
||||
],
|
||||
"explanation": "The recording says: I am from Spain. The correct answer is Spain.",
|
||||
"reference_answer_language": "en-US"
|
||||
},
|
||||
"instruction_language": "en-US",
|
||||
"variant_id": "intro_en_explanations_v1_listening_01",
|
||||
"canonical_task_id": "a1bank_en_v1_introductions_listening_01",
|
||||
"content_revision": "introductions-en-explanations-v1",
|
||||
"instruction": "Which country is Anna from?"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "intro_en_explanations_v1_listening_02",
|
||||
"revision": "introductions-en-explanations-v1",
|
||||
"base_entry_id": "a1bank_en_v1_introductions_listening_02",
|
||||
"canonical_task_id": "a1bank_en_v1_introductions_listening_02",
|
||||
"instruction_language": "en-US",
|
||||
"module_title": "Greetings & introductions",
|
||||
"module_description": "Greet people, introduce a name and origin; I am, my name is",
|
||||
"equivalence": "translation",
|
||||
"reviewed": true,
|
||||
"task": {
|
||||
"schema_version": 2,
|
||||
"module_id": "a1_introductions_v1",
|
||||
"origin_module_key": "introductions",
|
||||
"target_language": "en-US",
|
||||
"course_version": "a1-v2",
|
||||
"cefr_level": "A1",
|
||||
"skill": "listening",
|
||||
"kind": "choice",
|
||||
"prompt": "Where does Ben live?",
|
||||
"audio_text": "Ben is from Canada. He lives in London.",
|
||||
"options": [
|
||||
{
|
||||
"id": "o1",
|
||||
"label": "Rome",
|
||||
"role": "target",
|
||||
"language": "en-US"
|
||||
},
|
||||
{
|
||||
"id": "o2",
|
||||
"label": "London",
|
||||
"role": "target",
|
||||
"language": "en-US"
|
||||
},
|
||||
{
|
||||
"id": "o3",
|
||||
"label": "Paris",
|
||||
"role": "target",
|
||||
"language": "en-US"
|
||||
}
|
||||
],
|
||||
"answer_key": {
|
||||
"correct_option_id": "o2",
|
||||
"reference_answer": "London",
|
||||
"rubric": [
|
||||
"Choose the one answer supported by the sentence, word meaning or supplied text."
|
||||
],
|
||||
"explanation": "The recording says: He lives in London. The correct answer is London.",
|
||||
"reference_answer_language": "en-US"
|
||||
},
|
||||
"instruction_language": "en-US",
|
||||
"variant_id": "intro_en_explanations_v1_listening_02",
|
||||
"canonical_task_id": "a1bank_en_v1_introductions_listening_02",
|
||||
"content_revision": "introductions-en-explanations-v1",
|
||||
"instruction": "Where does Ben live?"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "intro_en_explanations_v1_speaking_01",
|
||||
"revision": "introductions-en-explanations-v1",
|
||||
"base_entry_id": "a1bank_en_v1_introductions_speaking_01",
|
||||
"canonical_task_id": "a1bank_en_v1_introductions_speaking_01",
|
||||
"instruction_language": "en-US",
|
||||
"module_title": "Greetings & introductions",
|
||||
"module_description": "Greet people, introduce a name and origin; I am, my name is",
|
||||
"equivalence": "translation",
|
||||
"reviewed": true,
|
||||
"task": {
|
||||
"schema_version": 2,
|
||||
"module_id": "a1_introductions_v1",
|
||||
"origin_module_key": "introductions",
|
||||
"target_language": "en-US",
|
||||
"course_version": "a1-v2",
|
||||
"cefr_level": "A1",
|
||||
"skill": "speaking",
|
||||
"kind": "repeat_sentence",
|
||||
"prompt": "Repeat the English sentence.",
|
||||
"reference_text": "My name is Anna.",
|
||||
"answer_key": {
|
||||
"reference_answer": "My name is Anna.",
|
||||
"rubric": [
|
||||
"Repeat the displayed English sentence clearly.",
|
||||
"Do not claim pronunciation or fluency errors without actual Azure measurements."
|
||||
],
|
||||
"explanation": "Repeat the complete sentence clearly. Pronunciation feedback uses only available speech measurements.",
|
||||
"reference_answer_language": "en-US"
|
||||
},
|
||||
"instruction_language": "en-US",
|
||||
"variant_id": "intro_en_explanations_v1_speaking_01",
|
||||
"canonical_task_id": "a1bank_en_v1_introductions_speaking_01",
|
||||
"content_revision": "introductions-en-explanations-v1",
|
||||
"instruction": "Repeat the English sentence."
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "intro_en_explanations_v1_speaking_11",
|
||||
"revision": "introductions-en-explanations-v1",
|
||||
"base_entry_id": "a1bank_en_v2_introductions_speaking_11",
|
||||
"canonical_task_id": "a1bank_en_v2_introductions_speaking_11",
|
||||
"instruction_language": "en-US",
|
||||
"module_title": "Greetings & introductions",
|
||||
"module_description": "Greet people, introduce a name and origin; I am, my name is",
|
||||
"equivalence": "translation",
|
||||
"reviewed": true,
|
||||
"task": {
|
||||
"schema_version": 2,
|
||||
"module_id": "a1_introductions_v1",
|
||||
"origin_module_key": "introductions",
|
||||
"target_language": "en-US",
|
||||
"course_version": "a1-v2",
|
||||
"cefr_level": "A1",
|
||||
"skill": "speaking",
|
||||
"kind": "free_speech",
|
||||
"prompt": "Introduce yourself. Give a name and the country you are from. You may use real or invented details. Say two or three short English sentences in at most 30 seconds.",
|
||||
"answer_key": {
|
||||
"reference_answer": "My name is Nina. I am not from Germany. I am from Poland.",
|
||||
"rubric": [
|
||||
"Introduce one person with a name and country of origin.",
|
||||
"Two or three short, understandable English sentences meet the required length.",
|
||||
"Real or invented names and countries are allowed. Facts in the example are not mandatory.",
|
||||
"Assess only the explicitly requested content and actual language errors. Do not penalise ambiguity in the task.",
|
||||
"For this free-speech response, assess content and English formulations only. Azure pronunciation is displayed separately and is not part of this score."
|
||||
],
|
||||
"explanation": "The example is only one possible answer. You may choose your own name and country. Content and language are assessed separately from Azure pronunciation.",
|
||||
"reference_answer_language": "en-US"
|
||||
},
|
||||
"instruction_language": "en-US",
|
||||
"variant_id": "intro_en_explanations_v1_speaking_11",
|
||||
"canonical_task_id": "a1bank_en_v2_introductions_speaking_11",
|
||||
"content_revision": "introductions-en-explanations-v1",
|
||||
"instruction": "Introduce yourself. Give a name and the country you are from. You may use real or invented details. Say two or three short English sentences in at most 30 seconds."
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "intro_en_explanations_v1_writing_01",
|
||||
"revision": "introductions-en-explanations-v1",
|
||||
"base_entry_id": "a1bank_en_v1_introductions_writing_01",
|
||||
"canonical_task_id": "intro_en_explanations_v1_writing_01",
|
||||
"instruction_language": "en-US",
|
||||
"module_title": "Greetings & introductions",
|
||||
"module_description": "Greet people, introduce a name and origin; I am, my name is",
|
||||
"equivalence": "adaptation",
|
||||
"reviewed": true,
|
||||
"task": {
|
||||
"schema_version": 2,
|
||||
"module_id": "a1_introductions_v1",
|
||||
"origin_module_key": "introductions",
|
||||
"target_language": "en-US",
|
||||
"course_version": "a1-v2",
|
||||
"cefr_level": "A1",
|
||||
"skill": "writing",
|
||||
"kind": "short_text",
|
||||
"prompt": "Write one short English sentence to introduce yourself. For this exercise, use the name Anna. Name: Anna",
|
||||
"answer_key": {
|
||||
"reference_answer": "My name is Anna.",
|
||||
"rubric": [
|
||||
"Write one understandable English sentence to introduce yourself using the explicitly requested name Anna.",
|
||||
"Accept My name is Anna., I am Anna. and other correct equivalent formulations.",
|
||||
"Consider actual A1 language errors proportionately. Do not invent additional requirements."
|
||||
],
|
||||
"explanation": "My name is Anna. and I am Anna. are both valid answers. Use the name Anna as requested; other correct ways to introduce yourself are allowed.",
|
||||
"reference_answer_language": "en-US"
|
||||
},
|
||||
"instruction_language": "en-US",
|
||||
"variant_id": "intro_en_explanations_v1_writing_01",
|
||||
"canonical_task_id": "intro_en_explanations_v1_writing_01",
|
||||
"content_revision": "introductions-en-explanations-v1",
|
||||
"instruction": "Write one short English sentence to introduce yourself. For this exercise, use the name Anna.",
|
||||
"stimulus": {
|
||||
"role": "context",
|
||||
"language": "en-US",
|
||||
"text": "Name: Anna"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "intro_en_explanations_v1_writing_11",
|
||||
"revision": "introductions-en-explanations-v1",
|
||||
"base_entry_id": "a1bank_en_v2_introductions_writing_11",
|
||||
"canonical_task_id": "a1bank_en_v2_introductions_writing_11",
|
||||
"instruction_language": "en-US",
|
||||
"module_title": "Greetings & introductions",
|
||||
"module_description": "Greet people, introduce a name and origin; I am, my name is",
|
||||
"equivalence": "translation",
|
||||
"reviewed": true,
|
||||
"task": {
|
||||
"schema_version": 2,
|
||||
"module_id": "a1_introductions_v1",
|
||||
"origin_module_key": "introductions",
|
||||
"target_language": "en-US",
|
||||
"course_version": "a1-v2",
|
||||
"cefr_level": "A1",
|
||||
"skill": "writing",
|
||||
"kind": "free_text",
|
||||
"prompt": "Introduce yourself. Give a name and the country you are from. You may use real or invented details. Write two or three short English sentences.",
|
||||
"answer_key": {
|
||||
"reference_answer": "My name is Nina. I am not from Germany. I am from Poland.",
|
||||
"rubric": [
|
||||
"Introduce one person with a name and country of origin.",
|
||||
"Two or three short, understandable English sentences meet the required length.",
|
||||
"Real or invented names and countries are allowed. Facts in the example are not mandatory.",
|
||||
"Assess only the explicitly requested content and actual language errors. Do not penalise ambiguity in the task."
|
||||
],
|
||||
"explanation": "The example is only one possible answer. You may choose your own name and country. Only the explicitly requested information is required.",
|
||||
"reference_answer_language": "en-US"
|
||||
},
|
||||
"instruction_language": "en-US",
|
||||
"variant_id": "intro_en_explanations_v1_writing_11",
|
||||
"canonical_task_id": "a1bank_en_v2_introductions_writing_11",
|
||||
"content_revision": "introductions-en-explanations-v1",
|
||||
"instruction": "Introduce yourself. Give a name and the country you are from. You may use real or invented details. Write two or three short English sentences."
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"total": 12,
|
||||
"editorial_review_sha256": "05cf8e999f341338101fa42d667e031ea6509a19f56b116b5af7329457a75368",
|
||||
"files": {
|
||||
"introductions.en-US.v1.json": "07f3a3935290760bd64935ca66825c6321654aa017cd45c5651b86ddd9fbdc39"
|
||||
}
|
||||
}
|
||||
@@ -131,7 +131,7 @@ func recordPronunciation(ctx context.Context, tx *sql.Tx, user, source, sourceID
|
||||
// and free speech do not enter the scripted profile. Historical practice rows
|
||||
// without metadata remain untouched and explicitly unverified.
|
||||
func (s *PostgresStore) BackfillPronunciation(ctx context.Context) error {
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT a.id,a.user_id,a.task_id,t.language,COALESCE(a.exam_id,''),a.created_at,a.speech
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT a.id,a.user_id,COALESCE(NULLIF(t.data->>'evidence_task_id',''),a.task_id),t.language,COALESCE(a.exam_id,''),a.created_at,a.speech
|
||||
FROM course_v2_attempts a JOIN course_v2_tasks t ON t.id=a.task_id
|
||||
WHERE a.speech->>'provider'='azure' AND a.demo=false AND a.legacy_id IS NULL AND t.kind='repeat_sentence'
|
||||
AND NOT EXISTS(SELECT 1 FROM pronunciation_assessments p WHERE p.source='course' AND p.source_id=a.id)`)
|
||||
|
||||
@@ -182,9 +182,9 @@ func (s *PostgresStore) CreateUser(ctx context.Context, email, passwordHash, uiL
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
INSERT INTO users (id, email, password_hash, ui_language)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id, email, ui_language, role, created_at
|
||||
RETURNING id, email, ui_language, instruction_language, role, created_at
|
||||
`, user.ID, user.Email, passwordHash, uiLanguage)
|
||||
if err := row.Scan(&user.ID, &user.Email, &user.UILanguage, &user.Role, &user.CreatedAt); err != nil {
|
||||
if err := row.Scan(&user.ID, &user.Email, &user.UILanguage, &user.InstructionLanguage, &user.Role, &user.CreatedAt); err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
return user, nil
|
||||
@@ -194,11 +194,11 @@ func (s *PostgresStore) GetPasswordHashByEmail(ctx context.Context, email string
|
||||
var user domain.User
|
||||
var hash string
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
SELECT id, email, password_hash, ui_language, role, created_at
|
||||
SELECT id, email, password_hash, ui_language, instruction_language, role, created_at
|
||||
FROM users
|
||||
WHERE email = $1
|
||||
`, strings.ToLower(strings.TrimSpace(email)))
|
||||
if err := row.Scan(&user.ID, &user.Email, &hash, &user.UILanguage, &user.Role, &user.CreatedAt); err != nil {
|
||||
if err := row.Scan(&user.ID, &user.Email, &hash, &user.UILanguage, &user.InstructionLanguage, &user.Role, &user.CreatedAt); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return domain.User{}, "", ErrNotFound
|
||||
}
|
||||
@@ -210,11 +210,11 @@ func (s *PostgresStore) GetPasswordHashByEmail(ctx context.Context, email string
|
||||
func (s *PostgresStore) GetUser(ctx context.Context, id string) (domain.User, error) {
|
||||
var user domain.User
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
SELECT id, email, ui_language, role, created_at
|
||||
SELECT id, email, ui_language, instruction_language, role, created_at
|
||||
FROM users
|
||||
WHERE id = $1
|
||||
`, id)
|
||||
if err := row.Scan(&user.ID, &user.Email, &user.UILanguage, &user.Role, &user.CreatedAt); err != nil {
|
||||
if err := row.Scan(&user.ID, &user.Email, &user.UILanguage, &user.InstructionLanguage, &user.Role, &user.CreatedAt); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return domain.User{}, ErrNotFound
|
||||
}
|
||||
@@ -265,20 +265,48 @@ func (s *PostgresStore) GetPrimaryProfile(ctx context.Context, userID string) (d
|
||||
}
|
||||
|
||||
func (s *PostgresStore) UpdatePreferences(ctx context.Context, userID, uiLanguage, targetLanguage, cefrLevel string, dailyGoalMinutes int) (domain.LearnerProfile, error) {
|
||||
if uiLanguage != "" {
|
||||
if _, err := s.db.ExecContext(ctx, `UPDATE users SET ui_language = $1 WHERE id = $2`, uiLanguage, userID); err != nil {
|
||||
return domain.LearnerProfile{}, err
|
||||
}
|
||||
if uiLanguage != "" && !domain.SupportedUILanguage(uiLanguage) {
|
||||
return domain.LearnerProfile{}, errors.New("unsupported UI language")
|
||||
}
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return domain.LearnerProfile{}, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
// Lock the account so partial preference updates preserve the active course
|
||||
// and cannot race with another language/profile switch.
|
||||
var accountID string
|
||||
if err = tx.QueryRowContext(ctx, `SELECT id FROM users WHERE id=$1 FOR UPDATE`, userID).Scan(&accountID); err != nil {
|
||||
return domain.LearnerProfile{}, err
|
||||
}
|
||||
var current domain.LearnerProfile
|
||||
err = tx.QueryRowContext(ctx, `SELECT p.target_language,p.cefr_level,p.daily_goal_minutes
|
||||
FROM learner_profiles p JOIN users u ON u.id=p.user_id WHERE p.user_id=$1
|
||||
ORDER BY (p.id=u.active_profile_id) DESC NULLS LAST,p.created_at,p.id LIMIT 1`, userID).Scan(¤t.TargetLanguage, ¤t.CEFRLevel, ¤t.DailyGoalMinutes)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return current, err
|
||||
}
|
||||
targetLanguage = valueOr(targetLanguage, valueOr(current.TargetLanguage, "en-US"))
|
||||
cefrLevel = valueOr(cefrLevel, valueOr(current.CEFRLevel, "A1"))
|
||||
if dailyGoalMinutes <= 0 {
|
||||
dailyGoalMinutes = current.DailyGoalMinutes
|
||||
}
|
||||
if dailyGoalMinutes <= 0 {
|
||||
dailyGoalMinutes = 10
|
||||
}
|
||||
profile, err := s.CreateLearnerProfile(ctx, userID, valueOr(targetLanguage, "en-US"), valueOr(cefrLevel, "A1"), dailyGoalMinutes)
|
||||
var profile domain.LearnerProfile
|
||||
err = tx.QueryRowContext(ctx, `INSERT INTO learner_profiles(id,user_id,target_language,cefr_level,daily_goal_minutes)
|
||||
VALUES($1,$2,$3,$4,$5) ON CONFLICT(user_id,target_language) DO UPDATE SET
|
||||
cefr_level=EXCLUDED.cefr_level,daily_goal_minutes=EXCLUDED.daily_goal_minutes
|
||||
RETURNING id,user_id,target_language,cefr_level,daily_goal_minutes,created_at`, NewID("lrn"), userID, targetLanguage, cefrLevel, dailyGoalMinutes).Scan(&profile.ID, &profile.UserID, &profile.TargetLanguage, &profile.CEFRLevel, &profile.DailyGoalMinutes, &profile.CreatedAt)
|
||||
if err != nil {
|
||||
return profile, err
|
||||
}
|
||||
_, err = s.db.ExecContext(ctx, `UPDATE users SET active_profile_id=$2 WHERE id=$1`, userID, profile.ID)
|
||||
return profile, err
|
||||
_, err = tx.ExecContext(ctx, `UPDATE users SET active_profile_id=$2,ui_language=CASE WHEN $3='' THEN ui_language ELSE $3 END WHERE id=$1`, userID, profile.ID, uiLanguage)
|
||||
if err != nil {
|
||||
return profile, err
|
||||
}
|
||||
return profile, tx.Commit()
|
||||
}
|
||||
|
||||
func (s *PostgresStore) ListLanguages(ctx context.Context) ([]domain.LanguageProfile, error) {
|
||||
@@ -665,6 +693,13 @@ CREATE TABLE IF NOT EXISTS users (
|
||||
ALTER TABLE users ADD COLUMN IF NOT EXISTS role TEXT NOT NULL DEFAULT 'learner'
|
||||
CHECK (role IN ('learner', 'admin'));
|
||||
|
||||
-- Previously ui_language also selected the language of explanations. Preserve
|
||||
-- that preference once, independently of all future interface-language changes.
|
||||
ALTER TABLE users ADD COLUMN IF NOT EXISTS instruction_language TEXT;
|
||||
UPDATE users SET instruction_language=ui_language WHERE instruction_language IS NULL;
|
||||
ALTER TABLE users ALTER COLUMN instruction_language SET DEFAULT 'de-DE';
|
||||
ALTER TABLE users ALTER COLUMN instruction_language SET NOT NULL;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS learner_profiles (
|
||||
id TEXT PRIMARY KEY,
|
||||
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
|
||||
Reference in New Issue
Block a user