File Manager

File Manager

by Antonio Garmendía -
Number of replies: 25

Hi everyone,

I'm working with the file manager and I have two problems. The first is that I need to reduce the width and I don't know how and the second one is that I want the file manager hidden the first time I call and then show it when the user clic on the button. Hope you can help me. Thanks in advance.

Cheers,

Tony

Average of ratings: -
In reply to Antonio Garmendía

Re: File Manager

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Dear Tony,

The File Manager is used all over the place and in different contexts - and therefore with different styling.  So, could you say more about where you are using it with possibly a screen shot to show the problem.

Cheers,

Gareth

In reply to Gareth J Barnard

Re: File Manager

by Antonio Garmendía -

Sorry, I have to explain more the problem. The first thing I did it, is trying to call the file manager with and ajax call, didn't work. After that I call the file manager in the php function and it work!! but is too width and I need to reduce it. I'll trace the css and change the width and overwrite css(see the picture) but I want that the textarea and filemanager be next to each other. The other thing is that i want to hide and show the filemanager, with ajax if is possible... Thanks in advance!!!

 

Cheers,

Tony

Attachment filemanager.jpg
In reply to Antonio Garmendía

Re: File Manager

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Dear Tony,

Thanks for the screen shot, but 'where' are you using the file manager - label, assignment, theme settings.... ?

Then that would help place the problem in context and give a hint at how the AJAX or similar could be implemented.  Perhaps JavaScript 'display: none' type toggle might be better.

At the moment I'm now not sure if this is a course format's or theme's issue so will leave in this forum for the moment.

Cheers,

Gareth

In reply to Gareth J Barnard

Re: File Manager

by Antonio Garmendía -

Dear Gareth,

 

I'm using the file manager to create a course format and the users can share files among them. I use the following code:

 

<?php
    include_once($CFG->libdir . '/formslib.php');
    class resource_form extends moodleform {
    
        function definition()
        {
            global $CFG,$USER,$COURSE,$OUTPUT;
            $mform = & $this->_form;    
            // FILE MANAGER
            $mform->addElement('filemanager', 'attachments', 'A&ntildeadir Recurso', null,
                    array('subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 10,
                    'accepted_types' => array('document','.jpg')));
            
            //echo $mform->{action};
             //var_dump($mform);
             //Buttons
             $buttons = array();
             $buttons[] =& $mform->createElement('submit', 'bbs_save_file', 'Guardar Archivo');
                         
             //Add Group
             $mform->addGroup($buttons, 'buttons', 'actions' , array(' '), false);
            
        }        
        
         function validation($data, $files) {
            return array();
        }
        
    }
?>

 

¿where is the best way to put display:none?

css(using the id of the form id=mform1) o there is other way?

 

Cheers,

Tony

In reply to Antonio Garmendía

Re: File Manager

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Dear Tony,

Thank you, makes sense now smile.

Ok, as you are making the form you can set up a container 'div' around the calling '$mform->display();' code that has a CSS class that you define.  Then you can add your course format specific CSS to the 'style.css' file to control the size of the file manager.  Also it looks like the label is causing an issue by being inline.  Again with your CSS div class wrapper you can define CSS to fix that.

If you need examples of using a form in a course format, then please see the files 'editimage.php' and 'editimage_form.php' in the Grid Format.

Ok, with the AJAX issue, what is the criteria for showing / hiding?  By 'display: none' I mean reacting to user clicks on the screen.  This is how Collapsed Topics works with it's toggle.  It also does not use AJAX directly but uses Tim Hunt's 'user preference' code - look at Collapsed Topic's 'format.php' and 'module.js' files.

Cheers,

Gareth

In reply to Gareth J Barnard

Re: File Manager

by Antonio Garmendía -

Dear Gareth:

Thanks!! I will do that and answer you later with my results.

Cheers,

Tony

 

 

In reply to Gareth J Barnard

Re: File Manager

by Antonio Garmendía -

Dear Gareth:

Thanks for the reply!!  but I have another problem. I show the file manager in this manner:

    $mform = new resource_form();
    ob_start();
    $mform->display();
    $formhtml = ob_get_clean();   

