Browse Source

Improving plugin system see #4450 adding new parameters in order to install a plugin

jmontoya 13 years ago
parent
commit
8996ea90b1

+ 3 - 2
main/admin/configure_extensions.php

@@ -1,5 +1,5 @@
-<?php // $Id: configure_homepage.php 9246 2006-09-25 13:24:53 +0000 (lun., 25 sept. 2006) bmol $
-/* For licensing terms, see /chamilo_license.txt */
+<?php
+/* For licensing terms, see /license.txt */
 
 // name of the language file that needs to be included
 $language_file='admin';
@@ -244,6 +244,7 @@ Display::display_header($nameTool);
 - please fill free to improve the global display of the document
 -->
 	<!-- VISIOCONFERENCE -->
+   
 	<div id="main_visio">
 		<div id="extension_header_visio" class="accordion_header">
 			<a href="#"><?php echo get_lang('Visioconf') ?></a>

+ 58 - 0
main/admin/configure_plugin.php

@@ -0,0 +1,58 @@
+<?php
+/* For licensing terms, see /license.txt */
+/**
+	@author Julio Montoya <gugli100@gmail.com> BeezNest 2012
+*	@package chamilo.admin
+*/
+
+// name of the language file that needs to be included
+$language_file = array ('registration','admin');
+$cidReset = true;
+require_once '../inc/global.inc.php';
+
+$plugin_name = $_GET['name'];
+
+$plugin_obj = new AppPlugin();
+$plugin_info = $plugin_obj->get_plugin_info($plugin_name, true);
+
+if (empty($plugin_info)) {
+    api_not_allowed();
+}
+
+$installed_plugins = $plugin_obj->get_installed_plugins();
+
+if (!in_array($plugin_name, $installed_plugins)) {
+    api_not_allowed();
+}
+
+global $_configuration;
+
+$content = '';
+
+if (isset($plugin_info['settings_form'])) {
+    $form = $plugin_info['settings_form'];
+    if (isset($form)) {
+        //We override the form attributes
+        $attributes = array('action'=>api_get_self().'?name='.$plugin_name, 'method'=>'POST');
+        $form->updateAttributes($attributes);        
+        $content = $form->toHtml();
+    }
+}
+
+if ($form->validate()) {
+    $values = $form->exportValues();
+    //api_delete_category_settings_by_subkey($plugin_name);
+    $access_url_id = api_get_current_access_url_id();
+    api_delete_settings_params(array('category = ? AND access_url = ? AND subkey = ? AND type = ? and variable <> ?' =>
+                                   array('Plugins', $access_url_id, $plugin_name, 'setting', "status")));
+    foreach ($values as $key => $value) {
+        $key = Database::escape_string($plugin_name.'_'.$key);
+        api_add_setting($value, $key, $plugin_name, 'setting', 'Plugins', $plugin_name, null, null, null, $_configuration['access_url'], 1);        
+    }
+}
+
+$tpl = new Template($tool_name);
+$tpl->assign('actions', $actions);
+$tpl->assign('message', $message);
+$tpl->assign('content', $content);
+$tpl->display_one_col_template();

+ 3 - 2
main/admin/index.php

