Plugins 带有文件管理器的新插件模块中出现Moodle开发错误

Plugins 带有文件管理器的新插件模块中出现Moodle开发错误,plugins,module,moodle,Plugins,Module,Moodle,如果你能帮我解决这个问题,我正在Moodle(2.4版)中开发一个新模块(mod_problem),作为我硕士论文的一部分 在mod_表单中I包含一个文件管理器元素,如下所示: //** file //------------------------------------------------------- $mform->addElement('header', 'content', get_string('problemfile', 'problem')); $mform->

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

mod_表单中
I包含一个文件管理器元素,如下所示:

//** 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\u 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\u problem\u 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\u problem\u 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\u save\u draft\u area\u files()相同。

您的错误跟踪与您发布的代码不匹配。将整个脚本发布到公共存储库中会很有帮助。函数
mod\u problem\u pluginfile
函数中的最后一个参数应传递给
send\u storaged\u file
函数,而不是硬编码值
数组('preview'=>$preview)
。这只是一个猜测。