MDL-41508 move_courses() bug fix and DB query improvement