Call Moodle API from a plugin

Call Moodle API from a plugin

Jeffrey Bannister -
Number of replies: 5

Hi All,

This may be a very basic question, but I am trying to add functionality to a plugin to create a new user and then enrol them in a specific course. This impacts a lot of tables, so was looking to see if I can use the webservices API for core_user_create_users and enrol_manual_enrol_users. However I cannot get the format for using the internal curl function correct. Can anyone give me some examples? Or is there a better way I should be doing this?

Thanks,

Jeff

Ngā whakawākanga toharite: -
In reply to Jeffrey Bannister

Re: Call Moodle API from a plugin

Renaat Debleu -
Pikitia o Core developers Pikitia o Particularly helpful Moodlers Pikitia o Plugin developers

In a Moodle plugin, you do not need webservices to create or enrol a user.

As a sample, you can add a user to a course with 3 lines of code :

$enrol = $DB->get_record('enrol', $params);
$plugin = \enrol_get_plugin('manual');
$plugin->enrol_user($enrol, $userid, $enrol->roleid, $enrol->enrolstartdate, $enrol->enrolenddate);
Ngā whakawākanga toharite: Useful (3)
In reply to Renaat Debleu

Re: Call Moodle API from a plugin

Jeffrey Bannister -
Thanks Renaat
Ngā whakawākanga toharite: -
In reply to Jeffrey Bannister

Re: Call Moodle API from a plugin

GIOVANNI SCALMATI -
Hello Jeffrey, how are you? Can I ask you a question?

Because I'm trying to make exactly what you are making, a plugin to create an user (that comes from outside the moodle platform) and enrol it automatically in a course, but I'm very new in the moodle platform and in development and I don't know where to start or how to use it, can you give come tips?

Did you finish te plugin or found another problem?
Ngā whakawākanga toharite: -
In reply to GIOVANNI SCALMATI

Re: Call Moodle API from a plugin

Michael Milette -
Pikitia o Core developers Pikitia o Documentation writers Pikitia o Particularly helpful Moodlers Pikitia o Plugin developers Pikitia o Testers Pikitia o Translators
Hi Giovanni,

You can have users register for an account using the normal Moodle email self-registration and then use the AutoEnrol plugin (available for all versions of Moodle) to automatically enrol them in the course. See:
https://moodle.org/plugins/enrol_autoenrol

Hope you find this helpful.

Best regards,

Michael
Ngā whakawākanga toharite: -
In reply to GIOVANNI SCALMATI

Re: Call Moodle API from a plugin

Jeffrey Bannister -
Hi Giovanni,
Sure. The bones of my plugin is as follows:
Create a user (you should have some logic that validates the user doesn't already exist):
$newuser = new stdClass();

$newuser->username = $username;
$newuser->password = $password_hashed;
$newuser->firstname = $firstname;
$newuser->lastname = $lastname;
$newuser->email = $email;
$newuser->auth = 'manual';
$newuser->confirmed = 1;
$newuser->institution = $institution;
$newuser->mnethostid = 1;
$newuser->city = $city;
$newuser->country = $country;
$newuser->timecreated = time();
$newuser->timemodified = time();
$newuser->description = '';

$resp = $DB->insert_record('user',$newuser);

Enrol the user in a course:
$timestart = time();
$duration = 7;
$timeend = time()+($duration*86400); //7 days
$role = 5 //This is for a student - better to do a DB call to get though

$enrolid = whichever entry in the database equates to your course + method (e.g. courseid and 'manual');
$thisenrol = $DB->get_record('enrol',array('id'=>$enrolid));
$plugin->enrol_user($thisenrol,$userid,$role,$timestart,$timeend);

If you're just starting out to develop your own plugin, I can recommend the following Udemy course:
I found it very helpful.
Best,
Jeff
Ngā whakawākanga toharite: -