Pages

Sunday, April 10, 2011

Moodle Course Updates: How to make moodle to reflect course updates in course modules


 All the modules added to courses are stored in the “mdl_course_modules” table in the moodle database. You can retrieve all the modules add to a particular course by obtaining all the records that contain the course id of that course. The course ids are stored in “course” column. Then you can obtain module id of those records which is indicated in the “module” column. Id of the instance of the module can be obtained for those records which are indicated in the “instance” column.
Next step would be to obtain the module name using the id of the module. This can be done by querying “mdl_modules” table. All standard course modules has a table in moodle with this name(mdl_modulename). The particular instances of the module in the modified course can be found in this table. Those can be obtained using instance id obtained earlier. Id field of this table refers to the instance id of the “mdl_course_modules” table. These are the records that you need to modify in order to reflect changes in the course.
The issue in finding the right fields to modify arise here. This is because fields in different modules differ drastically. For example modules that have start date and end date will store those in columns with different names. It is unfortunate that these common attributes doesn't have a standard name. So you have to handle each module differently. What you can do is switch the updating process based on the module name.
The description above describes how your function or the event_handler can perform necessary modification to the database. Below you will see how to call your function or the event hadler.
Moodle provides a standard event handling mechanism, in which you can register a listner and moodle will invoke your listener. Here you would want to listen to the course update event. But unfortunately moodle will pass only the modified record of the course to your event handler. This information is not going to be enough for us to modify field such as start date and end date of modules. This is because we would need to know the amount by which the course date has been shifted. Of course you can add the information to the event data in event_trigger method, but this would affect other event handlers wich are listning to the same event and expecting standard information. Therefore you have to call your function using some other method.
Course editing happens in course/edit.php file. So you need to call the function in this file just after the modification happens. But in order to get the old information you need to obtain a copy of the old record of the course prior to modification.
I could not find any other way to hadle this without modification to the original moodle code. I did this with moodle 2.0. Hope this might help some one.