Note: You are currently viewing documentation for Moodle 2.2. Up-to-date documentation for the latest stable version is available here: Repository plugins.

Development:Repository plugins: Difference between revisions

From MoodleDocs
 
(39 intermediate revisions by 7 users not shown)
Line 1: Line 1:
'''Audience:'''
A guide for developers on how to create a repository plugin.
:Plug-in Developer


'''Related Documents:'''
*[[Development:Repository API| Repository API]]
*[[Development:Repository_Interface_for_Moodle/Course/User| Repository Interface for Moodle/Course/User]]
*[[QA:Use Case Number Attribution| Use Case Number Attribution]]
<br />


== Introduction ==
== Introduction ==
===Requirement===
===Prerequisites===
Before starting coding, it is mandatory to know how to use repository administration pages and how to use the file picker.
Before starting coding, it is necessary to know how to use repository administration pages and how to use the file picker.
 
There is a template attached to MDL-16543 which might be useful to help get you started.


===First steps===
===First steps===
# create a folder for your plugin into moodle/repository/ (eg. moodle/repository/''myplugin'')
# Create a folder for your plugin in ''moodle/repository/'' e.g. ''moodle/repository/myplugin''
# into this folder, create the following files:
# Create the following files and add them to the plugin folder:
#* repository.class.php
#* ''lib.php''
#* icon.png (icon displayed in the file picker)
#* ''pix/icon.png'' (the icon displayed in the file picker)
# create the language file:
#* "version.php"
#* moodle/lang/repository_''myplugin''
# Create the language file ''repository_myplugin.php'' and add it to the plugin folder, keeping the following folder structure:
#*''moodle/repository/myplugin/lang/en/repository_myplugin.php'' or ''moodle/lang/en/repository_myplugin.php''
# Create access.php and upgrade scripts
 
===Overview===
 
The 3 different parts to write
# Administration - You can customise the way administrators and users can configure their repositories.
# File picker integration - The core of your plugin, it will manage communication between Moodle and the repository service, and also the file picker display.
# I18n - Internationalization should be done at the same time as you're writing the other parts.
 
==Administration APIs==


===The 3 different parts to write===
As an example, let's create a Flickr plugin for accessing a public flickr account. The plugin will be called "Flickr Public".
# Administration: you can customise the way administrator/user can set their repositories.
# File picker integration: the core of your plugin, it will manage communication between Moodle and the repository service, it will manage the display into the file picker
# I18n: internationalization should be done at the same time as you're writting the other parts.


==APIs for Administration==
Firstly the skeleton:
===Quick Start===
As a Quick Start I will create a Flickr Plugin. This plugin will be used to access to a public flickr account. I will call my plugin Flickr Public.


Let's create my skeleton:
<code php>
<code php>
     <?php
     <?php
Line 43: Line 44:
</code>
</code>


Then consider the question "What does my plugin do?"


'''What does my plugin do?:''' in the Moodle file picker, I want to display some flickr public repositories directly linked to a flickr public account. For example ''My Public Flickr Pictures'', and also ''My Friend's Flickr Pictures''. When the user click one of these repositories, the public pictures are displayed into the file picker.
In the Moodle file picker, we want to display some flickr public repositories directly linked to a flickr public account. For example ''My Public Flickr Pictures'', and also ''My Friend's Flickr Pictures''. When the user clicks on one of these repositories, the public pictures are displayed in the file picker.


In order to access to a flickr public account the plugin need to know the email address of the Flickr public account owner. So the administrator will need to set an email address for every repository. Let's add an "email address" setting to every repository.
In order to access to a flickr public account, the plugin needs to know the email address of the Flickr public account owner. So the administrator will need to set an email address for every repository. Let's add an "email address" setting to every repository.


<code php>
<code php>
//I tell the API that the repositories have specific settings: "email address"
//We tell the API that the repositories have specific settings: "email address"
     public static function get_instance_option_names() {
     public static function get_instance_option_names() {
         return array('email_address');
         return array('email_address');
     }
     }


