Minor - Format code + adding docs.

1.9.x
Julio Montoya 12 years ago
parent be92284fbf
commit 4aa262f21b
  1. 33
      main/inc/lib/plugin.class.php
  2. 194
      main/inc/lib/plugin.lib.php

@ -2,8 +2,10 @@
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
/** /**
* Class Plugin
* Base class for plugins * Base class for plugins
* *
*
* This class has to be extended by every plugin. It defines basic methods * This class has to be extended by every plugin. It defines basic methods
* to install/uninstall and get information about a plugin * to install/uninstall and get information about a plugin
* *
@ -12,16 +14,15 @@
* @author Laurent Opprecht <laurent@opprecht.info> * @author Laurent Opprecht <laurent@opprecht.info>
* @author Julio Montoya <gugli100@gmail.com> added course settings support + lang variable fixes * @author Julio Montoya <gugli100@gmail.com> added course settings support + lang variable fixes
* @author Yannick Warnier <ywarnier@beeznest.org> added documentation * @author Yannick Warnier <ywarnier@beeznest.org> added documentation
*
*/ */
class Plugin class Plugin
{ {
protected $version = ''; protected $version = '';
protected $author = ''; protected $author = '';
protected $fields = array(); protected $fields = array();
private $settings = null; private $settings = null;
private $strings = null; //translation strings // Translation strings.
private $strings = null;
public $is_course_plugin = false; public $is_course_plugin = false;
/** /**
@ -68,7 +69,6 @@ class Plugin
public function get_info() public function get_info()
{ {
$result = array(); $result = array();
$result['title'] = $this->get_title(); $result['title'] = $this->get_title();
$result['comment'] = $this->get_comment(); $result['comment'] = $this->get_comment();
$result['version'] = $this->get_version(); $result['version'] = $this->get_version();
@ -83,12 +83,12 @@ class Plugin
$result[$name] = $value; $result[$name] = $value;
} }
} }
return $result; return $result;
} }
/** /**
* Returns the "system" name of the plugin in lowercase letters * Returns the "system" name of the plugin in lowercase letters
* @param string Name of the plugin
* @return string * @return string
*/ */
public function get_name() public function get_name()
@ -96,12 +96,12 @@ class Plugin
$result = get_class($this); $result = get_class($this);
$result = str_replace('Plugin', '', $result); $result = str_replace('Plugin', '', $result);
$result = strtolower($result); $result = strtolower($result);
return $result; return $result;
} }
/** /**
* Returns the title of the plugin * Returns the title of the plugin
* @param string title of the plugin
* @return string * @return string
*/ */
public function get_title() public function get_title()
@ -111,7 +111,6 @@ class Plugin
/** /**
* Returns the description of the plugin * Returns the description of the plugin
* @param string description of the plugin
* @return string * @return string
*/ */
public function get_comment() public function get_comment()
@ -121,7 +120,6 @@ class Plugin
/** /**
* Returns the version of the plugin * Returns the version of the plugin
* @param string Version of the plugin
* @return string * @return string
*/ */
public function get_version() public function get_version()
@ -131,7 +129,6 @@ class Plugin
/** /**
* Returns the author of the plugin * Returns the author of the plugin
* @param string Author(s) of the plugin
* @return string * @return string
*/ */
public function get_author() public function get_author()
@ -141,7 +138,6 @@ class Plugin
/** /**
* Returns the contents of the CSS defined by the plugin * Returns the contents of the CSS defined by the plugin
* @param string The CSS string
* @return array * @return array
*/ */
public function get_css() public function get_css()
@ -154,6 +150,7 @@ class Plugin
$css = array(); $css = array();
$css[] = file_get_contents($path); $css[] = file_get_contents($path);
$result = implode($css); $result = implode($css);
return $result; return $result;
} }
@ -197,12 +194,14 @@ class Plugin
} }
$result->setDefaults($defaults); $result->setDefaults($defaults);
$result->addElement('style_submit_button', 'submit_button', $this->get_lang('Save')); $result->addElement('style_submit_button', 'submit_button', $this->get_lang('Save'));
return $result; return $result;
} }
/** /**
* Returns the value of a given plugin global setting * Returns the value of a given plugin global setting
* @param string Name of the plugin * @param string Name of the plugin
*
* @return string Value of the plugin * @return string Value of the plugin
*/ */
public function get($name) public function get($name)
@ -226,12 +225,14 @@ class Plugin
$settings = api_get_settings_params(array("subkey = ? AND category = ? AND type = ? " => array($this->get_name(), 'Plugins', 'setting'))); $settings = api_get_settings_params(array("subkey = ? AND category = ? AND type = ? " => array($this->get_name(), 'Plugins', 'setting')));
$this->settings = $settings; $this->settings = $settings;
} }
return $this->settings; return $this->settings;
} }
/** /**
* Tells whether language variables are defined for this plugin or not * Tells whether language variables are defined for this plugin or not
* @param string System name of the plugin * @param string System name of the plugin
*
* @return boolean True if the plugin has language variables defined, false otherwise * @return boolean True if the plugin has language variables defined, false otherwise
*/ */
public function get_lang_plugin_exists($name) public function get_lang_plugin_exists($name)
@ -242,6 +243,7 @@ class Plugin
/** /**
* Hook for the get_lang() function to check for plugin-defined language terms * Hook for the get_lang() function to check for plugin-defined language terms
* @param string Name of the language variable we are looking for * @param string Name of the language variable we are looking for
*
* @return string The translated language term of the plugin * @return string The translated language term of the plugin
*/ */
public function get_lang($name) public function get_lang($name)
@ -275,6 +277,7 @@ class Plugin
if (isset($this->strings[$name])) { if (isset($this->strings[$name])) {
return $this->strings[$name]; return $this->strings[$name];
} }
return get_lang($name); return get_lang($name);
} }
@ -282,11 +285,12 @@ class Plugin
* Caller for the install_course_fields() function * Caller for the install_course_fields() function
* @param int The course's integer ID * @param int The course's integer ID
* @param boolean Whether to add a tool link on the course homepage * @param boolean Whether to add a tool link on the course homepage
*
* @return void * @return void
*/ */
public function course_install($course_id, $add_tool_link = true) public function course_install($courseId, $addToolLink = true)
{ {
$this->install_course_fields($course_id, $add_tool_link); $this->install_course_fields($courseId, $addToolLink);
} }
/** /**
@ -299,12 +303,12 @@ class Plugin
{ {
$plugin_name = $this->get_name(); $plugin_name = $this->get_name();
$t_course = Database::get_course_table(TABLE_COURSE_SETTING); $t_course = Database::get_course_table(TABLE_COURSE_SETTING);
$course_id = intval($course_id); $course_id = intval($course_id);
if (empty($course_id)) { if (empty($course_id)) {
return false; return false;
} }
//Ads course settings // Ads course settings.
if (!empty($this->course_settings)) { if (!empty($this->course_settings)) {
foreach ($this->course_settings as $setting) { foreach ($this->course_settings as $setting) {
$variable = Database::escape_string($setting['name']); $variable = Database::escape_string($setting['name']);
@ -388,6 +392,7 @@ class Plugin
/** /**
* Install the course fields and tool link of this plugin in all courses * Install the course fields and tool link of this plugin in all courses
* @param boolean Whether we want to add a plugin link on the course homepage * @param boolean Whether we want to add a plugin link on the course homepage
*
* @return void * @return void
*/ */
public function install_course_fields_in_all_courses($add_tool_link = true) public function install_course_fields_in_all_courses($add_tool_link = true)

