'Change the enrollment mode of users for a course.
Arguments:
course_key (CourseKey): to lookup the course.
from_mode (str): the enrollment mode to change.
to_mode (str): the enrollment mode to change to.
commit (bool): required to make the change to the database. Otherwise
just a count will be displayed.'

def move_users_for_course(self, course_key, from_mode, to_mode, commit):

    unicode_course_key = unicode(course_key)
    if (CourseMode.mode_for_course(course_key, to_mode) is None):
        logger.info('Mode    ({})    does    not    exist    for    course    ({}).'.format(to_mode, unicode_course_key))
        return
    course_enrollments = CourseEnrollment.objects.filter(course_id=course_key, mode=from_mode)
    logger.info('Moving    %d    users    from    %s    to    %s    in    course    %s.', course_enrollments.count(), from_mode, to_mode, unicode_course_key)
    if commit:
        for enrollment in course_enrollments:
            with transaction.atomic():
                enrollment.change_mode(mode=to_mode)
        logger.info('Finished    moving    users    from    %s    to    %s    in    course    %s.', from_mode, to_mode, unicode_course_key)
