def update_milestone(milestone, usage_key, prereq_milestone, user, grade_percentage=None, completion_percentage=None): """ Updates the milestone record based on evaluation of prerequisite met. Arguments: milestone: The gated milestone being evaluated usage_key: Usage key of the prerequisite subsection prereq_milestone: The gating milestone user: The user who has fulfilled milestone grade_percentage: Grade percentage of prerequisite subsection completion_percentage: Completion percentage of prerequisite subsection Returns: True if prerequisite has been met, False if not """ min_score, min_completion = _get_minimum_required_percentage(milestone) if not grade_percentage: grade_percentage = get_subsection_grade_percentage(usage_key, user) if min_score > 0 else 0 if not completion_percentage: completion_percentage = get_subsection_completion_percentage(usage_key, user) if min_completion > 0 else 0 if grade_percentage >= min_score and completion_percentage >= min_completion: milestones_helpers.add_user_milestone({'id': user.id}, prereq_milestone) return True else: milestones_helpers.remove_user_milestone({'id': user.id}, prereq_milestone) return False
def evaluate_prerequisite(course, subsection_grade, user): """ Evaluates any gating milestone relationships attached to the given subsection. If the subsection_grade meets the minimum score required by dependent subsections, the related milestone will be marked fulfilled for the user. """ prereq_milestone = gating_api.get_gating_milestone( course.id, subsection_grade.location, 'fulfills') if prereq_milestone: gated_content_milestones = defaultdict(list) for milestone in gating_api.find_gating_milestones( course.id, content_key=None, relationship='requires'): gated_content_milestones[milestone['id']].append(milestone) gated_content = gated_content_milestones.get(prereq_milestone['id']) if gated_content: for milestone in gated_content: min_percentage = _get_minimum_required_percentage(milestone) subsection_percentage = _get_subsection_percentage( subsection_grade) if subsection_percentage >= min_percentage: milestones_helpers.add_user_milestone({'id': user.id}, prereq_milestone) else: milestones_helpers.remove_user_milestone({'id': user.id}, prereq_milestone)
def evaluate_prerequisite(course, prereq_content_key, user_id): """ Finds the parent subsection of the content in the course and evaluates any milestone relationships attached to that subsection. If the calculated grade of the prerequisite subsection meets the minimum score required by dependent subsections, the related milestone will be fulfilled for the user. Arguments: user_id (int): ID of User for which evaluation should occur course (CourseModule): The course prereq_content_key (UsageKey): The prerequisite content usage key Returns: None """ xblock = modulestore().get_item(prereq_content_key) sequential = _get_xblock_parent(xblock, 'sequential') if sequential: prereq_milestone = gating_api.get_gating_milestone( course.id, sequential.location.for_branch(None), 'fulfills') if prereq_milestone: gated_content_milestones = defaultdict(list) for milestone in gating_api.find_gating_milestones( course.id, None, 'requires'): gated_content_milestones[milestone['id']].append(milestone) gated_content = gated_content_milestones.get( prereq_milestone['id']) if gated_content: user = User.objects.get(id=user_id) score = get_module_score(user, course, sequential) * 100 for milestone in gated_content: # Default minimum score to 100 min_score = 100 requirements = milestone.get('requirements') if requirements: try: min_score = int(requirements.get('min_score')) except (ValueError, TypeError): log.warning( 'Failed to find minimum score for gating milestone %s, defaulting to 100', json.dumps(milestone)) if score >= min_score: milestones_helpers.add_user_milestone({'id': user_id}, prereq_milestone) else: milestones_helpers.remove_user_milestone( {'id': user_id}, prereq_milestone)
def evaluate_prerequisite(course, prereq_content_key, user_id): """ Finds the parent subsection of the content in the course and evaluates any milestone relationships attached to that subsection. If the calculated grade of the prerequisite subsection meets the minimum score required by dependent subsections, the related milestone will be fulfilled for the user. Arguments: user_id (int): ID of User for which evaluation should occur course (CourseModule): The course prereq_content_key (UsageKey): The prerequisite content usage key Returns: None """ xblock = modulestore().get_item(prereq_content_key) sequential = _get_xblock_parent(xblock, 'sequential') if sequential: prereq_milestone = gating_api.get_gating_milestone( course.id, sequential.location.for_branch(None), 'fulfills' ) if prereq_milestone: gated_content_milestones = defaultdict(list) for milestone in gating_api.find_gating_milestones(course.id, None, 'requires'): gated_content_milestones[milestone['id']].append(milestone) gated_content = gated_content_milestones.get(prereq_milestone['id']) if gated_content: user = User.objects.get(id=user_id) score = get_module_score(user, course, sequential) * 100 for milestone in gated_content: # Default minimum score to 100 min_score = 100 requirements = milestone.get('requirements') if requirements: try: min_score = int(requirements.get('min_score')) except (ValueError, TypeError): log.warning( 'Failed to find minimum score for gating milestone %s, defaulting to 100', json.dumps(milestone) ) if score >= min_score: milestones_helpers.add_user_milestone({'id': user_id}, prereq_milestone) else: milestones_helpers.remove_user_milestone({'id': user_id}, prereq_milestone)
def evaluate_prerequisite(course, user, subsection_usage_key, new_score): """ Finds the parent subsection of the content in the course and evaluates any milestone relationships attached to that subsection. If the calculated grade of the prerequisite subsection meets the minimum score required by dependent subsections, the related milestone will be fulfilled for the user. Arguments: user (User): User for which evaluation should occur course (CourseModule): The course subsection_usage_key (UsageKey): Usage key of the updated subsection new_score (float): New score of the given subsection, in percentage. Returns: None """ prereq_milestone = gating_api.get_gating_milestone( course.id, subsection_usage_key, 'fulfills' ) if prereq_milestone: gated_content_milestones = defaultdict(list) for milestone in gating_api.find_gating_milestones(course.id, None, 'requires'): gated_content_milestones[milestone['id']].append(milestone) gated_content = gated_content_milestones.get(prereq_milestone['id']) if gated_content: for milestone in gated_content: # Default minimum score to 100 min_score = 100.0 requirements = milestone.get('requirements') if requirements: try: min_score = float(requirements.get('min_score')) except (ValueError, TypeError): log.warning( 'Failed to find minimum score for gating milestone %s, defaulting to 100', json.dumps(milestone) ) if new_score >= min_score: milestones_helpers.add_user_milestone({'id': user.id}, prereq_milestone) else: milestones_helpers.remove_user_milestone({'id': user.id}, prereq_milestone)
def evaluate_prerequisite(course, user, subsection_usage_key, new_score): """ Finds the parent subsection of the content in the course and evaluates any milestone relationships attached to that subsection. If the calculated grade of the prerequisite subsection meets the minimum score required by dependent subsections, the related milestone will be fulfilled for the user. Arguments: user (User): User for which evaluation should occur course (CourseModule): The course subsection_usage_key (UsageKey): Usage key of the updated subsection new_score (float): New score of the given subsection, in percentage. Returns: None """ prereq_milestone = gating_api.get_gating_milestone(course.id, subsection_usage_key, 'fulfills') if prereq_milestone: gated_content_milestones = defaultdict(list) for milestone in gating_api.find_gating_milestones( course.id, None, 'requires'): gated_content_milestones[milestone['id']].append(milestone) gated_content = gated_content_milestones.get(prereq_milestone['id']) if gated_content: for milestone in gated_content: # Default minimum score to 100 min_score = 100.0 requirements = milestone.get('requirements') if requirements: try: min_score = float(requirements.get('min_score')) except (ValueError, TypeError): log.warning( 'Failed to find minimum score for gating milestone %s, defaulting to 100', json.dumps(milestone)) if new_score >= min_score: milestones_helpers.add_user_milestone({'id': user.id}, prereq_milestone) else: milestones_helpers.remove_user_milestone({'id': user.id}, prereq_milestone)
def evaluate_prerequisite(course, user, subsection_usage_key, new_score): """ Finds the parent subsection of the content in the course and evaluates any milestone relationships attached to that subsection. If the calculated grade of the prerequisite subsection meets the minimum score required by dependent subsections, the related milestone will be fulfilled for the user. Arguments: course (CourseModule): The course prereq_content_key (UsageKey): The prerequisite content usage key user_id (int): ID of User for which evaluation should occur Returns: None """ prereq_milestone = gating_api.get_gating_milestone(course.id, subsection_usage_key, 'fulfills') if prereq_milestone: gated_content_milestones = defaultdict(list) for milestone in gating_api.find_gating_milestones( course.id, None, 'requires'): gated_content_milestones[milestone['id']].append(milestone) gated_content = gated_content_milestones.get(prereq_milestone['id']) if gated_content: for milestone in gated_content: # Default minimum score to 100 min_score = 100 requirements = milestone.get('requirements') if requirements: try: min_score = int(requirements.get('min_score')) except (ValueError, TypeError): # Use default value of 100 pass if new_score >= min_score: milestones_helpers.add_user_milestone({'id': user.id}, prereq_milestone) else: milestones_helpers.remove_user_milestone({'id': user.id}, prereq_milestone)
def update_milestone(milestone, subsection_grade, prereq_milestone, user_id): """ Updates the milestone record based on evaluation of prerequisite met. Arguments: milestone: The gated milestone being evaluated subsection_grade: The grade of the prerequisite subsection prerequisite_milestone: The gating milestone user_id: The id of the user Returns: True if prerequisite has been met, False if not """ min_percentage = _get_minimum_required_percentage(milestone) subsection_percentage = _get_subsection_percentage(subsection_grade) if subsection_percentage >= min_percentage: milestones_helpers.add_user_milestone({'id': user_id}, prereq_milestone) return True else: milestones_helpers.remove_user_milestone({'id': user_id}, prereq_milestone) return False
def evaluate_prerequisite(course, subsection_grade, user): """ Evaluates any gating milestone relationships attached to the given subsection. If the subsection_grade meets the minimum score required by dependent subsections, the related milestone will be marked fulfilled for the user. """ prereq_milestone = gating_api.get_gating_milestone(course.id, subsection_grade.location, 'fulfills') if prereq_milestone: gated_content_milestones = defaultdict(list) for milestone in gating_api.find_gating_milestones(course.id, content_key=None, relationship='requires'): gated_content_milestones[milestone['id']].append(milestone) gated_content = gated_content_milestones.get(prereq_milestone['id']) if gated_content: for milestone in gated_content: min_percentage = _get_minimum_required_percentage(milestone) subsection_percentage = _get_subsection_percentage(subsection_grade) if subsection_percentage >= min_percentage: milestones_helpers.add_user_milestone({'id': user.id}, prereq_milestone) else: milestones_helpers.remove_user_milestone({'id': user.id}, prereq_milestone)
def update_milestone(milestone, usage_key, prereq_milestone, user, grade_percentage=None, completion_percentage=None): """ Updates the milestone record based on evaluation of prerequisite met. Arguments: milestone: The gated milestone being evaluated usage_key: Usage key of the prerequisite subsection prereq_milestone: The gating milestone user: The user who has fulfilled milestone grade_percentage: Grade percentage of prerequisite subsection completion_percentage: Completion percentage of prerequisite subsection Returns: True if prerequisite has been met, False if not """ min_score, min_completion = _get_minimum_required_percentage(milestone) if not grade_percentage: grade_percentage = get_subsection_grade_percentage( usage_key, user) if min_score > 0 else 0 if not completion_percentage: completion_percentage = get_subsection_completion_percentage( usage_key, user) if min_completion > 0 else 0 if grade_percentage >= min_score and completion_percentage >= min_completion: milestones_helpers.add_user_milestone({'id': user.id}, prereq_milestone) return True else: milestones_helpers.remove_user_milestone({'id': user.id}, prereq_milestone) return False