enrol_database_sync.php from command line

enrol_database_sync.php from command line

by Mike Chaplin -
Number of replies: 4
This script seems to run perfectly when a user logs in. The users shows up in the course students' list in 'Assign roles' and has the correct My Courses list.

However, running the script from the comand line or CRON only seems to remove enrolments, even if that enrolment is still present in the external database. I only ever see the message:

"Unassigned role 5 for user 123456 in context 456789"

repeated hundreds of times (1 per course?). The students don't show up in the students' list in 'Assign roles' and in fact disappear from the list if run after the user has logged in.

Has anyone come across this? I am running 1.8 on MAC OS X.

The reason I am trying to get this to work is so that we can cron the script and then check who has been enrolled without waiting for students to login.

Any suggestions appreciated.

Best regards

Mike Chaplin
VLE Administrator MANCAT


Average of ratings: -
In reply to Mike Chaplin

Re: enrol_database_sync.php from command line

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Has anyone come across this?

Certainly not me. Judging from the message you quote, it seems Moodle thinks some (most? all?) of its courses are no longer available in the external enrolment database, so it simply unenrols everybody from those courses.

I don't know why this is happening, but adding a few debugging statements to the code might shed some light. Could you add a line like this:

var_dump ($extcourses);

just before the lines that read (arount line 367 in Moodle 1.8.4+)

$sql = " SELECT ra.roleid, ra.userid, ra.contextid FROM {$CFG->prefix}role_assignments ra ...

and run the script to see what might be going on?

The reason I am trying to get this to work is so that we can cron the script and then check who has been enrolled without waiting for students to login.

Make sure all the user accounts exist before running this script. Otherwise Moodle won't be able to enrol them, as they don't exist yet. User creation and user enrolments are two different processes in Moodle, and the latter depends on the former.

Saludos. Iñaki.

In reply to Iñaki Arenaza

Re: enrol_database_sync.php from command line

by Mike Chaplin -
Hi Iñaki

Thanks for the response on this.

The user definitely exists. If I login as admin (session 1), I can monitor his account profile.

If I login as the user from a different browser (session 2), I can watch the course appear in the user's profile from the admin session (session 1).

If I then connect to the server via SSH and run the enrol_database_sync.php script from there, the course disappears from the user's profile (session 1).

The debug statement gives these extra few lines in bold:

=== Syncing enrolments for role: student ===
course CR3002-43
array(1) {
[0]=>
string(9) "CR3002-43"
}
Unassigned role 5 from user 39751 in context 321
Unassigned role 5 from user 39751 in context 321 etc etc

BTW I am creating the accounts with the User Account Creation Database Sync module:
http://moodle.org/mod/data/view.php?d=13&rid=406

That appears to be successful - I can login with the details of any user in the external database I have setup.

I am going to try to insert more statement in the enrol.php script - perhaps there is a fundmental problem with running the enrol_database_sync.php script from SSH - after all the user isn't logged into Moodle there. Perhaps it's missing some info...?

Best regards

Mike



In reply to Mike Chaplin

Re: enrol_database_sync.php from command line

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

perhaps there is a fundmental problem with running the enrol_database_sync.php script from SSH - after all the user isn't logged into Moodle there. Perhaps it's missing some info...?

I don't think so. The script doesn't need the user to be logged in to work. It just needs that the user account already exists by the time you run it. And it does in your case.

Could you add the following line before:

var_dump ($sql);

before the line that reads (at around line 379):

$ers = $db->Execute($sql);

Also, if you execute the SQL query printed with the new var_dump() line directly on your Moodle database, what do you get?

Saludos. Iñaki.

In reply to Mike Chaplin

Re: enrol_database_sync.php from command line

by Mike Chaplin -
I've got the solution now!

When a user logs in, the function setup_enrolments is used.
When the sync script is run, it's function sync_enrolments instead.

There is also a problem with the latter: see

http://tracker.moodle.org/browse/MDL-8550

The fix for the SQL statement works so that not all enrolments are removed.

Thanks again for your time Iñaki.

Mike