Note: You are currently viewing documentation for Moodle 3.3. Up-to-date documentation for the latest stable version of Moodle is probably available here: Using the File API in Moodle forms.

Development:Using the File API in Moodle forms: Difference between revisions

From MoodleDocs
No edit summary
Line 1: Line 1:
{{Moodle 2.0}}In Moodle 2.o onward, we introduced [[Development:Repository API|Repository API]] to fetch files from external sources, [[Development:Repository API|Repository API]] will move to files into draft areas, then modules will process moving draft area files into a proper fileareas, this documentation will demonstrate how to do that.
{{Moodle 2.0}}In Moodle 2.0 onwards, we introduced [[Development:Repository API|Repository API]] to fetch files from external sources, [[Development:Repository API|Repository API]] will move to files into draft areas, then modules will process moving draft area files into a proper file areas, this documentation will demonstrate how to do that.


There are three form elements involved with [[Development:Repository API|Repository API]], they are file manager, file picker and editor, the legacy upload button will be replaced by file picker or file manager, file picker and file manager work similar, the only difference is file manager can fetch multiple files, file picker only fetch one file. Editor element is introduced to replace legacy htmleditor element, I will talk about them respectively.
There are three form elements involved with [[Development:Repository API|Repository API]], they are file manager, file picker and editor, the legacy upload button will be replaced by file picker or file manager, file picker and file manager work similar, the only difference is file manager can fetch multiple files, file picker only fetch one file. Editor element is introduced to replace legacy htmleditor element, I will talk about them respectively.


==editor element==
==Form element: editor==


The file picker has been integrated with with TinyMCE to make the editor element. Instances of the old htmleditor element in your forms should be replaced by the new editor element type. For example:
The file picker has been integrated with with TinyMCE to make the editor element. Instances of the old htmleditor element in your forms should be replaced by the new editor element type. For example:
Line 12: Line 12:
The editor element can take following options: maxfiles, maxbytes, filearea, subdirs and changeformat.
The editor element can take following options: maxfiles, maxbytes, filearea, subdirs and changeformat.


'''NOTE''', the element has included format option of the editor. You should no longer use the separate format element type.
'''Note''': the element has included format option of the editor. You should no longer use the separate format element type.


To retrieve editor content, you need to use following code:
To retrieve editor content, you need to use following code:
Line 27: Line 27:
If there are multiple files, they will share the same itemid.
If there are multiple files, they will share the same itemid.


===Process draft area files===
===Save the draft files===


When a user selects a file using the file picker, the file is initially stored in a draft file area, and a URL is inserted into the HTML in the editor that lets the person editing the content (but no one else) see the file.
When a user selects a file using the file picker, the file is initially stored in a draft file area, and a URL is inserted into the HTML in the editor that lets the person editing the content (but no one else) see the file.


When the user submits the form, we then need to copy the draft files to the correct place in permanent storage. (Just like you have to call $DB->update_record('tablename', $data); to have the other parts of the form submission stored correctly.)
When the user submits the form, we then need to save the draft files to the correct place in permanent storage. (Just like you have to call $DB->update_record('tablename', $data); to have the other parts of the form submission stored correctly.)


The save_files_from_draft_area function does this.
<code php>
<code php>
$messagetext = file_save_files_from_draftarea($messageitemid, $context->id, 'proper_file_area', $post->id, true, $messagetext);
$messagetext = save_files_from_draft_area($messageitemid,
        $context->id, 'proper_file_area', $post->id, true, $messagetext);
