Moodle resource / file database structure

Moodle resource / file database structure

by Rob Blagden -
Number of replies: 2

Hi,

I’m trying to work out how database tables link together in the back end of Moodle.

I can see that our mdl_resources lists documents associated with a course and that the file detail is stored in mdl_files but I can’t see how these files are logically linked back through to a specific course.

I’m wondering if contextid is the link from the file, but can’t see where that links to a courseid. 

Any help would be appreciated.

Thanks

Rob

Average of ratings: -
In reply to Rob Blagden

Re: Moodle resource / file database structure

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Moodle has a number of contexts arranged as a hierarchy (follow the link for more information).

The resource activity stores files in the 'module' context, which is a child of the 'course' context, so to find all the files that are linked to a course, you'd need to look at the id of the course context (the context record with instanceid = courseid and contextlevel = CONTEXT_COURSE (50, off the top of my head, but I'd have to check the code)), then you'd need to find the id of all context records that have that course context as a parent (this will include both activities and blocks).

To get from a contextid to an activity instance, look at the 'instanceid' of the context record (which will have contextlevel = CONTEXT_MODULE (70, off the top of my head)), this is the 'course module' id (which will match the id at the end of /mod/resource/view.php?id=XX ).
Average of ratings: Useful (1)