Awesome-omni-skills moodle-external-api-development
Moodle External API Development workflow skill. Use this skill when the user needs This skill guides you through creating custom external web service APIs for Moodle LMS, following Moodle's external API framework and coding standards and the operator should preserve the upstream workflow, copied support files, and provenance before merging or handing off.
git clone https://github.com/diegosouzapw/awesome-omni-skills
T=$(mktemp -d) && git clone --depth=1 https://github.com/diegosouzapw/awesome-omni-skills "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/moodle-external-api-development" ~/.claude/skills/diegosouzapw-awesome-omni-skills-moodle-external-api-development && rm -rf "$T"
skills/moodle-external-api-development/SKILL.mdMoodle External API Development
Overview
This public intake copy packages
plugins/antigravity-awesome-skills-claude/skills/moodle-external-api-development from https://github.com/sickn33/antigravity-awesome-skills into the native Omni Skills editorial shape without hiding its origin.
Use it when the operator needs the upstream workflow, support files, and repository context to stay intact while the public validator and private enhancer continue their normal downstream flow.
This intake keeps the copied upstream files intact and uses
metadata.json plus ORIGIN.md as the provenance anchor for review.
Moodle External API Development This skill guides you through creating custom external web service APIs for Moodle LMS, following Moodle's external API framework and coding standards.
Imported source sections that did not map cleanly to the public headings are still preserved below or in the support files. Notable imported sections: Core Architecture Pattern, Advanced Patterns, Testing Your API, Common Pitfalls & Solutions, Debugging Checklist, Plugin Structure Checklist.
When to Use This Skill
Use this section as the trigger filter. It should make the activation boundary explicit before the operator loads files, runs commands, or opens a pull request.
- Creating custom web services for Moodle plugins
- Implementing REST/AJAX endpoints for course management
- Building APIs for quiz operations, user tracking, or reporting
- Exposing Moodle functionality to external applications
- Developing mobile app backends using Moodle
- Use when the request clearly matches the imported source intent: This skill guides you through creating custom external web service APIs for Moodle LMS, following Moodle's external API framework and coding standards.
Operating Table
| Situation | Start here | Why it matters |
|---|---|---|
| First-time use | | Confirms repository, branch, commit, and imported path before touching the copied workflow |
| Provenance review | | Gives reviewers a plain-language audit trail for the imported source |
| Workflow execution | | Starts with the smallest copied file that materially changes execution |
| Supporting context | | Adds the next most relevant copied source file without loading the entire package |
| Handoff decision | | Helps the operator switch to a stronger native skill when the task drifts |
Workflow
This workflow is intentionally editorial and operational at the same time. It keeps the imported source useful to the operator while still satisfying the public intake standards that feed the downstream enhancer flow.
- Class must extend external_api
- Namespace follows: localpluginname\external or modmodname\external
- Include the security check: defined('MOODLE_INTERNAL') || die();
- Require externallib.php for base classes
- PARAM_INT - Integers
- PARAM_TEXT - Plain text (HTML stripped)
- PARAM_RAW - Raw text (no cleaning)
Imported Workflow Notes
Imported: Step-by-Step Implementation
Step 1: Create the External API Class File
Location:
/local/yourplugin/classes/external/your_api_name.php
<?php namespace local_yourplugin\external; defined('MOODLE_INTERNAL') || die(); require_once("$CFG->libdir/externallib.php"); use external_api; use external_function_parameters; use external_single_structure; use external_value; class your_api_name extends external_api { // Three required methods will go here }
Key Points:
- Class must extend
external_api - Namespace follows:
orlocal_pluginname\externalmod_modname\external - Include the security check:
defined('MOODLE_INTERNAL') || die(); - Require externallib.php for base classes
Step 2: Define Input Parameters
public static function execute_parameters() { return new external_function_parameters([ 'userid' => new external_value(PARAM_INT, 'User ID', VALUE_REQUIRED), 'courseid' => new external_value(PARAM_INT, 'Course ID', VALUE_REQUIRED), 'options' => new external_single_structure([ 'includedetails' => new external_value(PARAM_BOOL, 'Include details', VALUE_DEFAULT, false), 'limit' => new external_value(PARAM_INT, 'Result limit', VALUE_DEFAULT, 10) ], 'Options', VALUE_OPTIONAL) ]); }
Common Parameter Types:
- IntegersPARAM_INT
- Plain text (HTML stripped)PARAM_TEXT
- Raw text (no cleaning)PARAM_RAW
- Boolean valuesPARAM_BOOL
- Floating point numbersPARAM_FLOAT
- Alphanumeric with extended charsPARAM_ALPHANUMEXT
Structures:
- Single valueexternal_value
- Object with named fieldsexternal_single_structure
- Array of itemsexternal_multiple_structure
Value Flags:
- Parameter must be providedVALUE_REQUIRED
- Parameter is optionalVALUE_OPTIONAL
- Optional with defaultVALUE_DEFAULT, defaultvalue
Step 3: Implement Business Logic
public static function execute($userid, $courseid, $options = []) { global $DB, $USER; // 1. Validate parameters $params = self::validate_parameters(self::execute_parameters(), [ 'userid' => $userid, 'courseid' => $courseid, 'options' => $options ]); // 2. Check permissions/capabilities $context = \context_course::instance($params['courseid']); self::validate_context($context); require_capability('moodle/course:view', $context); // 3. Verify user access if ($params['userid'] != $USER->id) { require_capability('moodle/course:viewhiddenactivities', $context); } // 4. Database operations $sql = "SELECT id, name, timecreated FROM {your_table} WHERE userid = :userid AND courseid = :courseid LIMIT :limit"; $records = $DB->get_records_sql($sql, [ 'userid' => $params['userid'], 'courseid' => $params['courseid'], 'limit' => $params['options']['limit'] ]); // 5. Process and return data $results = []; foreach ($records as $record) { $results[] = [ 'id' => $record->id, 'name' => $record->name, 'timestamp' => $record->timecreated ]; } return [ 'items' => $results, 'count' => count($results) ]; }
Critical Steps:
- Always validate parameters using
validate_parameters() - Check context using
validate_context() - Verify capabilities using
require_capability() - Use parameterized queries to prevent SQL injection
- Return structured data matching return definition
Step 4: Define Return Structure
public static function execute_returns() { return new external_single_structure([ 'items' => new external_multiple_structure( new external_single_structure([ 'id' => new external_value(PARAM_INT, 'Item ID'), 'name' => new external_value(PARAM_TEXT, 'Item name'), 'timestamp' => new external_value(PARAM_INT, 'Creation time') ]) ), 'count' => new external_value(PARAM_INT, 'Total items') ]); }
Return Structure Rules:
- Must match exactly what
returnsexecute() - Use appropriate parameter types
- Document each field with description
- Nested structures allowed
Step 5: Register the Service
Location:
/local/yourplugin/db/services.php
<?php defined('MOODLE_INTERNAL') || die(); $functions = [ 'local_yourplugin_your_api_name' => [ 'classname' => 'local_yourplugin\external\your_api_name', 'methodname' => 'execute', 'classpath' => 'local/yourplugin/classes/external/your_api_name.php', 'description' => 'Brief description of what this API does', 'type' => 'read', // or 'write' 'ajax' => true, 'capabilities'=> 'moodle/course:view', // comma-separated if multiple 'services' => [MOODLE_OFFICIAL_MOBILE_SERVICE] // Optional ], ]; $services = [ 'Your Plugin Web Service' => [ 'functions' => [ 'local_yourplugin_your_api_name' ], 'restrictedusers' => 0, 'enabled' => 1 ] ];
Service Registration Keys:
- Full namespaced class nameclassname
- Always 'execute'methodname
- 'read' (SELECT) or 'write' (INSERT/UPDATE/DELETE)type
- Set true for AJAX/REST accessajax
- Required Moodle capabilitiescapabilities
- Optional service bundlesservices
Step 6: Implement Error Handling & Logging
private static function log_debug($message) { global $CFG; $logdir = $CFG->dataroot . '/local_yourplugin'; if (!file_exists($logdir)) { mkdir($logdir, 0777, true); } $debuglog = $logdir . '/api_debug.log'; $timestamp = date('Y-m-d H:i:s'); file_put_contents($debuglog, "[$timestamp] $message\n", FILE_APPEND | LOCK_EX); } public static function execute($userid, $courseid) { global $DB; try { self::log_debug("API called: userid=$userid, courseid=$courseid"); // Validate parameters $params = self::validate_parameters(self::execute_parameters(), [ 'userid' => $userid, 'courseid' => $courseid ]); // Your logic here self::log_debug("API completed successfully"); return $result; } catch (\invalid_parameter_exception $e) { self::log_debug("Parameter validation failed: " . $e->getMessage()); throw $e; } catch (\moodle_exception $e) { self::log_debug("Moodle exception: " . $e->getMessage()); throw $e; } catch (\Exception $e) { // Log detailed error info $lastsql = method_exists($DB, 'get_last_sql') ? $DB->get_last_sql() : '[N/A]'; self::log_debug("Fatal error: " . $e->getMessage()); self::log_debug("Last SQL: " . $lastsql); self::log_debug("Stack trace: " . $e->getTraceAsString()); throw $e; } }
Error Handling Best Practices:
- Wrap logic in try-catch blocks
- Log errors with timestamps and context
- Capture SQL queries on database errors
- Preserve stack traces for debugging
- Re-throw exceptions after logging
Imported: Core Architecture Pattern
Moodle external APIs follow a strict three-method pattern:
- Defines input parameter structureexecute_parameters()
- Contains business logicexecute()
- Defines return structureexecute_returns()
Examples
Example 1: Ask for the upstream workflow directly
Use @moodle-external-api-development to handle <task>. Start from the copied upstream workflow, load only the files that change the outcome, and keep provenance visible in the answer.
Explanation: This is the safest starting point when the operator needs the imported workflow, but not the entire repository.
Example 2: Ask for a provenance-grounded review
Review @moodle-external-api-development against metadata.json and ORIGIN.md, then explain which copied upstream files you would load first and why.
Explanation: Use this before review or troubleshooting when you need a precise, auditable explanation of origin and file selection.
Example 3: Narrow the copied support files before execution
Use @moodle-external-api-development for <task>. Load only the copied references, examples, or scripts that change the outcome, and name the files explicitly before proceeding.
Explanation: This keeps the skill aligned with progressive disclosure instead of loading the whole copied package by default.
Example 4: Build a reviewer packet
Review @moodle-external-api-development using the copied upstream files plus provenance, then summarize any gaps before merge.
Explanation: This is useful when the PR is waiting for human review and you want a repeatable audit packet.
Imported Usage Notes
Imported: Examples from Real Implementation
Simple Read API (Get Quiz Attempts)
<?php namespace local_userlog\external; defined('MOODLE_INTERNAL') || die(); require_once("$CFG->libdir/externallib.php"); use external_api; use external_function_parameters; use external_single_structure; use external_value; class get_quiz_attempts extends external_api { public static function execute_parameters() { return new external_function_parameters([ 'userid' => new external_value(PARAM_INT, 'User ID'), 'courseid' => new external_value(PARAM_INT, 'Course ID') ]); } public static function execute($userid, $courseid) { global $DB; self::validate_parameters(self::execute_parameters(), [ 'userid' => $userid, 'courseid' => $courseid ]); $sql = "SELECT COUNT(*) AS quiz_attempts FROM {quiz_attempts} qa JOIN {quiz} q ON qa.quiz = q.id WHERE qa.userid = :userid AND q.course = :courseid"; $attempts = $DB->get_field_sql($sql, [ 'userid' => $userid, 'courseid' => $courseid ]); return ['quiz_attempts' => (int)$attempts]; } public static function execute_returns() { return new external_single_structure([ 'quiz_attempts' => new external_value(PARAM_INT, 'Total number of quiz attempts') ]); } }
Complex Write API (Create Quiz from Categories)
See attached
create_quiz_from_categories.php for a comprehensive example including:
- Multiple database insertions
- Course module creation
- Quiz instance configuration
- Random question selection with tags
- Group-based access restrictions
- Extensive error logging
- Transaction management
Best Practices
Treat the generated public skill as a reviewable packaging layer around the upstream repository. The goal is to keep provenance explicit and load only the copied source material that materially improves execution.
- Always validate input parameters using validate_parameters()
- Check user context and capabilities before operations
- Use parameterized SQL queries (never string concatenation)
- Implement comprehensive error handling and logging
- Follow Moodle naming conventions (lowercase, underscores)
- Document all parameters and return values clearly
- Test with different user roles and permissions
Imported Operating Notes
Imported: Guidelines
- Always validate input parameters using
validate_parameters() - Check user context and capabilities before operations
- Use parameterized SQL queries (never string concatenation)
- Implement comprehensive error handling and logging
- Follow Moodle naming conventions (lowercase, underscores)
- Document all parameters and return values clearly
- Test with different user roles and permissions
- Consider transaction safety for write operations
- Purge caches after service registration changes
- Keep API methods focused and single-purpose
Troubleshooting
Problem: The operator skipped the imported context and answered too generically
Symptoms: The result ignores the upstream workflow in
plugins/antigravity-awesome-skills-claude/skills/moodle-external-api-development, fails to mention provenance, or does not use any copied source files at all.
Solution: Re-open metadata.json, ORIGIN.md, and the most relevant copied upstream files. Load only the files that materially change the answer, then restate the provenance before continuing.
Problem: The imported workflow feels incomplete during review
Symptoms: Reviewers can see the generated
SKILL.md, but they cannot quickly tell which references, examples, or scripts matter for the current task.
Solution: Point at the exact copied references, examples, scripts, or assets that justify the path you took. If the gap is still real, record it in the PR instead of hiding it.
Problem: The task drifted into a different specialization
Symptoms: The imported skill starts in the right place, but the work turns into debugging, architecture, design, security, or release orchestration that a native skill handles better. Solution: Use the related skills section to hand off deliberately. Keep the imported provenance visible so the next skill inherits the right context instead of starting blind.
Related Skills
- Use when the work is better handled by that native specialization after this imported skill establishes context.@monte-carlo-monitor-creation
- Use when the work is better handled by that native specialization after this imported skill establishes context.@monte-carlo-prevent
- Use when the work is better handled by that native specialization after this imported skill establishes context.@monte-carlo-push-ingestion
- Use when the work is better handled by that native specialization after this imported skill establishes context.@monte-carlo-validation-notebook
Additional Resources
Use this support matrix and the linked files below as the operator packet for this imported skill. They should reflect real copied source material, not generic scaffolding.
| Resource family | What it gives the reviewer | Example path |
|---|---|---|
| copied reference notes, guides, or background material from upstream | |
| worked examples or reusable prompts copied from upstream | |
| upstream helper scripts that change execution or validation | |
| routing or delegation notes that are genuinely part of the imported package | |
| supporting assets or schemas copied from the source package | |
Imported Reference Notes
Imported: Quick Reference: Common Moodle Tables
| Table | Purpose |
|---|---|
| User accounts |
| Courses |
| Activity instances in courses |
| Available activity types (quiz, forum, etc.) |
| Quiz configurations |
| Quiz attempt records |
| Question bank |
| Question categories |
| Gradebook items |
| Student grades |
| Course groups |
| Group memberships |
| Activity logs |
Imported: Additional Resources
- Moodle External API Documentation
- Moodle Coding Style
- Moodle Database API
- Web Services API Documentation
Imported: Advanced Patterns
Complex Database Operations
// Transaction example $transaction = $DB->start_delegated_transaction(); try { // Insert record $recordid = $DB->insert_record('your_table', $dataobject); // Update related records $DB->set_field('another_table', 'status', 1, ['recordid' => $recordid]); // Commit transaction $transaction->allow_commit(); } catch (\Exception $e) { $transaction->rollback($e); throw $e; }
Working with Course Modules
// Create course module $moduleid = $DB->get_field('modules', 'id', ['name' => 'quiz'], MUST_EXIST); $cm = new \stdClass(); $cm->course = $courseid; $cm->module = $moduleid; $cm->instance = 0; // Will be updated after activity creation $cm->visible = 1; $cm->groupmode = 0; $cmid = add_course_module($cm); // Create activity instance (e.g., quiz) $quiz = new \stdClass(); $quiz->course = $courseid; $quiz->name = 'My Quiz'; $quiz->coursemodule = $cmid; // ... other quiz fields ... $quizid = quiz_add_instance($quiz, null); // Update course module with instance ID $DB->set_field('course_modules', 'instance', $quizid, ['id' => $cmid]); course_add_cm_to_section($courseid, $cmid, 0);
Access Restrictions (Groups/Availability)
// Restrict activity to specific user via group $groupname = 'activity_' . $activityid . '_user_' . $userid; // Create or get group if (!$groupid = $DB->get_field('groups', 'id', ['courseid' => $courseid, 'name' => $groupname])) { $groupdata = (object)[ 'courseid' => $courseid, 'name' => $groupname, 'timecreated' => time(), 'timemodified' => time() ]; $groupid = $DB->insert_record('groups', $groupdata); } // Add user to group if (!$DB->record_exists('groups_members', ['groupid' => $groupid, 'userid' => $userid])) { $DB->insert_record('groups_members', (object)[ 'groupid' => $groupid, 'userid' => $userid, 'timeadded' => time() ]); } // Set availability condition $restriction = [ 'op' => '&', 'show' => false, 'c' => [ [ 'type' => 'group', 'id' => $groupid ] ], 'showc' => [false] ]; $DB->set_field('course_modules', 'availability', json_encode($restriction), ['id' => $cmid]);
Random Question Selection with Tags
private static function get_random_questions($categoryid, $tagname, $limit) { global $DB; $sql = "SELECT q.id FROM {question} q INNER JOIN {question_versions} qv ON qv.questionid = q.id INNER JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid INNER JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid JOIN {tag_instance} ti ON ti.itemid = q.id JOIN {tag} t ON t.id = ti.tagid WHERE LOWER(t.name) = :tagname AND qc.id = :categoryid AND ti.itemtype = 'question' AND q.qtype = 'multichoice'"; $qids = $DB->get_fieldset_sql($sql, [ 'categoryid' => $categoryid, 'tagname' => strtolower($tagname) ]); shuffle($qids); return array_slice($qids, 0, $limit); }
Imported: Testing Your API
1. Via Moodle Web Services Test Client
- Enable web services: Site administration > Advanced features
- Enable REST protocol: Site administration > Plugins > Web services > Manage protocols
- Create service: Site administration > Server > Web services > External services
- Test function: Site administration > Development > Web service test client
2. Via curl
# Get token first curl -X POST "https://yourmoodle.com/login/token.php" \ -d "username=admin" \ -d "password=yourpassword" \ -d "service=moodle_mobile_app" # Call your API curl -X POST "https://yourmoodle.com/webservice/rest/server.php" \ -d "wstoken=YOUR_TOKEN" \ -d "wsfunction=local_yourplugin_your_api_name" \ -d "moodlewsrestformat=json" \ -d "userid=2" \ -d "courseid=3"
3. Via JavaScript (AJAX)
require(['core/ajax'], function(ajax) { var promises = ajax.call([{ methodname: 'local_yourplugin_your_api_name', args: { userid: 2, courseid: 3 } }]); promises[0].done(function(response) { console.log('Success:', response); }).fail(function(error) { console.error('Error:', error); }); });
Imported: Common Pitfalls & Solutions
1. "Function not found" Error
Solution:
- Purge caches: Site administration > Development > Purge all caches
- Verify function name in services.php matches exactly
- Check namespace and class name are correct
2. "Invalid parameter value detected"
Solution:
- Ensure parameter types match between definition and usage
- Check required vs optional parameters
- Validate nested structure definitions
3. SQL Injection Vulnerabilities
Solution:
- Always use placeholder parameters (
):paramname - Never concatenate user input into SQL strings
- Use Moodle's database methods:
,get_record()
, etc.get_records()
4. Permission Denied Errors
Solution:
- Call
early in execute()self::validate_context($context) - Check required capabilities match user's permissions
- Verify user has role assignments in the context
5. Transaction Deadlocks
Solution:
- Keep transactions short
- Always commit or rollback in finally blocks
- Avoid nested transactions
Imported: Debugging Checklist
- Check Moodle debug mode: Site administration > Development > Debugging
- Review web services logs: Site administration > Reports > Logs
- Check custom log files in
$CFG->dataroot/local_yourplugin/ - Verify database queries using
$DB->set_debug(true) - Test with admin user to rule out permission issues
- Clear browser cache and Moodle caches
- Check PHP error logs on server
Imported: Plugin Structure Checklist
local/yourplugin/ ├── version.php # Plugin version and metadata ├── db/ │ ├── services.php # External service definitions │ └── access.php # Capability definitions (optional) ├── classes/ │ └── external/ │ ├── your_api_name.php # External API implementation │ └── another_api.php # Additional APIs ├── lang/ │ └── en/ │ └── local_yourplugin.php # Language strings └── tests/ └── external_test.php # Unit tests (optional but recommended)
Imported: Limitations
- Use this skill only when the task clearly matches the scope described above.
- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.