def enroll(self, course_run_id, mode, cohort=None):
enrollment_api_client = EnrollmentApiClient()<EOL>course_enrollment = enrollment_api_client.get_course_enrollment(self.username, course_run_id) or {}<EOL>enrolled_in_course = course_enrollment and course_enrollment.get('<STR_LIT>', False)<EOL>audit_modes = getattr(settings, '<STR_LIT>', ['<STR_LIT>', '<STR_LIT>'])<EOL>paid_modes = ['<STR_LIT>', '<STR_LIT>']<EOL>is_upgrading = mode in paid_modes and course_enrollment.get('<STR_LIT>') in audit_modes<EOL>if not enrolled_in_course or is_upgrading:<EOL><INDENT>if cohort and not self.enterprise_customer.enable_autocohorting:<EOL><INDENT>raise CourseEnrollmentPermissionError("<STR_LIT>")<EOL><DEDENT>enrollment_api_client.enroll_user_in_course(self.username, course_run_id, mode, cohort=cohort)<EOL>utils.track_event(self.user_id, '<STR_LIT>', {<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT:label>': course_run_id,<EOL>'<STR_LIT>': str(self.enterprise_customer.uuid),<EOL>'<STR_LIT>': self.enterprise_customer.name,<EOL>'<STR_LIT>': mode,<EOL>'<STR_LIT>': cohort,<EOL>'<STR_LIT>': is_upgrading,<EOL>})<EOL>EnterpriseCourseEnrollment.objects.get_or_create(<EOL>enterprise_customer_user=self,<EOL>course_id=course_run_id<EOL>)<EOL><DEDENT>elif enrolled_in_course and course_enrollment.get('<STR_LIT>') in paid_modes and mode in audit_modes:<EOL><INDENT>raise CourseEnrollmentDowngradeError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(<EOL>course_run_id=course_run_id,<EOL>current_mode=course_enrollment.get('<STR_LIT>'),<EOL>given_mode=mode,<EOL>)<EOL>)<EOL><DEDENT>
Enroll a user into a course track, and register an enterprise course enrollment.