External database


目录

Using an external database to control enrolments

You may use an external database (of nearly any kind) to control your enrolments. It is assumed your external database contains a field containing a course ID, a field containing a user ID, and optionally a field containing a role. These are compared against fields that you choose in the local course, user tables, and role tables.

The following are the supported data sources, but note that you will need to have to compiled PHP with the appropriate options or through ODBC.

Enrolment and unenrolment

External database enrolment happens at the moment when a user logs into Moodle. The plugin will attempt to automatically enrol the student in all their courses according to the data in the external database and, optionally, create empty courses where they do not already exist. To check if it is working, you can log in as a student and then check that their list of courses is as you would expect.

The process also unenrols users from courses if they are no longer in the database. User records are marked according to their original enrolment method. Therefore, the external database plugin can only unenrol users who were enroled by the plugin in the first place.

Hidden courses

Courses that are set to "Course is not available to students" can be ignored for enrolment purposes by setting the "enrol_db_ignorehiddencourse" to yes.

Enrolment and roles

The "enrol_database | defaultrole" setting in the plugin settings page specifies the role that the user will take when they are added to the course. The default setting will set them to the course default setting (initially "student"). However, you can specify a field in the external table (specified in the "enrol_database | remoterolefield" setting) that contains the short name or id for the user's role. This could, for example, be used to enrol both students and teachers into courses using a suitably configured database.

Unenrolment

The External unenrol action ("enrol_database | unenrolaction") setting in the plugin settings page defines what action should be taken when a user enrolment disappears from external enrolment source. Each setting does the following:

  1. "Unenrol user from course" When the user disappears from the external source, the enrolment is completely removed and all the roles removed. This means some user data and settings are purged from course during course unenrolment (that usually include grades, activity attempts, etc.)
  2. "Keep user enrolled" When the user disappears from the external source, the enrolment is kept as is, and the user is still able to enter the course and perform activities, access resources, etc. It's a "do nothing" option.
  3. "Disable course enrolment" When the user disappears from the external source, user enrolment is suspended (the user can't access the course, but user data and settings are kept), and roles are kept as is. You might use this because in some cases the user needs a role with some capability to be visible in UI - such has in gradebook, assignments, etc.
  4. "Disable course enrolment and remove roles" When the user disappears from the external source, the enrolment is suspended and roles assigned by enrol instance are removed. Please note that user may "disappear" from gradebook and other areas.

Creating courses

Optionally courses that do not exist in the Moodle site can be created.

You can additionally specify the Category into which the new course will be placed, in the New course category id field. The data in this field must be the id of a currently existing category; it will not create a new category. The id number is number assigned by Moodle in the database when the category is created (e.g. mdl_course_categories.id).

Default new course category is the category to which courses will be assigned and created in, unless you set up and so indicate in the data field of the "New course category id field."

You may also specify a New course template: a "template" course from which the new course will be copied. The data for this field should be the shortname of the template course.

In Moodle 4.5 onwards, you can set a new course start and end date in Site administration > Plugins > External database. If dates are not set, the course start date will be set to the current date and the course end date will be set according to the default course duration.

Synchronising external database enrolments

Scheduled task

A scheduled task called "Synchronise external database enrolments task" synchronises all user enrolments at once - both adding and removing user enrolments (and creating courses if specified) (MDL-59986). The settings for this are in Site administration > Server > Tasks > Scheduled tasks. You should make sure that this script is enabled and set to run as often as you need to perform the enrolments. By default it is disabled and set to run only once a day.

(Note: a deprecated script had been provided from Moodle 3.6 to 3.10 to use in case of issues with the Scheduled task added to Moodle 3.6. This has been removed as of Moodle 3.11).

User accounts

This is a reminder that you need to make sure all the users present in the external enrolments are already have accounts created in Moodle, because users not existing in Moodle can't be enrolled in a course. If you are using external authentication plugins (db, ldap, etc.) you can use the scripts provided by those plugins to synchronise your users before running this script.

Setting up enrolment sync (How to)

You will need to perform (as a minimum) the following steps to enable external database enrolment - only a single table is required in the database which contains a record for every user/course combination. If the table is large it is a good idea to make sure appropriate indexes have been created:

Field Mapping Example

Choose your fields from the Moodle database:

Create a view in your external database which matches the chosen field values from Moodle:

Potential gotchas

Errors and diagnostics

The plugin produces a number of diagnostic messages and/or errors which are recorded to the PHP error log (as defined in the php.ini file). In addition messages about courses that are in the database for the user but that do not exist in the Moodle site will only be produced if debugging is set to ALL or DEVELOPER.

See also


Source: docs.moodle.org/503/en/External database — MoodleDocs (GPL) · Imported 2026-11-10.


版本号 #3
由 cocobook-sync 创建于 2026-11-10 13:10:01 UTC
由 cocobook-sync 更新于 2026-11-10 23:27:24 UTC