@@ -126,8 +126,7 @@ if (api_is_platform_admin()) {
     
     /* Platform */    
     $blocks['platform']['icon']  = Display::return_icon('platform.png', get_lang('Platform'), array(), ICON_SIZE_MEDIUM, false);
-    $blocks['platform']['label'] = api_ucfirst(get_lang('Platform'));
-    
+    $blocks['platform']['label'] = api_ucfirst(get_lang('Platform'));    
     
     $search_form = ' <form method="get" action="settings.php" class="form-search">
 							<input type="text" name="search_field" value="" >
@@ -139,6 +138,8 @@ if (api_is_platform_admin()) {
     
     $items = array();
     $items[] = array('url'=>'settings.php', 				'label' => get_lang('PlatformConfigSettings'));
+    $items[] = array('url'=>'settings.php?category=Plugins','label' => get_lang('Plugins'));
+    $items[] = array('url'=>'settings.php?category=Regions','label' => get_lang('Regions'));
     $items[] = array('url'=>'system_announcements.php', 	'label' => get_lang('SystemAnnouncements'));
     $items[] = array('url'=>api_get_path(WEB_CODE_PATH).'calendar/agenda_js.php?type=admin', 'label' => get_lang('GlobalAgenda'));
     $items[] = array('url'=>'configure_homepage.php', 		'label' => get_lang('ConfigureHomePage'));

+ 131 - 74
main/admin/settings.lib.php

@@ -11,20 +11,14 @@
  * @package chamilo.admin
  */
 
-
-
 /**
- * This function allows easy activating and inactivating of plugins
- * @todo: a similar function needs to be written to activate or inactivate additional tools.
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+ * This function allows easy activating and inactivating of regions
+ * @author Julio Montoya <gugli100@gmail.com> Beeznest 2012
  */
-function handle_plugins() {
-    global $SettingsStored;
-    $userplugins = array();
-    $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
-
+function handle_regions() {
+    
     if (isset($_POST['submit_plugins'])) {
-        store_plugins();
+        store_regions();
         // Add event to the system log.        
         $user_id = api_get_user_id();
         $category = $_GET['category'];
@@ -33,32 +27,22 @@ function handle_plugins() {
     }
     
     $plugin_obj = new AppPlugin();
-    $possible_plugins = $plugin_obj->read_plugins_from_path();    
+    $possible_plugins  = $plugin_obj->read_plugins_from_path();
+    $installed_plugins = $plugin_obj->get_installed_plugins(); 
 
+    if (!empty($installed_plugins)) {
+        $not_installed = array_diff($possible_plugins, $installed_plugins);
+    } else {
+        $not_installed = $possible_plugins;
+    }
     echo '<form name="plugins" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).'">';
     echo '<table class="data_table">';
     echo '<tr>';
     echo '<th width="400px">';
     echo get_lang('Plugin');
     echo '</th><th>';
-    echo get_lang('PluginArea').'';
+    echo get_lang('Blocks');
     echo '</th>';
-    /*
-    echo get_lang('LoginPageMenu').'<br />(loginpage_menu)';
-    echo '</th><th>';
-    echo get_lang('CampusHomepageMainArea').'<br />(campushomepage_main)';
-    echo '</th><th>';
-    echo get_lang('CampusHomepageMenu').'<br />(campushomepage_menu)';
-    echo '</th><th>';
-    echo get_lang('MyCoursesMainArea').'<br />(mycourses_main)';
-    echo '</th><th>';
-    echo get_lang('MyCoursesMenu').'<br />(mycourses_menu)';
-    echo '</th><th>';
-    echo get_lang('Header').'<br />(header)';
-    echo '</th><th>';
-    echo get_lang('Footer').'<br />(footer)';
-    echo '</th><th>';
-    echo get_lang('CourseTool').'';*/
     echo '</th>';
     echo '</tr>';
     
@@ -70,7 +54,7 @@ function handle_plugins() {
         $plugin_list[$plugin_item] = $plugin_item;
     }
 
-    foreach ($possible_plugins as $plugin) {
+    foreach ($installed_plugins as $plugin) {
         $plugin_info_file = api_get_path(SYS_PLUGIN_PATH).$plugin.'/plugin.php';
         
         if (file_exists($plugin_info_file)) {
@@ -78,12 +62,12 @@ function handle_plugins() {
             require $plugin_info_file;
             echo '<tr>';
             echo '<td>';            
-            echo '<h3>'.$plugin_info['title'].' <small>v '.$plugin_info['version'].'</small></h3>';
+            echo '<h4>'.$plugin_info['title'].' <small>v '.$plugin_info['version'].'</small></h4>';
             echo '<p>'.$plugin_info['comment'].'</p>';
             echo '<p>'.get_lang('Author').': '.$plugin_info['author'].'</p>';
             
             if (file_exists(api_get_path(SYS_PLUGIN_PATH).$plugin.'/readme.txt')) {
-                echo "<a href='".api_get_path(WEB_PLUGIN_PATH).$plugin."/readme.txt'>readme.txt</a>";
+                echo "<a href='".api_get_path(WEB_PLUGIN_PATH).$plugin."/readme.txt' class='ajax'>readme.txt</a>";
             }
             echo '</td><td>';                        
             $selected_plugins = $plugin_obj->get_areas_by_plugin($plugin);            
@@ -97,21 +81,89 @@ function handle_plugins() {
     echo '<br />';
     echo '<button class="save" type="submit" name="submit_plugins">'.get_lang('EnablePlugins').'</button></form>';
     echo '<br />';
+    
 }
 
 
-function display_plugin_cell($location, $plugin_info, $current_plugin, $active_plugins) {
-    echo '<td align="center">';
-    if (in_array($location, $plugin_info['location'])) {
-        if (isset($active_plugins[$location]) && is_array($active_plugins[$location])
-            && in_array($current_plugin, $active_plugins[$location])) {
-            $checked = 'checked';
-        } else {
-            $checked = '';
+/**
+ * This function allows easy activating and inactivating of plugins
+ * @todo: a similar function needs to be written to activate or inactivate additional tools.
+ * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+ * @author Julio Montoya <gugli100@gmail.com> Beeznest 2012
+ */
+function handle_plugins() {
+    $plugin_obj = new AppPlugin();
+    
+     if (isset($_POST['submit_plugins'])) {
+        store_plugins();
+        // Add event to the system log.        
+        $user_id = api_get_user_id();
+        $category = $_GET['category'];
+        event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, api_get_utc_datetime(), $user_id);
+        Display :: display_confirmation_message(get_lang('SettingsStored'));
+    }
+    
+    $all_plugins = $plugin_obj->read_plugins_from_path(); 
+    $installed_plugins = $plugin_obj->get_installed_plugins(); 
+    $not_installed = array_diff($all_plugins, $installed_plugins);    
+    
+    //Plugins NOT installed
+    echo '<form name="plugins" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).'">';
+    echo '<table class="data_table">';
+    echo '<tr>';
+    echo '<th width="20px">';
+    echo get_lang('Plugin');
+    echo '</th><th>';
+    echo get_lang('InstallPlugin');
+    echo '</th>';    
+
+    echo '</th>';
+    echo '</tr>';
+
+    
+    $plugin_list = array();
+    $my_plugin_list = $plugin_obj->get_plugin_blocks();
+    foreach($my_plugin_list as $plugin_item) {
+        $plugin_list[$plugin_item] = $plugin_item;
+    }
+
+    foreach ($all_plugins as $plugin) {
+        $plugin_info_file = api_get_path(SYS_PLUGIN_PATH).$plugin.'/plugin.php';
+        
+        if (file_exists($plugin_info_file)) {
+            $plugin_info = array();
+            require $plugin_info_file;
+            echo '<tr>';
+            echo '<td>';
+            //Checkbox
+            if (in_array($plugin, $installed_plugins)) {              
+                echo '<input type="checkbox" name="plugin_'.$plugin.'[]" checked="checked">';
+               
+            } else {            
+                echo '<input type="checkbox" name="plugin_'.$plugin.'[]">';
+            }
+            echo '</td><td>';
+            
+            echo '<h4>'.$plugin_info['title'].' <small>v '.$plugin_info['version'].'</small></h4>';
+            echo '<p>'.$plugin_info['comment'].'</p>';
+            echo '<p>'.get_lang('Author').': '.$plugin_info['author'].'</p>';
+            
+            echo '<div class="btn-group">';
+            if (in_array($plugin, $installed_plugins)) {     
+                 echo Display::url(get_lang('Configure'), 'configure_plugin.php?name='.$plugin, array('class' => 'btn'));
+            }
+            
+            if (file_exists(api_get_path(SYS_PLUGIN_PATH).$plugin.'/readme.txt')) {
+                 echo Display::url("readme.txt", api_get_path(WEB_PLUGIN_PATH).$plugin."/readme.txt", array('class' => 'btn ajax', '_target' => '_blank'));
+            }
+            echo '</div>';
+            echo '</td></tr>';        
         }
-        echo '<input type="checkbox" name="'.$current_plugin.'-'.$location.'" value="true" '.$checked.'/>';
     }
-    echo "</td>";
+    echo '</table>';
+    echo '<br />';
+    echo '<button class="save" type="submit" name="submit_plugins">'.get_lang('EnablePlugins').'</button></form>';
+    echo '<br />';
 }
 
 /**
@@ -348,53 +400,60 @@ function upload_stylesheet($values, $picture) {
     return $result;
 }
 
-/**
- * This function allows easy activating and inactivating of plugins
- * @todo: A similar function needs to be written to activate or inactivate additional tools.
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
-*/
-function store_plugins() { 
-    global $_configuration;
-
-    $plugin_obj = new AppPlugin();
+function store_regions() {
+     $plugin_obj = new AppPlugin();
 
     // Get a list of all current 'Plugins' settings
-    $installed_plugins = api_get_settings('Plugins','list', $_configuration['access_url']);
+    $installed_plugins = $plugin_obj->get_installed_plugins();
+    
     $shortlist_installed = array();
     foreach ($installed_plugins as $plugin) {
         $shortlist_installed[] = $plugin['subkey'];
     }
     $shortlist_installed = array_flip(array_flip($shortlist_installed));
-
-    $r = api_delete_category_settings('Plugins', $_configuration['access_url']);
-
     $plugin_list = $plugin_obj->read_plugins_from_path();
 
     foreach ($plugin_list as $plugin) {
         if (isset($_POST['plugin_'.$plugin])) {
             $areas_to_installed = $_POST['plugin_'.$plugin];
-            foreach ($areas_to_installed as $area) {
-                api_add_setting($plugin, $area, $plugin, null, 'Plugins', $plugin, null, null, null, $_configuration['access_url'], 1);
-                $pluginpath = api_get_path(SYS_PLUGIN_PATH).$plugin.'/install.php';
-                if (is_file($pluginpath) && is_readable($pluginpath)) {
-                    //execute the install procedure
-                    require $pluginpath;
-                }
+            $plugin_obj->remove_all_regions($plugin);
+            foreach ($areas_to_installed as $region) {
+                $plugin_obj->add_to_region($plugin, $region);
             }
         }
     }
+}
 
-    foreach ($shortlist_installed as $plugin) {
-        // if one plugin was really deleted, execute the uninstall script
-    	if (!in_array($plugin,$shortlist_required)) {
-            // check if there is an uninstall procedure
-            $pluginpath = api_get_path(SYS_PLUGIN_PATH).$plugin.'/uninstall.php';
-            if (is_file($pluginpath) && is_readable($pluginpath)) {
-                //execute the uninstall procedure
-                require $pluginpath;
-            }
-    	}
+/**
+ * This function allows easy activating and inactivating of plugins
+ * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+*/
+function store_plugins() {
+
+    $plugin_obj = new AppPlugin();
+
+    // Get a list of all current 'Plugins' settings
+   
+    $plugin_list = $plugin_obj->read_plugins_from_path();
+    
+    $installed_plugins = array();
+    
+    foreach ($plugin_list as $plugin) {
+        if (isset($_POST['plugin_'.$plugin])) {
+            $plugin_obj->install($plugin);
+            $installed_plugins[] = $plugin;
+        }
+    }
+    
+    if (!empty($installed_plugins)) {
+        $remove_plugins = array_diff($plugin_list, $installed_plugins);
+    } else {
+        $remove_plugins = $plugin_list;
+    }    
+    foreach ($remove_plugins as $plugin) {
+        $plugin_obj->uninstall($plugin);
     }
+  
 }
 
 
@@ -404,8 +463,6 @@ function store_plugins() {
 */
 function store_stylesheets() {
     global $_configuration;
-    // Database table definitions.
-    $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
 
     // Insert the stylesheet.
     $style = Database::escape_string($_POST['style']);

+ 3 - 1
main/admin/settings.php

@@ -386,6 +386,9 @@ if (isset($form) && $form->validate()) {
 
 if (!empty($_GET['category'])) {
     switch ($_GET['category']) {
+        case 'Regions':
+            handle_regions();
+            break;
         case 'Plugins':
             // Displaying the extensions: Plugins.
             // This will be available to all the sites (access_urls).
@@ -401,7 +404,6 @@ if (!empty($_GET['category'])) {
             }
             handle_plugins();
             DashboardManager::handle_dashboard_plugins();
-
             break;
         case 'stylesheets':
             // Displaying the extensions: Stylesheets.

+ 14 - 8
main/inc/lib/database.lib.php

@@ -1141,7 +1141,10 @@ class Database {
     /**
      * Experimental useful database finder
      * @todo lot of stuff to do here
-     * @todo known issues, it doesn't work when using LIKE conditions example: array('where'=>array('course_code LIKE "?%"'))
+     * @todo known issues, it doesn't work when using LIKE conditions 
+     * @example array('where'=> array('course_code LIKE "?%"'))
+     * @example array('where'=> array('type = ? AND category = ?' => array('setting', 'Plugins'))
+     * @example array('where'=> array('name = "Julio" AND lastname = "montoya"))
     */
 
     public static function select($columns, $table_name,  $conditions = array(), $type_result = 'all', $option = 'ASSOC') {
@@ -1195,10 +1198,11 @@ class Database {
             }
             $type_condition = strtolower($type_condition);
             switch ($type_condition) {
-                case 'where':                                       
-                    foreach ($condition_data as $condition => $value_array) {                        
+                case 'where':            
+                    
+                    foreach ($condition_data as $condition => $value_array) {                       
                         if (is_array($value_array)) {
-                            $clean_values = array();
+                            $clean_values = array();                       
                             foreach($value_array as $item) {
                                 $item = Database::escape_string($item);
                                 $clean_values[]= $item;
@@ -1207,22 +1211,24 @@ class Database {
                             $value_array = Database::escape_string($value_array);
                             $clean_values = $value_array;                            
                         }
-                       
+
                         if (!empty($condition) && $clean_values != '') {                                                                                      
                             $condition = str_replace('%',"'@percentage@'", $condition); //replace "%"
                             $condition = str_replace("'?'","%s", $condition);
                             $condition = str_replace("?","%s", $condition);
-                            
+
                             $condition = str_replace("@%s@","@-@", $condition);
                             $condition = str_replace("%s","'%s'", $condition);
                             $condition = str_replace("@-@","@%s@", $condition);
-                            
+
                             //Treat conditons as string                            
                             $condition = vsprintf($condition, $clean_values);                                                        
                             $condition = str_replace('@percentage@','%', $condition); //replace "%"                            
                             $where_return .= $condition;
-                        }
+                        }                       
                     }
+                                         
+                        
                     if (!empty($where_return)) {
                         $return_value = " WHERE $where_return" ;
                     }

+ 26 - 8
main/inc/lib/main_api.lib.php

@@ -1137,7 +1137,6 @@ function api_get_cidreq() {
  */
 function api_get_course_info($course_code = null) {
     if (!empty($course_code)) {
-        //global $_course;var_dump($_course);
         $course_code        = Database::escape_string($course_code);
         $course_table       = Database::get_main_table(TABLE_MAIN_COURSE);
         $course_cat_table   = Database::get_main_table(TABLE_MAIN_CATEGORY);
@@ -1913,6 +1912,25 @@ function api_get_setting($variable, $key = null) {
     return $value;
 }
 
+/**
+ * Returns the value of a setting from the web-adjustable admin config settings.
+ **/
+function api_get_settings_params($params) {
+    $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
+    $result = Database::select('*', $table, array('where' => $params));
+    return $result;
+}
+
+/**
+ * Returns the value of a setting from the web-adjustable admin config settings.
+ **/
+function api_delete_settings_params($params) {
+    $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
+    $result = Database::delete($table, $params);
+    return $result;
+}
+
+
 /**
  * Returns an escaped version of $_SERVER['PHP_SELF'] to avoid XSS injection
  * @return string   Escaped version of $_SERVER['PHP_SELF']
@@ -4181,6 +4199,7 @@ function & api_get_settings_categories($exceptions = array(), $access_url = 1) {
  * @param int       Access URL
  * @return boolean  False on failure, true on success
  */
+
 function api_delete_setting($v, $s = null, $a = 1) {
     if (empty($v)) { return false; }
     $t_cs = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
@@ -4200,17 +4219,16 @@ function api_delete_setting($v, $s = null, $a = 1) {
 
 /**
  * Deletes all the settings from one category
- * @param string    Category
+ * @param string    Subkey
  * @param int       Access URL
  * @return boolean  False on failure, true on success
  */
-function api_delete_category_settings($c, $a = 1) {
-    if (empty($c)) { return false; }
+function api_delete_category_settings_by_subkey($subkey, $access_url_id = 1) {
+    if (empty($subkey)) { return false; }
     $t_cs = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
-    $c = Database::escape_string($c);
-    $a = (int) $a;
-    if (empty($a)) { $a = 1; }
-    $sql = "DELETE FROM $t_cs WHERE category = '$c' AND access_url = $a";
+    $subkey = Database::escape_string($subkey);
+    $access_url_id = intval($access_url_id);
+    $sql = "DELETE FROM $t_cs WHERE subkey = '$subkey' AND access_url = $access_url_id";
     $r = Database::query($sql);
     return $r;
 }

+ 81 - 16
main/inc/lib/plugin.lib.php

@@ -50,7 +50,6 @@ class AppPlugin {
         return $possible_plugins;
     }
     
-    
     function get_installed_plugins_by_block(){
         $usedplugins = array();
         /* We retrieve all the active plugins. */    
@@ -63,16 +62,41 @@ class AppPlugin {
     
     function get_installed_plugins() {
         $installed_plugins = array();
-        $result = api_get_settings('Plugins'); 
-        if (!empty($result)) {
-            foreach ($result as $row) {
-                $installed_plugins[$row['selected_value']] = true;
+        $plugin_array = 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;
             }
             $installed_plugins = array_keys($installed_plugins);
         }
         return $installed_plugins;
     }
     
+    function install($plugin_name, $access_url_id) {
+        $access_url_id = api_get_current_access_url_id();
+        api_add_setting('installed', 'status', $plugin_name, 'setting', 'Plugins', $plugin_name, null, null, null, $access_url_id, 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';
+        if (is_file($pluginpath) && is_readable($pluginpath)) {
+            //execute the install procedure
+            require $pluginpath;
+        }    
+    }
+    
+    function uninstall($plugin_name) {
+        $access_url_id = api_get_current_access_url_id();
+        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;
+        }
+    }
+    
     function get_areas_by_plugin($plugin_name) {
         $result = api_get_settings('Plugins');        
         $areas = array();
@@ -117,16 +141,18 @@ class AppPlugin {
      * @param smarty obj      
      * @todo improve this function
      */
-    function get_all_plugin_contents_by_block($block, $main_template) {
+    function get_all_plugin_contents_by_block($block, $template) {
         global $_plugins;
         if (isset($_plugins[$block]) && is_array($_plugins[$block])) {
             foreach ($_plugins[$block] as $plugin_name) {
                 //Load the plugin information
-                //
+                
                 //The plugin_info variable is available inside the plugin index
+                
                 $plugin_info = $this->get_plugin_info($plugin_name);
+                
                 //We also where the plugin is
-                $plugin_info['current_block'] = $block;    
+                $plugin_info['current_region'] = $block;    
                 
                 // Loading the plugin/XXX/index.php file                
                 require api_get_path(SYS_PLUGIN_PATH)."$plugin_name/index.php";
@@ -134,7 +160,7 @@ class AppPlugin {
                 //We set the $template variable in order to use smarty
                 if (isset($_template) && !empty($_template)) {                        
                     foreach($_template as $key =>$value) {
-                        $main_template->assign($key, $value);                          
+                        $template->assign($key, $value);                          
                     }
                 }                
                 
@@ -142,26 +168,33 @@ class AppPlugin {
                 $template_list = array();
                 if (isset($plugin_info) && isset($plugin_info['templates'])) {
                     $template_list = $plugin_info['templates'];
-                }
-                
-                //var_dump($plugin_info);                
+                }           
                
                 if (!empty($template_list)) {
                     foreach($template_list as $plugin_tpl) {
                         if (!empty($plugin_tpl)) {
                             $template_plugin_file = api_get_path(SYS_PLUGIN_PATH)."$plugin_name/$plugin_tpl";                            
-                            $main_template->display($template_plugin_file);                                                
+                            $template->display($template_plugin_file);                                                
                         }
                     }                
                 }               
             }
         }
-        return false;
+        return true;
     }
     
-    function get_plugin_info($plugin_name) {
+    /**
+     *
+     * Loads plugin info 
+     * @staticvar array $plugin_data
+     * @param type $plugin_name
+     * @param type bool
+     * @todo filter setting_form
+     * @return array 
+     */
+    function get_plugin_info($plugin_name, $forced = false) {
         static $plugin_data = array();
-        if (isset($plugin_data[$plugin_name])) {
+        if (isset($plugin_data[$plugin_name]) && $forced == false) {
             return $plugin_data[$plugin_name]; 
         } else {
             $plugin_file = api_get_path(SYS_PLUGIN_PATH)."$plugin_name/plugin.php";
@@ -170,10 +203,22 @@ class AppPlugin {
                 require $plugin_file;            
             }
             $plugin_data[$plugin_name] = $plugin_info;
+           
+            //extra options
+            $plugin_settings = api_get_settings_params(array("subkey = ? AND category = ? AND type = ? " =>
+                                            array($plugin_name, 'Plugins','setting')));
+            $settings_filtered = array();
+            foreach ($plugin_settings as $item) {
+                $settings_filtered[$item['variable']] = $item['selected_value'];
+            }
+            $plugin_info['settings'] = $settings_filtered;            
             return $plugin_info;
         }
     }
     
+    /*
+     * Get the template list
+     */
     function get_templates_list($plugin_name) {
         $plugin_info = $this->get_plugin_info($plugin_name);        
         if (isset($plugin_info) && isset($plugin_info['templates'])) {
@@ -182,4 +227,24 @@ class AppPlugin {
             return false;
         }
     }
+    
+    /* *
+     * Remove all regions of an specific plugin
+     */
+    function remove_all_regions($plugin) {
+        $access_url_id = api_get_current_access_url_id();
+        if (!empty($plugin)) {
+            api_delete_settings_params(array('category = ? AND variable = ? AND access_url = ? AND subkey = ? ' =>
+                                array('Plugins', 'region', $access_url_id, $plugin)));
+    
+        }
+    }
+    
+    /* 
+     * Add a plugin to a region
+     */
+    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);      
+    }
 }

+ 2 - 4
main/inc/lib/template.lib.php

@@ -77,10 +77,8 @@ class Template extends Smarty {
         $plugin_blocks = $this->plugin->get_plugin_blocks();
         foreach ($plugin_blocks as $block) {
             $this->set_plugin_block($block);
-        }
-        
-        $this->load_plugin_template();       
-       
+        }        
+        $this->load_plugin_template();      
 	}
     
     function set_help($help_input = null) {        

+ 7 - 4
plugin/hello_world/index.php

@@ -3,7 +3,10 @@
  * Controller for example date plugin
  * @package chamilo.plugin.date
  */
-/**
- * Code
- */
-echo "<h2>Hello world</h2>";
+
+
+if (!empty($plugin_info['settings']['hello_world_show_type'])) {
+    echo "<h2>".$plugin_info['settings']['hello_world_show_type']."</h2>";
+} else {
+    echo "<h2>Hello worlds</h2>";  
+}

+ 13 - 1
plugin/hello_world/plugin.php

@@ -16,4 +16,16 @@ $plugin_info['comment']     = "Shows a hello world message";
 //the plugin version
 $plugin_info['version']     = '1.0';
 //the plugin author
-$plugin_info['author']      = 'Julio Montoya';
+$plugin_info['author']      = 'Julio Montoya';
+
+
+//More complex options for the plugin (optional)
+
+$form = new FormValidator();
+
+//A simple select
+$options = array('hello_world' => 'Hello World', 'hello' =>'Hello', 'hi' =>'Hi!');
+$form->addElement('select', 'show_type', 'Hello world types', $options);
+$form->addElement('style_submit_button', 'submit_button', get_lang('Save'));  
+
+$plugin_info['settings_form'] = $form;

+ 3 - 2
plugin/show_template_blocks/index.php → plugin/show_regions/index.php

@@ -1,8 +1,9 @@
 <?php
-//only show the block information if the admin is logged in
+
+//only show the region information if the admin is logged in
 if (api_is_platform_admin()) {
     echo '<div style="color:black;height:50px;width:200px;background-color:#FFE378">';
     //We can have access to the current block and the block information with the variable $plugin_info (see your plugin.php)
-    echo $plugin_info['current_block'];
+    echo $plugin_info['current_region'];
     echo '</div>';    
 }

+ 4 - 3
plugin/show_template_blocks/plugin.php → plugin/show_regions/plugin.php

@@ -10,10 +10,11 @@
  */
 
 //the plugin title
-$plugin_info['title']       = 'Show template blocks';
+$plugin_info['title']       = 'Show regions';
 //the comments that go with the plugin
-$plugin_info['comment']     = "This is useful when trying to find the blocks, (<strong>only the admin </strong>can view this plugin). You have to activate all the items in the Plugin area";
+$plugin_info['comment']     = "This is useful when trying to find the Chamilo regions, (<strong>only the admin </strong>can view this plugin). You have to activate all the items in the Regions area";
 //the plugin version
 $plugin_info['version']     = '1.0';
 //the plugin author
-$plugin_info['author']      = 'Julio Montoya';
+$plugin_info['author']      = 'Julio Montoya';
+

+ 0 - 0
plugin/show_template_blocks/readme.txt → plugin/show_regions/readme.txt