No. You have to write your own for that. Something like this (untested):
function check_enrol($shortname, $userid, $roleid, $enrolmethod = 'manual') { global $DB; $user = $DB->get_record('user', array('id' => $userid, 'deleted' => 0), '*', MUST_EXIST); $course = $DB->get_record('course', array('shortname' => $shortname), '*', MUST_EXIST); $context = context_course::instance($course->id); if (!is_enrolled($context, $user)) { $enrol = enrol_get_plugin($enrolmethod); if ($enrol === null) { return false; } $instances = enrol_get_instances($course->id, true); $manualinstance = null; foreach ($instances as $instance) { if ($instance->name == $enrolmethod) { $manualinstance = $instance; break; } } if ($manualinstance !== null) { $instanceid = $enrol->add_default_instance($course); if ($instanceid === null) { $instanceid = $enrol->add_instance($course); } $instance = $DB->get_record('enrol', array('id' => $instanceid)); } $enrol->enrol_user($instance, $userid, $roleid); } return true; }
That is now tested. thx you.
Is there a method to get roleId using role-Name?
All roles are stored in role table so to get them by name or short name you can use this:
$roleid = $DB->get_field('role', 'id', array('shortname' => $shortname));
Hi,
it possible to get role-id for course-id and user-id?
thx.
I think
if ($instance->name == $enrolmethod)
should be
if ($instance->enrol == $enrolmethod)
and
if ($manualinstance !== null)
should be
if ($manualinstance == null)
"name" field in the the enrol table have null values. I'm using (2.8)