我正在尝试为文件上传创建一个moodle配置文件字段插件
我edit_field_add
field.class
中的function edit_field_add(&$mform) {
$maxlength = 1024*1024;
$fieldtype = $this->field->param2;
/// Create the form field
$mform->addElement('filepicker', $this->inputname, format_string($this->field->name), null,
array('maxbytes' => $maxlength, 'accepted_types' => $fieldtype));
$mform->setType($this->inputname,PARAM_FILE);
}
是:
data
这会正确显示和保存文件但是
在插件的766686554
字段中保存一个数字(例如display_data
)
如何找到上传文件的URL以通过'field.class`中的function edit_save_data_preprocess($data, &$datarecord) {
$draftitemid=file_get_submitted_draft_itemid($this->inputname);
if (empty($entry->id)) {
$entry = new stdClass;
$entry->id = 0;
}
$context = context_user::instance($this->userid);
file_save_draft_area_files($draftitemid, $context->id, 'profile_field_fileupload', $this->inputname,$entry->id);
return $draftitemid;
}
建立链接?
修改
我正在保存文件:
{{1}}
但草案仍然存在,我无法找到如何检索已保存的文件!
答案 0 :(得分:1)
假设您在表单发布后保存了文件 - http://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms#filepicker
然后,您可以使用File API检索文件 - http://docs.moodle.org/dev/File_API#Serving_files_to_users
例如,显示插件的所有文件的链接:
$out = array();
$fs = get_file_storage();
$files = $fs->get_area_files($contextid, $pluginname, $pluginfolder);
foreach ($files as $file) {
$filename = $file->get_filename();
$url = moodle_url::make_file_url('/pluginfile.php', array(
$file->get_contextid(),
$pluginname,
$pluginfolder,
$file->get_itemid(),
$file->get_filepath(), $filename)
);
$out[] = html_writer::link($url, $filename);
}
$br = html_writer::empty_tag('br');
echo implode($br, $out);