You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Moodle插件安装时自动启用Web服务及RESTful协议?

在Moodle插件安装时自动启用Web服务及RESTful协议的方法

一、先说明依赖声明的局限

你在version.php里加的$plugin->dependencies只能保证指定插件(比如webservice_restful)已经安装,但没法自动启用Web服务系统或对应的协议——这些属于Moodle的系统配置项,不是插件依赖关系能覆盖的。

二、自动启用的实现方式

要实现安装时自动开启,得在插件的安装/升级脚本里调用Moodle核心API修改配置:

  1. 修改安装脚本
    在插件的db/install.php文件里加这段代码(已有该文件就追加逻辑):
<?php
// 开启Web服务全局开关
set_config('enablewebservices', 1);

// 启用RESTful协议
external_enable_protocol('restful');

// 可选:如果需要启用默认的"moodle_mobile_app"服务,可加这段
$service = $DB->get_record('external_services', ['shortname' => 'moodle_mobile_app']);
if ($service && !$service->enabled) {
    $service->enabled = 1;
    $DB->update_record('external_services', $service);
}
  1. 升级脚本兼容
    如果后续插件升级也要确保配置生效,把相同逻辑加到db/upgrade.php里,配合版本号判断执行:
<?php
function xmldb_yourpluginname_upgrade($oldversion) {
    global $DB;

    $dbman = $DB->get_manager();

    if ($oldversion < 2024052000) {
        // 启用Web服务及RESTful协议
        set_config('enablewebservices', 1);
        external_enable_protocol('restful');

        upgrade_plugin_savepoint(true, 2024052000, 'yourplugincomponent', 'yourplugintype');
    }

    return true;
}

三、提示用户手动启用的方案

要是不想自动改系统配置(比如尊重管理员权限偏好),可以在安装时或插件设置页加提示:

  1. 安装时弹出警告
    在install.php里检测配置状态,没启用就输出提示:
<?php
if (!get_config('enablewebservices')) {
    \core\notification::warning('请前往【站点管理>服务器>概览>启用Web服务】开启Web服务功能,否则本插件部分功能无法正常使用。');
}

if (!external_is_protocol_enabled('restful')) {
    \core\notification::warning('请前往【站点管理>服务器>Web服务>协议】启用RESTful协议,否则本插件部分功能无法正常使用。');
}
  1. 插件设置页加持久提示
    在插件的settings.php里添加固定提示:
<?php
if (!get_config('enablewebservices')) {
    $setting = new admin_setting_heading('yourplugin_webservice_warning', '',
        '<div class="alert alert-warning">本插件需要启用Web服务功能,请前往站点管理>服务器>概览开启。</div>');
    $settings->add($setting);
}

内容的提问来源于stack exchange,提问作者Syed Saad Ullah Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 18:35:23