General developer forum

Let's create a working example of a FileManager

 
 
Andrew Normore
Let's create a working example of a FileManager
 

Hi every one, I've been using Moodle for 6 months, and CANNOT figure out how to build a filemanager properly. I've seen many posts about the same topic, so let's provide a working example. 

It's a great feature, poorly documented. (No examples)

 

Download Here

 

This 1 file should:
 - Display a form to user
 - Allow the user to upload an image / file via FileManager
 - Save the form
 - Refreshing the page should display the previous file user uploaded 

 

 

<?php
// ===============
//
// FILE MANAGER EXAMPLE
//
// ===============
// The point of this file is to demonstrate how to manage files within Moodle 2.3
// Written by Andrew Normore
// Why? Because file management is incredibly hard for some reason.


require_once( '../../config.php' );
global $CFG, $USER, $DB, $OUTPUT;

require_login();
$PAGE->set_url('/local/filemanager/_filemanager_test.php');
$PAGE->set_pagelayout( 'admin' );
$context = get_context_instance( CONTEXT_COURSE, SITEID );
$PAGE->set_context( $context );
$strtitle = get_string( 'title', 'local_filemanager' );
$PAGE->set_title( $strtitle );
$PAGE->set_heading( $strtitle );

$tid = optional_param( 'tid', '', PARAM_RAW );

require_login();
print_header('File Manager', 'File Manager', build_navigation(array(array('name'=>'File Manager','link'=>'','type'=>'misc'))));


//DEFINITIONS
require_once($CFG->libdir.'/formslib.php');
require_once($CFG->libdir.'/completionlib.php');

 

 

 

