Moodle additional functions web service

Not long ago I was tasked to make a tool to manage Moodle courses by moving them through categories, creating and cleaning them up. Now I had to make some way to “refresh” the course participants and that means unenrolling teachers and managers, assign different roles, suspending students, adding students, all that stuff.

This was going pretty good and easy until I hit upon a web service function core_enrol_edit_user_enrolment. It’s an External function that updates a given user enrolment, it is needed to suspend students from a course.
From the official API docs, we can see that it takes in required arguments courseid (User enrolment ID), ueid (User enrolment ID) and status (Enrolment status).

The problem here is with a ueid (User enrolment ID) argument because there’s no function that returns such an ID. At least I wasn’t able to find one easily accessible and I had to make my own web service function to get that ID.

It appears that Moodle plugin development isn’t thoroughly documented and straightforward, so I took an example template and built on it by reading here and there.

The sources that I used were:
https://docs.moodle.org/dev/Enrolment_API
https://docs.moodle.org/dev/Adding_a_web_service_to_a_plugin
http://www.examulator.com/er/components/course.html
https://docs.moodle.org/dev/Tutorial
and some various forum posts.

From the table diagram I made out this SQL query to get the record ID:

SELECT {user_enrolments}.id AS id, {user_enrolments}.enrolid AS enrolid, {user_enrolments}.userid AS userid, {enrol}.courseid AS courseid
FROM {user_enrolments}, {enrol}
WHERE {user_enrolments}.enrolid = {enrol}.id AND {enrol}.courseid = ? AND {user_enrolments}.userid = ?

Because the function expects not the Enrol ID (ID of the record in enrol table), but the ID of the record from user_enrolments table, that is what I returned.

And so I made a functioning plugin which will be as a container for functions that I might need later in life :P the plugin can be found on my Github: https://github.com/kulverstukas1/moodle-additional-functions

To use it, upload the contents of that repo to a folder /local/additional_functions and install in Moodle as normal. A function local_get_user_enrolment_id appears, you can add it to your web service and query it as normal by giving a user ID to find and a course ID to search in.

Leave a Reply

Your email address will not be published. Required fields are marked *