//I add an "email address" text box to the create/edit repository instance Moodle form
//We add an "email address" text box to the create/edit repository instance Moodle form
     public function instance_config_form(&$mform) {
     public function instance_config_form(&$mform) {
         $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
         $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
Line 60: Line 62:
     }
     }
</code>
</code>
So at this moment all my Flickr Public Repositories will have a specific email address. However it is not enough. In order to communicate with Flickr, Moodle needs to know a Flickr API (http://www.flickr.com/services/api/). This API key is the same for any repository. We could add it with the email address setting but the administrator would have to enter the same API key for every repository. Hopefully the administrator can add settings to the plugin level, impacting all repositories. The code is similar the repository instance settings:
 
So at this moment all our Flickr Public Repositories will have a specific email address. However this is not enough. In order to communicate with Flickr, Moodle needs to know a Flickr API key (http://www.flickr.com/services/api/). This API key is the same for any repository. We could add it with the email address setting but the administrator would have to enter the same API key for every repository. Hopefully the administrator can add settings to the plugin level, impacting all repositories. The code is similar the repository instance settings:
<code php>
<code php>
//I tell the API that the repositories have general settings: "api_key"
//We tell the API that the repositories have general settings: "api_key"
     public static function get_type_option_names() {
     public static function get_type_option_names() {
         return array('api_key');
         return array('api_key');
     }
     }


//I add an "api key" text box to the create/edit repository plugin Moodle form (also called as Repository type Moodle form)
//We add an "api key" text box to the create/edit repository plugin Moodle form (also called a Repository type Moodle form)
     public function type_config_form(&$mform) {
     public function type_config_form(&$mform) {
         //the following line is needed in order to retrieve the API key value from the database when Moodle displays the edit form
         //the following line is needed in order to retrieve the API key value from the database when Moodle displays the edit form
Line 76: Line 79:
     }
     }
</code>
</code>
'''Have we finished yet?''': in fact, yes! We created all that we need for the administration pages. But let's go further. It would be good that the user can enter any "Flickr public account email address" into the file picker. In fact we want to display into the file picker a Flickr Public repository that the Moodle administrator can never delete. Let's add:
 
Have we finished yet?
 
Yes! We have created everything necessary for the administration pages. But let's go further. It would be good if the user can enter any "Flickr public account email address" in the file picker. In fact we want to display in the file picker a Flickr Public repository that the Moodle administrator can never delete. Let's add:
 
<code php>
<code php>
     //this function is only called one time, when the Moodle administrator add the Flickr Public Plugin into the Moodle site.
     //this function is only called one time, when the Moodle administrator add the Flickr Public Plugin into the Moodle site.
Line 85: Line 92:
     }
     }
</code>
</code>
That's all. I'm done with the administration part of my Flickr Public plugin. For your information, Box.net, Flickr, and Flickr Public have been created in the same time that the API, have a look. Enjoy your coding.


===Methods===
That's all - the administration part of our Flickr Public plugin is done. For your information, Box.net, Flickr, and Flickr Public all have similar administration APIs.
All these methods are optional. If you don't implement any of these functions your plugin will not manual settings and only one instance (which is created automatically).


==== public static function get_instance_option_names()====
===Functions===
All of the following functions are optional. If they're not implemented, your plugin will not have manual settings and will have only one instance displayed in the File Picker (The repository API creates this unique instance when the administrator add the plugin).
 
==== get_instance_option_names====
''This function must be declared static''<br>
Return an array of strings. These strings are setting names. These settings are specific to an instance.
Return an array of strings. These strings are setting names. These settings are specific to an instance.
If the function return an empty array, the API will consider that the plugin displays only one repository into the file picker.
If the function returns an empty array, the API will consider that the plugin displays only one repository in the file picker.
Parent function return an empty array.
Parent function returns an empty array.


