Minor - format code, updating docs

1.9.x
Julio Montoya 12 years ago
parent 7deec39dd7
commit 2e211641e3
  1. 174
      main/inc/lib/plugin.class.php
  2. 51
      main/inc/lib/plugin.lib.php

@ -5,15 +5,15 @@
* Class Plugin * 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
* *
* @copyright (c) 2012 University of Geneva * @author Julio Montoya <gugli100@gmail.com>
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html * @author Yannick Warnier <ywarnier@beeznest.org>
* @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 * @copyright 2012 University of Geneva
* @author Yannick Warnier <ywarnier@beeznest.org> added documentation * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
*
*/ */
class Plugin class Plugin
{ {
@ -23,7 +23,7 @@ class Plugin
private $settings = null; private $settings = null;
// Translation strings. // Translation strings.
private $strings = null; private $strings = null;
public $is_course_plugin = false; public $isCoursePlugin = false;
/** /**
* When creating a new course, these settings are added to the course, in * When creating a new course, these settings are added to the course, in
@ -48,9 +48,9 @@ class Plugin
/** /**
* Default constructor for the plugin class. By default, it only sets * Default constructor for the plugin class. By default, it only sets
* a few attributes of the object * a few attributes of the object
* @param string Version of this plugin * @param string $version of this plugin
* @param string Author of this plugin * @param string $author of this plugin
* @param array Array of global settings to be proposed to configure the plugin * @param array $settings settings to be proposed to configure the plugin
*/ */
protected function __construct($version, $author, $settings = array()) protected function __construct($version, $author, $settings = array())
{ {
@ -74,7 +74,7 @@ class Plugin
$result['version'] = $this->get_version(); $result['version'] = $this->get_version();
$result['author'] = $this->get_author(); $result['author'] = $this->get_author();
$result['plugin_class'] = get_class($this); $result['plugin_class'] = get_class($this);
$result['is_course_plugin'] = $this->is_course_plugin; $result['is_course_plugin'] = $this->isCoursePlugin;
if ($form = $this->get_settings_form()) { if ($form = $this->get_settings_form()) {
$result['settings_form'] = $form; $result['settings_form'] = $form;
@ -200,7 +200,7 @@ class Plugin
/** /**
* 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
*/ */
@ -236,9 +236,9 @@ class Plugin
/** /**
* 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 $name System name of the plugin
* *
* @return boolean True if the plugin has language variables defined, false otherwise * @return bool True if the plugin has language variables defined, false otherwise
*/ */
public function get_lang_plugin_exists($name) public function get_lang_plugin_exists($name)
{ {
@ -247,7 +247,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
*/ */
@ -288,8 +288,8 @@ 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 $courseId
* @param boolean Whether to add a tool link on the course homepage * @param boolean $addToolLink Whether to add a tool link on the course homepage
* *
* @return void * @return void
*/ */
@ -300,17 +300,18 @@ class Plugin
/** /**
* Add course settings and, if not asked otherwise, add a tool link on the course homepage * Add course settings and, if not asked otherwise, add a tool link on the course homepage
* @param int Course integer ID * @param int $courseId Course integer ID
* @param boolean Whether to add a tool link or not (some tools might just offer a configuration section and act on the backend) * @param boolean $add_tool_link Whether to add a tool link or not
* (some tools might just offer a configuration section and act on the backend)
* @return boolean False on error, null otherwise * @return boolean False on error, null otherwise
*/ */
public function install_course_fields($course_id, $add_tool_link = true) public function install_course_fields($courseId, $add_tool_link = true)
{ {
$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); $courseId = intval($courseId);
if (empty($course_id)) { if (empty($courseId)) {
return false; return false;
} }
// Ads course settings. // Ads course settings.
@ -327,18 +328,22 @@ class Plugin
} }
if (isset($setting['group'])) { if (isset($setting['group'])) {
$group = Database::escape_string($setting['group']); $group = Database::escape_string($setting['group']);
$sql = "SELECT value FROM $t_course WHERE c_id = $course_id AND variable = '$group' AND subkey = '$variable' "; $sql = "SELECT value FROM $t_course
WHERE c_id = $courseId AND variable = '$group' AND subkey = '$variable' ";
$result = Database::query($sql); $result = Database::query($sql);
if (!Database::num_rows($result)) { if (!Database::num_rows($result)) {
$sql_course = "INSERT INTO $t_course (c_id, variable, subkey, value, category, type) VALUES ($course_id, '$group', '$variable', '$value', 'plugins', '$type')"; $sql = "INSERT INTO $t_course (c_id, variable, subkey, value, category, type) VALUES
Database::query($sql_course); ($courseId, '$group', '$variable', '$value', 'plugins', '$type')";
Database::query($sql);
} }
} else { } else {
$sql = "SELECT value FROM $t_course WHERE c_id = $course_id AND variable = '$variable' "; $sql = "SELECT value FROM $t_course
WHERE c_id = $courseId AND variable = '$variable' ";
$result = Database::query($sql); $result = Database::query($sql);
if (!Database::num_rows($result)) { if (!Database::num_rows($result)) {
$sql_course = "INSERT INTO $t_course (c_id, variable, value, category, subkey, type) VALUES ($course_id, '$variable','$value', 'plugins', '$plugin_name', '$type')"; $sql = "INSERT INTO $t_course (c_id, variable, value, category, subkey, type) VALUES
Database::query($sql_course); ($courseId, '$variable','$value', 'plugins', '$plugin_name', '$type')";
Database::query($sql);
} }
} }
} }
@ -351,27 +356,28 @@ class Plugin
//Add an icon in the table tool list //Add an icon in the table tool list
$t_tool = Database::get_course_table(TABLE_TOOL_LIST); $t_tool = Database::get_course_table(TABLE_TOOL_LIST);
$sql = "SELECT name FROM $t_tool WHERE c_id = $course_id AND name = '$plugin_name' "; $sql = "SELECT name FROM $t_tool
WHERE c_id = $courseId AND name = '$plugin_name' ";
$result = Database::query($sql); $result = Database::query($sql);
if (!Database::num_rows($result)) { if (!Database::num_rows($result)) {
$tool_link = "$plugin_name/start.php"; $tool_link = "$plugin_name/start.php";
$visibility = string2binary(api_get_setting('course_create_active_tools', $plugin_name)); $visibility = string2binary(api_get_setting('course_create_active_tools', $plugin_name));
$sql_course = "INSERT INTO $t_tool $sql = "INSERT INTO $t_tool VALUES
VALUES ($course_id, NULL, '$plugin_name', '$tool_link', '$plugin_name.png',' ".$visibility."','0', 'squaregrey.gif','NO','_self','plugin','0')"; ($courseId, NULL, '$plugin_name', '$tool_link', '$plugin_name.png',' ".$visibility."','0', 'squaregrey.gif','NO','_self','plugin','0')";
Database::query($sql_course); Database::query($sql);
} }
} }
/** /**
* Delete the fields added to the course settings page and the link to the * Delete the fields added to the course settings page and the link to the
* tool on the course's homepage * tool on the course's homepage
* @param int The integer course ID * @param int $courseId
* @return void * @return void
*/ */
public function uninstall_course_fields($course_id) public function uninstall_course_fields($courseId)
{ {
$course_id = intval($course_id); $courseId = intval($courseId);
if (empty($course_id)) { if (empty($courseId)) {
return false; return false;
} }
$plugin_name = $this->get_name(); $plugin_name = $this->get_name();
@ -389,13 +395,13 @@ class Plugin
continue; continue;
} }
$sql = "DELETE FROM $t_course $sql = "DELETE FROM $t_course
WHERE c_id = $course_id AND variable = '$variable'"; WHERE c_id = $courseId AND variable = '$variable'";
Database::query($sql); Database::query($sql);
} }
} }
$plugin_name = Database::escape_string($plugin_name); $plugin_name = Database::escape_string($plugin_name);
$sql = "DELETE FROM $t_tool WHERE c_id = $course_id AND name = '$plugin_name'"; $sql = "DELETE FROM $t_tool WHERE c_id = $courseId AND name = '$plugin_name'";
Database::query($sql); Database::query($sql);
} }
@ -455,30 +461,30 @@ class Plugin
/** /**
* Method to be extended when changing the setting in the course * Method to be extended when changing the setting in the course
* configuration should trigger the use of a callback method * configuration should trigger the use of a callback method
* @param array Values sent back from the course configuration script * @param array $values sent back from the course configuration script
* @return void * @return void
*/ */
public function course_settings_updated($values = array()) public function course_settings_updated($values = array())
{ {
} }
/** /**
* Add a tab to chamilo's platform * Add a tab to chamilo's platform
* @param type $tabName * @param string $tabName
* @return int
*/ */
public function addTab($tabName, $url) public function addTab($tabName, $url)
{ {
$sql = "SELECT * $sql = "SELECT *
FROM settings_current FROM settings_current
WHERE variable = 'show_tabs' WHERE variable = 'show_tabs'
AND subkey like 'custom_tab_%'"; AND subkey like 'custom_tab_%'";
$result = Database::query($sql); $result = Database::query($sql);
$customTabsNum = Database::count_rows($result); $customTabsNum = Database::count_rows($result);
$tabNum = $customTabsNum + 1; $tabNum = $customTabsNum + 1;
//Avoid Tab Name Spaces //Avoid Tab Name Spaces
$tabNameNoSpaces = preg_replace('/\s+/', '', $tabName); $tabNameNoSpaces = preg_replace('/\s+/', '', $tabName);
$subkeytext = "Tabs" . $tabNameNoSpaces; $subkeytext = "Tabs" . $tabNameNoSpaces;
@ -497,42 +503,42 @@ class Plugin
'access_url_locked' => 0 'access_url_locked' => 0
); );
$resp = Database::insert('settings_current', $attributes); $resp = Database::insert('settings_current', $attributes);
//Save the id //Save the id
$settings = $this->get_settings(); $settings = $this->get_settings();
$setData = array ( $setData = array (
'comment' => $subkey 'comment' => $subkey
); );
$whereCond = array( $whereCondition = array(
'id = ?' => key($settings) 'id = ?' => key($settings)
); );
Database::update('settings_current', $setData, $whereCond); Database::update('settings_current', $setData, $whereCondition);
return $resp; return $resp;
} }
/** /**
* Delete a tab to chamilo's platform * Delete a tab to chamilo's platform
* @param type $key * @param string $key
*/ */
public function deleteTab($key) public function deleteTab($key)
{ {
$sql = "SELECT * $sql = "SELECT *
FROM settings_current FROM settings_current
WHERE variable = 'show_tabs' WHERE variable = 'show_tabs'
AND subkey <> '$key'"; AND subkey <> '$key'";
$resp = $result = Database::query($sql); $resp = $result = Database::query($sql);
$customTabsNum = Database::count_rows($result); $customTabsNum = Database::count_rows($result);
if (!empty($key)) { if (!empty($key)) {
$whereCond = array( $whereCondition = array(
'variable = ? AND subkey = ?' => array('show_tabs', $key) 'variable = ? AND subkey = ?' => array('show_tabs', $key)
); );
Database::delete('settings_current', $whereCond); Database::delete('settings_current', $whereCondition);
//if there is more than one tab //if there is more than one tab
//reenumerate them //re enumerate them
if ($customTabsNum > 0) { if ($customTabsNum > 0) {
$i = 1; $i = 1;
while ($row = Database::fetch_assoc($result)) { while ($row = Database::fetch_assoc($result)) {
@ -542,13 +548,12 @@ class Plugin
$resp = $this->updateTab($row['subkey'], $attributes); $resp = $this->updateTab($row['subkey'], $attributes);
$i++; $i++;
} }
} }
} }
return $resp; return $resp;
} }
/** /**
* Update the tabs attributes * Update the tabs attributes
* @param string $key * @param string $key
@ -557,18 +562,19 @@ class Plugin
*/ */
public function updateTab($key, $attributes) public function updateTab($key, $attributes)
{ {
$whereCond = array( $whereCondition = array(
'variable = ? AND subkey = ?' => array('show_tabs', $key) 'variable = ? AND subkey = ?' => array('show_tabs', $key)
); );
$resp = Database::update('settings_current', $attributes, $whereCond); $resp = Database::update('settings_current', $attributes, $whereCondition);
return $resp; return $resp;
} }
/** /**
* Add aditional plugin Settings * Add additional plugin Settings
* @param array $settings * @param array $settings
* @return bool
*/ */
public function addExtraSettings($settings) public function addExtraSettings($settings)
{ {
$pluginName = $this->get_name(); $pluginName = $this->get_name();
$resp = false; $resp = false;
@ -579,55 +585,57 @@ class Plugin
'selected_value' => $value, 'selected_value' => $value,
'category' => 'PluginSettings' 'category' => 'PluginSettings'
); );
if (empty($this->getExtraSettingValue($setting))) { $valueSetting = $this->getExtraSettingValue($setting);
if (empty($valueSetting)) {
$resp = Database::insert('settings_current', $attributes); $resp = Database::insert('settings_current', $attributes);
} }
} }
return $resp; return $resp;
} }
/** /**
* Edit aditional Plugin Settings * Edit additional Plugin Settings
* @param array $settings * @param array $settings
* @return bool
*/ */
public function editExtraSetting($key, $attributes) public function editExtraSetting($key, $attributes)
{ {
$pluginName = $this->get_name(); $pluginName = $this->get_name();
$whereCond = array( $whereCondition = array(
'variable = ? AND subkey = ?' => array('plugin_settings_' . $pluginName, $key) 'variable = ? AND subkey = ?' => array('plugin_settings_' . $pluginName, $key)
); );
$resp = Database::update('settings_current', $attributes, $whereCond); $resp = Database::update('settings_current', $attributes, $whereCondition);
return $resp; return $resp;
} }
/** /**
* Delete all aditional plugin settings * Delete all additional plugin settings
*/ */
public function deleteExtraSettings() public function deleteExtraSettings()
{ {
$pluginName = $this->get_name(); $pluginName = $this->get_name();
$whereCond = array( $whereCond = array(
'variable = ?' => 'plugin_settings_' . $pluginName 'variable = ?' => 'plugin_settings_' . $pluginName
); );
$resp = Database::delete('settings_current', $whereCond); $resp = Database::delete('settings_current', $whereCond);
return $resp; return $resp;
} }
/** /**
* Give extra setting value * Give extra setting value
* @param string $settingName * @param string $settingName
* @return string
*/ */
public function getExtraSettingValue($settingName) public function getExtraSettingValue($settingName)
{ {
$pluginName = $this->get_name(); $pluginName = $this->get_name();
$fullSetting = api_get_full_setting('plugin_settings_' . $pluginName, $settingName); $fullSetting = api_get_full_setting('plugin_settings_' . $pluginName, $settingName);
if (empty($fullSetting)) { if (empty($fullSetting)) {
return false; return false;
} else { } else {
@ -635,4 +643,4 @@ class Plugin
return $setting['selected_value']; return $setting['selected_value'];
} }
} }
} }

