Programmatically Creating Courses

Programmatically Creating Courses

Amy Tucker -
Erantzun kopurua: 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? 

Puntuazioen batez bestekoa: -
Amy Tucker(e)ri erantzunda

Re: Programmatically Creating Courses

Darko Miletić -
Core developers-ren irudia Moodle HQ-ren irudia Plugin developers-ren irudia

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);




Puntuazioen batez bestekoa:Useful (2)
Amy Tucker(e)ri erantzunda

Re: Programmatically Creating Courses

Marina Glancy -
Core developers-ren irudia Moodle Workplace team-ren irudia Peer reviewers-ren irudia Plugin developers-ren irudia Testers-ren irudia

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

Puntuazioen batez bestekoa:Useful (1)
Marina Glancy(e)ri erantzunda

Re: Programmatically Creating Courses

Tulio Velásquez -

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