 <p>You should use <code>add_moduleinfo()</code> instead of inserting records manually. This will do most of the work for you.</p> <p>For an example see <a href="http://stackoverflow.com/questions/32696295/create-moodle-activities-programmatically/32733290#32733290">Create Moodle activities programmatically</a></p>