Call Moodle API from a plugin

Call Moodle API from a plugin

av Jeffrey Bannister -
Antall svar: 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

Gjennomsnittlig vurdering: -
Som svar til Jeffrey Bannister

Re: Call Moodle API from a plugin

av Renaat Debleu -
Bilde av Core developers Bilde av Particularly helpful Moodlers Bilde av 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);
Gjennomsnittlig vurdering:Useful (3)
Som svar til Renaat Debleu

Re: Call Moodle API from a plugin

av Jeffrey Bannister -
Thanks Renaat
Som svar til Jeffrey Bannister

Re: Call Moodle API from a plugin

av 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?
Som svar til GIOVANNI SCALMATI

Re: Call Moodle API from a plugin

av Michael Milette -
Bilde av Core developers Bilde av Documentation writers Bilde av Particularly helpful Moodlers Bilde av Plugin developers Bilde av Testers Bilde av 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
Som svar til GIOVANNI SCALMATI

Re: Call Moodle API from a plugin

av 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