I'm showing the file manager from php but I'want that the form sent with ajax. I'll trace with firebug and only sent 5 parameters (see picture), how do I get the files to save??. I red http://docs.moodle.org/dev/User:Mark_Johnson/Mforms_and_AJAX and https://moodle.org/mod/forum/discuss.php?d=212377 but I can't find the hint I need.The thing is that I want to save that file or files and show the url or urls updating that page with ajax. Hope you can help me. Thanks in advance.

Cheers,

Tony

 

Attachment Sin título.png
In reply to Antonio Garmendía

Re: File Manager

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Dear Tony,

To be honest I've never used this mechanism before.  Is the code in a public repository such that I could look at it all?

Cheers,

Gareth

In reply to Gareth J Barnard

Re: File Manager

by Antonio Garmendía -

Dear Gareth,

Sorry but I have no repository. I will explain better my problem and share the code. I need to save files and I'm using the file manager. So, now I'm trying to get th url of the file that I saved from the draft area. The variable $out gave me the url but when I put that address in the browser gave an error, that the file cannot be found. Thanks in advance!! Cheers, Tony

if (!defined('AJAX_SCRIPT')) {
    define('AJAX_SCRIPT', true);
}

require_once(dirname(__FILE__).'/../../../../config.php');

global $DB, $USER, $CFG, $COURSE;

require_once($CFG->dirroot.'/course/format/socialmedia/lib.php');

$courseid = required_param('courseid', PARAM_INT);


    $fs = get_file_storage();
    $draftid = file_get_submitted_draft_itemid('attachments');//Generate unique id
    $context = context_system::instance();
    
    var_dump($context);
    
    // Save the files submitted
    $itemid = 0;
    $filemanageropts = array('subdirs' => 0, 'maxbytes' => '0', 'maxfiles' => 10, 'context' => $context);
    file_save_draft_area_files($draftid, $context->id, 'local_filemanager', 'attachments', $itemid, $filemanageropts);
    
    //Get area files
    $files = $fs->get_area_files($context->id, 'local_filemanager', 'attachments', $itemid);
        
    foreach($files as $file)
    {
        $out = $file->get_filename();
        if ($file->is_directory()) {
                $out = $file->get_filepath();
            }
        else {
            $fileurl = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(),
                                                       $file->get_itemid(), $file->get_filepath(), $file->get_filename());
            $out = html_writer::link($fileurl, $out);
        }
        
        echo $out;
    }

In reply to Antonio Garmendía

Re: File Manager

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Dear Tony,

Without being able to run and debug the code and by looking at it I would guess that there is an issue them having the wrong component and file area in their record as against 'local_filemanager' and 'attachments'.  I think that you need to look in the 'files' table and see what is going on.

Have you read http://docs.moodle.org/dev/File_API_internalshttp://docs.moodle.org/dev/Using_the_File_API and http://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms?

Cheers,

Gareth

In reply to Gareth J Barnard

Re: File Manager

by Antonio Garmendía -

Dear Gareth,

Sorry, but I've been out. I red the 3 documents you said and I can't find my mistake yet. I'm using ajax to retrieve the form to this .php:

 

 $courseid = required_param('courseid', PARAM_INT);
    
    $context = get_context_instance(CONTEXT_COURSE, $courseid);
    
    //$context = context_system::instance();
    
    $fs = get_file_storage();
    $draftid = file_get_submitted_draft_itemid('attachments');//Generate unique id
    
    $PAGE->set_context( $context );    
    //$itemid = 0;
    
    $itemid = file_get_submitted_draft_itemid('attachments');
    
    $filemanageropts = array('subdirs' => 0, 'maxbytes' => '0', 'maxfiles' => 10, 'accepted_types' => array('document','.jpg'),'context' => $context);    
    
    // Copy all the files from the 'real' area, into the draft area
    file_prepare_draft_area($draftid, $context->id, 'format_socialmedia', 'attachment', $itemid, $filemanageropts);
    
    file_save_draft_area_files($draftid, $context->id, 'format_socialmedia', 'attachment', $itemid, $filemanageropts);
    
    //Get area files
    $files = $fs->get_area_files($context->id, 'format_socialmedia', 'attachment', $itemid);
        
    foreach($files as $file)
    {
        $out = $file->get_filename();
        if ($file->is_directory()) {
                $out = $file->get_filepath();
            }
        else {
            $fileurl = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(),
                                                       $file->get_itemid(), $file->get_filepath(), $file->get_filename());
            $out = html_writer::link($fileurl, $out);
        }
                  
        
        echo $out;
    }

 

