Moodle Plugin Development 标志

Moodle Plugin Development

社区
SaadRahman01
moodle-plugin-development

Use when creating, modifying, upgrading, or reviewing Moodle plugins (local, mod, block, format, theme, auth, enrol, report, qtype, filter, repository) - covers version.php, db/install.xml + upgrade.php, db/access.php capabilities, db/services.php web services, lang strings, classes/ PSR-4 autoloading, lib.php hooks, settings.php, privacy provider, and Moodle coding standards.

概览

发布者SaadRahman01
代码仓库claude-moodle-dev
技能名称moodle-plugin-development
星标数
42
复刻数
7
附带文件
仅含指令
许可协议MIT
链接
  • Markdown 格式的指令

    模型按需加载 SKILL.md 文件,因此只有在请求确实与该技能匹配时才会消耗 Token。

  • 兼容任何大语言模型

    AI 技能采用纯 Markdown,而非特定提供商的代码,因此可搭配 GPT、Claude、Gemini、Grok 或本地模型使用。

  • 指令自成一体

    模型所需的一切都包含在指令中,无需同步额外文件。

  • 开源

    由 SaadRahman01 发布于 GitHub。请在安装前查看技能的源文件。

安装

在 TypingMind 中安装 Moodle Plugin Development AI 技能,即可搭配任意大语言模型使用;也可以将其放入其他支持读取 SKILL.md 的智能体中。

1

安装到 TypingMind

TypingMind 可直接从 GitHub 文件夹安装技能:读取 SKILL.md,打包资源文件,并将结果保存在本地。

  1. 打开应用,前往“插件 → 技能”。
  2. 选择“从 GitHub 安装”。
  3. 粘贴下方的技能文件夹 URL 并确认。
    https://github.com/SaadRahman01/claude-moodle-dev/tree/main/skills/moodle-plugin-development
  4. 请在每个需要使用该技能的聊天中启用它。
依次选择“插件 → 技能 → 添加技能 → 从 GitHub URL 导入”,然后粘贴文件夹 URL 并点击“继续”。
2

安装到其他智能体

任何支持 Agent Skills 格式的智能体都可以使用此技能,只需将文件夹复制到该智能体的技能目录中。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/SaadRahman01/claude-moodle-dev.git /tmp/claude-moodle-dev
mkdir -p .claude/skills
cp -r /tmp/claude-moodle-dev/skills/moodle-plugin-development .claude/skills/moodle-plugin-development
复制后,请重启 Claude Code,以加载新技能。

在 TypingMind 中使用此技能

在任意 TypingMind 聊天中启用 Moodle Plugin Development,后续即可交由模型处理。技能的名称和描述会放在系统提示词中,一旦请求匹配,模型就会自动加载完整指令,无需您手动调用,且在实际使用前不会消耗 Token。

一旦请求与技能匹配,模型就会自动加载 Moodle Plugin Development。

兼容任何 AI 模型

AI 技能是纯 Markdown 指令,而非特定提供商的代码,因此 Moodle Plugin Development 不局限于最初适配的模型。只需在 TypingMind 中安装一次,即可搭配 GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 或您自行运行的本地模型使用,全程使用您自己的 API 密钥。

  • 仅在需要时加载

    系统提示词中仅包含技能名称和描述。首次遇到匹配的请求时才会加载完整指令,因此技能未使用时,这些指令不会消耗 Token。

  • 在聊天中切换模型

    技能是指令而非代码,因此切换模型不会导致技能失效,新模型会读取同一个 SKILL.md。

技能指令

这是模型会加载的 SKILL.md 内容。请在安装前阅读,因为技能就是模型将遵循的指令。

Moodle Plugin Development

Overview

Moodle plugins follow strict frankenstyle naming and a fixed file layout. Every plugin lives under a type-specific directory (local/, mod/, blocks/, course/format/, theme/, auth/, enrol/, report/, question/type/, filter/, repository/, etc.) and is identified by <type>_<name> (e.g., local_school, mod_quiz, format_schoolgram).

Core principle: Moodle code must declare defined('MOODLE_INTERNAL') || die(); at top of every PHP file (except classes under classes/ autoloaded via PSR-4), use $DB for all DB access, use get_string() for user-facing text, and bump version.php for any DB or capability change.