// ===============
//
//
// (LIBRARY) this would usually be include(lib.php)
//
//
// ===============
class simplehtml_form extends moodleform {

function definition() {
  global $CFG;

  $mform =& $this->_form; // Don't forget the underscore!

  $mform->addElement('text', 'email', get_string('email')); // Add elements to your form
  $mform->setType('email', PARAM_NOTAGS); //Set type of element
  $mform->setDefault('email', 'Please enter email'); //Default value

  // FILE MANAGER
  $mform->addElement('filemanager', 'attachments', 'test', null, array('subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 50, 'accepted_types' => array('*') ));

  // Buttons
  $this->add_action_buttons();

  }

  function validation($data, $files) {
    return array();
  }
}

 

 

 

 

 

// ===============
//
//
// FORM LOGIC
//
//
// ===============

$mform = new simplehtml_form();

if ($mform->is_cancelled()) {
  echo '<h1>Cancelled</h1>';
  echo 'Handle form cancel operation, if cancel button is present on form';
} else if ($fromform = $mform->get_data()) {
  echo '<h1>Submit Success!</h1>';
  echo 'In this case you process validated data. $mform->get_data() returns data posted in form.';
} else {
  echo '<h1>Default</h1>';
  echo 'This is the form first display OR "errors"';
  $mform->display();
}


// --------
// Form was valid, process!
// --------

// CONFIGURE FILE MANAGER
// From http://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms#filemanager
if (empty($entry->id)) {
  $entry = new stdClass;
  $entry->id = null;
}

$draftitemid = file_get_submitted_draft_itemid('attachments');

file_prepare_draft_area($draftitemid, $context->id, 'mod_glossary', 'attachment', $entry->id,
array('subdirs' => 0, 'maxbytes' => '0', 'maxfiles' => 50));

$entry->attachments = $draftitemid;

// Set form data
$mform->set_data($entry);

 

 

 


echo $OUTPUT->footer();

 
Average of ratings:Useful (1)
Picture of Tomasz Muras
Re: Let's create a working example of a FileManager
Group DevelopersGroup Particularly helpful MoodlersGroup Plugins guardiansGroup Translators

Very nice initiative Andrew, I know it's not straigh-forward to get it working in 100%.

May I suggest putting your file on github, so we can cooperate efficiently?

I'm interested in the working code snippets like this, as I want to add them to the code generation feature of moosh.

cheers,
Tomek

 
Average of ratings: -
Andrew Normore
Re: Let's create a working example of a FileManager
 

Roger that, I'll create a GitHub now. I'll make the above mentioned code changes and submit. I will reply here shortly.

https://github.com/AndyNormore/MoodleFileManager.git

 

 

 
Average of ratings: -
Davo
Re: Let's create a working example of a FileManager
Group DevelopersGroup Particularly helpful Moodlers

OK, a few comments, as they occur to me:

$PAGE->set_url - you should always call this before you call require_login (otherwise Moodle doesn't know where to take you back to if you've had to visit the login page).

$tid = optional_param('tid', '', PARAM_RAW); // Not quite sure what this line is for - $tid is never used

print_header - this looks like it is left over from Moodle 1.9 - should be: echo $OUTPUT->header();

Down to the forms definition:

$mform  =& $this->_form; // Should not include '&', all objects are passed by reference in PHP 5.

The 'email' bits aren't really relevant to the example, so might be better to leave them out.

Would be better to use 'get_string()' for the name of the 'filemanager' element, rather than 'test'.

It is also a good idea to pass the 'options' for the filemanager into the form via the _customData element (the 2nd paramater when creating a form), as that way they can be defined once outside the form (but that is a minor detail).

$mform->get_data should be called after $mform->set_data (so the whole section labelled 'Form was valid, process!', should be immediately after the '$mform = new simplehtml_form()' bit.

After all this, you then reach the point, just before echo $OUTPUT->footer() where it is the right place to output the form (and also the right place to call echo $OUTPUT->header() )

In terms of calling file_prepare_draft_area, you should be using 'local_filemanager' (the name of your plugin), not 'mod_glossary'.

You will also need, in the processing section, to call 'file_save_draft_area_files() to copy the updated files back out of the draft area and into the 'real' file area.

That should be a reasonable starting point...

 
Average of ratings: -
Andrew Normore
Re: Let's create a working example of a FileManager
 

Thanks. I appreciate your time in being so detailed. I've fixed it up to your specifications. Still not quite working.

 

Please view the source on Github

https://github.com/AndyNormore/filemanager/blob/master/index.php

 
Average of ratings: -
Davo
Re: Let's create a working example of a FileManager
Group DevelopersGroup Particularly helpful Moodlers

I've added a pull request with a fixed version of the code.

I'm sure there are ways it could be tweaked and improved further, but it works.

 
Average of ratings: -
Picture of Howard Miller
Re: Let's create a working example of a FileManager
Group DevelopersGroup Documentation writersGroup Particularly helpful Moodlers

Good move.... I was looking at this recently and the documentation is hardly helpful.

Could I strongly encourage you to improve the documentation if you actually understand it. Yeh - how hard can it be to store a file....

 
Average of ratings: -
Picture of Howard Miller
Re: Let's create a working example of a FileManager
Group DevelopersGroup Documentation writersGroup Particularly helpful Moodlers

This bit...

if (empty($entry->id)) {
  $entry = new stdClass;
  $entry->id = null;
}

....this worries me. Under what circumstances coult $entry->id *not* be empty? It's not set anywhere in previous code. However, that check must be there for some reason.

 
Average of ratings:Useful (1)
Picture of Abdul Bashet
Re: Let's create a working example of a FileManager
 

Hi there,

I was also stugling with filemanager. I was looking for exactly code, so I can understand how it works! But I couldn't find. But now it is very easy and simple for me. I thought it could help some others.

You can find this codes from this link

https://github.com/bashet/local_filemanager

if you install this plugin to your moodle, link1 will show you the page to upload the images and link2 will show the uploaded images.

link1= http://localhost/moodle/local/filemanager/index.php

link2= http://localhost/moodle/local/filemanager/showimages.php

I tried to make it simple; so there should not be any difficulty to understand for anyone who have idea of php code.

Regards,

Abdul Bashet

 

 
Average of ratings:Useful (1)
Davo
Re: Let's create a working example of a FileManager
Group DevelopersGroup Particularly helpful Moodlers

I had a quick glance at the code - most of it looks reasonable, but I'd really avoid using DB queries to search through the 'files' table. In the case you showed, $fs->get_area_files(...) would be far more appropriate.

 
Average of ratings: -
Davo
Re: Let's create a working example of a FileManager
Group DevelopersGroup Particularly helpful Moodlers

An example of where it would not be null:

$entryid = required_param('entryid', PARAM_INT);
$entry = $DB->get_record('myplugin_entry', array('id' => $entryid));

That is, if you have some other data (e.g. a forum post) associated with the files. My updated version of the code may make it a bit clearer:

https://github.com/davosmith/filemanager

 

 
Average of ratings: -
Andrew Normore
Re: Let's create a working example of a FileManager
 

2

 
Average of ratings: -
Picture of Howard Miller
Re: Let's create a working example of a FileManager
Group DevelopersGroup Documentation writersGroup Particularly helpful Moodlers

Ahh... makes sense. Thanks smile

 
Average of ratings: -
Picture of Владимир Зотов
Re: Let's create a working example of a FileManager
 

Thank you for your example!

It was very useful in understanding Moodle File API! 

How can I use this filemanager form to browse files that are "belong" a particular component? For example:

I used your code to make filepicker that upload files into "block_my_block" component in "my_block_docs" file area. There are  many files in this block and file area. But in filemanager form I can see only files uploaded by current user. But I want user can see all files in the block.

Can you help me - how can I do that?


 
Average of ratings: -