]> git.mjollnir.org Git - moodle.git/commitdiff
filters: MDL-17684 Look for the filter name in filter_myfilter.php first.
authortjhunt <tjhunt>
Mon, 30 Mar 2009 08:33:13 +0000 (08:33 +0000)
committertjhunt <tjhunt>
Mon, 30 Mar 2009 08:33:13 +0000 (08:33 +0000)
This makes filters more plugginable, becuase with this lang file name, get_string will look for the filter name in filter/myfilter/lang/en_utf8/filter_myfilter.php.

To do this, there is a new function filter_get_name in filterlib that contains the logic.

Also, a new function filter_get_all_installed to replace the logic for getting all filters that was duplicated in three places.

filter_get_name no longer does such a nice fall-back if the name is missing, to encourage people to supply the right string. The fallback now looks like '[[filtername]] (filter/tidy)'.

admin/settings/plugins.php
lang/en_utf8/filter_tidy.php [new file with mode: 0644]
lib/adminlib.php
lib/filterlib.php
lib/moodlelib.php

index 8391a61892be1c02b3ac3a86d6d8e289c8270026..ed32575d1464b4955df7ca81b45fbcd4945e2f73 100644 (file)
@@ -119,35 +119,19 @@ if ($hassiteconfig || has_capability('moodle/question:config', $systemcontext))
     } else {
         $activefilters = explode(',', $CFG->textfilters);
     }
