user1882752
user1882752

Reputation: 576

how to enroll a user in all courses on Moodle

I have a Moodle with a SSO.

Users sign onto a site and click on a link to come to my Moodle.

When they arrive, I want to enroll them on every course on my Moodle.

However, I am unable to get the sql to work.

Here is what I have:

$ra = new object();
$ra->roleid = 5;
$ra->contextid = $contextid;
$ra->userid = $user->id;
$ra->hidden = 0;
$ra->enrol = 'manual';
//$ra->enrol = 'self';
/// Always round timestart downto 100 secs to help DBs to use their own caching algorithms
/// by repeating queries with the same exact parameters in a 100 secs time window
$ra->timestart = 0;
$ra->timeend = 0;
$ra->timemodified = time();
$ra->modifierid = 0;

// Enrol the User for the Course
$ra->id = $DB->insert_record('role_assignments',$ra);

Upvotes: 2

Views: 6672

Answers (3)

vinoth
vinoth

Reputation: 111

You can use auto enrol plugin for the purpose of enrolment. When the user clicks into course they get automatically enrolled to the courses by use of this plugin. Link: https://moodle.org/plugins/enrol_auto

Upvotes: 1

iankit
iankit

Reputation: 9382

To enroll a user, a following code snippet might be useful It emulates manual enrollments in an automated way.

function enroll_user($userid, $course, $modifier) {                                                
    global $DB;                                                                                    
    $enrolData = $DB->get_record('enrol', array('enrol'=>'manual', 'courseid'=>$course));          
    $user_enrolment = new stdClass();                                                              
        $user_enrolment->enrolid = $enrolData->id;                                                 
        $user_enrolment->status = '0';                                                             
        $user_enrolment->userid = $userid;                                                         
        $user_enrolment->timestart = time();                                                       
        $user_enrolment->timeend =  '0';                                                           
        $user_enrolment->modifierid = $modifier;                                                   
        //Modifierid in this table is userid who enrolled this user manually
        $user_enrolment->timecreated = time();                                                     
        $user_enrolment->timemodified = time();                                                    
    $insertId = $DB->insert_record('user_enrolments', $user_enrolment);                            
    //addto log                                                                                    
    $context = $DB->get_record('context', array('contextlevel'=>50, 'instanceid'=>$course));          
    $role = new stdClass();                                                                        
        $role->roleid = 5;                                                                         
        $role->contextid = $context->id;                                                           
        $role->userid = $userid;                                                                   
        $role->component = '';                                                                     
        $role->itemid = 0;                                                                         
        $role->timemodified = time();                                                              
        $role->modifierid = $modifier;                                                             
    $insertId2 = $DB->insert_record('role_assignments', $role);                                    
    add_to_log($course, '', $modifierid, 'automated');                
    return array('user_enrolment'=>$insertId, 'role_assignment'=>$insertId2);                      
}           

Upvotes: 7

Mike
Mike

Reputation: 322

This is the way I automatically enroll students to courses. You just have to manipulate it a little bit to use it for all course ids. I hope this helps you.

// enroll student to course (roleid = 5 is student role)
function enroll_to_course($courseid, $userid, $roleid=5, $extendbase=3, $extendperiod=0)  {
    global $DB;

    $instance = $DB->get_record('enrol', array('courseid'=>$courseid, 'enrol'=>'manual'), '*', MUST_EXIST);
    $course = $DB->get_record('course', array('id'=>$instance->courseid), '*', MUST_EXIST);
    $today = time();
    $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);

    if(!$enrol_manual = enrol_get_plugin('manual')) { throw new coding_exception('Can not instantiate enrol_manual'); }
    switch($extendbase) {
        case 2:
            $timestart = $course->startdate;
            break;
        case 3:
        default:
            $timestart = $today;
            break;
    }  
    if ($extendperiod <= 0) { $timeend = 0; }   // extendperiod are seconds
    else { $timeend = $timestart + $extendperiod; }
    $enrolled = $enrol_manual->enrol_user($instance, $userid, $roleid, $timestart, $timeend);
    add_to_log($course->id, 'course', 'enrol', '../enrol/users.php?id='.$course->id, $course->id);

    return $enrolled;
}

Upvotes: 3

Related Questions