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

时间:2014-04-02 16:01:39

标签: 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);

当我查看模块时,我获得了包含链接的文件列表,但是当我点击链接时,它给出了我的错误:Sorry, the requested file could not be found。当我读取未找到的文件时,它会在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()
//------------------------------------------

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

如果我没有时间解决此类问题,请随时提供帮助。

1 个答案:

答案 0 :(得分:0)

好像你没有发布完整的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()

相同
相关问题