Module 1: Moodle Architecture

Understanding the Core Structure and Lifecycle

1. Directory Structure

Moodle's root directory contains several key folders:

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".

$context = context_course::instance($courseid);
require_capability('mod/quiz:view', $context);

4. The Request Lifecycle

Every Moodle page follows a similar lifecycle:

  1. Bootstrap: require('config.php') loads database settings and core libraries.
  2. Session Init: User session is started or resumed.
  3. Auth Check: require_login() ensures the user is authenticated.
  4. Setup: $PAGE object is configured (URL, Context, Title).
  5. Logic: The script performs its specific logic (DB queries, processing).
  6. 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]

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.