When to Use

  • Creating new plugin under local/, mod/, blocks/, course/format/, theme/, etc.
  • Adding/modifying DB tables → db/install.xml + db/upgrade.php
  • Adding capabilities → db/access.php
  • Adding web services / external functions → db/services.php + classes/external/
  • Adding scheduled tasks → db/tasks.php + classes/task/
  • Adding events / observers → db/events.php + classes/event/
  • Adding admin settings page → settings.php
  • Translating strings → lang/<lang>/<component>.php
  • Reviewing PR for Moodle coding-style compliance
  • Bumping version.php after schema/capability change

Skip when: working purely on frontend AMD modules without backend changes, or non-Moodle PHP code.

Plugin Skeleton

Minimum files for a local_<name> plugin:

local/<name>/
  version.php                    # required: component, version, requires, maturity
  lang/en/local_<name>.php       # required: 'pluginname' string
  lib.php                        # optional: hooks (extend_navigation, pluginfile, etc.)
  settings.php                   # optional: admin settings page
  db/
    install.xml                  # DB schema (edit via /admin/tool/xmldb/)
    upgrade.php                  # versioned upgrade steps
    access.php                   # capabilities
    services.php                 # web service definitions
    tasks.php                    # scheduled tasks
    events.php                   # event observers
    caches.php                   # cache definitions
  classes/                       # PSR-4: \local_<name>\foo\bar -> classes/foo/bar.php
    external/                    # external (web service) functions
    task/                        # scheduled task classes
    event/                       # custom events
    privacy/provider.php         # GDPR provider (REQUIRED)
  templates/                     # Mustache templates
  amd/src/                       # ES modules (built via grunt -> amd/build/)
  tests/                         # PHPUnit + Behat

version.php Template

php
<?php
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'local_example';      // frankenstyle, must match dir
$plugin->version   = 2026042500;           // YYYYMMDDXX, bump on any db/capability change
$plugin->requires  = 2024100700;           // min Moodle version (4.5 LTS); use 2025041400 for 5.0+, 2025100600 for 5.1+, 2026042000 for 5.2+
$plugin->release   = '1.0.0';
$plugin->maturity  = MATURITY_STABLE;      // ALPHA | BETA | RC | STABLE
$plugin->dependencies = ['mod_quiz' => 2024100700];  // optional

db/install.xml + upgrade.php

  • Edit install.xml via Moodle XMLDB editor (Site admin -> Development -> XMLDB editor) — never hand-edit.
  • Every schema change requires:
    1. Bump $plugin->version in version.php
    2. Add upgrade step in db/upgrade.php keyed on old version:
php
function xmldb_local_example_upgrade($oldversion) {
    global $DB;
    $dbman = $DB->get_manager();

    if ($oldversion < 2026042500) {
        $table = new xmldb_table('local_example_items');
        $field = new xmldb_field('status', XMLDB_TYPE_INTEGER, '4', null,
            XMLDB_NOTNULL, null, '0', 'name');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_plugin_savepoint(true, 2026042500, 'local', 'example');
    }
    return true;
}

db/access.php Capabilities

php
$capabilities = [
    'local/example:view' => [
        'captype'      => 'read',
        'contextlevel' => CONTEXT_SYSTEM,
        'archetypes'   => [
            'user'           => CAP_ALLOW,
            'editingteacher' => CAP_ALLOW,
        ],
    ],
];

Runtime check: require_capability('local/example:view', $context); or has_capability(...).

db/services.php (Web Services / AJAX)

php
$functions = [
    'local_example_get_items' => [
        'classname'    => 'local_example\external\get_items',
        'methodname'   => 'execute',
        'description'  => 'Return items',
        'type'         => 'read',
        'ajax'         => true,
        'capabilities' => 'local/example:view',
    ],
];

External class extends \core_external\external_api (Moodle 4.2+; older = external_api) and defines execute_parameters(), execute(), execute_returns().

Lang Strings

lang/en/local_example.php:

php
$string['pluginname']    = 'Example';
$string['example:view']  = 'View example';        // capability strings: <plugin>:<cap>
$string['greeting']      = 'Hello, {$a->name}';   // placeholders

Use: get_string('greeting', 'local_example', ['name' => $user->firstname]);

DB Access — Always $DB

