'Updates an enrollment for a user in a class.  This includes options
like changing the mode, toggling is_active True/False, etc.
Also emits relevant events for analytics purposes.
This saves immediately.'

def update_enrollment(self, mode=None, is_active=None, skip_refund=False):

    activation_changed = False
    if ((self.is_active != is_active) and (is_active is not None)):
        self.is_active = is_active
        activation_changed = True
    mode_changed = False
    if ((self.mode != mode) and (mode is not None)):
        self.mode = mode
        mode_changed = True
    if (activation_changed or mode_changed):
        self.save()
        self._update_enrollment_in_request_cache(self.user, self.course_id, CourseEnrollmentState(self.mode, self.is_active))
    if activation_changed:
        if self.is_active:
            self.emit_event(EVENT_NAME_ENROLLMENT_ACTIVATED)
            dog_stats_api.increment('common.student.enrollment', tags=[u'org:{}'.format(self.course_id.org), u'offering:{}'.format(self.course_id.offering), u'mode:{}'.format(self.mode)])
        else:
            UNENROLL_DONE.send(sender=None, course_enrollment=self, skip_refund=skip_refund)
            self.emit_event(EVENT_NAME_ENROLLMENT_DEACTIVATED)
            self.send_signal(EnrollStatusChange.unenroll)
            dog_stats_api.increment('common.student.unenrollment', tags=[u'org:{}'.format(self.course_id.org), u'offering:{}'.format(self.course_id.offering), u'mode:{}'.format(self.mode)])
    if mode_changed:
        self.emit_event(EVENT_NAME_ENROLLMENT_MODE_CHANGED)
