def get_milestones(namespace): """ Client API operation adapter/wrapper """ if not ENABLE_MILESTONES_APP.is_enabled(): return [] return milestones_api.get_milestones(namespace)
def _get_prerequisite_milestone(prereq_content_key): """ Get gating milestone associated with the given content usage key. Arguments: prereq_content_key (str|UsageKey): The content usage key Returns: dict: Milestone dict """ milestones = milestones_api.get_milestones("{usage_key}{qualifier}".format( usage_key=prereq_content_key, qualifier=GATING_NAMESPACE_QUALIFIER)) if not milestones: log.warning("Could not find gating milestone for prereq UsageKey %s", prereq_content_key) return None if len(milestones) > 1: # We should only ever have one gating milestone per UsageKey # Log a warning here and pick the first one log.warning("Multiple gating milestones found for prereq UsageKey %s", prereq_content_key) return milestones[0]
def _get_prerequisite_milestone(prereq_content_key): """ Get gating milestone associated with the given content usage key. Arguments: prereq_content_key (str|UsageKey): The content usage key Returns: dict: Milestone dict """ milestones = milestones_api.get_milestones("{usage_key}{qualifier}".format( usage_key=prereq_content_key, qualifier=GATING_NAMESPACE_QUALIFIER )) if not milestones: log.warning("Could not find gating milestone for prereq UsageKey %s", prereq_content_key) return None if len(milestones) > 1: # We should only ever have one gating milestone per UsageKey # Log a warning here and pick the first one log.warning("Multiple gating milestones found for prereq UsageKey %s", prereq_content_key) return milestones[0]
def get_milestones(namespace): """ Client API operation adapter/wrapper """ if not settings.FEATURES.get('MILESTONES_APP'): return [] return milestones_api.get_milestones(namespace)
def get_milestones(namespace): """ Client API operation adapter/wrapper """ if not settings.FEATURES.get('MILESTONES_APP', False): return [] from milestones import api as milestones_api return milestones_api.get_milestones(namespace)
def _create_entrance_exam(request, course_key, entrance_exam_minimum_score_pct=None): """ Internal workflow operation to create an entrance exam """ # Provide a default value for the minimum score percent if nothing specified if entrance_exam_minimum_score_pct is None: entrance_exam_minimum_score_pct = float(settings.ENTRANCE_EXAM_MIN_SCORE_PCT) # Confirm the course exists course = modulestore().get_course(course_key) if course is None: return HttpResponse(status=400) # Create the entrance exam item (currently it's just a chapter) payload = { "category": "chapter", "display_name": "Entrance Exam", "parent_locator": unicode(course.location), "is_entrance_exam": True, "in_entrance_exam": True, } factory = RequestFactory() internal_request = factory.post("/", json.dumps(payload), content_type="application/json") internal_request.user = request.user created_item = json.loads(create_item(internal_request).content) # Set the entrance exam metadata flags for this course # Reload the course so we don't overwrite the new child reference course = modulestore().get_course(course_key) metadata = { "entrance_exam_enabled": True, "entrance_exam_minimum_score_pct": entrance_exam_minimum_score_pct / 100, "entrance_exam_id": created_item["locator"], } CourseMetadata.update_from_dict(metadata, course, request.user) # Add an entrance exam milestone if one does not already exist milestone_namespace = generate_milestone_namespace(NAMESPACE_CHOICES["ENTRANCE_EXAM"], course_key) milestones = milestones_api.get_milestones(milestone_namespace) if len(milestones): milestone = milestones[0] else: description = "Autogenerated during {} entrance exam creation.".format(unicode(course.id)) milestone = milestones_api.add_milestone( {"name": "Completed Course Entrance Exam", "namespace": milestone_namespace, "description": description} ) relationship_types = milestones_api.get_milestone_relationship_types() milestones_api.add_course_milestone(unicode(course.id), relationship_types["REQUIRES"], milestone) milestones_api.add_course_content_milestone( unicode(course.id), created_item["locator"], relationship_types["FULFILLS"], milestone ) return HttpResponse(status=201)
def remove_prerequisite(prereq_content_key): """ Removes the Milestone and CourseContentMilestones related to the gating prerequisite which the given course content fulfills Arguments: prereq_content_key (str|UsageKey): The prerequisite content usage key Returns: None """ milestones = milestones_api.get_milestones("{usage_key}{qualifier}".format( usage_key=prereq_content_key, qualifier=GATING_NAMESPACE_QUALIFIER)) for milestone in milestones: milestones_api.remove_milestone(milestone.get('id'))
def remove_prerequisite(prereq_content_key): """ Removes the Milestone and CourseContentMilestones related to the gating prerequisite which the given course content fulfills Arguments: prereq_content_key (str|UsageKey): The prerequisite content usage key Returns: None """ milestones = milestones_api.get_milestones("{usage_key}{qualifier}".format( usage_key=prereq_content_key, qualifier=GATING_NAMESPACE_QUALIFIER )) for milestone in milestones: milestones_api.remove_milestone(milestone.get('id'))
def test_get_milestones(self): """ Unit Test: test_get_milestones """ namespace = 'test_get_milestones' api.add_milestone({ 'display_name': 'Local Milestone 1', 'name': 'local_milestone_1', 'namespace': namespace, 'description': 'Local Milestone 1 Description' }) api.add_milestone({ 'display_name': 'Local Milestone 2', 'name': 'local_milestone_2', 'namespace': namespace, 'description': 'Local Milestone 2 Description' }) with self.assertNumQueries(1): milestones = api.get_milestones(namespace=namespace) self.assertEqual(len(milestones), 2)
def _create_entrance_exam(request, course_key, entrance_exam_minimum_score_pct=None): """ Internal workflow operation to create an entrance exam """ # Provide a default value for the minimum score percent if nothing specified if entrance_exam_minimum_score_pct is None: entrance_exam_minimum_score_pct = float( settings.ENTRANCE_EXAM_MIN_SCORE_PCT) # Confirm the course exists course = modulestore().get_course(course_key) if course is None: return HttpResponse(status=400) # Create the entrance exam item (currently it's just a chapter) payload = { 'category': "chapter", 'display_name': "Entrance Exam", 'parent_locator': unicode(course.location), 'is_entrance_exam': True, 'in_entrance_exam': True, } factory = RequestFactory() internal_request = factory.post('/', json.dumps(payload), content_type="application/json") internal_request.user = request.user created_item = json.loads(create_item(internal_request).content) # Set the entrance exam metadata flags for this course # Reload the course so we don't overwrite the new child reference course = modulestore().get_course(course_key) metadata = { 'entrance_exam_enabled': True, 'entrance_exam_minimum_score_pct': entrance_exam_minimum_score_pct / 100, 'entrance_exam_id': created_item['locator'], } CourseMetadata.update_from_dict(metadata, course, request.user) # Add an entrance exam milestone if one does not already exist milestone_namespace = generate_milestone_namespace( NAMESPACE_CHOICES['ENTRANCE_EXAM'], course_key) milestones = milestones_api.get_milestones(milestone_namespace) if len(milestones): milestone = milestones[0] else: description = 'Autogenerated during {} entrance exam creation.'.format( unicode(course.id)) milestone = milestones_api.add_milestone({ 'name': 'Completed Course Entrance Exam', 'namespace': milestone_namespace, 'description': description }) relationship_types = milestones_api.get_milestone_relationship_types() milestones_api.add_course_milestone(unicode(course.id), relationship_types['REQUIRES'], milestone) milestones_api.add_course_content_milestone(unicode(course.id), created_item['locator'], relationship_types['FULFILLS'], milestone) return HttpResponse(status=201)
def _create_entrance_exam(request, course_key, entrance_exam_minimum_score_pct=None): """ Internal workflow operation to create an entrance exam """ # Provide a default value for the minimum score percent if nothing specified if entrance_exam_minimum_score_pct is None: entrance_exam_minimum_score_pct = _get_default_entrance_exam_minimum_pct() # Confirm the course exists course = modulestore().get_course(course_key) if course is None: return HttpResponse(status=400) # Create the entrance exam item (currently it's just a chapter) payload = { 'category': "chapter", 'display_name': "Entrance Exam", 'parent_locator': unicode(course.location), 'is_entrance_exam': True, 'in_entrance_exam': True, } parent_locator = unicode(course.location) created_block = create_xblock( parent_locator=parent_locator, user=request.user, category='chapter', display_name='Entrance Exam', is_entrance_exam=True ) # Set the entrance exam metadata flags for this course # Reload the course so we don't overwrite the new child reference course = modulestore().get_course(course_key) metadata = { 'entrance_exam_enabled': True, 'entrance_exam_minimum_score_pct': unicode(entrance_exam_minimum_score_pct), 'entrance_exam_id': unicode(created_block.location), } CourseMetadata.update_from_dict(metadata, course, request.user) # Create the entrance exam section item. create_xblock( parent_locator=unicode(created_block.location), user=request.user, category='sequential', display_name=_('Entrance Exam - Subsection') ) # Add an entrance exam milestone if one does not already exist milestone_namespace = generate_milestone_namespace( NAMESPACE_CHOICES['ENTRANCE_EXAM'], course_key ) milestones = milestones_api.get_milestones(milestone_namespace) if len(milestones): milestone = milestones[0] else: description = 'Autogenerated during {} entrance exam creation.'.format(unicode(course.id)) milestone = milestones_api.add_milestone({ 'name': 'Completed Course Entrance Exam', 'namespace': milestone_namespace, 'description': description }) relationship_types = milestones_api.get_milestone_relationship_types() milestones_api.add_course_milestone( unicode(course.id), relationship_types['REQUIRES'], milestone ) milestones_api.add_course_content_milestone( unicode(course.id), unicode(created_block.location), relationship_types['FULFILLS'], milestone ) return HttpResponse(status=201)