So I'm trying to obtain the url to get the document. I think that I have to put the course_socialmedia_pluginfile in lib.php so:

function format_socialmedia_pluginfile($course,$cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
    global $CFG, $DB;

            $fs = get_file_storage();
            global $DB;
            
            list($itemid, $filename) = $args;
            
            $params = array(
                'component' => 'format_socialmedia',
                'filearea' => $filearea,
                'itemid' => $itemid,
                'filename' => $filename
            );
            
             $instanceid = $DB->get_field('files', 'id', $params);

        if (empty($instanceid)) {
            send_file_not_found();
        } else {
            $file = $fs->get_file_by_id($instanceid);
            send_stored_file($file);
        }
            
                    
    }

When I get the url I put as browser's address and gave the error file not found. Hope you can help me. Thanks in advance!!

Cheers,

Tony

 

 

 

 

 

In reply to Antonio Garmendía

Re: File Manager

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Dear Tony,

To be fair I think the only way I can help you now is to have a copy of the code.  I need to see what is going on internally with the 'files' table and compare that with the data in the variables to understand where the issue is and to test different URL scenarios.

Cheers,

Gareth

In reply to Gareth J Barnard

Re: File Manager

by Antonio Garmendía -

Hi everyone,

This is the code. Hope you can help me. Thanks in advance.

Cheers,

Tony

In reply to Antonio Garmendía

Re: File Manager

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Dear Tony,

I tried to replicate the issue and could not as the 'BBS' screen just uploads to the draft files area and nothing else - I could not trace through as there was no code to deal with the form data in 'resource.php' - also there are no return parameters from the form back to the course.  Please look at the Grid format's 'editimage' code.

Also to get working I corrected three stack trace issues - updated version attached.

I suggest for the mean time work on sorting all known bugs, tidying up the code (lots of tabs all over the place) and perhaps seeing if you could put code in the same place to eliminate any duplication that could be causing a problem.  Then might be able to sort out the issue.

Cheers,

Gareth

In reply to Gareth J Barnard

Re: File Manager

by Antonio Garmendía -

Dear Gareth,

Thanks for the advices!! but I can't find my mistake yet. The problem may be that I sent the form with AJax. I think the file is saving right but when i copy in the filearea(format_socialmedia) I cannot get it back. Any idea? Thanks in advance. 

Cheers,

Tony

 

In reply to Antonio Garmendía

Re: File Manager

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Dear Tony,

At the moment I cannot figure out the flow of the format because code is not in the places I expect them to be with all of the existing code I know about for this sort of thing.  For example, the code that deals with the processing of the file is not in the form file that does the display, i.e. the '$mform->display()' containing code.  And with all the tabs, commented out code and jumping around between files it makes it difficult to understand and debug.  Is there a test plan for the issue so I know the steps I need to do to replicate the issue as currently guessing?

I only seem to be able to get the file into the draft area as things stand and no further.  The code that saves it to the 'format_socialmedia' file area does not execute as you say in previous posts.

I suggest that you clean up the code and remove anything that is not required including removing any known bugs before trying to solve this.

Cheers,

Gareth

In reply to Gareth J Barnard

Re: File Manager

by Antonio Garmendía -

Dear Gareth,

I'll try to explain the code in deep. I have a php function print_wall_section_page in renderer.php and I include the .php that I need. The .php that had the filemanager is presentation/header.php that include resource.php( $mform->display) and the class is in presentation/resource_form.php. That form is submitted with YUI throught a function in smf_plugin the code below:

M.format_socialmedia.Asignar_eventos_header = function()
{
Y.one("#bbs_save_comment").on("click", M.format_socialmedia.onclick_guardar );
Y.one("#bbs_save_resource").on("click", function(event)
{
//Sent the form via Ajax
var formObject = document.getElementById('mform1');

var cfg = {
on: {
complete: M.format_socialmedia.submit_form
},
context: this,
form: formObject
};

Y.io(M.cfg.wwwroot + '/course/format/socialmedia/actions/save_resource.php', cfg);

} );
return false;
}

 

Finally in actions/save_resource.php I'm trying to save the file and retrieve the url to get it. IF you don't understand the flow let me know. I attached the code again. Thanks in advance!!

Cheers,

Tony

 

In reply to Antonio Garmendía

Re: File Manager

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Dear Tony,

