title: Moodle development error in new plugin module with file manager
body: <p>Please if you can help me in my problem, I'm developing a new module in Moodle (v. 2.4) (<code>mod_problem</code>), as part of my master thesis.</p>
  
  <p>In the <code>mod_form</code> I included a file manager element as follows:</p>
  
  <pre><code>//** file
  //-------------------------------------------------------
  $mform-&gt;addElement('header', 'content', get_string('problemfile', 'problem'));
  $mform-&gt;addElement('filemanager', 'problemfile', get_string('files'), null, array('subdirs'=&gt;1, 'accepted_types'=&gt;'*'));
  
  //-------------------------------------------------------
  </code></pre>
  
  <p>In the preprocessing method I added this:</p>
  
  <pre><code>function data_preprocessing(&amp;$default_values) {
  if ($this-&gt;current-&gt;instance) {
  // editing existing instance - copy existing files into draft area
  $draftitemid = file_get_submitted_draft_itemid('problemfile');
  file_prepare_draft_area($draftitemid, $this-&gt;context-&gt;id, 'mod_problem', 'content', 0, array('subdirs'=&gt;1, 'accepted_types'=&gt;'*'));
  $default_values['problemfile'] = $draftitemid;
  }
  }
  </code></pre>
  
  <p>Then in the <code>lib.php</code> file of my module I added the following:</p>
  
  <pre><code>$draftitemid = $problem-&gt;problemfile; 
  $problem-&gt;id = $DB-&gt;insert_record('problem', $problem);
  $cmid = $problem-&gt;coursemodule;
  
  $context = context_module::instance($cmid);
  
  if (!empty($problem-&gt;problemfile)) {
  $draftitemid = $problem-&gt;problemfile;
  file_save_draft_area_files($draftitemid, $context-&gt;id, 'mod_problem', 'content', 0, array('subdirs'=&gt;1, 'accepted_types'=&gt;'*'));
  }
  </code></pre>
  
  <p>I also created the (<code>mod_problem_pluginfile()</code>) function that include:</p>
  
  <pre><code>$fs = get_file_storage();
  
  $filename = array_pop($args);
  $filepath = $args ? '/'.implode('/', $args).'/' : '/';
  
  if (!$file = $fs-&gt;get_file($context-&gt;id, 'mod_problem', 'content', 0, $filepath, $filename) or $file-&gt;is_directory()) {
  send_file_not_found();
  }
  
  send_stored_file($file, 0, 0, true, array('preview' =&gt; $preview));
  </code></pre>
  
  <p>Then when I want to print the file list for students in <code>view.php</code>:</p>
  
  <pre><code>require_once($CFG-&gt;libdir.'/filelib.php');
  require_once($CFG-&gt;dirroot.'/repository/lib.php');
  
  $fs = get_file_storage();
  $files = $fs-&gt;get_area_files($context-&gt;id, 'mod_problem', 'content', 0);
  //try 2
  foreach ($files as $file) {
  $filename = $file-&gt;get_filename();
  $url = moodle_url::make_pluginfile_url($file-&gt;get_contextid(), $file-&gt;get_component(),
  $file-&gt;get_filearea(),$file-&gt;get_itemid(), $file-&gt;get_filepath(), $filename, false);
  $out[] = html_writer::link($url, $filename);
  }
  $br = html_writer::empty_tag('br');
  
  echo implode($br, $out);
  </code></pre>
  
  <p>when I view the module I got the list of files with the links but when I click in the link it gives me this error: <code>Sorry, the requested file could not be found</code>. It stop in the <code>mod_problem_pluginfile()</code> function when I read the files it is not found</p>
  
  <p>This is the error I got:</p>
  
  <pre><code>//---------------------------------
  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()
  //------------------------------------------
  </code></pre>
  
  <p>I'm not sure why it's not reading the files, even though they are stored.</p>
  
  <p>Please any help with this is very appreciated as I'm running out of time in fixing such problem.</p>