]> git.mjollnir.org Git - moodle.git/commitdiff
get_string: MDL-13816 allow third-partly modules to register sub-plugin types that...
authortjhunt <tjhunt>
Mon, 30 Mar 2009 06:13:35 +0000 (06:13 +0000)
committertjhunt <tjhunt>
Mon, 30 Mar 2009 06:13:35 +0000 (06:13 +0000)
I suppose to be really useful we should deal with database, capabiltities, etc. too in due course. Still, this is a start.

lib/moodlelib.php
lib/simpletest/teststringmanager.php

index 1dd58ef9fb7c6498c9ddaa8fe96e63f305a2a2b4..5839210af9562bfac6ce095bd9d0761272490901 100644 (file)
@@ -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);
index b091e7fc879c2ea67f36a3345b3eec1227f37166..880f2a42cab2f8593a600cd1cb34930fea9b1739 100644 (file)
@@ -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'));