@ -28,6 +28,9 @@ class AppPlugin
public $installedPluginListName = array(); public $installedPluginListName = array();
public $installedPluginListObject = array(); public $installedPluginListObject = array();
/**
*
*/
public function __construct() public function __construct()
{ {
} }
@ -136,7 +139,7 @@ class AppPlugin
/** /**
* @param string $pluginName * @param string $pluginName
* @param int $urlId * @param int $urlId
*/ */
public function install($pluginName, $urlId = null) public function install($pluginName, $urlId = null)
{ {
@ -170,9 +173,9 @@ class AppPlugin
} }
/** /**
* @param string $pluginName * @param string $pluginName
* @param int $urlId * @param int $urlId
*/ */
public function uninstall($pluginName, $urlId = null) public function uninstall($pluginName, $urlId = null)
{ {
if (empty($urlId)) { if (empty($urlId)) {
@ -248,12 +251,12 @@ class AppPlugin
} }
/** /**
* @param string $region * @param string $region
* @param string $template * @param string $template
* @param bool $forced * @param bool $forced
* *
* @return null|string * @return null|string
*/ */
public function load_region($region, $template, $forced = false) public function load_region($region, $template, $forced = false)
{ {
if ($region == 'course_tool_plugin') { if ($region == 'course_tool_plugin') {
@ -270,8 +273,9 @@ class AppPlugin
/** /**
* Loads the translation files inside a plugin if exists. It loads by default english see the hello world plugin * Loads the translation files inside a plugin if exists. It loads by default english see the hello world plugin
* *
* @todo add caching
* @param string $plugin_name * @param string $plugin_name
*
* @todo add caching
*/ */
public function load_plugin_lang_variables($plugin_name) public function load_plugin_lang_variables($plugin_name)
{ {
@ -305,9 +309,12 @@ class AppPlugin
} }
/** /**
* @param string $region
* @param Template $template
* @param bool $forced
*
* @return bool
* *
* @param string $block
* @param Template $template
* @todo improve this function * @todo improve this function
*/ */
public function get_all_plugin_contents_by_region($region, $template, $forced = false) public function get_all_plugin_contents_by_region($region, $template, $forced = false)
@ -369,21 +376,25 @@ class AppPlugin
} }
/** /**
* @param $plugin_name * @param string $plugin_name
* @param bool $forced * @param bool $forced
*
* @deprecated * @deprecated
*/ */
public function get_plugin_info($plugin_name, $forced = false) { public function get_plugin_info($plugin_name, $forced = false)
{
return $this->getPluginInfo($plugin_name, $forced); return $this->getPluginInfo($plugin_name, $forced);
} }
/** /**
* Loads plugin info * Loads plugin info
*
* @staticvar array $plugin_data * @staticvar array $plugin_data
* @param string plugin name * @param string $plugin_name
* @param bool load from DB or from the static array * @param bool $forced load from DB or from the static array
* @todo filter setting_form *
* @return array * @return array
* @todo filter setting_form
*/ */
public function getPluginInfo($plugin_name, $forced = false) public function getPluginInfo($plugin_name, $forced = false)
{ {
@ -535,7 +546,7 @@ class AppPlugin
/** /**
* When saving the plugin values in the course settings, check whether * When saving the plugin values in the course settings, check whether
* a callback method should be called and send it the updated settings * a callback method should be called and send it the updated settings
* @param array The new settings the user just saved * @param array $values The new settings the user just saved
* @return void * @return void
*/ */
public function saveCourseSettingsHook($values) public function saveCourseSettingsHook($values)

Loading…
Cancel
Save