-    $filterlocations = array('mod','filter');
-    foreach ($filterlocations as $filterlocation) {
-        $filters = get_list_of_plugins($filterlocation);
-
-        $filterbyname = array();
-
-        foreach ($filters as $filter) {
-            $strfiltername = get_string('filtername', $filter);
-            // Deal with filters which are lacking the language string
-            if ($strfiltername == '[[filtername]]') {
-                $textlib = textlib_get_instance();
-                $strfiltername = $textlib->strtotitle($filter);
-            }
-            $filterbyname[$strfiltername] = "$filterlocation/$filter";
-        }
-        ksort($filterbyname);
-
-        foreach ($filterbyname as $strfiltername=>$filterfull) {
-            if (file_exists("$CFG->dirroot/$filterfull/filtersettings.php")) {
-                $settings = new admin_settingpage('filtersetting'.str_replace('/', '', $filterfull), $strfiltername, 'moodle/site:config', !in_array($filterfull, $activefilters));
-                if ($ADMIN->fulltree) {
-                    include("$CFG->dirroot/$filterfull/filtersettings.php");
-                }
-                $ADMIN->add('filtersettings', $settings);
+    $filternames = filter_get_all_installed();
+    foreach ($filternames as $filterpath => $strfiltername) {
+        if (file_exists("$CFG->dirroot/$filterpath/filtersettings.php")) {
+            $settings = new admin_settingpage('filtersetting'.str_replace('/', '', $filterpath),
+                    $strfiltername, 'moodle/site:config', !in_array($filterpath, $activefilters));
+            if ($ADMIN->fulltree) {
+                include("$CFG->dirroot/$filterpath/filtersettings.php");
             }
+            $ADMIN->add('filtersettings', $settings);
         }
     }
 
-    $catname =get_string('portfolios', 'portfolio');
+    $catname = get_string('portfolios', 'portfolio');
     $manage = get_string('manageportfolios', 'portfolio');
     $url = "$CFG->wwwroot/$CFG->admin/portfolio.php";
 
diff --git a/lang/en_utf8/filter_tidy.php b/lang/en_utf8/filter_tidy.php
new file mode 100644 (file)
index 0000000..cbbdcf3
--- /dev/null
@@ -0,0 +1,4 @@
+<?php // $Id$
+// Language string for filter/myfilter.
+$string['filtername'] = 'HTML tidy';
index f259ed8f808bc2ff4e563cccdf52255fe27e2367..8bbd04a72e5bc2ef45faa03c897aceeb76f307b9 100644 (file)
@@ -3720,18 +3720,15 @@ class admin_setting_managefilters extends admin_setting {
             return true;
         }
 
+        $filternames = filter_get_all_installed();
         $textlib = textlib_get_instance();
-        $filterlocations = array('mod','filter');
-        foreach ($filterlocations as $filterlocation) {
-            $plugins = get_list_of_plugins($filterlocation);
-            foreach ($plugins as $plugin) {
-                if (strpos($plugin, $query) !== false) {
-                    return true;
-                }
-                $name = get_string('filtername', $plugin);
-                if (strpos($textlib->strtolower($name), $query) !== false) {
-                    return true;
-                }
+        foreach ($filternames as $path => $strfiltername) {
+            if (strpos($textlib->strtolower($strfiltername), $query) !== false) {
+                return true;
+            }
+            list($type, $filter) = explode('/', $path);
+            if (strpos($filter, $query) !== false) {
+                return true;
             }
         }
         return false;
@@ -3752,25 +3749,12 @@ class admin_setting_managefilters extends admin_setting {
         // get a list of possible filters (and translate name if possible)
         // note filters can be in the dedicated filters area OR in their
         // associated modules
-        $installedfilters = array();
+        $installedfilters = filter_get_all_installed();
         $filtersettings_new = array();
-        $filterlocations = array('mod','filter');
-        foreach ($filterlocations as $filterlocation) {
-            $plugins = get_list_of_plugins($filterlocation);
-            foreach ($plugins as $plugin) {
-                $pluginpath = "$CFG->dirroot/$filterlocation/$plugin/filter.php";
-                $settingspath_new = "$CFG->dirroot/$filterlocation/$plugin/filtersettings.php";
-                if (is_readable($pluginpath)) {
-                    $name = trim(get_string("filtername", $plugin));
-                    if (empty($name) or ($name == '[[filtername]]')) {
-                        $textlib = textlib_get_instance();
-                        $name = $textlib->strtotitle($plugin);
-                    }
-                    $installedfilters["$filterlocation/$plugin"] = $name;
-                    if (is_readable($settingspath_new)) {
-                        $filtersettings_new[] = "$filterlocation/$plugin";
-                    }
-                }
+        foreach ($installedfilters as $path => $strfiltername) {
+            $settingspath_new = $CFG->dirroot . '/' . $path . '/filtersettings.php';
+            if (is_readable($settingspath_new)) {
+                $filtersettings_new[] = $path;
             }
         }
 
@@ -3790,8 +3774,8 @@ class admin_setting_managefilters extends admin_setting {
             }
         }
 
-        // construct the display array with installed filters
-        // at the top in the right order
+        // Get the list of all filters, and pull the active filters
+        // to the top.
         $displayfilters = array();
         foreach ($activefilters as $activefilter) {
             $name = $installedfilters[$activefilter];
index db32633f1809e190a5615f5de43b37d7a9a6558a..74f203fed89ef69a4e90aa7d7891be49537784bd 100644 (file)
@@ -56,7 +56,7 @@ abstract class filter_base {
 /// Define one exclusive separator that we'll use in the temp saved tags
 /// keys. It must be something rare enough to avoid having matches with
 /// filterobjects. MDL-18165
-define ('EXCL_SEPARATOR', '-%-');
+define('EXCL_SEPARATOR', '-%-');
 
 /**
  * This is just a little object to define a phrase and some instructions 
@@ -96,6 +96,62 @@ class filterobject {
     }
 }
 
+/**
+ * Look up the name of this filter in the most appropriate location.
+ * If $filterlocation = 'mod' then does get_string('filtername', $filter);
+ * else if $filterlocation = 'filter' then does get_string('filtername', 'filter_' . $filter);
+ * with a fallback to get_string('filtername', $filter) for backwards compatibility.
+ * These are the only two options supported at the moment.
+ * @param string $filterlocation 'filter' or 'mod'.
+ * @param string $filter the folder name where the filter lives.
+ * @return string the human-readable name for this filter.
+ */
+function filter_get_name($filterlocation, $filter) {
+    switch ($filterlocation) {
+        case 'filter':
+            $strfiltername = get_string('filtername', 'filter_' . $filter);
+            if (substr($strfiltername, 0, 2) != '[[') {
+                // found a valid string.
+                return $strfiltername;
+            }
+            // Fall through to try the legacy location.
+
+        case 'mod':
+            $strfiltername = get_string('filtername', $filter);
+            if (substr($strfiltername, 0, 2) == '[[') {
+                $strfiltername .= ' (' . $filterlocation . '/' . $filter . ')';
+            }
+            return $strfiltername;
+
+        default:
+            throw new coding_exception('Unknown filter location ' . $filterlocation);
+    }
+}
+
+/**
+ * Get the names of all the filters installed in this Moodle.
+ * @return array path => filter name from the appropriate lang file. e.g.
+ * array('mod/glossary' => 'Glossary Auto-linking', 'filter/tex' => 'TeX Notation');
+ * sorted in alphabetical order of name.
+ */
+function filter_get_all_installed() {
+    global $CFG;
+    $filternames = array();
+    $filterlocations = array('mod', 'filter');
+    foreach ($filterlocations as $filterlocation) {
+        $filters = get_list_of_plugins($filterlocation);
+        foreach ($filters as $filter) {
+            $path = $filterlocation . '/' . $filter;
+            if (is_readable($CFG->dirroot . '/' . $path . '/filter.php')) {
+                $strfiltername = filter_get_name($filterlocation, $filter);
+                $filternames[$path] = $strfiltername;
+            }
+        }
+    }
+    asort($filternames, SORT_LOCALE_STRING);
+    return $filternames;
+}
+
 /**
  * Process phrases intelligently found within a HTML text (such as adding links)
  *
@@ -104,7 +160,7 @@ class filterobject {
  * param  ignoretagsopen   an array of opening tags that we should ignore while filtering
  * param  ignoretagsclose  an array of corresponding closing tags
  **/
-function filter_phrases ($text, &$link_array, $ignoretagsopen=NULL, $ignoretagsclose=NULL) {
+function filter_phrases($text, &$link_array, $ignoretagsopen=NULL, $ignoretagsclose=NULL) {
 
     global $CFG;
 
@@ -296,11 +352,8 @@ function filter_phrases ($text, &$link_array, $ignoretagsopen=NULL, $ignoretagsc
 
 
     return $text;
-
 }
 
-
-
 function filter_remove_duplicates($linkarray) {
 
     $concepts  = array(); // keep a record of concepts as we cycle through
index 5839210af9562bfac6ce095bd9d0761272490901..08692780368ce22e6a7c5d82b0f1f247b26ce341 100644 (file)
@@ -8342,11 +8342,7 @@ function get_plugin_name($plugin, $type='mod') {
             }
             break;
         case 'filter':
-            $plugin_name = trim(get_string('filtername', $plugin));
-            if (empty($plugin_name) or ($plugin_name == '[[filtername]]')) {
-                $textlib = textlib_get_instance();
-                $plugin_name = $textlib->strtotitle($plugin);
-            }
+            $plugin_name = filter_get_name('filter', $plugin);
             break;
         default:
             $plugin_name = $plugin;