I've been developing a plugin for Moodle (current ver. 3.5.1+), which is designed to communicate with an external website. I created the Plugin's external API functions. They seem to work properly with special webservice user, service and token I've created via the moodle site administation interface manually.
I want to have a webservice user, webservice service (with the Plugin's ext functions included) and a webservice token created automatically during the Plugin's installation process (when its folder is placed in /mod and moodle site detects it on refresh).
Is it possible?
Is there a working example I can look at?
1. Create webservice with necessary functions
Do I use functions in $CFG->dirroot . '/webservice/lib.php' for that?
Like creating new class $webservice = new webservice();
and then using $webservice->add_external_service(...); or some other way is expected?
2. Create a special user with relevant capabities
I stuck here. Do I use 'user/lib.php' functions?
user_create_user(...); - this one or other way is expected?
And which function do I use to give a new user required capabilites..
Example would be great...
Read this document
A web service is defined by adding services.php to your plugin (as outlined in the link above)
Example of that can be found here
Use this for creating user
$userdata = [ 'auth' => 'manual', 'confirmed' => true, 'username' => 'foo', 'password' => 'foo', 'firstname' => 'foo', 'lastname' => 'faa', 'email' => 'email@example.com' ]; $userid = user_create_user($userdata);
For assigning capabilities use API assign_capability.