kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Quizzes created before 0.17 can't be taken or reviewed: exam API drops their questions

Open Rishika-dev opened this issue 1 month ago • 2 comments

🙂 Looking for an issue? Welcome! This issue is open for contribution. If this is the first time you’re requesting an issue, please:

  • Read Contributing guidelines carefully. Pay extra attention to Using generative AI. Pull requests and comments that don’t follow the guidelines won’t be answered.
  • Confirm that you’ve read the guidelines in your comment.

Target branch: develop (regression in 0.20.0 alphas, not in 0.19.5)

Observed behavior

For any quiz with data_model_version 0, 1 or 2 (every quiz created before V3 sections arrived in 0.17), GET /api/exams/exam/<id>/ replaces each entry of question_sources with {"learners_see_fixed_order": false}.

Stored question_sources (V1):

[{"exercise_id": "5127051047b14b1c84dc745ddbca0864", "question_id": "76ca1b8d724d5143948ebd379fbefc03", "title": "..."}, ...]

API response:

"data_model_version": 1,
"question_sources": [{"learners_see_fixed_order": false}, {"learners_see_fixed_order": false}, {"learners_see_fixed_order": false}]

The frontend's convertExamQuestionSources then upgrades these empty entries to V3. Every question ends up with exercise_id undefined, so the exercises are requested with GET /api/content/contentnode/?ids=&no_available_filtering=true. As a result:

Who Where What they see
Learner Taking the quiz Every question shows "Resource not found on device". Submit is blocked: "Unable to submit quiz because some resources are missing or not supported"
Coach Quiz preview "Resource not found on device"
Coach A learner's quiz report Score is shown, but every answer shows "Resource not found on device"

The exercises are on the device (available=True). Only the API response is wrong.

Learner taking a V1 quiz:

learner-v1-quiz Coach quiz preview: coach-preview-v1-quiz Coach viewing a learner's quiz report: coach-learner-report-v1-quiz

Errors and logs

[Vue warn]: Duplicate keys detected: 'answer-history-item-undefined:undefined'. This may cause an update error.
found in ---> <AnswerHistory> at kolibri/plugins/learn/frontend/views/ExamPage/AnswerHistory.vue

Cause

#14842 moved ExamViewset to the serializer-derived ValuesViewset pattern. ExamSerializer.question_sources is QuizSectionSerializer(many=True) (kolibri/core/exams/viewsets/exam.py L87). Because it's a declared field, derive_values_from_serializer maps the raw JSON through field.to_representation (kolibri/core/utils/values_viewset/introspect.py L471–474).

QuizSectionSerializer describes a V3 section. A V0–V2 entry is a question (or a V0 exercise source), so it has none of the section fields. section_title, description and questions are required=False and get skipped, and learners_see_fixed_order falls back to its default. Each question becomes {"learners_see_fixed_order": false}.

In 0.19.5, ExamViewset listed question_sources in its explicit values, so the stored JSON was returned unchanged. Nothing upgrades stored quizzes to V3: migration 0007_bump_data_model_version_to_3 only changes the default, and kolibri/core/exams/upgrade.py has no conversion. So these quizzes are still V0–V2 in existing databases, and the frontend is expected to convert them on read.

Expected behavior

The exam API returns question_sources exactly as stored for every data_model_version, so the frontend can convert older quizzes as before. Learners can take and submit them, and coaches can preview them and review learners' answers.

User-facing consequences

After upgrading to 0.20, any facility that has quizzes from 0.16 or earlier, or syncs with a device running one, loses access to them:

  • Learners can't finish an active old quiz.
  • Coaches can no longer review learners' answers on old quizzes, which is historical report data.

Steps to reproduce

Through the API test client (the quickest):

exam = Exam.objects.create(title="legacy", collection=classroom, creator=admin, active=True, question_count=1, question_sources=[])
Exam.objects.filter(pk=exam.id).update(
    data_model_version=1,
    question_sources=[{"exercise_id": uuid4().hex, "question_id": uuid4().hex, "title": "Ex"}],
)
response = client.get(reverse("kolibri:core:exam-detail", kwargs={"pk": exam.id}))
response.data["question_sources"]  # [{"learners_see_fixed_order": False}]

The same test fails for V0 ({exercise_id, number_of_questions, title}) and V2 ({exercise_id, question_id, title, counter_in_exercise}).

In the UI:

  1. Generate dev data with kolibri manage generateuserdata. Its quizzes are V1, with all 3 exercises available on the device.
  2. Sign in as a learner in the class and open an assigned quiz. Every question shows "Resource not found on device" and Submit is blocked.
  3. Sign in as a coach and open Quizzes → the quiz → Preview, or a learner's row in the report. Same message.

Context

  • Kolibri develop at 9cd759ba45 (dev server); also in v0.20.0-alpha0 and alpha1
  • Not in v0.19.5
  • macOS 26.6.2, Chromium
  • Kolibri QA Channel imported from Studio

Possible fixes, which I'd be glad to work on once the approach is agreed:

  1. Serialize question_sources as raw JSON on read, and keep QuizSectionSerializer for validating writes only (writes are already V3-only).
  2. Pick the representation by data_model_version, so V3 goes through QuizSectionSerializer and older versions pass through unchanged.

A regression test that round-trips V0, V1 and V2 question_sources through exam-detail would cover it. No current test reads a pre-V3 quiz back from the API.

AI usage

I used Claude Code to drive a browser through learner and coach quiz flows on my local Kolibri, where it found this. It traced the empty ids= request back to the API response and the serializer, and drafted this issue. I checked the code references against develop and v0.19.5, reproduced it in the UI as a learner and a coach, ran a test that round-trips V0/V1/V2 question_sources through the API (all three fail), and edited the text.

Rishika-dev avatar Oct 06 '26 20:10 Rishika-dev

Confirmed. Let's go with option 1, implemented as a custom field:

  • Subclass DRF's JSONField and override to_internal_value to validate with QuizSectionSerializer(many=True), returning its validated_data.
  • Use it for question_sources on ExamSerializer, so stored JSON is passed through unchanged on read.
  • Add a test under override_settings(DEBUG=True) that reads V0, V1 and V2 question_sources back through exam-detail.

rtibbles avatar Oct 06 '26 21:10 rtibbles

Opened https://github.com/learningequality/kolibri/pull/15535 with the JSONField approach and V0/V1/V2 round-trip tests under DEBUG=True.

Rishika-dev avatar Oct 07 '26 04:10 Rishika-dev