How does Moodle create topics and activities in php?

Re: How does Moodle create topics and activities in php?

by Michael Hughes -
Number of replies: 0
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
>Honestly, for a Moo newbie, this is the deep end

For a Moodle pro, this scares me too! We tried to do stuff like this a few years ago and it left us with a real mess to clear up.

In addition to sort of understanding where the activity will go in the course, taking into account how the course's course format is doing things, you'd also need to know all of the parameters on the post request, both the ones specific to the activity you're adding but also all of the "core" shared attributes that may or may not need to be specified (and you probably also need to be aware of any defaults that would be set if you don't explicitly set them in *your* code).

It's worth looking at the Unit Data data generators as they basically do this so that activities can be created programmatically for unit testing, and also have a look in the /course/modlib.php as this now has a whole load of functions that deal with *some* of the shared adding & updating of modules, especially add_moduleinfo() (and update_moduleinfo()), which specifically says in its documentation: "The function creates course module, module instance, add the module to the correct section."