Moodle php code : To enrol a user into a course

// get manual enrolment plugin , if enabled at system level

    $manual = enrol_get_plugin('manual');
    if ($manual) { 

// get all enrolment instnace avaialable in course

        if ($instances = enrol_get_instances($course_id, false)) {
            foreach ($instances as $instance) {
                if ($instance->enrol === 'manual') {



// if course does not have manual enrolment instnace  added, then add one
       $instance =  $manual->add_instance($course_id);

    if ($manual && $instance) {

// manual enrolment is enabled at system level and enrollment instnace is available in course
//  pass enrolinstnace,
//  user id
//  role id [get from role table] to enrol with role
//  start time of enrolment and endtime of enrolment

        $manual->enrol_user($instance, $user->id, $role_id, $timestart, $timeend, ENROL_USER_ACTIVE);
        $status = true;

Leave a Reply

Your email address will not be published. Required fields are marked *