2

My scenario;

A user in Moodle completes a course. I'd like to update an external database once this happens. My understanding is that the course_completed event is triggered every time the cron job is run.

Which is the best approach to update an external database with some simple values e.g Username/ID, CourseID and maybe Date of Completion, of the completed courses?

I'd rather not hack the completion/cron.php to do this, but will if I have to!

S-F
  • 80
  • 1
  • 9

1 Answers1

2

You'll need to create a local plugin.

http://docs.moodle.org/dev/Local_plugins

Create the plugin in /local/myplugnname

Create an events.php file

/local/mypluginname/db/events.php

With this

$handlers = array (
    'course_completed' => array (
        'handlerfile'      => '/local/mypluginname/lib.php',
        'handlerfunction'  => 'local_mypluginname_course_completed',
        'schedule'         => 'cron',
        'internal'         => 1,
    ),

Have a look here for more info http://docs.moodle.org/dev/Events_API#Handling_an_event

You'll need a version.php file to install the plugin and add the event handler.

Then create a function

function local_mypluginname_course_completed($eventdata)

in

/local/mypluginname/lib.pgp

This will be called when the cron runs

To find out the contents of $eventdata have a look at

events_trigger('course_completed', $this->get_record_data());

in

/completion/completion_completion.php

To update a remote database have a look at the code in db authentication

/auth/db/auth.php

Something like

$mydb = ADONewConnection('mysql');
$mydb->Connect($dbhost, $dbuser, $dbpass, $dbname, false);
$mydb->Execute($insertsql);
$mydb->Close();
Russell England
  • 9,436
  • 1
  • 27
  • 41