php
global $DB;
$DB->get_record('local_example_items', ['id' => $id], '*', MUST_EXIST);
$DB->get_records_sql('SELECT * FROM {local_example_items} WHERE status = ?', [1]);
$DB->insert_record('local_example_items', $obj);
$DB->update_record('local_example_items', $obj);
$DB->delete_records('local_example_items', ['id' => $id]);

Never mysqli_* / PDO. Always use {tablename} placeholder (Moodle prefixes). Always bind params, never concatenate.

Hooks via lib.php

Common Moodle callbacks (function name = <component>_<hookname>):

  • local_example_extend_navigation(global_navigation $nav) — add nav nodes
  • local_example_before_http_headers() — runs before headers
  • local_example_extend_settings_navigation($settingsnav, $context)
  • local_example_pluginfile($course, $cm, $context, $filearea, $args, $forcedl, $options) — serve file area

Moodle 4.4+: prefer the new hooks API (\core\hook\manager) over magic callbacks where available.

Output — Renderers + Templates

  • HTML via $OUTPUT->render_from_template('local_example/foo', $data) (Mustache: templates/foo.mustache)
  • Custom renderer: classes/output/renderer.php extends plugin_renderer_base
  • Never echo raw HTML in business logic; always go through renderer or template

Privacy (GDPR) — Required

Every plugin must declare privacy. Minimum (no user data stored):

php
// classes/privacy/provider.php
namespace local_example\privacy;
defined('MOODLE_INTERNAL') || die();
class provider implements \core_privacy\local\metadata\null_provider {
    public static function get_reason(): string {
        return 'privacy:metadata';
    }
}

If plugin stores user data, implement \core_privacy\local\request\plugin\provider and define get_metadata, export_user_data, delete_data_for_user_in_context, delete_data_for_users, get_contexts_for_userid, get_users_in_context.

Quick Reference

