Using Moodle create users and enroll them in courses via SQL

Viewed 15543

I need to create an external application that creates, modifies and enrol users of Moodle.

I've been reading Moodle documentation but its more for front administrators that for developers. How can I do to create a user? which tables hold obligatory information about them? and how do I enrol existing users to Moodle?

3 Answers

Thanks Aschab for your snippet, it really helped me alot!

Indeed, it inspired me to create a VIEW in Mysql, so you can actually save at least one database call for every user insertion!

CREATE OR REPLACE VIEW v_mdl_meta_enrolment  AS  
SELECT MC.`id` AS course_id,MC.idnumber AS course,E.`id` AS enrol_id,C.`id` AS context_id
FROM ((mdl_course MC join mdl_enrol E) 
JOIN mdl_context C) 
WHERE E.courseid = MC.`id` and E.enrol = 'manual' 
AND C.contextlevel = 50 
AND C.instanceid = MC.`id`;
Related