====public function instance_config_form(&$mform)====
====instance_config_form(&$mform)====
You can modify the Moodle form displaying the settings specific to an instance.
This is for modifying the Moodle form displaying the settings specific to an instance.


For example, to add a required text box called email_address:
For example, to add a required text box called email_address:
Line 108: Line 117:
Parent function does nothing.
Parent function does nothing.


====public static function get_type_option_names()====
====get_type_option_names====
''This function must be declared static''<br>
Return an array of string. These strings are setting names. These settings are shared by all instances.
Return an array of string. These strings are setting names. These settings are shared by all instances.
Parent function return an empty array.
Parent function return an empty array.


====public function type_config_form(&$mform)====
====type_config_form(&$mform)====
You can modify the Moodle form displaying the plugin settings.
This is for modifying the Moodle form displaying the plugin settings.
Similar to ''instance_config_form(&$mform)''
Similar to ''instance_config_form(&$mform)''


====public static function plugin_init()====
====plugin_init()====
This function is called when the administrator add the plugin. So except if the administrator delete the plugin and re-add it, it should be called only one time.
''This function must be declared static''<br>
This function is called when the administrator adds the plugin. So unless the administrator deletes the plugin and re-adds it, it should be called only once.
Parent function does nothing.
Parent function does nothing.


