From: tjhunt Date: Mon, 30 Mar 2009 06:13:35 +0000 (+0000) Subject: get_string: MDL-13816 allow third-partly modules to register sub-plugin types that... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=e7b818807cd99f3e57a9137cbe5bab6766fa4f35;p=moodle.git get_string: MDL-13816 allow third-partly modules to register sub-plugin types that have their own lang files. I suppose to be really useful we should deal with database, capabiltities, etc. too in due course. Still, this is a start. --- diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 1dd58ef9fb..5839210af9 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -5278,6 +5278,7 @@ class string_manager { 'portfolio_' => array('portfolio/type'), '' => array('mod') ); + $this->restore_extra_locations_from_session(); if ($runninginstaller) { $stringnames = file($dirroot . '/install/stringnames.txt'); $this->installstrings = array_map('trim', $stringnames); @@ -5285,6 +5286,39 @@ class string_manager { } } + protected function restore_extra_locations_from_session() { + global $SESSION; + if (!empty($SESSION->extralangsearchlocations)) { + foreach ($SESSION->extralangsearchlocations as $plugintype => $path) { + $this->register_plugin_type($plugintype, $path); + } + } + } + + /** + * Register a new type of plugin with the string_manager class. A typical usage might be + * string_manager::instance()->register_plugin_type('mymodreport', 'mod/mymod/report'); + * This should never be needed for standard plugin types. It is intended for third-party + * plugins that in turn want to register a sub-plugin type. + * + * @param string $plugintype a new type of plugin + * @param string $path the path where plugins of this type live. + */ + public function register_plugin_type($plugintype, $path) { + global $SESSION; + $key = $plugintype . '_'; + if (isset($this->searchplacesbyplugintype[$key]) && $path == reset($this->searchplacesbyplugintype[$key])) { + // Nothing to do. + return; + } + $this->searchplacesbyplugintype[$key] = array($path); + // We store all registered extra plugin types in the session in order to + // allow links to help files to work. I cannot think of a better way to + // make this information available to help.php. Putting it in the URL + // would be insecure. + $SESSION->extralangsearchlocations[$plugintype] = $path; + } + protected function fix_deprecated_module_name($module) { debugging('The module name you passed to get_string is the deprecated format ' . 'like mod/mymod or block/myblock. The correct form looks like mymod, or block_myblock.' , DEBUG_DEVELOPER); diff --git a/lib/simpletest/teststringmanager.php b/lib/simpletest/teststringmanager.php index b091e7fc87..880f2a42ca 100644 --- a/lib/simpletest/teststringmanager.php +++ b/lib/simpletest/teststringmanager.php @@ -176,6 +176,30 @@ class string_manager_test extends UnitTestCase { )); } + public function test_register_plugin_type() { + $this->stringmanager->register_plugin_type('mymodreport', 'mod/mymod/report'); + $this->assertEqual($this->stringmanager->locations_to_search('mymodreport_test'), array( + $this->basedir . 'moodle/lang/' => 'mymodreport_test/', + $this->basedir . 'moodledata/lang/' => 'mymodreport_test/', + $this->basedir . 'moodle/mod/mymod/report/test/lang/' => 'test/', + )); + } + + public function test_register_plugin_type_session_usage() { + $this->stringmanager->register_plugin_type('mymodreport', 'mod/mymod/report'); + + // Create a new string_manager to see if it picks up the 'mymodreport' + // custom plugin type from session without us having to re-register it. + // This is required to make help files work. + $newstringmanager = new testable_string_manager($this->basedir . 'moodle', + $this->basedir . 'moodledata', 'adminpath', false); + $this->assertEqual($newstringmanager->locations_to_search('mymodreport_test'), array( + $this->basedir . 'moodle/lang/' => 'mymodreport_test/', + $this->basedir . 'moodledata/lang/' => 'mymodreport_test/', + $this->basedir . 'moodle/mod/mymod/report/test/lang/' => 'test/', + )); + } + public function test_parse_module_name_module() { $this->assertEqual($this->stringmanager->parse_module_name('forum'), array('', 'forum'));