I’m trying to find examples/documentation about the Moodle API that would allow me to add a Self Enrollment method to a course and add a key (along with other parameters like enrolment duration etc).
I don’t want to add a user I just want to add an enrollment method with a key.
Any help or suggestions would be much appreciated. I can’t find any info anywhere.
I cannot find any documentation anywhere or perhaps it can’t be done.
2
Answers
Many thanks for your reply davosmith.
The only solution I could think of was do an insert query (mysql) directly in to the mdl_enrol table and it seems to be working so far. Something like:
INSERT INTO
mdl_enrol
(id
,enrol
,status
,courseid
,sortorder
,name
,enrolperiod
,enrolstartdate
,enrolenddate
,expirynotify
,expirythreshold
,notifyall
,password
,cost
,currency
,roleid
,customint1
,customint2
,customint3
,customint4
,customint5
,customint6
,customint7
,customint8
,customchar1
,customchar2
,customchar3
,customdec1
,customdec2
,customtext1
,customtext2
,customtext3
,customtext4
,timecreated
,timemodified
) VALUES (NULL, 'self', '0', '44', '4', 'SelfEnrol', '31536000', '0', '0', '0', '0', '0', 'test', NULL, NULL, '5', '0', '0', '200', '1', '0', '1', NULL, NULL, NULL, NULL, NULL, NULL, NULL, '', NULL, NULL, NULL, '1678028362', '1678028458');The enrol_self plugin only supports 2 webservices ( https://github.com/moodle/moodle/blob/master/enrol/self/db/services.php ):
A search through the core webservices ( https://github.com/moodle/moodle/blob/master/lib/db/services.php ), suggests only:
(and a few others, that are alos not likely to be relevant).
As far as I can see, there is no standard webservice in Moodle that can create an enrolment instance (only those that can list existing enrolment instances and use them to enrol a user into a course, or unenrol a user who is already enrolled). I’m guessing this is because webservices are usually more focussed on the management of users and courses, rather than on configuration, so no one has previously needed a webservice like you have described.
If you want to be able to do this, then the easiest method would be to create a custom plugin (probably a ‘local’ plugin) and define a webservice of your own within that plugin: https://moodledev.io/docs/apis/subsystems/external/writing-a-service