==APIs for File picker==
==File picker APIs==
===Methods you *MUST* override===
=== Quick Start ===
'''To be completed'''<br>
First of all, the File Picker using intensively Ajax you will need a easy way to debug. Install FirePHP (MDL-16371) and make it works. It will save you a lot of time. (You might give the [http://moodle.org/mod/forum/discuss.php?d=119961 FirePHP plugin for Moodle] a try, it's still work in progress, though.)
 
Your first question when you write your plugin specification is 'Does the user need to log-in'?<br>
.....
 
For most of plugins, you need to establish a connection with the remote repository. This connection can be done into the get_listing(), constructor() function...<br>
.....
 
You wanna display a list of files once the user is logged<br>
.... get_listing() .....
 
You wanna retrieve the file that the user selected<br>
....
 
Optional question that you should ask yourself is 'Does the user can execute a search'<br>
.... search() ....
 
===Functions you *MUST* override===
====__construct====
====__construct====
You may initialize your plugin here, such as:
You may initialize your plugin here, such as:
# Get options from database
# Get options from database
# Get user name and password from HTTP POST.
# Get user name and password from HTTP POST


====get_listing====
====get_listing($path="", $page="")====
This function will return a list of files, the list must be a array like this:
This function will return a list of files, the list must be a array like this:
<code php>
<code php>
$list = array(
$list = array(
'path'=>'/var/repo/',
//this will be used to build navigation bar
'path'=>array(array('name'=>'root','path'=>'/'), array('name'=>'subfolder', 'path'=>'/subfolder')),
'manage'=>'http://webmgr.moodle.com',
'manage'=>'http://webmgr.moodle.com',
array('title'=>'filename1', 'date'=>'01/01/2009', 'size'=>'10MB', 'source'=>'http://www.moodle.com/dl.rar'),
'list'=> array(
array('title'=>'folder', 'date'=>'01/01/2009', 'size'=>'0', 'children'=>array())
    array('title'=>'filename1', 'date'=>'01/01/2009', 'size'=>'10MB', 'source'=>'http://www.moodle.com/dl.rar'),
    array('title'=>'folder', 'date'=>'01/01/2009', 'size'=>'0', 'children'=>array())
)
);
);
</code>
</code>
The full specification:
'''The full specification of list element:'''
<code>
<code php>
    * array(
array(
    *  'path' => (string) path for the current folder
  // Used to build navegation bar, so you need to include all parents folders
    *  'dynload' => (bool) use dynamic loading,
  // array(array('name'=>'root','path'=>'/'), array('name'=>'subfolder', 'path'=>'/subfolder'))
    *  'manage' => (string) link to file manager,
  'path' => (array) this will be used to build navigation bar
    *  'nologin' => (bool) requires login,
  // dynload tells file picker to fetch list dynamically, when user click
    *  'nosearch' => (bool) no search link,
  // the folder, it will send a ajax request to server side.
    *  'search_result' => (bool) this list is a searching result,
  // if you are using pagination, 'page' and 'pages' parameters should be used
    *  'upload' => array( // upload manager
  // and note, you'd better don't use pagination and page at the same time
     *    'name' => (string) label of the form element,
  'page' => (int) which page is this list
     *    'id' => (string) id of the form element
  'pages' => (pages) how many pages
    *  ),
  'dynload' => (bool) use dynamic loading,
    *  'list' => array(
  // will display a link in file picker
     *    array( // file
  'manage' => (string) url of the file manager,
    *      'title' => (string) file name,
  // set to true, the login link will be removed from file picker
    *      'date' => (string) file last modification time, usually userdate(...),
  'nologin' => (bool) requires login,
    *      'size' => (int) file size,
  // set to true, the search link will be removed from file picker
    *      'thumbnail' => (string) url to thumbnail for the file,
  'nosearch' => (bool) no search link,
    *      'source' => plugin-dependent unique path to the file (id, url, path, etc.),
  // set this option will display a upload form in file picker
    *      'url'=> the accessible url of file
  // only used in upload plugin currently
     *    ),
  'upload' => array( // upload manager
     *    array( // folder - same as file, but no 'source'.
     'label' => (string) label of the form element,
    *      'title' => (string) folder name,
     'id' => (string) id of the form element
    *      'path' => (string) path to this folder
  ),
    *      'date' => (string) folder last modification time, usually userdate(...),
  // file picker will build a file tree according this
    *      'size' => 0,
  // list
    *      'thumbnail' => (string) url to thumbnail for the folder,
  'list' => array(
    *      'children' => array( // an empty folder needs to have 'children' defined, but empty.
     array( // file
    *        // content (files and folders)
      'title' => (string) file name,
    *      )
      'shorttitle' => (string) optional, if you prefer to display a short title
     *    ),
      'date' => (string) file last modification time, usually userdate(...),
    *  )
      'size' => (int) file size,
    * )
      'thumbnail' => (string) url to thumbnail for the file,
      'thumbnail_width' => (int) the width of the thumbnail image,
      'source' => plugin-dependent unique path to the file (id, url, path, etc.),
      'url'=> the accessible url of file
     ),
     array( // folder - same as file, but no 'source'.
      'title' => (string) folder name,
      'shorttitle' => (string) optional, if you prefer to display a short title
      'path' => (string) path to this folder
      'date' => (string) folder last modification time, usually userdate(...),
      'size' => 0,
      'thumbnail' => (string) url to thumbnail for the folder,
      'children' => array( // an empty folder needs to have 'children' defined, but empty.
        // content (files and folders)
      )
     ),
  )
)
</code>
</code>
Dynamically loading
Some repositories contain many files which cannot load in one time, in this case, we need dynamically loading to fetch them step by step, files in subfolder won't be listed until user click the folder in file picker treeview.
As a plug-in developer, if you set dynload flag as '''true''', you should return files and folders (set children as a null array) in current path instead of building the whole file tree.
Example of dynamically loading
See [http://cvs.moodle.org/moodle/repository/alfresco/lib.php?view=log Alfresco] plug-in


===Methods you can override===
===Functions you can override===
====print_login====
====print_login====
This function will help to print a login form, for ajax file picker, this function will return a
This function will help to print a login form, for the Ajax file picker, this function will return a
PHP array to define this form.
PHP array to define this form.
<code php>
<code php>
Line 193: Line 250:
             $ret = array();
             $ret = array();
             $ret['login'] = array($user_field, $passwd_field);
             $ret['login'] = array($user_field, $passwd_field);
            // if you are going to rename submit button label, use this option
            //$ret['login_btn_label'] = get_string('submit_btn_label');
            // if you are going to display a search form instead of login form
            // set this option to true
            //$ret['login_search_form'] = true;
             return $ret;
             return $ret;
         }
         }
Line 201: Line 263:
If your plugin don't require logging in, you don't need to override it, it will call get_listing to list files automatically by default.
If your plugin don't require logging in, you don't need to override it, it will call get_listing to list files automatically by default.
====check_login====
====check_login====
This function will return a boolean value to tell moodle whether the user has logged in.
This function will return a boolean value to tell Moodle whether the user has logged in.
By default, this function will return true.
By default, this function will return true.
====logout====
====logout====
When user clicked logout button in file picker, this function will be called, you may clean up session or disconnect connection with remote server here.
When a user clicks the logout button in file picker, this function will be called. You may clean up the session or disconnect the connection with remote server here.
====global_search====
Moodle Repository API supports global search, this function will return a boolean value to tell Moodle if this plugin is ready to search, by default, it will return false, you need to override it to enable global search.
====print_search====
====print_search====
When user clicked search button on file picker, this function will be called to return a search form, by default, it will create a form with single search bar, you can override it to create a advanced search form.
When a user clicks the search button on file picker, this function will be called to return a search form. By default, it will create a form with single search bar - you can override it to create a advanced search form.
====search====
====search====
This function will do the searching job, you can obtain the POST parameters from the from the form you created in print_search function
This function will do the searching job. You can obtain the POST parameters from the from the form you created in print_search function
This function will return a file list exactly like the one from get_listing.
This function will return a file list exactly like the one from get_listing.
====get_file====
====get_file====
When user clicked "Get" button to transfer the file, this function will be called, basically, it will download a file to Moodle, you can override it to modify the file then move to a proper location.
When a user clicks the "Get" button to transfer the file, this function will be called. Basically, it will download a file to Moodle - you can override it to modify the file then move it to a better location.
====get_name====
====get_name====
This function will return the name of the repository instance  
This function will return the name of the repository instance.


== I18n - Internationalization ==
== I18n - Internationalization ==
These following strings are mandatory (moodle/lang/en_utf8/repository_''myplugin'')
These following strings are required in ''moodle/repository/myplugin/lang/en_utf8/repository_myplugin.php'' or ''moodle/lang/en_utf8/repository_myplugin.php'':


<code php>
<code php>
$string['configplugin'] = 'Box.net configuration';
$string['configplugin'] = 'Flickr Public configuration';
$string['repositorydesc'] = 'Repository on Box.net';
$string['repositorydesc'] = 'A Flickr public repository';
$string['repositoryname'] = 'Box.net';
$string['repositoryname'] = 'Flickr Public';
</code>
</code>
==Officially supported repository plugins list==
See:
http://tracker.moodle.org/browse/MDL-16543


== Standard repository plugins ==
== Standard repository plugins ==
This is the functional specification list of the officially supported repository plugins.
This is the functional specification list of the officially supported repository plugins.
For each plugins, the two mains part we are interesting in are:
For each plugins, the two mains part we are interested in are:
* How do I administrate the plugin ([[Development:Repository_Administration_Specification| Repository Administration Specification - UC001-3]])
* How do I administer the plugin? See [[Development:Repository_Administration_Specification| Repository Administration Specification - UC001-3]]
* How do I setup an account for this repository ([[Development:Repository_Interface_for_Moodle/Course/User| Repository Interface for Moodle/Course/User]])
* How do I set up an account for this repository? See [[Development:Repository_Interface_for_Moodle/Course/User| Repository Interface for Moodle/Course/User]]


=== Functional Specifications ===
=== Functional specifications ===
*[[Development:Box.net Repository Plugin|Box.net Repository Plugin]]
*[[Development:Box.net Repository Plugin|Box.net Repository Plugin]]
*[[Development:Flickr Repository Plugin|Flickr Repository Plugin]]
*[[Development:Flickr Repository Plugin|Flickr Repository Plugin]]
*[[Development:Moodle Repository Plugin|Moodle Local Repository Plugin]]
*[[Development:Moodle Repository Plugin|Remote Moodle Repository Plugin]]
*[[Development:MNET Repository Plugin|Moodle MNET Repository Plugin]]
 
==See also==
 
*[[Development:Repository API| Repository API]]
*[[Development:Repository_Interface_for_Moodle/Course/User| Repository Interface for Moodle/Course/User]]
*[[QA:Use Case Number Attribution| Use Case Number Attribution]]
* MDL-16543 - A list of officially supported repository plugins
* MDL-16543 - Template plugin for developers
[[Category:Repositories]]

Latest revision as of 13:15, 6 January 2011

A guide for developers on how to create a repository plugin.


Introduction

Prerequisites

Before starting coding, it is necessary to know how to use repository administration pages and how to use the file picker.

There is a template attached to MDL-16543 which might be useful to help get you started.

First steps

  1. Create a folder for your plugin in moodle/repository/ e.g. moodle/repository/myplugin
  2. Create the following files and add them to the plugin folder:
    • lib.php
    • pix/icon.png (the icon displayed in the file picker)
    • "version.php"
  3. Create the language file repository_myplugin.php and add it to the plugin folder, keeping the following folder structure:
    • moodle/repository/myplugin/lang/en/repository_myplugin.php or moodle/lang/en/repository_myplugin.php
  4. Create access.php and upgrade scripts

Overview

The 3 different parts to write

  1. Administration - You can customise the way administrators and users can configure their repositories.
  2. File picker integration - The core of your plugin, it will manage communication between Moodle and the repository service, and also the file picker display.
  3. I18n - Internationalization should be done at the same time as you're writing the other parts.

Administration APIs

As an example, let's create a Flickr plugin for accessing a public flickr account. The plugin will be called "Flickr Public".

Firstly the skeleton:

   <?php
   /**
    * repository_flickr_public class
    * Moodle user can access public flickr account
    *
    * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
   */
   class repository_flickr_public extends repository {
   }

?>

Then consider the question "What does my plugin do?"

In the Moodle file picker, we want to display some flickr public repositories directly linked to a flickr public account. For example My Public Flickr Pictures, and also My Friend's Flickr Pictures. When the user clicks on one of these repositories, the public pictures are displayed in the file picker.

In order to access to a flickr public account, the plugin needs to know the email address of the Flickr public account owner. So the administrator will need to set an email address for every repository. Let's add an "email address" setting to every repository.

//We tell the API that the repositories have specific settings: "email address"

   public static function get_instance_option_names() {
       return array('email_address');
   }

//We add an "email address" text box to the create/edit repository instance Moodle form

   public function instance_config_form(&$mform) {
       $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
       $mform->addRule('email_address', get_string('required'), 'required', null, 'client');
   }

So at this moment all our Flickr Public Repositories will have a specific email address. However this is not enough. In order to communicate with Flickr, Moodle needs to know a Flickr API key (http://www.flickr.com/services/api/). This API key is the same for any repository. We could add it with the email address setting but the administrator would have to enter the same API key for every repository. Hopefully the administrator can add settings to the plugin level, impacting all repositories. The code is similar the repository instance settings: //We tell the API that the repositories have general settings: "api_key"

   public static function get_type_option_names() {
       return array('api_key');
   }

//We add an "api key" text box to the create/edit repository plugin Moodle form (also called a Repository type Moodle form)

   public function type_config_form(&$mform) {
       //the following line is needed in order to retrieve the API key value from the database when Moodle displays the edit form
       $api_key = get_config('flickr_public', 'api_key');
       $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), 
                          array('value'=>$api_key,'size' => '40'));
       $mform->addRule('api_key', get_string('required'), 'required', null, 'client');
   }

