1. Directory Structure
Moodle's root directory contains several key folders:
/admin: Administration tools./auth: Authentication plugins./blocks: Side blocks./course: Course management code./lib: Core libraries and APIs./local: Local customizations (best place for site-wide hacks)./mod: Activity modules (Assignment, Quiz, Forum)./theme: Visual themes.
2. The Global $PAGE and $OUTPUT
Moodle uses global objects to manage the page state and rendering.
// Setting up the page
$PAGE->set_url('/local/myplugin/index.php');
$PAGE->set_context(context_system::instance());
$PAGE->set_title('My Plugin Page');
$PAGE->set_heading('Welcome to My Plugin');
// Outputting content
echo $OUTPUT->header();
echo $OUTPUT->heading('Hello World');
echo $OUTPUT->footer();
3. Contexts and Permissions
Permissions in Moodle are hierarchical based on "Contexts".
- System: Global level.
- Category: Course category.
- Course: Specific course.
- Module: Specific activity (e.g., a Quiz).
- Block: Specific block.
- User: User profile.
$context = context_course::instance($courseid);
require_capability('mod/quiz:view', $context);
4. The Request Lifecycle
Every Moodle page follows a similar lifecycle:
- Bootstrap:
require('config.php')loads database settings and core libraries. - Session Init: User session is started or resumed.
- Auth Check:
require_login()ensures the user is authenticated. - Setup:
$PAGEobject is configured (URL, Context, Title). - Logic: The script performs its specific logic (DB queries, processing).
- Output:
$OUTPUT->header()starts the HTML output, followed by content and footer.
5. The Component System (Frankenstyle)
Moodle uses a strict naming convention called "Frankenstyle" to manage thousands of plugins without collision.
Format: [plugin_type]_[plugin_name]
- mod_forum: Activity module (Forum).
- block_html: Block (HTML block).
- auth_ldap: Authentication plugin (LDAP).
- local_myplugin: Local plugin (Custom).
- theme_boost: Theme (Boost).
This prefix is used in function names (mod_forum_view), language strings, and CSS classes.
6. File Storage Internals
Moodle does not store uploaded files with their original names in the file system. This prevents directory traversal attacks and filename collisions.
- Content-Addressable Storage: Files are renamed to the SHA1 hash of their content.
- Directory Hashing: A file with hash
abcde...is stored inmoodledata/filedir/ab/cd/abcde.... - Database: The
mdl_filestable stores the metadata (filename, path, mime type) and links it to the content hash.