How to get activity list into same order as on the course?

How to get activity list into same order as on the course?

by Juho Jaakkola -
Number of replies: 2

I have a custom database query that lists activities throughout a specific course category.

I can get them easily ordered by the courses, but I'm having trouble getting the activities to the same order as on each course.

I found out that the course_sections table holds the sequence of the activities within a single section. The data is however saved as a string e.g. "71,123,532" so it cannot be used directly for ordering. Especially when my query contains activities from multiple courses.

Are there any tricks that I could use to achieve my goal?

Average of ratings: -
In reply to Juho Jaakkola

Re: How to get activity list into same order as on the course?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If you are within Moodle, $modinfo = get_fast_modinfo($course) will retrieve all the course activities in the correct order from the cache (so, usually, no DB access needed at all).

$modinfo->get_cms() will give you an ordered list of cm_info objects.

If you want to use a pure-DB approach to find this, then you're going to need to start parsing that field in the course_sections table ...

In reply to Juho Jaakkola

Re: How to get activity list into same order as on the course?

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

Davo's answer is the right one if you can process this one course at a time in PHP. Bear in mind that get_fast_modinfo is only 'fast' for the current course; if you do it for multiple courses in a loop it will be relatively slow. However this may well be acceptable.

If you really have to do it purely in the database, it should still be possible (albeit complicated and possibly not too performant) to write a db query that will split up the comma-separated list into an array or rowset and turn each entry into a number etc etc. Storing it as a comma-separated list makes it hard to write that query but I don't think it makes it impossible.

--sam