General developer forum

Self Enrol a user to a particular course, programatically.

 
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: -