moodle – Enroll User in a Course


This time for moodlers, have you ever faced such type of need, that you want to enrol user directly in a course using some predefined moodle functions. I was in need for this just after payment success, so thought I should share with you friends..

require("moodle/config.php");

$userid = 'jaswanttak'; // this is user's idnumber
$courseid = 'TEST-01'; // this is course's idnumber

// if you want to use only id than in get_records() you will have to write id in second parameter

$user = get_record("user", "idnumber", $userid);
$course = get_record("course", "idnumber", $courseid);

if (!enrol_into_course($course, $user, 'paypal')) { // paypal is enrollment method for my system you will use yours
echo 'error';
} else {
echo 'success';
}

Cheers,
Jaswant

Advertisements

3 thoughts on “moodle – Enroll User in a Course

  1. Nice to read your blog. Even I am in need of enrolling a teacher and tutor directly to the course. But it seems like ‘enrol_into_course’ method is deprecated and is no longer used. But nice article.

    Also wanted to introduce me. I worked in Trigyn Technologies and I have seen you. Though I have not worked a lot, I still remember you. Came across your blog through google searching for code for enrolling users.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s