Have we finished yet?

Yes! We have created everything necessary for the administration pages. But let's go further. It would be good if the user can enter any "Flickr public account email address" in the file picker. In fact we want to display in the file picker a Flickr Public repository that the Moodle administrator can never delete. Let's add:

    //this function is only called one time, when the Moodle administrator add the Flickr Public Plugin into the Moodle site.
    public static function plugin_init() {
       //here we create a default repository instance. The last parameter is 1 in order to set the instance as readonly.
       repository_static_function('flickr_public','create', 'flickr_public', 0, get_system_context(), 
                                   array('name' => 'default instance','email_address' => null),1);
    }

That's all - the administration part of our Flickr Public plugin is done. For your information, Box.net, Flickr, and Flickr Public all have similar administration APIs.

Functions

All of the following functions are optional. If they're not implemented, your plugin will not have manual settings and will have only one instance displayed in the File Picker (The repository API creates this unique instance when the administrator add the plugin).

get_instance_option_names

This function must be declared static
Return an array of strings. These strings are setting names. These settings are specific to an instance. If the function returns an empty array, the API will consider that the plugin displays only one repository in the file picker. Parent function returns an empty array.

instance_config_form(&$mform)

This is for modifying the Moodle form displaying the settings specific to an instance.

For example, to add a required text box called email_address: $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public')); $mform->addRule('email_address', $strrequired, 'required', null, 'client');

