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

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

Loading…
Cancel
Save