🐛 fix cohort-bug

when all questions are answered at least 3 times, it didn't fill the
last cohort with random questions

Co-authored-by: haraldnilsen <harald_998@hotmail.com>
Signed-off-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
This commit is contained in:
Sindre Kjelsrud 2024-01-17 18:52:10 +01:00
parent b249705cd8
commit 03a3d3f2d6
Signed by untrusted user who does not match committer: sidski
GPG key ID: D2BBDF3EDE6BA9A6

View file

@ -45,29 +45,30 @@ func GetUserQuestions(respondentID int) ([]UserQuestions, error) {
} }
getQuestionsStatement := ` getQuestionsStatement := `
WITH FilteredQuestions AS ( WITH FilteredQuestions AS (
SELECT s.SpørsmålID, s.tekst SELECT s.SpørsmålID, s.tekst
FROM Spørsmål s FROM Spørsmål s
LEFT JOIN ( LEFT JOIN (
SELECT ss.spørsmålID SELECT ss.spørsmålID
FROM Spørsmålsvar ss FROM Spørsmålsvar ss
LEFT JOIN SvarVurdering sv ON ss.svarID = sv.svarID LEFT JOIN SvarVurdering sv ON ss.svarID = sv.svarID
GROUP BY ss.spørsmålID GROUP BY ss.spørsmålID
HAVING COUNT(sv.vurderingID) < 3 HAVING COUNT(sv.vurderingID) < 3
) AS subquery ON s.spørsmålID = subquery.spørsmålID ) AS subquery ON s.spørsmålID = subquery.spørsmålID
WHERE subquery.spørsmålID IS NOT NULL WHERE subquery.spørsmålID IS NOT NULL
LIMIT 5 LIMIT 5
) ),
RandomQuestions AS (
SELECT * FROM FilteredQuestions SELECT s.SpørsmålID, s.tekst
UNION ALL FROM Spørsmål s
SELECT * FROM ( WHERE s.SpørsmålID NOT IN (SELECT SpørsmålID FROM FilteredQuestions)
SELECT s.SpørsmålID, s.tekst ORDER BY RANDOM()
FROM Spørsmål s LIMIT 5
ORDER BY RANDOM() )
LIMIT 5 SELECT * FROM FilteredQuestions
) RandomQuestions UNION ALL
WHERE NOT EXISTS (SELECT 1 FROM FilteredQuestions) SELECT * FROM RandomQuestions
LIMIT 5
` `
stmt, err := db.Prepare(getQuestionsStatement) stmt, err := db.Prepare(getQuestionsStatement)