Inscription par base de données externe

De MoodleDocs
Aller à :navigation, rechercher

Remarque : la traduction de cette page n'est pas terminée. N'hésitez pas à traduire tout ou partie de cette page ou à la compléter. Vous pouvez aussi utiliser la page de discussion pour vos recommandations et suggestions d'améliorations.

Utilisation d'une base de données externe pour gérer les inscriptions

Vous pouvez utiliser une base de données externe (de presque tout type) pour gérer vos inscriptions. Votre base de données doit contenir au moins un champ contenant un ID de cours, un champ contenant un ID d'utilisateur et éventuellement un champ contenant un rôle. Ceux-ci sont comparés à ceux que vous avez choisis pour les options Champ cours local (enrol_database | localcoursefield), Champ utilisateur local (enrol_database | localuserfield) et Champ rôle local (enrol_database | localrolefield) dans la page de paramétrage du plugin.

Voici les sources de données prises en charge, mais notez que vous devrez compiler PHP avec les options appropriées ou via ODBC.

  • access
  • ado
  • mssql
  • borland_ibase
  • csv
  • db2
  • fbsql
  • firebird
  • ibase
  • informix72
  • informix
  • mysql (déprécié, merci d'utiliser mysqli à la place)
  • mysqli
  • mysqlt
  • oci805
  • oci8
  • oci8po
  • odbc
  • odbc_mssql
  • odbc_oracle
  • oracle
  • postgres64
  • postgres7
  • postgres
  • proxy
  • sqlanywhere
  • sybase
  • vfp

Inscription et désinscription

L'inscription par base de données externe se passe au moment ou un utilisateur se connecte à Moodle. Le plugin tentera d'inscrire automatiquement l'étudiant dans tous ses cours en fonction des données issues de la base de données externe et, éventuellement, de créer des cours vides lorsqu'ils n'existent pas déjà. Pour vérifier si cela fonctionne, vous pouvez vous connecter en tant qu'étudiant, puis vérifier que sa liste de cours est conforme à vos attentes.

Le processus désinscrit également les utilisateurs des cours s’ils ne figurent plus dans la base de données externe. Les enregistrements des utilisateurs sont marqués en fonction de leur méthode d'inscription originale. Par conséquent, le plugin d'inscription par base de données externe peut uniquement désinscrire des utilisateurs qui ont été inscrits par celui-ci.

Cours cachés

Les cours qui ont été cachés aux étudiants peuvent être ignorés par le processus d'inscription en paramétrant l'option Ignorer les cours cachés (enrol_db_ignorehiddencourse) à Oui.

Inscriptions et rôles

L'option Rôle par défaut (enrol_database | defaultrole) sur la page de paramétrage du plugin permet de spécifier le rôle attribué à l’utilisateur lorsqu'il sera ajouté au cours. Le paramètre par défaut les définira sur le paramètre par défaut du cours (initialement "étudiant"). Toutefois, vous pouvez spécifier un champ dans la table externe (spécifiée dans le paramètre Champ rôle de la base de données externe (enrol_database | remoterolefield) ) contenant le nom abrégé ou l'ID du rôle de l'utilisateur. Cela pourrait, par exemple, être utilisé pour inscrire des étudiants et des enseignants à des cours utilisant une base de données configurée de manière appropriée.

Désinscription

L'option Action de désinscription externe (enrol_database | unenrolaction) dans la page de paramétrage du plugin permet de définir quel sera l'action lorsqu'un utilisateur est désinscrit de la base de données externe. Chaque paramètre fait ce qui suit :

  1. "Désinscrire du cours l'utilisateur" Quand un utilisateur disparaît de la source externe, l'inscription est supprimée ainsi que l'ensemble des rôles de l'utilisateur. Cela signifie que certaines données de l'utilisateur sont supprimées du cours pendant la désinscription (cela inclue les notes, les tentatives aux tests, etc.)
  2. "Garder l'utilisateur inscrit" Quand un utilisateur disparaît de la source externe, l'inscription est gardée telle quelle, et l'utilisateur sera toujours en mesure d'accéder au cours, de consulter les ressources, de participer aux activités, etc. C'est une option de type "ne rien faire".
  3. "Désactiver l'inscription au cours" Quand un utilisateur disparaît de la source externe, l'inscription de l'utilisateur est suspendue (l'utilisateur n'accède plus au cours mais ses données et ses paramétrages sont conservés), et les rôles sont gardés tels quels. Vous pouvez avoir besoin de cette option parce que dans certains cas, l'utilisateur a besoin d'un rôle avec certaines capacités afin d'être visible dans les interfaces utilisateur comme le carnet de notes, les rendus de devoirs, etc.
  4. "Désactiver l'inscription au cours et retirer l'attribution des rôles" Quand un utilisateur disparaît de la source externe, l'inscription est suspendue et les rôles attribués par le processus d'inscription sont supprimés. Veuillez noter que l'utilisateur peut "disparaître" du carnet de notes et d'autres interfaces utilisateur.

Création de cours

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).

  • Do not confuse this category id with the new custom category id number field that you can manually assign to a category. (See Trackers http://tracker.moodle.org/browse/MDL-28518 and http://tracker.moodle.org/browse/MDL-31845).
  • Leaving the category id data empty means that a course will be assigned to the default category.
  • If you assign data to categories that do not exist already in Moodle, the courses will not be created.

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.

Synchronization script

A script is provided that can synchronize all your user enrollments at once - both adding and removing user enrolments (and creating courses if specified). The script is called sync.php and is found in the enrol/database/cli folder.

This script is meant to be called from a system cronjob to sync moodle enrolments with enrolments in the external database. You need to make sure all the users present in the external enrolments are already created in moodle. If you are using external authentication plugins (db, ldap, etc.) you can use the scripts provided by those plugins to synchronize your users before running this script.

Example cron entry

   # 5 minutes past 4am
   5 4 * * * /usr/bin/php -c /path/to/php.ini /path/to/moodle/enrol/database/cli/sync.php

Notes:

  • If you have a large number of enrolments, 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 comment above)

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:

  • Use an existing database or create a new one. Use an existing table or create a new one with the following minimum fields.
    1. A unique course identifier to match one of the following fields.
      • the "idnumber" field in Moodle's course table (varchar 100), which is manually specified as the "Course ID number" when editing a course's settings
      • the "shortname" field in Moodle's course table (varchar 255), which is manually specified as the "Course short name" when editing a course's settings
      • the "id" field in Moodle's course table (int 10), which is based on course creation order
    2. A unique user identifier to match one of the following fields.
      • the "idnumber" field in Moodle's user table (varchar 255), which is manually specified as the "ID number" when editing a user's profile
      • the "username" field in Moodle's user table (varchar 100), which is manually specified as the "Username" when editing a user's profile
      • the "email" field in Moodle's user table (varchar 100), which is manually specified as the "Email address" when editing a user's profile
      • the "id" field in Moodle's user table (int 10), which is based on user creation order
    3. (optional) A unique role identifier to match one of the following fields.
      • the "shortname" field in Moodle's role table (varchar 100), for example editingteacher, coursecreator, student, ...
      • the "name" field in Moodle's role table (varchar 255), for example Teacher, Course creator, Student, ...
      • the "id" field in Moodle's role table (int 10), which is based on initial installation and new role creation order
  • Populate the database table. Each user/course combination to have a record in the table.
  • In Moodle, go to Site administration > Plugins > Enrolments > Manage enrol plugins, find External Database in the list, enable it (click the closed-eye icon) and click Settings.
  • In the top panel, select the database type (make sure you have the necessary configuration in PHP for that type) and then supply the information to connect to the database.
  • The middle panel creates the mapping between Moodle and the external database. The first three settings are for the local (Moodle) field names and the last three for the remote (external database) settings. They are in the same order.
    • enrol_localcoursefield / enrol_remotecoursefield - in Moodle the name of the field in the course settings the uniquely identifies the course (e.g., idnumber). In the external database the name of the matching field.
    • enrol_localuserfield / enrol_remoteuserfield - in Moodle the name of the field in the user profile that uniquely identified the user (e.g., idnumber). In the external database the name of the matching field.
    • enrol_db_localrolefield / enrol_db_remoterolefield - (optional) in Moodle the name of the field in the role edit page the uniquely identifies the role (e.g., shortname). In the external database the name of the matching field.
  • The Roles panel specifies the role that the user will get in the course if their role is not specified in the external database.
  • A new optional field enrol_database | remoteotheruserfield allows those with the role "Other users" to be added but not to be included as course participants.
  • The final panel enables auto creation of courses.
  • Save changes, and then tick the box to enable external database enrolment.

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"

Potential gotchas

  • It almost goes without saying that the integrity of the external database is important. If data is missing from the database then there is a potential for users being unenrolled from some or all of their courses. The unenrollment process will remove them from any group assignments and also poll each module type to give the module the option of removing that user's data if appropriate (for example, however, forum posts are never deleted). It is therefore prudent that you take the utmost care to ensure that the data in the external database is correct at all times.
  • Minor Security Issue Consider that if the ID number field you use to identify your students is editable by the students (in their profile), then there is a potential for them changing this to the id of another valid student and gaining access to resources that they should not. (However, they will still appear as themselves, they cannot impersonate the other user or otherwise gain access to their resources.) To prevent this and similar issues, you can lock the ID number as well as other fields so the user can not change them. Do this in the Data mapping section of Site administration > Plugins > Authentication > External database.

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.

You can get detailed progress information by executing the sync script with -v parameter:

php /path/to/moodle/enrol/database/cli/sync.php -v

See also