Thank you smile

Can I ask a silly question?  Why are you using AJAX upload when clicking on the 'submit' button will return back to the 'resource.php' page where you can do the processing of the file?  As AJAX is not required to make the page efficient as the user is going to go back to another page anyway.  Therefore 'resource.php' just needs to save the file and do a redirect just like the Grid format.

Cheers,

Gareth

In reply to Gareth J Barnard

Re: File Manager

by Antonio Garmendía -

Dear Gareth,

The thing is that I don't want to redirect to another page and I want to save the comment, the resource and show to the user without the update of the whole page. I' don't know if it possible but I would like to do the processing of the file in actions/save_resource.php. The code below:

 

$courseid = required_param('courseid', PARAM_INT);

$context = get_context_instance(CONTEXT_COURSE, $courseid);

//$context = context_system::instance();

$fs = get_file_storage();
$draftid = file_get_submitted_draft_itemid('attachments');//Generate unique id

$PAGE->set_context( $context );
//$itemid = 0;

$itemid = file_get_submitted_draft_itemid('attachments');

$filemanageropts = array('subdirs' => 0, 'maxbytes' => '0', 'maxfiles' => 10, 'accepted_types' => array('document','.jpg'),'context' => $context);

// Copy all the files from the 'real' area, into the draft area
file_save_draft_area_files($draftid, $context->id, 'format_socialmedia', 'attachment', $itemid, $filemanageropts);

//Get area files
$files = $fs->get_area_files($context->id, 'format_socialmedia', 'attachment', $itemid);

foreach($files as $file)
{
$out = $file->get_filename();
if ($file->is_directory()) {
$out = $file->get_filepath();
}
else {
$fileurl = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(),
$file->get_itemid(), $file->get_filepath(), $file->get_filename());
$out = html_writer::link($fileurl, $out);
}


$messagetext = file_rewrite_pluginfile_urls($messagetext, 'pluginfile.php',
$file->get_contextid(), 'format_socialmedia', 'attachment', $file->get_itemid());


echo $out;
}

Hope you can help me. Thanks again!!

Cheers,

Tony

 

In reply to Antonio Garmendía

Re: File Manager

by Antonio Garmendía -

Dear Gareth,

I'm checking the code smile. Let me ask you something, I have to put a function in lib.php, in this case is for a course format, then I should called,  format_socialmedia_pluginfile, is that right??

 

Cheers,

Tony

 

 

In reply to Antonio Garmendía

Re: File Manager

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Dear Tony,

That sounds right for file serving which is what I think you are asking about.  I've only ever done that in a theme but sounds correct.

A bit busy ATM but will look at the code when I can.

Cheers,

Gareth

In reply to Gareth J Barnard

Re: File Manager

by Antonio Garmendía -

Dear Gareth,

I know that you are busy and have not much time, look at the code when you can, Thanks!! still working but nothing yet...

Cheers,

Tony

In reply to Antonio Garmendía

Re: File Manager

by Antonio Garmendía -

Hi everyone,

I'm still working in the filemanager and seems quite strange. When I upload the file, is reflected in the database(see picture mdl_files), and when i copy the files to the filearea also reflected(see picture mdl_files). The url that i form:

http://mcolab.ii.uam.es/moodle-2.4.1/pluginfile.php/15/format_socialmedia/attachment/599517822/Systematic%20Mapping%20Studies%20in.pdf

but the response is file not found, however if i do this,

http://mcolab.ii.uam.es/moodle-2.4.1/draftfile.php/5/user/draft/599517822/Systematic%20Mapping%20Studies%20in.pdf

then i can download the file. What could be my mistake? Any hint??

Thanks!!

Cheers,

Tony

 

Attachment pic.jpg
In reply to Antonio Garmendía

Re: File Manager

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

Have you tried putting some 'echo' statements into your format_socialmedia_pluginfile function and then clicking on the link above?

That should at least tell you if the function is being called correctly.

If you then call 'var_dump()' on the parameters to this function, you should get some idea of what is going wrong.

(even better - make sure you have xdebug installed on your server and use a supported PHP IDE to step through the code one line at a time and see what his happening)

In reply to Davo Smith

Re: File Manager

by Antonio Garmendía -

Hi,

The function isn't called correctly, I had to debug the code with netbeans just like you said. Thanks both of you!!!

Cheers,

Tony