def _calculate_score_for_modules(user_id, course, modules): """ Calculates the cumulative score (percent) of the given modules """ # removing branch and version from exam modules locator # otherwise student module would not return scores since module usage keys would not match modules = [m for m in modules] locations = [ BlockUsageLocator( course_key=course.id, block_type=module.location.block_type, block_id=module.location.block_id ) if isinstance(module.location, BlockUsageLocator) and module.location.version else module.location for module in modules ] scores_client = ScoresClient(course.id, user_id) scores_client.fetch_scores(locations) # Iterate over all of the exam modules to get score percentage of user for each of them module_percentages = [] ignore_categories = ['course', 'chapter', 'sequential', 'vertical', 'randomize', 'library_content'] for index, module in enumerate(modules): if module.category not in ignore_categories and (module.graded or module.has_score): module_score = scores_client.get(locations[index]) if module_score: correct = module_score.correct or 0 total = module_score.total or 1 module_percentages.append(correct / total) return sum(module_percentages) / float(len(module_percentages)) if module_percentages else 0
def _calculate_entrance_exam_score(user, course_descriptor, exam_modules): """ Calculates the score (percent) of the entrance exam using the provided modules """ student_module_dict = {} scores_client = ScoresClient(course_descriptor.id, user.id) # removing branch and version from exam modules locator # otherwise student module would not return scores since module usage keys would not match locations = [ BlockUsageLocator(course_key=course_descriptor.id, block_type=exam_module.location.block_type, block_id=exam_module.location.block_id) if isinstance(exam_module.location, BlockUsageLocator) and exam_module.location.version else 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 index, exam_module in enumerate(exam_modules): exam_module_score = scores_client.get(locations[index]) if exam_module_score: student_module_dict[unicode(locations[index])] = { 'grade': exam_module_score.correct, 'max_grade': exam_module_score.total } exam_percentage = 0 module_percentages = [] ignore_categories = ['course', 'chapter', 'sequential', 'vertical'] for index, module in enumerate(exam_modules): if module.graded and module.category not in ignore_categories: module_percentage = 0 module_location = unicode(locations[index]) if module_location in student_module_dict and student_module_dict[ module_location]['max_grade']: student_module = student_module_dict[module_location] module_percentage = student_module['grade'] / student_module[ 'max_grade'] module_percentages.append(module_percentage) if module_percentages: exam_percentage = sum(module_percentages) / float( len(module_percentages)) return exam_percentage
def _calculate_entrance_exam_score(user, course_descriptor, exam_modules): """ Calculates the score (percent) of the entrance exam using the provided modules """ student_module_dict = {} 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'] for module in exam_modules: if module.graded and module.category not in ignore_categories: module_percentage = 0 module_location = unicode(module.location) if module_location in student_module_dict and student_module_dict[ module_location]['max_grade']: student_module = student_module_dict[module_location] module_percentage = student_module['grade'] / student_module[ 'max_grade'] module_percentages.append(module_percentage) if module_percentages: exam_percentage = sum(module_percentages) / float( len(module_percentages)) return exam_percentage
def condition_on_problem_list(self, problems): """ Returns the score for a list of problems """ # pylint: disable=no-member user_id = self.xmodule_runtime.user_id scores_client = ScoresClient(self.course_id, user_id) correct_neutral = {'correct': 0.0} total_neutral = {'total': 0.0} total = 0 correct = 0 def _get_usage_key(problem): loc = self.get_location_string(problem) try: uk = UsageKey.from_string(loc) except InvalidKeyError: uk = _get_draft_usage_key(problem) return uk def _get_draft_usage_key(problem): loc = self.get_location_string(problem, True) try: uk = UsageKey.from_string(loc) uk = uk.map_into_course(self.course_id) except InvalidKeyError: uk = None return uk def _to_reducible(score): correct_default = 0.0 total_default = 1.0 if not score.total: return {'correct': correct_default, 'total': total_default} else: return {'correct': score.correct, 'total': score.total} def _calculate_correct(first_score, second_score): correct = first_score['correct'] + second_score['correct'] return {'correct': correct} def _calculate_total(first_score, second_score): total = first_score['total'] + second_score['total'] return {'total': total} usages_keys = map(_get_usage_key, problems) scores_client.fetch_scores(usages_keys) scores = map(scores_client.get, usages_keys) scores = filter(None, scores) problems_to_answer = [score.total for score in scores] if self.operator in self.SPECIAL_COMPARISON_DISPATCHER.keys(): evaluation = self.SPECIAL_COMPARISON_DISPATCHER[self.operator]( self, problems_to_answer) return evaluation reducible_scores = map(_to_reducible, scores) correct = reduce(_calculate_correct, reducible_scores, correct_neutral) total = reduce(_calculate_total, reducible_scores, total_neutral) return self.compare_scores(correct['correct'], total['total'])