How does Moodle create topics and activities in php?

How does Moodle create topics and activities in php?

by James Poly -
Number of replies: 3

I am new with moodle and I would like to know how Moodle creates topics and activities with php language.

In particular I'm interested in how Moodle create an activity subsequent an another.

I know how create an activity with moodle and a subsequent activtity but I don't know how function in php.

Is there a guide?

Cheers,

James


Average of ratings: -
In reply to James Poly

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

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

This is a fairly fundamental part of Moodle's core code, so you're unlikely to find a guide to how it's done, since it's not a part most developers would touch under normal circumstances.

You might find it useful to understand how Course format plugins work: https://docs.moodle.org/dev/Course_formats

More generally, the code Moodle uses for a specific task is usually fairly transparent. There's no "routing" that you see in modern PHP frameworks - each page is a .php script, and if you want to find out how an change performed by a page is done in code, you can look at that page and follow the process through. You might find it useful to have an interactive debugger like Xdebug set up to follow a process through step-by-step.

Average of ratings: Useful (1)
In reply to James Poly

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

by Benjamin Ellis -
Picture of Particularly helpful Moodlers

Hi,

Not very straightforward.  Topics are generally part of the course format - which may not actually create topics - e.g. the social format - see https://docs.moodle.org/dev/Course_formats and activities will generally be created by calling the add_instance() method of the particular activity - so in effect, you would need to create an instance of the activity plugin and pass the correct parameters to that method.   Honestly, for a Moo newbie, this is the deep end smile

In reply to Benjamin Ellis

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

by Michael Hughes -
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."