Monday, May 5, 2014

Moodle 2.x External DB Authentication and enrollment - soup to nuts.

Steps I completed to synch moodle with an external DB for account creation and enrollment.

1 - figure out what was being asked for first.

Bottom line.  I needed to synch 2 moodle processes, account creation and course enrollment,  to an existing external DB.  The big one, two.  I found the native moodle functionality and enabled two scripts that cause moodle to pull data from the external DB and compare to itself and possibly create accounts and enrollment.

Details.

2 - figure out what i was doing

I spent a bit of time working out in my head what I was doing.  Big picture stuff.  It's how my brain works.  These are helpful moodle docs.  External DB authentication.  and External database enrollment.
I spent a bit of time in the forms listed in bottom of each moodle doc page.

3 - Enabling Authentication and Enrollment synching in Moodle

a - Account authentication
  Site administration | Plugins | Authentication| External database
Set up the user to connect with the external DB - this user needs to exist on the MySQL level, in the users table.  Set up database host = localhost, map the username field etc. I use phpMyAdmin and MySQL Workbench for DB administration.  I looked at the config.php file at the root of the site for user credentials to pass in the form.  When the account is removed from the master DB, it is left in moodle.  
b - Enrollment synching
Site administration | Plugins | Enrollments | External database
 Used the same setup info from last step.  Mapped the username, courseid fields.  Those are the two keys to matching enrollment.

4 - Activating two scripts

For me in a Windows environment, I wanted to call the php files from the command line.  So I verified how to call php files from the Windows command line and added the call to both of the scripts from the same Windows file.  I called the file sych_with_moodle.bat.  Its contains look like this
REM Part 1 - synch the users (Authentication)

c:\wamp\bin\php\php5.3.13\php.exe -f c:\wamp\www\student\auth\db\cli\sync_users.php -a

REM PART 2 - synch the enrollments

c:\wamp\bin\php\php5.3.13\php.exe -f c:\wamp\www\student\enrol\database\cli\sync.php -a

5 - Calling the scripts, automatically using cron script

I am using the native moodle cron script,  moodlesite/admin/cron.php, to call the sych_with_moodle.bat file.  I added this line to the bottom of the cron.php script to call the .bat file.
system("cmd /c c:/wamp/www/sych_with_moodle.bat");

6 - Ensure it is working

Test by creating accounts and enrollments while executing the cron.php script. Verity that accounts and enrollments are being created in moodle and removed when removed from the master syched side.

1 comment: