Just had a thought after posting this: Crafton, by any chance does your external database have just one course, which maps to the moodle course #1 (the "site" course)? That might explain the differences we're seeing.Well, the editing profile problem was related.
But I'm still not sure why your $user->students is getting populated and mine not. You mentioned enrol_student(), but that just saves to the database. Anyway, it's called from within the foreach loop and my problem is there no "loop" because its an array of one. The only place I can see it being populated is in datalib.php: line 957 "get_user_info_from_db" ,
$user->student[$site->id] = isstudent($site->id, $user->id);
If it wasn't working for you, I'd say it was a bug, but as it is, I'm stumped.
My problem with the editing profile problem was that courses/enrol.php looks at $user->students and checks it contains course 1 (the site course). If that's not there, then the error message. Problem was in get_student_courses (even after I'd populated the $user->students array) I was unseting it in the if statement. So I added an extra check:
if ($courseid != 1){
if (! $rs->RecordCount()) {
unenrol_student($user->id, $courseid);
unset ($user->student[$courseid]);
}
else{
enrol_student($user->id, $courseid);
}
}
Don't like to hard code a 1 in though. You aren't seeing this? That's odd. Or do you have a course in your external database that corresponds to moodle course number 1?
Problem #4
The other problem I found was that $CFG->prefix (which gets the database name prepended early on in the piece) should be restored to its original before leaving the function. I came across a couple of sql errors (the didn't seem to cause any harm, but ....) due to some other routine also prepending the database name to it before making a call, so you get something like SELECT * FROM moodle.moodle.courses ....
SAVE: add to beginning of routine: $cfgprefix = $CFG->prefix;
RESTORE: add to end: $CFG->prefix = $cfgprefix;
By the way, did you submit the problems to the bug database? I've never done that, but I suppose we'd better .....