Skip to content

Commit

Permalink
Merge pull request #12196 from AlexVelezLl/fix-question-count
Browse files Browse the repository at this point in the history
Update exam.question_count calculation
  • Loading branch information
rtibbles authored May 24, 2024
2 parents 4be649e + 4ecc912 commit b08ed71
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
10 changes: 8 additions & 2 deletions kolibri/core/exams/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ExamSerializer(ModelSerializer):
creator = PrimaryKeyRelatedField(
read_only=False, queryset=FacilityUser.objects.all()
)
question_count = IntegerField()
question_count = IntegerField(allow_null=True)
date_archived = DateTimeTzField(allow_null=True)
date_activated = DateTimeTzField(allow_null=True)

Expand All @@ -83,7 +83,7 @@ class Meta:
"learners_see_fixed_order",
"learner_ids",
)
read_only_fields = ("data_model_version",)
read_only_fields = ("data_model_version", "question_count")

def validate(self, attrs):
title = attrs.get("title")
Expand Down Expand Up @@ -137,6 +137,12 @@ def to_internal_value(self, data):
else:
# Otherwise we are just updating the exam, so allow a partial update
self.partial = True

question_sources = data.get("question_sources", [])
data["question_count"] = sum(
len(source.get("questions", [])) for source in question_sources
)

return super(ExamSerializer, self).to_internal_value(data)

def create(self, validated_data):
Expand Down
14 changes: 12 additions & 2 deletions kolibri/core/exams/test/test_exam_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ def setUpTestData(cls):

def make_basic_exam(self):
sections = self.make_basic_sections(1)
total_questions = sum(section["question_count"] for section in sections)
return {
"title": "Exam",
"question_count": total_questions,
"active": True,
"collection": self.classroom.id,
"learners_see_fixed_order": False,
Expand Down Expand Up @@ -551,3 +549,15 @@ def test_exam_model_get_questions_v2_v1(self):

self.exam.save()
self.assertEqual(len(self.exam.get_questions()), 1)

def test_exam_question_count_calculation(self):
self.login_as_admin()
exam = self.make_basic_exam()
question_count = sum(
len(source["questions"]) for source in exam["question_sources"]
)
response = self.post_new_exam(exam)
exam_id = response.data["id"]
self.assertEqual(response.status_code, 201)
exam_model_instance = models.Exam.objects.get(id=exam_id)
self.assertEqual(exam_model_instance.question_count, question_count)
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,6 @@ export default function useQuizCreation() {
* @throws {Error} if quiz is not valid
*/
function saveQuiz() {
const totalQuestions = get(allSections).reduce((acc, section) => {
acc += parseInt(section.question_count);
return acc;
}, 0);

set(_quiz, {
...get(_quiz),
question_count: totalQuestions,
});
if (!validateQuiz(get(_quiz))) {
throw new Error(`Quiz is not valid: ${JSON.stringify(get(_quiz))}`);
}
Expand Down
1 change: 1 addition & 0 deletions kolibri/plugins/learn/assets/src/views/ExamPage/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@
.answered {
display: inline-block;
margin-right: 8px;
margin-left: 8px;
white-space: nowrap;
}
Expand Down

0 comments on commit b08ed71

Please sign in to comment.