The user with id '0' assigned role.... how to track it down?

The user with id '0' assigned role.... how to track it down?

by Sali Kaceli -
Number of replies: 3

We use the external database enrollment option [via ODBC] in Moodle to synchronize the enrollments in the courses. The "Synchronise external database enrolments task" is scheduled to run at 4am daily but we are finding that the enrollments are also being updated at other random times throughout the day. 

Looking at the logs, the task description shows "The user with id '0' assigned the role with....". My question is, what other task would call on the synchronization. How do we find out which task is causing this? 

The 'synchronise external database task' log displays only the instances as scheduled for 4am. 

Environment: 

Moodle 3.11.2 LAMP


A little more detail: 

It looks like the role update is happening when the user logs in. The origin in the logs is listed 'web' and the ip address is the client ip. 

Is there a setting somewhere that checks the user's enrollment against the external database? My assumption is that when a user logs in, they simply access courses assigned locally in Moodle and that the enrollment is a separate process taking place as pre-determined. 


Average of ratings: -
In reply to Sali Kaceli

Re: The user with id '0' assigned role.... how to track it down?

by Randy Thornton -
Picture of Documentation writers
It's both, actually, the Moodle version of belt and suspenders. There's a "full sync" that does everything, run by the scheduled task, and "user sync" which just checks the enrolments.

The scheduled task is a "full sync" does check things such as, whether the courses exist, creates them if needed, will add the external enrolment method to the courses as required (you won't see it in the interface but it puts in the database) and do a full sync of all the enrolments.

On the other hand, the enrolment is checked when the user logs in, every time: if they need to be enroled in any new course, they are, but also they may need to be unenroled too if they are missing from the external table for a course there were enroled in. If there is nothing new to do, then user goes on their way to their existing courses.

This is by design, mostly for performance reasons. By default, the "full sync" scheduled task runs only once a day. So, doing both processes means users will not have to wait the full 24 hours for enrolments to happen.

In addition, the enrolments is called also during the restore of any courses: so if you restore a course including user data that has these enrolments in it, the restore process will call the sync_user_enrolments function to populate the course at that time as well.

So you should see two clusters of role assignments/enrolments in the logs: the scheduled task and course restores. And you should also see random ones if users log in during the day between full sync and there are any enrolment changes to process for them.
Average of ratings: Useful (1)
In reply to Randy Thornton

Re: The user with id '0' assigned role.... how to track it down?

by Sali Kaceli -
Thank you Randy,

This is very helpful.

Is there a way to deactivate the course enrollment update when the user logs in? In our case we want to update the enrollments only once a day.
In reply to Sali Kaceli

Re: The user with id '0' assigned role.... how to track it down?

by Randy Thornton -
Picture of Documentation writers
Not that I know of, at least without changing some of the code.