Programmatically Creating Courses

Programmatically Creating Courses

by Amy Tucker -
Number of replies: 4

We've written a script which programmatically adds courses to Moodle, these look to import fine and show correctly.  


However when we try to enrol students nothing happens.  We've tested and if we create a course in moodle we can enrol students without any issues.

I assume we've either got a setting wrong when importing course, or we are missing data.

At the moment we are creating a record in the mdl_course table and mdl_context tables, we also add records to mdl_course_sections after we have added the course acivities.

Can anyone point me in the direction of why the course aren't adding? 

Average of ratings: -
In reply to Amy Tucker

Re: Programmatically Creating Courses

by Darko Miletić -

You are doing it the wrong way. The right way for doing this is to use Moodle API since it contains all logic required to properly create a course.

I suggest to take a look at /course/edit.php to see what is being executed for course creation.

$course = create_course($data, $editoroptions);

// Get the context of the newly created course.
$context = context_course::instance($course->id, MUST_EXIST);




Average of ratings: Useful (2)
In reply to Amy Tucker

Re: Programmatically Creating Courses

by Marina Glancy -
Picture of Core developers Picture of Moodle HQ Picture of Moodle Workplace team Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hello Amy, I think you are re-inventing the bicycle and you may end up with square wheels.

Except for the comment about the method create_course() I would recommend you to use the script that already exists in Moodle: https://docs.moodle.org/28/en/Upload_courses

Average of ratings: Useful (1)
In reply to Marina Glancy

Re: Programmatically Creating Courses

by Tulix Velásquez -

i have a question, if i need a 1000 courses, the option "upload_course"  work ?