TaskFileBump version?
Add DB table/fielddb/install.xml + db/upgrade.phpYes
Add capabilitydb/access.php + lang stringYes
Add web service fndb/services.php + classes/external/Yes
Add scheduled taskdb/tasks.php + classes/task/Yes
Add event observerdb/events.phpYes
Add cache definitiondb/caches.phpYes
Add stringlang/en/<component>.phpNo
Add settingssettings.phpNo
Add templatetemplates/*.mustacheNo
Add rendererclasses/output/renderer.phpNo

Coding Standards

  • 4-space indent, no tabs
  • Opening <?php on line 1, no closing ?> at end of pure PHP files
  • defined('MOODLE_INTERNAL') || die(); immediately after license block (skip in classes/ PSR-4 files)
  • snake_case for functions/vars, PascalCase for classes
  • Run vendor/bin/phpcs --standard=moodle <path> before commit (local_codechecker plugin or moodle-cs ruleset)
  • All user-facing strings via get_string(), never hard-coded English
  • GPL-3.0-or-later license header on every PHP file

Common Mistakes

MistakeFix
Hand-editing install.xmlUse XMLDB editor at /admin/tool/xmldb/
Forgetting to bump version.php after schema changeBump + add upgrade.php step + upgrade_plugin_savepoint
Using raw SQL with {} quoting wrongUse {tablename} and ? / named params, never string concat
MOODLE_INTERNAL check inside classes/ PSR-4 fileRemove — autoloaded files don't need it
Storing user data without privacy providerImplement \core_privacy\local\request\plugin\provider
Hard-coded English strings in PHP/MustacheMove to lang/en/<component>.php + get_string / {{#str}}
Forgetting require_capability() on entry pointsAlways require_login() + capability check early
Not purging caches after version.php bumpphp admin/cli/upgrade.php or Site admin -> Purge caches
Direct $_GET/$_POST accessUse required_param() / optional_param() with type
Missing sesskey() check on state-changing requestsCall require_sesskey() on POST handlers

Plugin Type Cheatsheet

TypeDirFrankenstyleExtra required files
Locallocal/<name>local_<name>none beyond skeleton
Activity modulemod/<name>mod_<name>mod_form.php, view.php, lib.php w/ <name>_add_instance etc.
Blockblocks/<name>block_<name>block_<name>.php extends block_base
Course formatcourse/format/<name>format_<name>format.php, lib.php extends core_courseformat\base
Themetheme/<name>theme_<name>config.php, scss/, layout/
Authauth/<name>auth_<name>auth.php extends auth_plugin_base
Enrolenrol/<name>enrol_<name>lib.php extends enrol_plugin
Question typequestion/type/<name>qtype_<name>questiontype.php, question.php, renderer.php
Filterfilter/<name>filter_<name>filter.php extends moodle_text_filter
Repositoryrepository/<name>repository_<name>lib.php extends repository

Security Checklist

  • require_login() (and require_capability()) at top of every entry script
  • require_sesskey() on every state-changing POST
  • required_param($name, PARAM_INT) / optional_param(...) — never raw $_REQUEST
  • Use $DB->... with placeholders — no SQL concat
  • Escape output: s(), format_string(), format_text()
  • File serving via pluginfile.php + <component>_pluginfile() callback, never direct path

CLI Helpers

bash
php admin/cli/upgrade.php --non-interactive    # apply pending upgrades
php admin/cli/purge_caches.php                  # clear all caches
php admin/tool/behat/cli/init.php               # init behat tests
vendor/bin/phpunit --testsuite local_example_testsuite
vendor/bin/phpcs --standard=moodle local/example

Testing

  • PHPUnit: tests/<thing>_test.php extending advanced_testcase, use $this->resetAfterTest(), generators via self::getDataGenerator()->get_plugin_generator('local_example')
  • Behat: tests/behat/*.feature with @local_example tag, step definitions in tests/behat/behat_local_example.php

References

常见问题

Moodle Plugin Development AI 技能有什么作用?

Use when creating, modifying, upgrading, or reviewing Moodle plugins (local, mod, block, format, theme, auth, enrol, report, qtype, filter, repository) - covers version.php, db/install.xml + upgrade.php, db/access.php capabilities, db/services.php web services, lang strings, classes/ PSR-4 autoloading, lib.php hooks, settings.php, privacy provider, and Moodle coding standards.

为什么在 TypingMind 中使用 Moodle Plugin Development?

因为您只需安装一次,就能搭配任何模型使用。Moodle Plugin Development 采用纯 Markdown 格式,而非特定提供商专用的代码,因此同一技能可用于 GPT-5、Claude、Gemini、Grok 或本地模型。您甚至可以在聊天中途切换模型,而不会影响技能的正常使用。TypingMind 使用您自己的 API 密钥,因此您直接向提供商付费,而不是按席位订阅;您的技能和聊天记录也都保留在您自己的存储空间中。

如何在 TypingMind 中安装 Moodle Plugin Development?

在 TypingMind 中打开“插件 → 技能 → 从 GitHub 安装”,然后粘贴 https://github.com/SaadRahman01/claude-moodle-dev/tree/main/skills/moodle-plugin-development。TypingMind 会读取其中的 SKILL.md,并将其安装为技能,供您在各个聊天中按需启用。

哪些 AI 模型可以使用 Moodle Plugin Development?

您在 TypingMind 中连接的任何模型都可以。AI 技能采用纯 Markdown 指令,而非特定提供商专用的代码,因此 GPT、Claude、Gemini、Grok 和本地模型都能在请求匹配时加载此技能。

Moodle Plugin Development 可以搭配多少个 AI 模型使用?

数量不限。只要模型支持技能,您就可以将它与 Moodle Plugin Development 搭配使用。GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 等模型都可以通过您自己的 API 密钥接入 TypingMind。

Moodle Plugin Development AI 技能免费吗?

是的。SaadRahman01 已在 GitHub 上根据 MIT 许可证发布了该技能。您只需向自己的 AI 提供商支付所用 Token 的费用。

什么是 AI 技能?

AI 技能是一组可复用的指令,用于教会 AI 模型完成某项特定任务。它遵循开放的 Agent Skills 格式:包含名称和描述的 SKILL.md 文件,以及模型可能需要的脚本、模板或参考文件。模型仅在您的请求与技能匹配时才会读取这些指令,因此已安装的技能在使用前不会产生任何费用。

AI 技能与插件或 MCP 服务器有何不同?

插件或 MCP 服务器为模型提供可调用的新工具,也就是在某处运行并返回结果的代码。AI 技能则为模型提供知识和流程:如何处理任务、遵循哪些步骤,以及怎样的输出才算优质。技能采用纯 Markdown 格式,因此不需要服务器、API 密钥或运行时环境,并且适用于任何模型。

查看全部

立即创建您自己的 AI 工作区

订阅我们的电子简报,获取新功能和后续赠奖活动的通知 👇