Exemplo n.º 1
0
def unlink_program_enrollment(program_enrollment):
    """
    Unlinks CourseEnrollments from the ProgramEnrollment by doing the following for
    each ProgramCourseEnrollment associated with the Program Enrollment.
        1. unenrolling the corresponding user from the course
        2. moving the user into the audit track, if the track exists
        3. removing the link between the ProgramCourseEnrollment and the CourseEnrollment

    Arguments:
        program_enrollment: the ProgramEnrollment object
    """
    program_course_enrollments = program_enrollment.program_course_enrollments.all()

    for pce in program_course_enrollments:
        course_key = pce.course_enrollment.course.id
        modes = CourseMode.modes_for_course_dict(course_key)

        update_enrollment_kwargs = {
            'is_active': False,
            'skip_refund': True,
        }

        if CourseMode.contains_audit_mode(modes):
            # if the course contains an audit mode, move the
            # learner's enrollment into the audit mode
            update_enrollment_kwargs['mode'] = 'audit'

        # deactive the learner's course enrollment and move them into the
        # audit track, if it exists
        pce.course_enrollment.update_enrollment(**update_enrollment_kwargs)

        # sever ties to the user from the ProgramCourseEnrollment
        pce.course_enrollment = None
        pce.save()

    program_enrollment.user = None
    program_enrollment.save()