From 1794b211da8a2810726de9c0be5c17066e52d7ec Mon Sep 17 00:00:00 2001 From: Yannick Warnier Date: Fri, 13 Jul 2012 01:02:44 -0500 Subject: [PATCH] Added callback capability on course settings update for all plugins - refs #4796 --- main/inc/lib/plugin.class.php | 13 ++++++++++ main/inc/lib/plugin.lib.php | 45 ++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/main/inc/lib/plugin.class.php b/main/inc/lib/plugin.class.php index d9c92213b7..ed05adcf18 100644 --- a/main/inc/lib/plugin.class.php +++ b/main/inc/lib/plugin.class.php @@ -38,6 +38,11 @@ class Plugin { ); */ public $course_settings = array(); + /** + * This indicates whether changing the setting should execute the callback + * function. + */ + public $course_settings_callback = false; /** * Default constructor for the plugin class. By default, it only sets @@ -360,4 +365,12 @@ class Plugin { $this->uninstall_course_fields($row['id']); } } + /** + * Method to be extended when changing the setting in the course + * configuration should trigger the use of a callback method + * @param array Values sent back from the course configuration script + * @return void + */ + private function course_settings_updated($values = array()) { + } } diff --git a/main/inc/lib/plugin.lib.php b/main/inc/lib/plugin.lib.php index fdeded9a02..ba37cf2c46 100644 --- a/main/inc/lib/plugin.lib.php +++ b/main/inc/lib/plugin.lib.php @@ -361,4 +361,47 @@ class AppPlugin { } } } -} \ No newline at end of file + /** + * When saving the plugin values in the course settings, check whether + * a callback method should be called and send it the updated settings + * @param array The new settings the user just saved + * @return void + */ + function save_course_settings($values) { + $plugin_list = $this->get_installed_plugins(); + foreach ($plugin_list as $plugin_name) { + $settings = $this->get_plugin_course_settings($plugin_name); + $subvalues = array(); + $i = 0; + foreach ($settings as $v) { + if (isset($values[$v])) { + $subvalues[$v] = $values[$v]; + $i++; + } + } + if ($i>0) { + $plugin_info = $this->get_plugin_info($plugin_name); + $obj = $plugin_info['plugin_class']::create(); + $obj->course_settings_updated($subvalues); + } + } + } + /** + * Gets a nice array of keys for just the plugin's course settings + * @param string The plugin ID + * @return array Nice array of keys for course settings + */ + public function get_plugin_course_settings($plugin_name) { + $settings = array(); + if (empty($plugin_name)) { return $settings; } + $plugin_info = $this->get_plugin_info($plugin_name); + $obj = $plugin_info['plugin_class']::create(); + if (is_array($obj->course_settings)) { + foreach ($obj->course_settings as $item) { + $settings[] = $item['name']; + } + } + unset($obj); unset($plugin_info); + return $settings; + } +}