</code>
</code>
; $messageitemid : is the variable we retrieved above.
; $messageitemid : is the variable we retrieved above.
; $context->id, 'proper_file_area' and $post->id : correspond to the contextid, filearea and itemid columns in the [[Development:File_API#Table:_files|files table]].
; $context->id, 'proper_file_area' and $post->id : correspond to the contextid, filearea and itemid columns in the [[Development:File_API#Table:_files|files table]].
; $messagetext : this is the message text. As the files are saved to the real file area, the URLs in this content are rewritten.
; $messagetext : this is the message text. As the files are saved to the real file area, the URLs in this content are rewritten.
All URLs in content that point to files managed to the File API are converted to a form that starts '@@PLUGINFILE@@/' before the content is stored in the database. That is what we mean by rewriting.


===Move files into draft area===
===Move files into draft area===


When editing content, we need to move files to draft area again to edit them, to do so, we need to call
In the section about the editor form element, we skipped over a stage, becuase we must prepare the draft area before we show the form, and copy any existing files into it.
 
The function to do that is prepare_draft_area, which is the opposite of save_files_from_draft_area:
<code php>
<code php>
// $draftitemid can be null, file_prepare_draftarea will create a new one automatically.
// $filearea  the file area of files
// $itemid    the itemid of files
prepare_draft_area($draftitemid, $context->id, $filearea, $itemid);
prepare_draft_area($draftitemid, $context->id, $filearea, $itemid);
</code>
</code>
This function will return relinked embedded url, after editing content, you need to call save_files_from_draft_area again to convert draft area files.
; $draftitemid : may be null, in which case a new one will be created automatically.
; $context->id, $filearea and $itemid : correspond to the contextid, filearea and itemid columns in the [[Development:File_API#Table:_files|files table]].
; $messagetext : as with save_files_from_draft_area, this function will rewrite the links in some content if you pass some content in. In this case, the links are rewritten from the '@@PLUGINFILE@@/' form to point to the actual files in the draft area.


===Convert relative links starting with @@PLUGINFILE@@ into correct format===
===Convert relative links starting with @@PLUGINFILE@@ into correct format===


Call  file_convert_relative_pluginfiles to convert links:
Before content is displayed to the user, any URLs in the '@@PLUGINFILE@@/' form in the content need to be rewritten to the real URL where the user can access the files.
<code php>
<code php>
$message_text = file_convert_relative_pluginfiles($message_text, 'pluginfile.php', "$context->id/proper_file_area/$itemid/");
$messagetext = rewrite_pluginfile_urls($messagetext, 'pluginfile.php',  
        "$context->id/proper_file_area/$itemid/");
</code>
</code>
; $messagetext : is the content containing the @@PLUGINFILE@@ URLs from the database.
; 'pluginfile.php' : there are a number of different scripts that can serve files with different permissions checks. You need to specify which one to use.
; "$context->id/proper_file_area/$itemid/" : uniquely identifies the file area, as before.


==file picker==
==file picker==


==file manager==
==file manager==
=== Create file manager element in moodle form ===
=== Create file manager element in moodle form ===
<code php>
<code php>
$mform->addElement('filemanager', 'attachments', get_string('attachment', 'moodle'),
$mform->addElement('filemanager', 'attachments', get_string('attachment', 'moodle'),

Revision as of 04:27, 31 March 2009

Template:Moodle 2.0In Moodle 2.0 onwards, we introduced Repository API to fetch files from external sources, Repository API will move to files into draft areas, then modules will process moving draft area files into a proper file areas, this documentation will demonstrate how to do that.

There are three form elements involved with Repository API, they are file manager, file picker and editor, the legacy upload button will be replaced by file picker or file manager, file picker and file manager work similar, the only difference is file manager can fetch multiple files, file picker only fetch one file. Editor element is introduced to replace legacy htmleditor element, I will talk about them respectively.

Form element: editor

The file picker has been integrated with with TinyMCE to make the editor element. Instances of the old htmleditor element in your forms should be replaced by the new editor element type. For example: $mform->addElement('editor', 'message', get_string('message', 'forum'),

       array('maxfiles' => EDITOR_UNLIMITED_FILES, 'filearea' => 'forum_post'));

The editor element can take following options: maxfiles, maxbytes, filearea, subdirs and changeformat.

Note: the element has included format option of the editor. You should no longer use the separate format element type.

To retrieve editor content, you need to use following code: if ($fromform = $mform->get_data()) {

   // content of editor
   $messagetext = $fromform->message['text'];
   // format of content
   $messageformat  = $fromform->message['format'];
   // draft itemid
   $messageitemid  = $fromform->message['itemid'];

} If there are multiple files, they will share the same itemid.

Save the draft files

When a user selects a file using the file picker, the file is initially stored in a draft file area, and a URL is inserted into the HTML in the editor that lets the person editing the content (but no one else) see the file.

When the user submits the form, we then need to save the draft files to the correct place in permanent storage. (Just like you have to call $DB->update_record('tablename', $data); to have the other parts of the form submission stored correctly.)

The save_files_from_draft_area function does this. $messagetext = save_files_from_draft_area($messageitemid,

       $context->id, 'proper_file_area', $post->id, true, $messagetext);

$messageitemid
is the variable we retrieved above.
$context->id, 'proper_file_area' and $post->id
correspond to the contextid, filearea and itemid columns in the files table.
$messagetext
this is the message text. As the files are saved to the real file area, the URLs in this content are rewritten.

All URLs in content that point to files managed to the File API are converted to a form that starts '@@PLUGINFILE@@/' before the content is stored in the database. That is what we mean by rewriting.

Move files into draft area

In the section about the editor form element, we skipped over a stage, becuase we must prepare the draft area before we show the form, and copy any existing files into it.

The function to do that is prepare_draft_area, which is the opposite of save_files_from_draft_area: prepare_draft_area($draftitemid, $context->id, $filearea, $itemid);

$draftitemid
may be null, in which case a new one will be created automatically.
$context->id, $filearea and $itemid
correspond to the contextid, filearea and itemid columns in the files table.
$messagetext
as with save_files_from_draft_area, this function will rewrite the links in some content if you pass some content in. In this case, the links are rewritten from the '@@PLUGINFILE@@/' form to point to the actual files in the draft area.

Convert relative links starting with @@PLUGINFILE@@ into correct format

Before content is displayed to the user, any URLs in the '@@PLUGINFILE@@/' form in the content need to be rewritten to the real URL where the user can access the files. $messagetext = rewrite_pluginfile_urls($messagetext, 'pluginfile.php',

       "$context->id/proper_file_area/$itemid/");

$messagetext
is the content containing the @@PLUGINFILE@@ URLs from the database.
'pluginfile.php'
there are a number of different scripts that can serve files with different permissions checks. You need to specify which one to use.
"$context->id/proper_file_area/$itemid/"
uniquely identifies the file area, as before.

file picker

file manager

Create file manager element in moodle form

$mform->addElement('filemanager', 'attachments', get_string('attachment', 'moodle'),

  array('subdirs'=>0,
    'maxbytes'=>$maxbytes,
    'maxfiles'=>50,
    'filetypes'=>'*'

)); You can specify what file types are accepted by filemanger, all file types are listed at moodle/lib/file/file_types.mm, this is a freemind file, you can edit it freely, the changes will be reflected in moodle.

Retrieve files

  1. Call get_draftarea_info to get how many files in this draftarea
  2. Call file_convert_draftarea to convert draft area files to a proper file area

See also

Template:CategoryDeveloper