problem in my new module with file manager

problem in my new module with file manager

by khulood ali -
Number of replies: 2

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

//** This is the error I got
//---------------------------------
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()

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

 

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

 

Average of ratings: -
In reply to khulood ali

Re: problem in my new module with file manager

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

The easiest way to trace such problems is by installing xdebug + suitable IDE, then putting a breakpoint in your mod_problem_pluginfile.

If you're not able to do that, it would probably help if you used echo / var_dump to output the parameters you are passing into $fs->get_file(...) inside mod_problem_pluginfile.

It would also be helpful to look carefully at the URL you have generated for the file.

However, from experience, I think I can see what is wrong with your code.

There is a difference between passing itemid = null and itemid = 0 into moodle_url::make_pluginfile_url.

If you pass in itemid = null, then the itemid param is completely missed out of the URL. If you pass itemid = 0, then a 0 is added into the URL.

So your URL will either be:

[site]/pluginfile.php/[contextid]/mod_problem/content/[path]/[filename]  (if you pass null)

OR

[site]/pluginfile.php/[contextid]/mod_problem/content/0/[path]/[filename]  (if you pass 0)

Your code appears to be passing itemid = $file->get_itemid() - which will return 0, but your mod_problem_pluginfile code assumes that there is no itemid in the URL and is, I suspect, adding '/0/' onto the start of the filepath.

Two possible solutions - change the make_pluginfile_url code to pass null, instead of get_itemid() OR add $itemid = aray_shift($args) into your mod_problem_pluginfile (before generating the $filepath).

Average of ratings: Useful (1)
In reply to Davo Smith

Re: problem in my new module with file manager

by khulood ali -

Thanks a lot for your solution it works fine now.

However, when I upload a file then download it. When I open the file it give me error that the file is corrupted. Any help with this error is appreciated?