0

I'm trying make solution by Using MOODLE create users and enroll them in courses via SQL

Add user - done

Add user to course - done

But user without role into course. No roles in role column.

'googleoauth2' as auth value for login with social network API. It's work. The problem is remains If I change 'auth' value to 'manual'.

Into database all records as in queries.

Help, please.

UPD: sorry, I need to change enlorID to courseID in instanceid field...

Code:

    async function test(){
        var query = `INSERT INTO mdl_user (auth, confirmed, username, password, firstname, lastname, email, mnethostid) 
            VALUES ('googleoauth2', 1, '${pseudo}', 'not cached', '${name}', 
                   '${secondname}', '${email}', 1);`;
        var insertOutput = await getMysqlQuery(query);
        var userId = insertOutput.results.insertId;
        var courseRecords = await getMysqlQuery("SELECT id FROM mdl_course WHERE idnumber=\"" + shortname + "\"");  

        if(courseRecords.length < 1)
            throw 'Course not found';
        var courseId = courseRecords.results[0].id;
        var enrolRecords = await getMysqlQuery(`SELECT id FROM mdl_enrol WHERE courseid=${courseId} AND enrol='manual';`);

        if(enrolRecords.length < 1)
            throw 'Enrol not found';
        var enrolId = enrolRecords.results[0].id;
        var contextRecords = await getMysqlQuery(`SELECT id FROM mdl_context WHERE contextlevel=50 AND instanceid=${courseId};`);

        if(contextRecords.length < 1)
            throw 'Context not found';
        var now = (new Date()).getTime() / 1000 ;
        var contextId = contextRecords.results[0].id;

        await getMysqlQuery(`INSERT INTO mdl_user_enrolments (status, enrolid, userid, 
                              timestart, timeend, timecreated, timemodified) VALUES 
       (0, ${enrolId}, ${userId}, '${now}', '${now + 60*60*24*2}', '${now}', '${now}')`);

        await getMysqlQuery(`INSERT INTO mdl_role_assignments 
                                    (roleid, contextid, userid, timemodified) 
                               VALUES (5, ${contextId}, '${userId}', '${now}');`);
    }
Eugene
  • 345
  • 1
  • 7

1 Answers1

0

It looks like you are using JavaScript. I have a solution to enroll the student in course with a role in PHP programming language as below.

$plugin = enrol_get_plugin('manual');

$course = $DB->get_record('course', array('id' => $courseid)); // Get course object by courseid

$instance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'));
if (empty($instance)) {
    // Only add an enrol instance to the course if non-existent.
    $enrolid = $plugin->add_instance($course);
    $instance = $DB->get_record('enrol', array('id' => $enrolid));
}

// Take care of timestart/timeend in course settings.
   $timestart = time();
   // Remove time part from the timestamp and keep only the date part.
   $timestart = make_timestamp(date('Y', $timestart), date('m', $timestart), date('d', $timestart), 0, 0, 0);
   if ($instance->enrolperiod) {
       $timeend = $timestart + $instance->enrolperiod;
   } else {
       $timeend = 0;
   }
    // Enrol the user with this plugin instance.
    $plugin->enrol_user($instance, $user->id, $roleid); // This will enroll user in course with roleid you provided.
halfer
  • 19,824
  • 17
  • 99
  • 186
Sonil Gandhi
  • 179
  • 11