@ -1,6 +1,9 @@
<?php <?php
/* See license terms in /license.txt */ /* See license terms in /license.txt */
/**
* Class AppPlugin
*/
class AppPlugin class AppPlugin
{ {
public $plugin_regions = array( public $plugin_regions = array(
@ -27,144 +30,193 @@ class AppPlugin
} }
/** /**
* Read plugin from path
* @return array * @return array
*/ */
function read_plugins_from_path() public function read_plugins_from_path()
{ {
/* We scan the plugin directory. Each folder is a potential plugin. */ /* We scan the plugin directory. Each folder is a potential plugin. */
$pluginpath = api_get_path(SYS_PLUGIN_PATH); $pluginPath = api_get_path(SYS_PLUGIN_PATH);
$possible_plugins = array(); $plugins = array();
$handle = @opendir($pluginpath); $handle = @opendir($pluginPath);
while (false !== ($file = readdir($handle))) { while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..' && is_dir(api_get_path(SYS_PLUGIN_PATH).$file)) { if ($file != '.' && $file != '..' && is_dir(api_get_path(SYS_PLUGIN_PATH).$file)) {
$possible_plugins[] = $file; $plugins[] = $file;
} }
} }
@closedir($handle); @closedir($handle);
sort($possible_plugins); sort($plugins);
return $possible_plugins;
return $plugins;
} }
/** /**
* @return array * @return array
*/ */
function get_installed_plugins_by_region() public function get_installed_plugins_by_region()
{ {
$used_plugins = array(); $plugins = array();
/* We retrieve all the active plugins. */ /* We retrieve all the active plugins. */
$result = api_get_settings('Plugins'); $result = api_get_settings('Plugins');
if (!empty($result)) {
foreach ($result as $row) { foreach ($result as $row) {
$used_plugins[$row['variable']][] = $row['selected_value']; $plugins[$row['variable']][] = $row['selected_value'];
} }
return $used_plugins; }
return $plugins;
} }
/** /**
* @return array * @return array
*/ */
function get_installed_plugins() public function get_installed_plugins()
{ {
$installed_plugins = array(); $installedPlugins = array();
$plugin_array = api_get_settings_params(array("variable = ? AND selected_value = ? AND category = ? " => $plugins = api_get_settings_params(
array('status', 'installed', 'Plugins'))); array(
"variable = ? AND selected_value = ? AND category = ? " => array('status', 'installed', 'Plugins')
)
);
if (!empty($plugin_array)) { if (!empty($plugins)) {
foreach ($plugin_array as $row) { foreach ($plugins as $row) {
$installed_plugins[$row['subkey']] = true; $installedPlugins[$row['subkey']] = true;
} }
$installed_plugins = array_keys($installed_plugins); $installedPlugins = array_keys($installedPlugins);
} }
return $installed_plugins;
return $installedPlugins;
} }
/** /**
* @param string $plugin_name * @param string $pluginName
* @param int $access_url_id * @param int $urlId
*/ */
function install($plugin_name, $access_url_id = null) public function install($pluginName, $urlId = null)
{ {
if (empty($access_url_id)) { if (empty($urlId)) {
$access_url_id = api_get_current_access_url_id(); $urlId = api_get_current_access_url_id();
} else { } else {
$access_url_id = intval($access_url_id); $urlId = intval($urlId);
} }
api_add_setting('installed', 'status', $plugin_name, 'setting', 'Plugins', $plugin_name, null, null, null, $access_url_id, 1);
api_add_setting(
'installed',
'status',
$pluginName,
'setting',
'Plugins',
$pluginName,
null,
null,
null,
$urlId,
1
);
//api_add_setting($plugin, $area, $plugin, null, 'Plugins', $plugin, null, null, null, $_configuration['access_url'], 1); $pluginPath = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/install.php';
$pluginpath = api_get_path(SYS_PLUGIN_PATH).$plugin_name.'/install.php';
if (is_file($pluginpath) && is_readable($pluginpath)) { if (is_file($pluginPath) && is_readable($pluginPath)) {
//execute the install procedure // Execute the install procedure.
require $pluginpath;
require $pluginPath;
} }
} }
/** /**
* @param string $plugin_name * @param string $pluginName
* @param int $access_url_id * @param int $urlId
*/ */
public function uninstall($plugin_name, $access_url_id = null) public function uninstall($pluginName, $urlId = null)
{ {
if (empty($access_url_id)) { if (empty($urlId)) {
$access_url_id = api_get_current_access_url_id(); $urlId = api_get_current_access_url_id();
} else { } else {
$access_url_id = intval($access_url_id); $urlId = intval($urlId);
} }
api_delete_settings_params(array('category = ? AND access_url = ? AND subkey = ? ' => api_delete_settings_params(
array('Plugins', $access_url_id, $plugin_name))); array('category = ? AND access_url = ? AND subkey = ? ' => array('Plugins', $urlId, $pluginName))
$pluginpath = api_get_path(SYS_PLUGIN_PATH).$plugin_name.'/uninstall.php'; );
if (is_file($pluginpath) && is_readable($pluginpath)) { $pluginPath = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/uninstall.php';
//execute the uninstall procedure if (is_file($pluginPath) && is_readable($pluginPath)) {
require $pluginpath; // Execute the uninstall procedure.
require $pluginPath;
} }
} }
/** /**
* @param string $plugin_name * @param string $pluginName
*
* @return array * @return array
*/ */
public function get_areas_by_plugin($plugin_name) public function get_areas_by_plugin($pluginName)
{ {
$result = api_get_settings('Plugins'); $result = api_get_settings('Plugins');
$areas = array(); $areas = array();
foreach ($result as $row) { foreach ($result as $row) {
if ($plugin_name == $row['selected_value']) { if ($pluginName == $row['selected_value']) {
$areas[] = $row['variable']; $areas[] = $row['variable'];
} }
} }
return $areas; return $areas;
} }
function is_valid_plugin_location($location) /**
* @param string $location
*
* @return bool
*/
public function is_valid_plugin_location($location)
{ {
return in_array($location, $this->plugin_list); return in_array($location, $this->plugin_list);
} }
function is_valid_plugin($plugin_name) /**
* @param string $pluginName
*
* @return bool
*/
public function is_valid_plugin($pluginName)
{ {
if (is_dir(api_get_path(SYS_PLUGIN_PATH).$plugin_name)) { if (is_dir(api_get_path(SYS_PLUGIN_PATH).$pluginName)) {
if (is_file(api_get_path(SYS_PLUGIN_PATH).$plugin_name.'/index.php')) { if (is_file(api_get_path(SYS_PLUGIN_PATH).$pluginName.'/index.php')) {
return true; return true;
} }
} }
return false; return false;
} }
function get_plugin_regions() /**
* @return array
*/
public function get_plugin_regions()
{ {
sort($this->plugin_regions); sort($this->plugin_regions);
return $this->plugin_regions; return $this->plugin_regions;
} }
function load_region($region, $main_template, $forced = false) /**
* @param string $region
* @param string $template
* @param bool $forced
*
* @return null|string
*/
public function load_region($region, $template, $forced = false)
{ {
if ($region == 'course_tool_plugin') { if ($region == 'course_tool_plugin') {
return null; return null;
} }
ob_start(); ob_start();
$this->get_all_plugin_contents_by_region($region, $main_template, $forced); $this->get_all_plugin_contents_by_region($region, $template, $forced);
$content = ob_get_contents(); $content = ob_get_contents();
ob_end_clean(); ob_end_clean();
return $content; return $content;
} }
@ -174,7 +226,7 @@ class AppPlugin
* @todo add caching * @todo add caching
* @param string $plugin_name * @param string $plugin_name
*/ */
function load_plugin_lang_variables($plugin_name) public function load_plugin_lang_variables($plugin_name)
{ {
global $language_interface; global $language_interface;
$root = api_get_path(SYS_PLUGIN_PATH); $root = api_get_path(SYS_PLUGIN_PATH);
@ -208,10 +260,10 @@ class AppPlugin
/** /**
* *
* @param string $block * @param string $block
* @param obj template obj * @param Template $template
* @todo improve this function * @todo improve this function
*/ */
function get_all_plugin_contents_by_region($region, $template, $forced = false) public function get_all_plugin_contents_by_region($region, $template, $forced = false)
{ {
global $_plugins; global $_plugins;
if (isset($_plugins[$region]) && is_array($_plugins[$region])) { if (isset($_plugins[$region]) && is_array($_plugins[$region])) {
@ -277,7 +329,7 @@ class AppPlugin
* @todo filter setting_form * @todo filter setting_form
* @return array * @return array
*/ */
function get_plugin_info($plugin_name, $forced = false) public function get_plugin_info($plugin_name, $forced = false)
{ {
static $plugin_data = array(); static $plugin_data = array();
@ -306,12 +358,12 @@ class AppPlugin
/** /**
* Get the template list * Get the template list
* @param string $plugin_name * @param string $pluginName
* @return bool * @return bool
*/ */
function get_templates_list($plugin_name) public function get_templates_list($pluginName)
{ {
$plugin_info = $this->get_plugin_info($plugin_name); $plugin_info = $this->get_plugin_info($pluginName);
if (isset($plugin_info) && isset($plugin_info['templates'])) { if (isset($plugin_info) && isset($plugin_info['templates'])) {
return $plugin_info['templates']; return $plugin_info['templates'];
} else { } else {
@ -326,9 +378,11 @@ class AppPlugin
{ {
$access_url_id = api_get_current_access_url_id(); $access_url_id = api_get_current_access_url_id();
if (!empty($plugin)) { if (!empty($plugin)) {
api_delete_settings_params(array('category = ? AND type = ? AND access_url = ? AND subkey = ? ' => api_delete_settings_params(
array('Plugins', 'region', $access_url_id, $plugin))); array(
'category = ? AND type = ? AND access_url = ? AND subkey = ? ' => array('Plugins', 'region', $access_url_id, $plugin)
)
);
} }
} }
@ -337,7 +391,7 @@ class AppPlugin
* @param string $plugin * @param string $plugin
* @param string $region * @param string $region
*/ */
function add_to_region($plugin, $region) public function add_to_region($plugin, $region)
{ {
$access_url_id = api_get_current_access_url_id(); $access_url_id = api_get_current_access_url_id();
api_add_setting($plugin, $region, $plugin, 'region', 'Plugins', $plugin, null, null, null, $access_url_id, 1); api_add_setting($plugin, $region, $plugin, 'region', 'Plugins', $plugin, null, null, null, $access_url_id, 1);
@ -346,7 +400,7 @@ class AppPlugin
/** /**
* @param int $course_id * @param int $course_id
*/ */
function install_course_plugins($course_id) public function install_course_plugins($course_id)
{ {
$plugin_list = $this->get_installed_plugins(); $plugin_list = $this->get_installed_plugins();
@ -366,7 +420,7 @@ class AppPlugin
/** /**
* @param FormValidator $form * @param FormValidator $form
*/ */
function add_course_settings_form($form) public function add_course_settings_form($form)
{ {
$plugin_list = $this->get_installed_plugins(); $plugin_list = $this->get_installed_plugins();
foreach ($plugin_list as $plugin_name) { foreach ($plugin_list as $plugin_name) {
@ -406,7 +460,7 @@ class AppPlugin
/** /**
* @param array $values * @param array $values
*/ */
function set_course_settings_defaults(& $values) public function set_course_settings_defaults(& $values)
{ {
$plugin_list = $this->get_installed_plugins(); $plugin_list = $this->get_installed_plugins();
foreach ($plugin_list as $plugin_name) { foreach ($plugin_list as $plugin_name) {
@ -433,7 +487,7 @@ class AppPlugin
* @param array The new settings the user just saved * @param array The new settings the user just saved
* @return void * @return void
*/ */
function save_course_settings($values) public function save_course_settings($values)
{ {
$plugin_list = $this->get_installed_plugins(); $plugin_list = $this->get_installed_plugins();
foreach ($plugin_list as $plugin_name) { foreach ($plugin_list as $plugin_name) {

Loading…
Cancel
Save