Dear all,
please if you can help me in my problem, I'm developing a new module in Moodle (mod_problem), as part of my master thesis.
****in the mod_form I included a file manager element as follows:
//** file
//-------------------------------------------------------
$mform->addElement('header', 'content', get_string('problemfile', 'problem'));
$mform->addElement('filemanager', 'problemfile', get_string('files'), null, array('subdirs'=>1, 'accepted_types'=>'*'));
//-------------------------------------------------------
***In the preprocessing method I added this:
//------------------------------------------------------------
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;
}
}
//--------------------------------------------------------------------------
***Then in the lib.php file of my module I added the following:
//------------------------------------------------------------------
$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'=>'*'));
}
//-----------------------------------
**** I also created the (mod_problem_pluginfile()) function that include
//---------------
$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));
//---------------
***Then when I want to print the file list for students in 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);
//------------------------------------
***when I view the module I got the list of files with the links but when I click in the link it give me error
(Sorry, the requested file could not be found) It stop in the mod_problem_pluginfile() function when I read the files it is not found
Error code: filenotfound
- 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()
//------------------------------------------
I'm not sure why its not reading the file, eventhough they are stored??
Please any help with this is very appriciated as I'm running out of time in fixing such problem.
Thanks to all