1. I only noticed that because $user->student(1) (the site "course") is already set when get_student_courses is called, and when I first fiddled with the function, I was deleting that and just setting my new courses. If, instead I appended the new courses, then the problem didn't occur. I'll take your word for it though but if the function returns with $user->student(1) set then the problem doesn't occur. I wasn't sure where else that is referenced, but thought it best that get_student_courses didn't stomp on it especially as it was just "freshly" set (in datalib.php in function get_user_info_from_db just before get_student_courses is called)
2. About my query ... sorry, I should have explained that better. The query is fine, just we support groups within a course in our external database, so have to query 2 tables (courses to groups, groups to users) to get the course to user mapping. So it's just our special case.