Registering in a specific group

Re: Registering in a specific group

by Franco Pantoja -
Number of replies: 0
Picture of Particularly helpful Moodlers

Hi Mihail

We suppose you have some courses in 3 categories, so you should select every course grouped by this category.

$courses= $DB->get_records_sql('SELECT * FROM {course_categories} WHERE id = ?', array($mycategory));

Then we enroll the users in these courses, that way we let them access only in that category courses.

You also should a function like this to enroll the user (thanks very much Darko Miletić smile )

$roleid = 5, student

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;
}

Regards!!


Average of ratings: Useful (1)