Moodle LMS Administration

Moodle Custom API · Plugin & API Integration

Examine Moodle modular plugin architectures, folder directories topology, custom capabilities mappings, and parameter validation structures conceptually.

Pipeline L: Web Service Interception & Hook Lifecycle

Route
webservice/rest
Client Endpoint Request
Auth & Token
Token Check
Validate Security Keys
Service MAP
db/services.php
Map Custom Functions
Execute
externallib.php
Process Parameters & Return

1. Folder Structures & Concepts

Moodle relies on a strict modular namespace. To build custom web service endpoints, we place our code within a local plugin directory. Let's trace how the directories are organized and explain why each file is critical.

local/courseprogress/ # Root directory of the custom courseprogress plugin
├── version.php # Crucial: Declares version metadata and component signature
├── db/ # Contains database mapping configuration files
│ ├── access.php # Registers custom capability keys inside Moodle database
│ └── services.php # Declares function endpoints, validation methods, and permissions
├── classes/ # Code block containing autoloaded external namespaces
│ └── external/
│ └── progress.php # Class file mapping parameter, logical output structures
└── lang/en/ # Folder containing standard localization language arrays
    └── local_courseprogress.php # Translation file (Required: prevents execution crashes)

Understanding the File Topology

File Target What it Means Why it is Important
version.php Declares plugin component name, release versions, and platform dependencies. Moodle reads this to execute database schema updates. Skipping this prevents the server from recognizing new functions.
db/services.php Registers your function array, parameter verification class, and security layers. This registers endpoints inside mdl_external_functions, binding capabilities and ajax configurations.
classes/external/progress.php Contains the actual processing logic, returning structural parameters. Declares parameter and output schemas using Moodle's built-in validation methods.
db/access.php Defines custom platform permissions or capabilities (like local/courseprogress:view). Allows lock-step user role access checks during API requests, preventing unauthorized data views.
lang/en/local_courseprogress.php Declares user-facing text strings for local translations. Moodle strictly checks for translation files. Missing translation logs will produce severe warning exceptions.

2. Moodle Capabilities & Parameter Validation

Moodle validates all inputs and outputs through the external_api framework. This enforces security and structural integrity before logic executions begin:

Input Parameter Types (PARAM_*)

Moodle web services restrict incoming variables to safe data classes to prevent SQL injections and path traversal exploits:

  • PARAM_INT: Strict numeric values (such as userid or courseid).
  • PARAM_ALPHA: Standard alphabetical strings only (such as status or action).
  • PARAM_TEXT: Raw user-facing description logs.
Capability Access Control Checks

Access keys defined under db/access.php (such as local/courseprogress:view) verify if the request owner holds permission inside the target context_course::instance($courseid). This ensures students cannot query administrative logs.

3. Database Sync & Versioning Loops

API deployment is a continuous iteration cycle. Understanding the distinction between registering a new endpoint and modifying a class is vital to maintaining uptime:

Registering a Brand-New Function

Requires declaring the configuration block inside db/services.php, bumping the decimal version value inside version.php, and running the Moodle database upgrade controller via CLI. Without a version bump, Moodle ignores configuration additions.

Modifying Class Logic Signatures

If you are only updating code logic inside classes/external/progress.php (and the endpoint structure remains identical), database upgrade commands are bypassed. However, you must execute a cache purge command to refresh PHP reflection variables cached in system memory.

API Playground

Looking for the complete, copy-paste codebase files and specification logs for all 13 custom APIs?

Go to Web Service Playground

Conceptual Guidelines

Service Namespace:

Always prefix functions with your component name to avoid class conflicts (e.g. local_courseprogress_).

Capabilities:

Require minimal necessary permissions. Limit admin functions to system scope contexts.