General developer forum

Self Enrol a user to a particular course, programatically.

 
Picture of augusto canessa
Self Enrol a user to a particular course, programatically.
 

At the moment I am able to enrol a user into a course using the following code. 


enrol_try_internal_enrol($_POST['course_id'], $_POST['user_id'], $_POST['role_id'], time());


The issue is that it enroles the user as MANUAL, but I need the user to be enroled as SELF ENROLMENT.


How can I achieve that? What function can I use?


Thank you!



 
Average of ratings: -
Renaat
Re: Self Enrol a user to a particular course, programatically.
Core developersParticularly helpful MoodlersPlugin developers
Make sure that no malicious code is injected in your form and call something like:

$enrols = $DB->get_records('enrol',['enrol' => 'self', 'courseid' => $course_id])
$plugin = \enrol_get_plugin('self');
for each ($enrols as $enrol) {
   $plugin->enrol_user($enrol, $user_id,  $role_id);
}

 
Average of ratings: -