Note:

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

lib.php

From MoodleDocs

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 a locallib.php or equivalent.

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.