以编程方式在 Moodle 中的课程下上传 PDF(或任何文件)

Uploading a PDF (or any file) under a course in Moodle Programmatically

我的公司要求我创建一个引擎,该引擎将在 Moodle 课程下移动我们系统中的所有文件(材料主要是 PDF 文件)。

我已将所有文件复制到 moodledata 目录中的一个文件夹下,并且我有一个 excel sheet,其中包含所有文件名和路径以及课程(在 Moodle 下创建的课程)是相关的。

我需要的是如何使用文件 API 来 link 某个文件到某个课程,以便该文件显示在 Moodle 的课程下。

欢迎任何意见。

对此没有一个简单、快速的答案,因为 Moodle 文件 API 只是问题的一半——另一半是将 'resource' 模块的实例添加到当然,对于每个文件,因此用户可以通过某种方式访问​​您添加的文件。

对于每个文件,您需要:

  1. 创建草稿文件区
  2. 将文件添加到草稿文件区(使用文件API和$fs->create_file_from_pathname()函数)
  3. 使用要创建的 'resource' 模块实例的详细信息调用函数 add_moduleinfo()(这将传递给函数 add_resource_instance,后者将提取从草稿区中提取文件并将其存储在需要的位置)。

如果不为您写出所有代码,我无法详细介绍。

嘿,这对我来说仍然很重要!

@达沃斯

I can't go into much more detail without writing out all the code for you.

真的吗?伙计,你是在 Whosebug 上发帖,而不是在你大学的留言板上发帖:(

我得到的是:

$pdf_output = $dompdf->output();


$fs = get_file_storage();
$context = context_course::instance($course->id);
// Prepare file record object
$fileinfo = array(
    'contextid' => $context->id, // ID of context
    'component' => 'mod_mymod', // usually = table name
    'filearea' => 'mymod',     // usually = table name
    'itemid' => $id,   // usually = ID of row in table
    'filepath' => '/',           // any path beginning and ending in /
    'filename' => 'test.pdf'); // any filename

$file = $fs->get_file($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'],
        $fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename']);

// Delete it if it exists
if ($file) {
    $file->delete();
}

$file = $fs->create_file_from_string($fileinfo, $pdf_output);

$file = $fs->get_file($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'],
                      $fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename']);

// Read contents
if ($file) {
  $contents = $file->get_content();
  var_dump($contents);
}

而且我可以读取内容并将它们转储到浏览器。 我想不通的是如何使用 "add_moduleinfo()" 将我的 pdf 存储在课程下。

函数签名是:

function add_moduleinfo($moduleinfo, $course, $mform = null) {

唉,没有解释 $moduleinfo 应该包含什么,因为 moodle 没有文档。

非常欢迎任何提示。