Problem Description
Please if you can help me in my problem, I'm developing a new module in Moodle (v. 2.4) (`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 = h...
AI-Generated Solution
Powered by LMSouq AI · GPT-4.1-mini
Analyzing problem and generating solution…
Was this solution helpful?