Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

lib.php: Difference between revisions

From MoodleDocs
(Created page with "Within a module or plugin, the file <tt>lib.php</tt> contains the main API for the plugin. == API functions == <tt>lib.php</tt> contains a number of functions, some of which ar...")
 
No edit summary
(4 intermediate revisions by one other user not shown)
Line 10: Line 10:


These functions are not documented yet. There's a little bit more information about some of them here: [[NEWMODULE_Documentation#lib.php]].
These functions are not documented yet. There's a little bit more information about some of them here: [[NEWMODULE_Documentation#lib.php]].
=== Required functions ===


=== Optional functions ===
=== Optional functions ===
Line 19: Line 21:
For a full list of FEATURE_xx constants, see the code. Here are some:
For a full list of FEATURE_xx constants, see the code. Here are some:


* *FEATURE_BACKUP_MOODLE2* - return true if you implement Moodle 2 backup/restore system.
* FEATURE_BACKUP_MOODLE2 - return true if you implement Moodle 2 backup/restore system.
* *FEATURE_MOD_ARCHETYPE* - (modules only): controls how users can add the module. Default is to add using the 'Add activity' dropdown. Return MOD_ARCHETYPE_RESOURCE to add using the 'Add resource' dropdown, or MOD_ARCHETYPE_SYSTEM if the module cannot be added by users at all but is only added programmatically by system components.
* FEATURE_MOD_ARCHETYPE (modules only) - controls how users can add the module. Default is to add using the 'Add activity' dropdown. Return MOD_ARCHETYPE_RESOURCE to add using the 'Add resource' dropdown, or MOD_ARCHETYPE_SYSTEM (Moodle 2.3+) if the module cannot be added by users at all but is only added programmatically by system components.


== Other functions ==
== Other functions ==


<tt>lib.php</tt> should not contain any functions other than the API functions (and possibly a few small supporting functions). Any other significant functions should be placed in a <tt>locallib.php</tt> or equivalent.
<tt>lib.php</tt> should not contain any functions other than the API functions (and possibly a few small supporting functions). Any other significant functions should be placed in appropriate classes within [[Automatic_class_loading#Class_files_and_locations|your plugin's <tt>classes/</tt> directory]].


The reason for this is that <tt>lib.php</tt> for all modules is included on every single Moodle page, even if it is not much used. For performance reasons it is therefore important to ensure the file is as short as possible.
The reason for this is that <tt>lib.php</tt> for all modules is included on every single Moodle page, even if it is not much used. For performance reasons it is therefore important to ensure the file is as short as possible.

Revision as of 09:33, 9 February 2018

Within a module or plugin, the file lib.php contains the main API for the plugin.

API functions

lib.php contains a number of functions, some of which are required and some are optional, which may be called by the Moodle system.

The name of all these functions begins with your module or plugin name, e.g. forum_supports or format_topics_supports. In the list below we show only the part of the name after that underline.

TODO Document all functions here

These functions are not documented yet. There's a little bit more information about some of them here: NEWMODULE_Documentation#lib.php.

Required functions

Optional functions

_supports($feature)

Returns metadata about the plugin. $feature is a FEATURE_xx constant and, in order to obtain non-default behaviour, you return a specific value for certain constants.

For a full list of FEATURE_xx constants, see the code. Here are some:

  • FEATURE_BACKUP_MOODLE2 - return true if you implement Moodle 2 backup/restore system.
  • FEATURE_MOD_ARCHETYPE (modules only) - controls how users can add the module. Default is to add using the 'Add activity' dropdown. Return MOD_ARCHETYPE_RESOURCE to add using the 'Add resource' dropdown, or MOD_ARCHETYPE_SYSTEM (Moodle 2.3+) if the module cannot be added by users at all but is only added programmatically by system components.

Other functions

lib.php should not contain any functions other than the API functions (and possibly a few small supporting functions). Any other significant functions should be placed in appropriate classes within your plugin's classes/ directory.

The reason for this is that lib.php for all modules is included on every single Moodle page, even if it is not much used. For performance reasons it is therefore important to ensure the file is as short as possible.

Requiring other files

lib.php should not require any other files at the top level. If any of the functions within lib.php need to refer to functions from other files, the require_once statements should be placed within that function.

The reason for this is that lib.php for all modules is included on every single Moodle page, even if it is not much used. For performance reasons it is therefore important to avoid requiring any other files as a consequence.