External database enrolment problem - thanks Crafton

External database enrolment problem - thanks Crafton

by James M -
Number of replies: 9

I incorrectly posted an enrollments issue in the authentication forum. blush http://moodle.org/mod/forum/discuss.php?d=12664. It was to do with enrol.php and a couple of changes.

Thanks Crafton for replying. I looked at your code and you seem to have fixed problems 2 and 3 that I saw. I still can't work out how the $user->students array is populated correctly with the course ids (that was my problem #1). I'll have a look at it tomorrow. Maybe that's the cause of another problem I'm seeing which is even though the student gets automatically enrolled into the correct courses, when he tries to edit his profile (by clicking on username in the top right of the homepage) he gets an error message saying he has to be enrolled in a course to do this. Oddly, he can edit his profile if he goes in while inside a course. Hopefully sort that out tomorrow too.

Average of ratings: -
In reply to James M

Re: External database enrolment problem - thanks Crafton

by Crafton Williams -
No prob James.

If i'm not mistaken, the $user->students array is populated in enrol_students(). Secondly, I don't seem to have the editing profile problem. Students can edit their profile whether from the main page or within the course, as long as they'e been enroled.

I'll take another look at your code and see if I can pick up anything.
In reply to Crafton Williams

Re: External database enrolment problem - thanks Crafton

by James M -
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 ....mixed) 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 .....

In reply to James M

Re: External database enrolment problem - thanks Crafton

by Crafton Williams -
No not at all, I don't have a course in the external database corresponding to the site course#1. The query I use only cycles through courses within the site.

Actually, I didn't use $user->student to cycle through the array, I used a query to get all the courses registered on the site($coursesavail).

I actually did submit the problem to bug report and my solution, but i haven't gotten any feedback as to how optimal it is.




In reply to Crafton Williams

Re: External database enrolment problem - thanks Crafton

by James M -

Sorry, I wasn't looking at your code correctly. Not sure what I was looking at ...  Anyway, I tried your code out and worked out why I was seeing differences with mine.

I did have to change the query get_records_sql("SELECT shortname, id FROM mdl_course WHERE 1") because you're using shortname and I'm using another field (so I used $CFG->enrol_localcoursefield).

Then everything worked. The user profile editing issue I was seeing doesn't happen with your code because in the unenroll bit:
     unenrol_student($user->id, $course->id);
     unset ($user->student[$courseid]);
$courseid now isn't being used, doesn't hold anything, so nothing gets unset. If you change that to $user->student[$course->id] then you see the same problem I saw.

I suppose the main difference between your code and mine is whether or not $user->student array is loaded with the students valide courses (including 1 for site) when the function exits. Not sure how important this is, as not sure where that information is later used (besides the edit profile page), but probably safest to have it in. 

Thanks for your help with this. I'm feeling a lot better about what's happening, and everything's working too.

In reply to James M

Re: External database enrolment problem - thanks Crafton

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Can someone attach a final working copy?   As you've found out the original code was never actually tested.
In reply to Martin Dougiamas

Re: External database enrolment problem - thanks Crafton

by Crafton Williams -
This is what worked for me..
In reply to Crafton Williams

Re: External database enrolment problem - thanks Crafton

by C R -

I get an error on line 95 of the enrol.php(unkown function or object) when I login.  It appears to be the recordcount function.  I'm using Microsoft SQL Server or mssql.  Anyone know why it can't find the Recordcount function?

Thanks

In reply to Martin Dougiamas

Re: External database enrolment problem - thanks Crafton

by Tim Allen -

Hi Martin,

Just wondering whether this enrolment issue has been attended to yet with code such as has been posted in this thread.   My bug report was http://moodle.org/bugs/bug.php?op=show&bugid=1960&pos=2

When I tried the code attached by James M above it worked.  It would be great to have this integrated into moodle, because my students find using an enrolment key in order to enrol in a course, despite my painstaking directions, difficult and complicated.

There's no hurry btw, just wondering what the state of play is.  smile