Note: mform has by default a name text box (cannot be removed).

Parent function does nothing.

get_type_option_names

This function must be declared static
Return an array of string. These strings are setting names. These settings are shared by all instances. Parent function return an empty array.

type_config_form(&$mform)

This is for modifying the Moodle form displaying the plugin settings. Similar to instance_config_form(&$mform)

plugin_init()

This function must be declared static
This function is called when the administrator adds the plugin. So unless the administrator deletes the plugin and re-adds it, it should be called only once. Parent function does nothing.

File picker APIs

Quick Start

To be completed
First of all, the File Picker using intensively Ajax you will need a easy way to debug. Install FirePHP (MDL-16371) and make it works. It will save you a lot of time. (You might give the FirePHP plugin for Moodle a try, it's still work in progress, though.)

Your first question when you write your plugin specification is 'Does the user need to log-in'?
.....

For most of plugins, you need to establish a connection with the remote repository. This connection can be done into the get_listing(), constructor() function...
.....

You wanna display a list of files once the user is logged
.... get_listing() .....

You wanna retrieve the file that the user selected
....

Optional question that you should ask yourself is 'Does the user can execute a search'
.... search() ....

Functions you *MUST* override

__construct

You may initialize your plugin here, such as:

  1. Get options from database
  2. Get user name and password from HTTP POST

get_listing($path="", $page="")

This function will return a list of files, the list must be a array like this: $list = array(

//this will be used to build navigation bar

'path'=>array(array('name'=>'root','path'=>'/'), array('name'=>'subfolder', 'path'=>'/subfolder')), 'manage'=>'http://webmgr.moodle.com', 'list'=> array(

   array('title'=>'filename1', 'date'=>'01/01/2009', 'size'=>'10MB', 'source'=>'http://www.moodle.com/dl.rar'),
   array('title'=>'folder', 'date'=>'01/01/2009', 'size'=>'0', 'children'=>array())

) ); The full specification of list element:

array(
  // Used to build navegation bar, so you need to include all parents folders
  // array(array('name'=>'root','path'=>'/'), array('name'=>'subfolder', 'path'=>'/subfolder'))
  'path' => (array) this will be used to build navigation bar
  // dynload tells file picker to fetch list dynamically, when user click
  // the folder, it will send a ajax request to server side.
  // if you are using pagination, 'page' and 'pages' parameters should be used
  // and note, you'd better don't use pagination and page at the same time
  'page' => (int) which page is this list
  'pages' => (pages) how many pages
  'dynload' => (bool) use dynamic loading,
  // will display a link in file picker
  'manage' => (string) url of the file manager,
  // set to true, the login link will be removed from file picker
  'nologin' => (bool) requires login,
  // set to true, the search link will be removed from file picker
  'nosearch' => (bool) no search link,
  // set this option will display a upload form in file picker
  // only used in upload plugin currently
  'upload' => array( // upload manager
    'label' => (string) label of the form element,
    'id' => (string) id of the form element
  ),
  // file picker will build a file tree according this 
  // list
  'list' => array(
    array( // file
      'title' => (string) file name,
      'shorttitle' => (string) optional, if you prefer to display a short title
      'date' => (string) file last modification time, usually userdate(...),
      'size' => (int) file size,
      'thumbnail' => (string) url to thumbnail for the file,
      'thumbnail_width' => (int) the width of the thumbnail image,
      'source' => plugin-dependent unique path to the file (id, url, path, etc.),
      'url'=> the accessible url of file
    ),
    array( // folder - same as file, but no 'source'.
      'title' => (string) folder name,
      'shorttitle' => (string) optional, if you prefer to display a short title
      'path' => (string) path to this folder
      'date' => (string) folder last modification time, usually userdate(...),
      'size' => 0,
      'thumbnail' => (string) url to thumbnail for the folder,
      'children' => array( // an empty folder needs to have 'children' defined, but empty.
        // content (files and folders)
      )
    ),
  )
)

Dynamically loading Some repositories contain many files which cannot load in one time, in this case, we need dynamically loading to fetch them step by step, files in subfolder won't be listed until user click the folder in file picker treeview.

As a plug-in developer, if you set dynload flag as true, you should return files and folders (set children as a null array) in current path instead of building the whole file tree.

Example of dynamically loading See Alfresco plug-in

Functions you can override

print_login

This function will help to print a login form, for the Ajax file picker, this function will return a PHP array to define this form.

   public function print_login(){
       if ($this->options['ajax']) {
           $user_field->label = get_string('username', 'repository_boxnet').': ';
           $user_field->id    = 'box_username';
           $user_field->type  = 'text';
           $user_field->name  = 'boxusername';
           $user_field->value = $ret->username;
           
           $passwd_field->label = get_string('password', 'repository_boxnet').': ';
           $passwd_field->id    = 'box_password';
           $passwd_field->type  = 'password';
           $passwd_field->name  = 'boxpassword';
           $ret = array();
           $ret['login'] = array($user_field, $passwd_field);
           // if you are going to rename submit button label, use this option
           //$ret['login_btn_label'] = get_string('submit_btn_label');
           // if you are going to display a search form instead of login form
           // set this option to true
           //$ret['login_search_form'] = true;
           return $ret;
       }
   }

This will help to generate a form by file picker which contains user name and password input elements.

If your plugin don't require logging in, you don't need to override it, it will call get_listing to list files automatically by default.

check_login

This function will return a boolean value to tell Moodle whether the user has logged in. By default, this function will return true.

logout

When a user clicks the logout button in file picker, this function will be called. You may clean up the session or disconnect the connection with remote server here.

print_search

When a user clicks the search button on file picker, this function will be called to return a search form. By default, it will create a form with single search bar - you can override it to create a advanced search form.

search

This function will do the searching job. You can obtain the POST parameters from the from the form you created in print_search function This function will return a file list exactly like the one from get_listing.

get_file

When a user clicks the "Get" button to transfer the file, this function will be called. Basically, it will download a file to Moodle - you can override it to modify the file then move it to a better location.

get_name

This function will return the name of the repository instance.

I18n - Internationalization

These following strings are required in moodle/repository/myplugin/lang/en_utf8/repository_myplugin.php or moodle/lang/en_utf8/repository_myplugin.php:

$string['configplugin'] = 'Flickr Public configuration'; $string['repositorydesc'] = 'A Flickr public repository'; $string['repositoryname'] = 'Flickr Public';

Standard repository plugins

This is the functional specification list of the officially supported repository plugins. For each plugins, the two mains part we are interested in are:

Functional specifications

See also