Some time ago I posted the code I wrote that automatically uploads users each night into our Moodle. The file is VERY similar to the one needed for a manual upload. This is just that upload code, modified slightly.
I have an automated task that creates the csv file each night. The PHP runs to import all of the scheduling changes each night. This ensures any changes made by guidance/scheduling are in the system the next day.
1. The course must already exist, groups will be created dynamically.
2. Existing enrollments will not change (although user information will.)
3. No one is disenrolled if they are not in the new file. Teachers must disenroll manually.
I have been messaged by several users asking for updated code. This works on 3.8, 3.9. I have not tested on newer versions as we are LTS.