Note: You are currently viewing documentation for Moodle 3.1. Up-to-date documentation for the latest stable version of Moodle is probably available here: External database enrolment.

External database enrolment: Difference between revisions

From MoodleDocs
No edit summary
No edit summary
Line 32: Line 32:


External database enrolment happens at the moment when a user logs into Moodle. 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.
External database enrolment happens at the moment when a user logs into Moodle. 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.
The "enrol_db_defaultcourseroleid" 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_db_remoterolefield" setting) that contains the short name for the user's role. This could, for example, be used to enrol both students and teachers into courses using a suitably configured database.
Optionally courses that do not exist in the Moodle site can be created. Switch the *enrol_db_autocreate" option to "yes" in the plugin settings. You can additionally specify the Category into which the new course will be placed and may also specify a "template" course from which the new course will be copied.


== Synchronization Script ==
== Synchronization Script ==

Revision as of 15:15, 19 June 2007

You may use a 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 compile PHP with the appropriate options or through ODBC.

  • access
  • ado
  • mssql
  • borland_ibase
  • csv
  • db2
  • fbsql
  • firebird
  • ibase
  • informix72
  • informix
  • mysql
  • mysqlt
  • oci805
  • oci8
  • oci8po
  • odbc
  • odbc_mssql
  • odbc_oracle
  • oracle
  • postgres64
  • postgres7
  • postgres
  • proxy
  • sqlanywhere
  • sybase
  • vfp

External database enrolment happens at the moment when a user logs into Moodle. 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.

The "enrol_db_defaultcourseroleid" 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_db_remoterolefield" setting) that contains the short name for the user's role. This could, for example, be used to enrol both students and teachers into courses using a suitably configured database.

Optionally courses that do not exist in the Moodle site can be created. Switch the *enrol_db_autocreate" option to "yes" in the plugin settings. You can additionally specify the Category into which the new course will be placed and may also specify a "template" course from which the new course will be copied.

Synchronization Script

A script is provided that can synchronize all your user enrollments at once - both adding and removing users (and courses if specified). The script is called auth_db_sync_users.php and is found in the auth/db folder.

This script is meant to be called from a system cronjob to sync moodle user accounts with external database. It is required when using internal passwords (== passwords not defined in external database). Example cron entry:

   # 5 minutes past 4am
   5 4 * * * /usr/bin/php -c /path/to/php.ini /path/to/moodle/auth/db/auth_db_sync_users.php

Notes:

  • If you have a large number of users, you may want to raise the memory limits by passing -d memory_limit=256M
  • For debugging & better logging, you are encouraged to use in the command line: -d log_errors=1 -d error_reporting=E_ALL -d display_errors=0 -d html_errors=0
  • This only works for users that already exist in your Moodle site (see your authorization plugin)


Field Mapping Example:

Choose your fields from the Moodle database:

  • enrol_localcoursefield: A course identifier from mdl_course, e.g. "idnumber"
  • enrol_localuserfield: A user identifier from mdl_user, e.g. "idnumber"
  • enrol_localrolefield: (optional) A role identifier from mdl_role, e.g. "shortname"

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

  • enrol_remotecoursefield: A matching course identifier from your external database table, e.g. "course_number"
  • enrol_remoteuserfield: A matching user identifier from your external database table, e.g. "userid"
  • enrol_remoterolefield: (optional) A matching role identifier from your external database table, e.g. "role_name"

See also