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

使用update_moduleinfo触发‘无效课程模块ID’错误的求助

Moodle update_moduleinfo 报"invalid course module id" 问题排查方案

以下是针对性的排查和解决方向:

  • 检查$cm对象完整性:get_coursemodule_from_instance返回的对象可能缺失id等核心字段,建议用var_dump($cm)输出确认,必须确保$cm->id存在且与数据库course_modules表中对应条目一致。
  • 规范$moduleinfo参数结构:update_moduleinfo要求$moduleinfo包含module(需与$cm->module匹配)、instance、course等核心字段,且参数类型要符合对应Moodle版本要求(部分版本要求为数组而非对象)。
  • 验证操作权限:调用该接口需要当前用户拥有课程活动管理权限,可通过has_capability('moodle/course:manageactivities', context_course::instance($course->id))提前校验,权限不足会触发内部验证失败。
  • 重新拉取最新$cm对象:如果之前对$cm做过修改,建议重新从数据库拉取最新数据,避免使用已篡改的对象传入接口。
  • 核对版本兼容性:不同Moodle版本的update_moduleinfo参数逻辑有差异,比如部分版本需要传入$mform=null作为第三个参数,建议对照对应版本的官方文档调整。

修正后的示例代码:

// 先验证活动实例合法性
$instance = $DB->get_record($moduleName, ['id' => $activityID, 'course' => $course->id]);
if (!$instance) {
    throw new moodle_exception('invalidinstance', 'error');
}

// 获取完整且有效的课程模块对象
$cm = get_coursemodule_from_instance($moduleName, $activityID, $course->id, false, MUST_EXIST);

// 构建符合要求的moduleinfo数组
$moduleinfo = [
    'id' => $cm->id,
    'module' => $cm->module,
    'instance' => $activityID,
    'course' => $course->id,
    // 按需添加需要更新的字段
    'name' => '更新后的考勤活动名称',
    'visible' => 1
];

// 尝试调用更新接口
try {
    $updated_cm = update_moduleinfo($moduleinfo, $course);
} catch (moodle_exception $e) {
    debugging('更新失败:' . $e->getMessage(), DEBUG_DEVELOPER);
}

内容的提问来源于stack exchange,提问作者maxi hraschan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 15:45:43