【问题标题】:Uploading a PDF (or any file) under a course in Moodle Programmatically以编程方式在 Moodle 中的课程下上传 PDF(或任何文件)
【发布时间】:2019-11-17 23:40:40
【问题描述】:

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

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

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

感谢任何意见。

【问题讨论】:

    标签: api pdf moodle


    【解决方案1】:

    对此没有一个简单、快速的答案,因为 Moodle 文件 API 只是问题的一半 - 另一半是为每个文件添加一个“资源”模块的实例到课程中,所以用户可以通过某种方式访问​​您添加的文件。

    对于每个文件,您需要:

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

    如果不为你写出所有代码,我不能再详细说明了。

    【讨论】:

      【解决方案2】:

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

      @davos

      如果不为您写出所有代码,我就无法详细说明。

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

      我得到的是:

      $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 没有记录。

      非常欢迎任何提示。

      【讨论】:

      • 找到 $moduleinfo 所需的内容并不难 - github.com/moodle/moodle/blob/master/course/modlib.php#L44 (至于'未记录' - Moodle 可能没有完整记录它的每个部分,但有很多PHPDocs 和 Moodle 文档中的许多内容 - 如果您觉得缺少某些内容,欢迎您对其进行扩展)。
      • 嘿伙计,感谢您的快速回复。我仍然不知道应该把 $file 放在哪里。它应该是 $moduleinfo 对象的一部分吗?它应该是一个新的 Resources 对象吗?
      • 这就是您从传入的通用数据移动到 add_moduleinfo 的所有活动和资源模块的特定数据的地方。您需要将文件放入“草稿”文件区域,然后将该区域的 ID 作为“文件”字段传递。看看我在 course/dndupload.php 中做了什么,然后在 repository/upload/lib.php 中调用 process_upload() - 这可能比您需要的更多,但这是一个很好的起点。
      • 你的意思是受保护的函数 handle_file_upload() { ?如果我设法通过它,我会看看并编辑我的回复。如果没有,我想我们的运营商将需要手动上传 pdf...:/
      • 是的 - 在 handle_file_upload() 里面 - 你不能直接使用它,但你应该能够查看它获取“上传”存储库以将文件放入草稿区域的方式,然后将其传递给 mod_resource,后者在函数 resource_dndupload_handle() 中将“draftitemid”存储到“文件”中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-20
      • 2010-12-31
      • 1970-01-01
      • 2023-04-11
      相关资源
      最近更新 更多