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

如何在R中将R-exams试题导入Moodle课程实例外的分类

关于R exams包导入Moodle题库层级与XML结构的解决方案

一、直接导入更高层级课程分类的设置方法

默认情况下exams2moodle生成的XML会绑定到课程实例上下文,可通过以下两种R端设置实现直接导入更高层级分类:

1. 元信息+函数参数组合设置

在试题.Rmd的元信息头中,指定完整的目标分类路径,例如要导入到STAT100下的子分类:

exsection: "STAT100/统计基础章节"

调用exams2moodle时添加context = NULL参数,强制XML不绑定到特定课程实例:

exams2moodle("stat_question.Rmd", context = NULL)

生成的XML在Moodle导入时,即使未取消“从文件获取上下文”,也会按指定路径直接导入到STAT100层级。

2. 自定义XML生成逻辑(进阶)

封装自定义函数,手动修改XML中的分类标签,跳过课程实例上下文:

custom_exams2moodle <- function(file, target_category = "STAT100", ...) {
  # 生成原始XML
  raw_xml <- exams2moodle(file, ...)
  # 替换分类路径,移除$course$前缀并替换为目标层级
  updated_xml <- gsub("<category><text>$course$/(.*)</text></category>",
                      paste0("<category><text>", target_category, "/\\1</text></category>"),
                      raw_xml)
  return(updated_xml)
}

# 使用示例
final_xml <- custom_exams2moodle("stat_question.Rmd", target_category = "STAT100")
writeLines(final_xml, "stat_question_updated.xml")

二、修改exsection在XML中的位置

默认exsection会附在$course$之后(即<category><text>$course$/xxx</text></category>),可通过以下方式调整:

1. 生成后直接替换XML内容

生成XML后,用字符串替换移除$course$前缀,让exsection直接对应目标层级:

mdl_xml <- exams2moodle("stat_question.Rmd")
# 移除$course$前缀
clean_xml <- gsub("<category><text>$course$/(.*)</text></category>",
                  "<category><text>\\1</text></category>",
                  mdl_xml)
writeLines(clean_xml, "stat_question_clean.xml")

2. 临时重写包内函数(灵活定制)

重写exams包的moodle_category函数,直接生成不含$course$的分类标签:

# 自定义moodle_category函数,跳过$course$前缀
moodle_category <- function(section, context = NULL) {
  section <- if(is.null(section)) "" else section
  category <- if(nzchar(section)) section else "Default"
  paste0("<category><text>", xml2::xml_escape_char(category), "</text></category>")
}

# 将自定义函数绑定到exams包命名空间
environment(moodle_category) <- asNamespace("exams")
assignInNamespace("moodle_category", moodle_category, ns = "exams")

之后调用exams2moodle生成的XML中,exsection将直接作为分类路径,不再带有$course$前缀。

注意:重写包内函数属于临时方案,更新exams包后需重新执行该代码。


内容的提问来源于stack exchange,提问作者Alex Michael

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 18:05:01