簡體   English   中英

在moodle中編輯文件管理器時如何檢索文件

[英]how to retrieve files when editing a file manager in moodle

我目前正在實習,我嘗試制作一個活動模塊來顯示文件管理員提供的視頻中的播放列表。 我成功將視頻發送到數據庫,但是當我想編輯模塊時,它在文件管理器中不顯示任何視頻。

我閱讀了有關文件API的moodle文檔,並決定使用以下代碼(將現有文件加載到草稿區)

 if (empty($entry->id)) {
$entry = new stdClass;
$entry->id = null;
}

$draftitemid = file_get_submitted_draft_itemid('attachments');
file_prepare_draft_area($draftitemid, $context->id, 'mod_glossary','attachment', $entry->id,array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 50));
$entry->attachments = $draftitemid;
$mform->set_data($entry);

所以我在我的mod_form.php中放了以下幾行:

  $filemanager_options = array();
    $filemanager_options['accepted_types'] = '*';
    $filemanager_options['maxbytes'] = 0;
    $filemanager_options['maxfiles'] = -1;
    $filemanager_options['mainfile'] = true;

        $mform->addElement('filemanager', 'files', get_string('selectfiles'), null, $filemanager_options);



if (empty($entry->id)) {
    $entry = new stdClass;
    $entry->id = null;
}

$draftitemid = file_get_submitted_draft_itemid('mymanager');


file_prepare_draft_area($draftitemid, $this->context->id, 'mod_playlist', 'content', 0,
                      array('subdirs'=>true));

$entry->attachments = $draftitemid;

$mform->set_data($entry);

問題是文件管理器仍然是空的,行“ $ mform-> set_data($ entry);”使頁面崩潰(空白)。

這是用於上傳文件的模板。

在local / myplugin / upload.php中

require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
require_once(dirname(__FILE__) . '/upload_form.php');

require_login();

$context = context_system::instance();

require_capability('local/myplugin:upload', $context);

$pageurl = new moodle_url('/local/myplugin/upload.php');
$heading = get_string('myupload', 'local_myplugin');
$PAGE->set_context($context);
$PAGE->set_heading(format_string($heading));
$PAGE->set_title(format_string($heading));
$PAGE->set_url('/local/myplugin/upload.php');

echo $OUTPUT->header();
echo $OUTPUT->heading($heading);

$fileoptions = array(
        'maxbytes' => 0,
        'maxfiles' => '1',
        'subdirs' => 0,
        'context' => context_system::instance()
);

$data = new stdClass();

$data = file_prepare_standard_filemanager($data, 'myfiles',
        $fileoptions, context_system::instance(), 'local_myplugin', 'myfiles', 0); // 0 is the item id.

$mform = new upload_form(
    null,
    array(
        'fileoptions' => $fileoptions,
    )
);

if ($formdata = $mform->get_data()) {
    // Save the file.
    $data = file_postupdate_standard_filemanager($data, 'myfiles',
            $fileoptions, context_system::instance(), 'local_myplugin', 'myfiles', 0);
} else {
    // Display the form.
    $mform->set_data($data);
    $mform->display();
}

echo $OUTPUT->footer();

然后在local / myplugin / upload_form.php

defined('MOODLE_INTERNAL') || die;

require_once($CFG->libdir . '/formslib.php');

class upload_form extends moodleform {

    public function definition() {
        $mform =& $this->_form;

        $fileoptions = $this->_customdata['fileoptions'];

        $mform->addElement('filemanager', 'myfiles_filemanager',
                get_string('myfiles', 'local_myplugin'), null, $fileoptions);

        $this->add_action_buttons(false, get_string('save', 'local_myplugin'));

    }
}

您還將在/local/myplugin/lib.php中需要它

function local_myplugin_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {

    if ($context->contextlevel != CONTEXT_SYSTEM) {
        send_file_not_found();
    }

    $fs = get_file_storage();
    $file = $fs->get_file($context->id, 'local_myplugin', $filearea, $args[0], '/', $args[1]);

    send_stored_file($file);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM