Source for file plugins.php
Documentation is available at plugins.php
* Defines a library class for managing SLOODLE Plugins.
* It is constructed and used through a SloodleSession object.
* @copyright Copyright (c) 2009 Sloodle (various contributors)
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
* @contributor Peter R. Bloomfield
// This library expects that the Sloodle config file has already been included
// (along with the Moodle libraries)
/** Include the general Sloodle functionality. */
require_once(SLOODLE_DIRROOT.
'/lib/general.php');
* A class to load SLOODLE plugins, and provide a means to access them.
* Internal only - reference to the containing {@link SloodleSession} object.
* Note: always check that it is not null before use!
* Stores the plugins which have already been created, to prevent new ones being instantiated unnecessarily.
* Is an associative array of plugin names to plugin objects.
* @param object &$_session Reference to the containing {@link SloodleSession} object, if available.
* Loads all available plugins from the specified folder.
* @param string $folder Name of the folder to load plugins from. This is a sub-folder of the 'sloodle/plugin' folder. It will only load files which are directly contained inside it.
* @return bool True if successful, or false if it fails. (It will only report failure if the folder does not exist, or there is an error accessing it.)
if (empty($folder)) return false;
// Get a list of all the files in the specified folder
if (!$files) return false;
if (count($files) ==
0) return true;
// Start by including the relevant base class files, if they are available
@include_once($pluginFolder.
'/_base.php');
// Go through each filename
foreach ($files as $file) {
// Include the specified file
include_once($pluginFolder.
'/'.
$file);
* Gets an array of the names of all SLOODLE plugins derived from the specified type.
* By default, this gets all plugins. Specify a different base class to get others.
* NOTE: this will search all plugins loaded by all plugin managers in the current PHP script.
* (There is no way to tell which manager loaded which plugins.)
* Plugin names correspond to class names, with the 'SloodlePlugin' prefix.
* @param string $type Name of a plugin base class.
* @return array Numeric array of plugin names. These names correspond to class names.
// We want to create an array of plugin names
// Go through all declared classes
foreach ($classes as $className) {
// Make sure this is a SLOODLE plugin by checking that it starts "SloodlePlugin" but not "SloodlePluginbase"
if (strpos($className, 'SloodlePlugin') !==
0 ||
strpos($className, 'SloodlePluginBase') ===
0) continue;
// Make sure this is not one of the supporting classes
if ($className ==
'SloodlePluginBase' ||
$className ==
'SloodlePluginManager') continue;
// Make sure it is in fact a plugin by ensuring it is appropriately derived from the given base plugin class
$tempPlugin =
@new $className();
// Remove the 'SloodlePlugin' prefix from the class name
$className =
substr($className, 13);
* Gets an instance of the specified plugin type.
* This only works if the plugin has been loaded, and if it is derived from SloodlePluginBase.
* @param string $name Name of the plugin type to get. If it does not start with "SloodlePlugin", then that is added to the start.
* @param bool $forcenew If false (default) then a cached instance of the plugin will be returned. Set this to true to force the manager to create a new plugin instance.
* @return object|boolAn object descended from SloodlePluginBase if successful, or false on failure.
// Prepend 'SloodlePlugin' if necessary
if (strpos($name, 'SloodlePlugin') !==
0) $name =
'SloodlePlugin'.
$name;
// Make sure the specified class exists
// Do we have a cached plugin of this type?
// Attempt to construct an instance of the plugin
// Make sure it is a valid plugin
Documentation generated on Fri, 17 Jul 2009 11:02:22 +0100 by phpDocumentor 1.4.0