plugins - 使用文件管理器的新插件模块中的 Moodle 开发错误

标签 plugins module moodle

如果您能帮助我解决我的问题,我正在 Moodle (v. 2.4) (mod_problem) 中开发一个新模块,作为我硕士论文的一部分。

mod_form 中,我包含了一个文件管理器元素,如下所示:

//** file
//-------------------------------------------------------
$mform->addElement('header', 'content', get_string('problemfile', 'problem'));
$mform->addElement('filemanager', 'problemfile', get_string('files'), null, array('subdirs'=>1, 'accepted_types'=>'*'));

//-------------------------------------------------------

在预处理方法中我添加了以下内容:

function data_preprocessing(&$default_values) {
if ($this->current->instance) {
// editing existing instance - copy existing files into draft area
$draftitemid = file_get_submitted_draft_itemid('problemfile');
file_prepare_draft_area($draftitemid, $this->context->id, 'mod_problem', 'content', 0, array('subdirs'=>1, 'accepted_types'=>'*'));
$default_values['problemfile'] = $draftitemid;
}
}

然后在我的模块的 lib.php 文件中添加以下内容:

$draftitemid = $problem->problemfile; 
$problem->id = $DB->insert_record('problem', $problem);
$cmid = $problem->coursemodule;

$context = context_module::instance($cmid);

if (!empty($problem->problemfile)) {
$draftitemid = $problem->problemfile;
file_save_draft_area_files($draftitemid, $context->id, 'mod_problem', 'content', 0, array('subdirs'=>1, 'accepted_types'=>'*'));
}

我还创建了 (mod_problem_pluginfile()) 函数,其中包括:

$fs = get_file_storage();

$filename = array_pop($args);
$filepath = $args ? '/'.implode('/', $args).'/' : '/';

if (!$file = $fs->get_file($context->id, 'mod_problem', 'content', 0, $filepath, $filename) or $file->is_directory()) {
send_file_not_found();
}

send_stored_file($file, 0, 0, true, array('preview' => $preview));

然后当我想在view.php中打印学生的文件列表时:

require_once($CFG->libdir.'/filelib.php');
require_once($CFG->dirroot.'/repository/lib.php');

$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'mod_problem', 'content', 0);
//try 2
foreach ($files as $file) {
$filename = $file->get_filename();
$url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(),
$file->get_filearea(),$file->get_itemid(), $file->get_filepath(), $filename, false);
$out[] = html_writer::link($url, $filename);
}
$br = html_writer::empty_tag('br');

echo implode($br, $out);

当我查看模块时,我得到了带有链接的文件列表,但是当我单击链接时,它给出了此错误:抱歉,找不到请求的文件。当我读取未找到的文件时,它停止在 mod_problem_pluginfile() 函数中

这是我得到的错误:

//---------------------------------
Debug info: 
Error code: filenotfound
Stack trace:
line 476 of \lib\setuplib.php: moodle_exception thrown
line 1977 of \lib\filelib.php: call to print_error()
line 412 of \mod\problem\lib.php: call to send_file_not_found()
line 4314 of \lib\filelib.php: call to mod_problem_pluginfile()
line 38 of \pluginfile.php: call to file_pluginfile()
//------------------------------------------

我不确定为什么它不读取文件,即使它们已存储。

非常感谢您提供任何帮助,因为我已经没有时间来解决此类问题了。

最佳答案

看来您没有发布完整的 mod_problem_pluginfile() 函数。

if (!$file = $fs->get_file($context->id, 'mod_problem', 'content', 0, $filepath, $filename) or $file->is_directory()) {
    send_file_not_found();
}

这可能是您的 $context->id,请确保它与您调用的 file_save_draft_area_files() 相同

关于plugins - 使用文件管理器的新插件模块中的 Moodle 开发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22817009/

相关文章:

c# - 检索实现给定接口(interface)的对象列表

javascript - 在同一页面上分享这个倍数

java - DDD - java 9 模块项目组织

python - Flask 教程故障排除 : ModuleNotFoundError: No module named 'app'

url - 登录失败时将用户从 Moodle 重定向到另一个站点

proxy - 带有反向代理的 Moodle 3.5

cordova - ionic / Cordova : How to integrate Cordova Plugins into existing Ionic project?

plugins - Gradle:重新初始化/重新加载settings.gradle

python - 尝试导入请求或漂亮的汤时出现非常奇怪的错误

php - Moodle数据库连接错误