diff --git a/lms/djangoapps/courseware/entrance_exams.py b/lms/djangoapps/courseware/entrance_exams.py index d5b684bc4c..7a02196dc0 100644 --- a/lms/djangoapps/courseware/entrance_exams.py +++ b/lms/djangoapps/courseware/entrance_exams.py @@ -4,8 +4,7 @@ This file contains all entrance exam related utils/logic. from django.conf import settings from courseware.access import has_access -from courseware.model_data import FieldDataCache -from courseware.models import StudentModule +from courseware.model_data import FieldDataCache, ScoresClient from opaque_keys.edx.keys import UsageKey from student.models import EntranceExamConfiguration from util.milestones_helpers import get_required_content @@ -88,21 +87,19 @@ def _calculate_entrance_exam_score(user, course_descriptor, exam_modules): """ Calculates the score (percent) of the entrance exam using the provided modules """ - # All of the exam module ids - exam_module_ids = [exam_module.location for exam_module in exam_modules] - - # All of the corresponding student module records - student_modules = StudentModule.objects.filter( - student=user, - course_id=course_descriptor.id, - module_state_key__in=exam_module_ids, - ) student_module_dict = {} - for student_module in student_modules: - student_module_dict[unicode(student_module.module_state_key)] = { - 'grade': student_module.grade, - 'max_grade': student_module.max_grade - } + scores_client = ScoresClient(course_descriptor.id, user.id) + locations = [exam_module.location for exam_module in exam_modules] + scores_client.fetch_scores(locations) + + # Iterate over all of the exam modules to get score of user for each of them + for exam_module in exam_modules: + exam_module_score = scores_client.get(exam_module.location) + if exam_module_score: + student_module_dict[unicode(exam_module.location)] = { + 'grade': exam_module_score.correct, + 'max_grade': exam_module_score.total + } exam_percentage = 0 module_percentages = [] ignore_categories = ['course', 'chapter', 'sequential', 'vertical'] diff --git a/lms/djangoapps/courseware/tests/test_entrance_exam.py b/lms/djangoapps/courseware/tests/test_entrance_exam.py index 43ec7f7406..636dcc1e3b 100644 --- a/lms/djangoapps/courseware/tests/test_entrance_exam.py +++ b/lms/djangoapps/courseware/tests/test_entrance_exam.py @@ -283,13 +283,15 @@ class EntranceExamTestCases(LoginEnrollmentTestCase, ModuleStoreTestCase): """ test entrance exam score. we will hit the method get_entrance_exam_score to verify exam score. """ - exam_score = get_entrance_exam_score(self.request, self.course) + with self.assertNumQueries(1): + exam_score = get_entrance_exam_score(self.request, self.course) self.assertEqual(exam_score, 0) answer_entrance_exam_problem(self.course, self.request, self.problem_1) answer_entrance_exam_problem(self.course, self.request, self.problem_2) - exam_score = get_entrance_exam_score(self.request, self.course) + with self.assertNumQueries(1): + exam_score = get_entrance_exam_score(self.request, self.course) # 50 percent exam score should be achieved. self.assertGreater(exam_score * 100, 50)