]> git.mjollnir.org Git - moodle.git/commitdiff
filters: MDL-7336 Finish admin settings page.
authortjhunt <tjhunt>
Mon, 13 Apr 2009 07:04:07 +0000 (07:04 +0000)
committertjhunt <tjhunt>
Mon, 13 Apr 2009 07:04:07 +0000 (07:04 +0000)
admin/filters.php
admin/settings/plugins.php
lang/en_utf8/filters.php
lib/adminlib.php
lib/db/install.php
lib/filelib.php
lib/filterlib.php
lib/simpletest/testfilterconfig.php

index 71e324e11cf09ea24e3bbe25e730ebe55a2e7dfd..4d5c8073ea24bd262e7c12f2f77d640818ceb3d9 100644 (file)
     case 'setstate':
         if ($newstate = optional_param('newstate', '', PARAM_INTEGER)) {
             filter_set_global_state($filterpath, $newstate);
+            if ($newstate == TEXTFILTER_DISABLED) {
+                filter_set_applies_to_strings($filterpath, false);
+            }
             unset($newfilters[$filterpath]);
         }
         break;
 
+    case 'setapplyto':
+        $applytostrings = optional_param('stringstoo', false, PARAM_BOOL);
+        filter_set_applies_to_strings($filterpath, $applytostrings);
+        break;
+
     case 'down':
         if (isset($filters[$filterpath])) {
             $oldpos = $filters[$filterpath]->sortorder;
index ed32575d1464b4955df7ca81b45fbcd4945e2f73..9a82a275c01be9a60eaec5faad6ed91d47b8494d 100644 (file)
@@ -77,36 +77,37 @@ if ($hassiteconfig || has_capability('moodle/question:config', $systemcontext))
     // "filtersettings" settingpage
     $temp = new admin_settingpage('managefilters', get_string('filtersettings', 'admin'));
     if ($ADMIN->fulltree) {
+        $cachetimes = array(
+            604800 => get_string('numdays','',7),
+            86400 => get_string('numdays','',1),
+            43200 => get_string('numhours','',12),
+            10800 => get_string('numhours','',3),
+            7200 => get_string('numhours','',2),
+            3600 => get_string('numhours','',1),
+            2700 => get_string('numminutes','',45),
+            1800 => get_string('numminutes','',30),
+            900 => get_string('numminutes','',15),
+            600 => get_string('numminutes','',10),
+            540 => get_string('numminutes','',9),
+            480 => get_string('numminutes','',8),
+            420 => get_string('numminutes','',7),
+            360 => get_string('numminutes','',6),
+            300 => get_string('numminutes','',5),
+            240 => get_string('numminutes','',4),
+            180 => get_string('numminutes','',3),
+            120 => get_string('numminutes','',2),
+            60 => get_string('numminutes','',1),
+            30 => get_string('numseconds','',30),
+            0 => get_string('no')
+        );
         $items = array();
         $items[] = new admin_setting_managefilters();
         $items[] = new admin_setting_heading('managefilterscommonheading', get_string('commonsettings', 'admin'), '');
-        $items[] = new admin_setting_configselect('cachetext', get_string('cachetext', 'admin'), get_string('configcachetext', 'admin'), 60, array(604800 => get_string('numdays','',7),
-                                                                                                                                               86400 => get_string('numdays','',1),
-                                                                                                                                               43200 => get_string('numhours','',12),
-                                                                                                                                               10800 => get_string('numhours','',3),
-                                                                                                                                               7200 => get_string('numhours','',2),
-                                                                                                                                               3600 => get_string('numhours','',1),
-                                                                                                                                               2700 => get_string('numminutes','',45),
-                                                                                                                                               1800 => get_string('numminutes','',30),
-                                                                                                                                               900 => get_string('numminutes','',15),
-                                                                                                                                               600 => get_string('numminutes','',10),
-                                                                                                                                               540 => get_string('numminutes','',9),
-                                                                                                                                               480 => get_string('numminutes','',8),
-                                                                                                                                               420 => get_string('numminutes','',7),
-                                                                                                                                               360 => get_string('numminutes','',6),
-                                                                                                                                               300 => get_string('numminutes','',5),
-                                                                                                                                               240 => get_string('numminutes','',4),
-                                                                                                                                               180 => get_string('numminutes','',3),
-                                                                                                                                               120 => get_string('numminutes','',2),
-                                                                                                                                               60 => get_string('numminutes','',1),
-                                                                                                                                               30 => get_string('numseconds','',30),
-                                                                                                                                               0 => get_string('no')));
-        $items[] = new admin_setting_configselect('filteruploadedfiles', get_string('filteruploadedfiles', 'admin'), get_string('configfilteruploadedfiles', 'admin'), 0, array('0' => get_string('none'),
-                                                                                                                                                                                '1' => get_string('allfiles'),
-                                                                                                                                                                                '2' => get_string('htmlfilesonly')));
+        $items[] = new admin_setting_configselect('cachetext', get_string('cachetext', 'admin'), get_string('configcachetext', 'admin'), 60, $cachetimes);
+        $items[] = new admin_setting_configselect('filteruploadedfiles', get_string('filteruploadedfiles', 'admin'), get_string('configfilteruploadedfiles', 'admin'), 0,
+                array('0' => get_string('none'), '1' => get_string('allfiles'), '2' => get_string('htmlfilesonly')));
         $items[] = new admin_setting_configcheckbox('filtermatchoneperpage', get_string('filtermatchoneperpage', 'admin'), get_string('configfiltermatchoneperpage', 'admin'), 0);
         $items[] = new admin_setting_configcheckbox('filtermatchonepertext', get_string('filtermatchonepertext', 'admin'), get_string('configfiltermatchonepertext', 'admin'), 0);
-        $items[] = new admin_setting_configcheckbox('filterall', get_string('filterall', 'admin'), get_string('configfilterall', 'admin'), 0);
         foreach ($items as $item) {
             $item->set_updatedcallback('reset_text_filters_cache');
             $temp->add($item);
@@ -114,16 +115,12 @@ if ($hassiteconfig || has_capability('moodle/question:config', $systemcontext))
     }
     $ADMIN->add('filtersettings', $temp);
 
-    if (empty($CFG->textfilters)) {
-        $activefilters = array();
-    } else {
-        $activefilters = explode(',', $CFG->textfilters);
-    }
+    $activefilters = filter_get_globally_enabled();
     $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));
+                    $strfiltername, 'moodle/site:config', !isset($activefilters[$filterpath]));
             if ($ADMIN->fulltree) {
                 include("$CFG->dirroot/$filterpath/filtersettings.php");
             }
index 7758886f2b177924058f32cf3a727af5a921ca1e..06fd8ceab85f6b446c73ed3011f328e6f88da39a 100644 (file)
@@ -21,6 +21,7 @@ $string['datelabelisbetween'] = '$a->label is between $a->after and $a->before';
 $string['disabled'] = 'Disabled';
 $string['doesnotcontain'] = 'doesn\'t contain';
 $string['endswith'] = 'ends with';
+$string['filterallwarning'] = 'Applying filters to headings as well as content can greatly increase the load on your server. Please use that \'Apply to\' settings sparingly. The main use is with the multilang filter.';
 $string['firstaccess'] = 'First access';
 $string['globalrolelabel'] = '$a->label is $a->value';
 $string['isactive'] = 'Active?';
index 524c727bf8ead2bf9a202ff20acf9bb3aede0dc8..d3d90e891bdc681bd15e954b23bcc1c0ae933dbc 100644 (file)
@@ -3747,16 +3747,19 @@ class admin_setting_managefilters extends admin_setting {
                 '<img src="' . $CFG->pixpath . '/t/' . $icon . '.gif" alt="' . $straction . '" /></a> ';
     }
 
-    protected function get_table_row($filterinfo, $isfirstrow, $islastactive) {
+    protected function get_table_row($filterinfo, $isfirstrow, $islastactive, $applytostrings) {
         global $CFG;
         $row = array();
         $filter = $filterinfo->filter;
 
+        // Filter name
         $row[] = $this->filternames[$filter];
 
+        // Disable/off/on
         $row[] = popup_form($this->action_url($filter, 'setstate') . '&amp;newstate=', $this->activechoices,
                 'active' . basename($filter), $filterinfo->active, '', '', '', true, 'self', '', NULL, get_string('save'));
 
+        // Re-order
         $updown = '';
         $spacer = '<img src="' . $CFG->pixpath . '/spacer.gif" class="iconsmall" alt="" /> ';
         if ($filterinfo->active != TEXTFILTER_DISABLED) {
@@ -3773,9 +3776,12 @@ class admin_setting_managefilters extends admin_setting {
         }
         $row[] = $updown;
 
-        $row[] = 'TODO Apply to col';
+        // Apply to strings.
+        $row[] = popup_form($this->action_url($filter, 'setapplyto') . '&amp;stringstoo=', $this->applytochoices,
+                'applyto' . basename($filter), $applytostrings, '', '', '', true, 'self', '', NULL, get_string('save'),
+                $filterinfo->active == TEXTFILTER_DISABLED);
 
-        // settings link (if defined)
+        // Settings link, if required
         $settings = '';
         if (filter_has_global_settings($filter)) {
             $settings = '<a href="' . $CFG->wwwroot . '/' . $CFG->admin . '/settings.php?section=filtersetting' .
@@ -3795,8 +3801,8 @@ class admin_setting_managefilters extends admin_setting {
             TEXTFILTER_ON => get_string('on', 'filters'),
         );
         $this->applytochoices = array(
-            1 => get_string('content', 'filters'),
-            3 => get_string('contentandheadings', 'filters'),
+            0 => get_string('content', 'filters'),
+            1 => get_string('contentandheadings', 'filters'),
         );
         $this->strup = get_string('up');
         $this->strdown = get_string('down');
@@ -3810,6 +3816,7 @@ class admin_setting_managefilters extends admin_setting {
         foreach ($filters as $filter => $notused) {
             unset($newfilters[$filter]);
         }
+        $stringfilters = filter_get_string_filters();
 
         $return = print_heading(get_string('actfilterhdr', 'filters'), '', 3, 'main', true);
         $return .= print_box_start('generalbox filtersui', '', true);
@@ -3818,7 +3825,7 @@ class admin_setting_managefilters extends admin_setting {
         $table->head  = array(get_string('filter'), get_string('isactive', 'filters'),
                 get_string('order'), get_string('applyto', 'filters'), $this->strsettings);
         $table->align = array('left', 'left', 'center', 'left', 'left');
-        $table->width = '90%';
+        $table->width = '100%';
         $table->data  = array();
 
         $lastactive = null;
@@ -3831,7 +3838,8 @@ class admin_setting_managefilters extends admin_setting {
         // iterate through filters adding to display table
         $firstrow = true;
         foreach ($filters as $filter => $filterinfo) {
-            $row = $this->get_table_row($filterinfo, $firstrow, $filter == $lastactive);
+            $applytostrings = isset($stringfilters[$filter]) && $filterinfo->active != TEXTFILTER_DISABLED;
+            $row = $this->get_table_row($filterinfo, $firstrow, $filter == $lastactive, $applytostrings);
             $table->data[] = $row;
             if ($filterinfo->active == TEXTFILTER_DISABLED) {
                 $table->rowclass[] = 'dimmed_text';
@@ -3844,13 +3852,14 @@ class admin_setting_managefilters extends admin_setting {
             $filterinfo = new stdClass;
             $filterinfo->filter = $filter;
             $filterinfo->active = TEXTFILTER_DISABLED;
-            $row = $this->get_table_row($filterinfo, $firstrow, $filter == $lastactive);
+            $row = $this->get_table_row($filterinfo, false, false, false);
             $table->data[] = $row;
             $table->rowclass[] = 'dimmed_text';
         }
 
         $return .= print_table($table, true);
-        $return .= get_string('tablenosave', 'filters');
+        $return .= '<p class="filtersettingnote">' . get_string('tablenosave', 'filters') . '</p>';
+        $return .= '<p class="filtersettingnote">' . get_string('filterallwarning', 'filters') . '</p>';
         $return .= print_box_end(true);
         return highlight($query, $return);
     }
index d703cadafa2c6ee7e28752f265c68505e95a0ba4..691bf0f9a73fe11e61c9be0753c0d8f97c1bf48c 100644 (file)
@@ -41,24 +41,25 @@ function xmldb_main_install() {
 /// create default course category
     $cat = get_course_category();
 
-
-    $defaults = array('rolesactive'           => '0',         // marks fully set up system
-                      'auth'                  => 'email',
-                      'auth_pop3mailbox'      => 'INBOX',
-                      'enrol'                 => 'manual',
-                      'enrol_plugins_enabled' => 'manual',
-                      'style'                 => 'default',
-                      'template'              => 'default',
-                      'theme'                 => 'standardwhite',
-                      'filter_multilang_converted' => 1,
-                      'siteidentifier'        => random_string(32).$_SERVER['HTTP_HOST'],
-                      'backup_version'        => 2008111700,
-                      'backup_release'        => '2.0 dev',
-                      'blocks_version'        => 2007081300, // might be removed soon
-                      'mnet_dispatcher_mode'  => 'off',
-                      'sessiontimeout'        => 7200,       // must be present during roles installation
-
-                     );
+    $defaults = array(
+        'rolesactive'           => '0', // marks fully set up system
+        'auth'                  => 'email',
+        'auth_pop3mailbox'      => 'INBOX',
+        'enrol'                 => 'manual',
+        'enrol_plugins_enabled' => 'manual',
+        'style'                 => 'default',
+        'template'              => 'default',
+        'theme'                 => 'standardwhite',
+        'filter_multilang_converted' => 1,
+        'siteidentifier'        => random_string(32).$_SERVER['HTTP_HOST'],
+        'backup_version'        => 2008111700,
+        'backup_release'        => '2.0 dev',
+        'blocks_version'        => 2007081300, // might be removed soon
+        'mnet_dispatcher_mode'  => 'off',
+        'sessiontimeout'        => 7200, // must be present during roles installation
+        'stringfilters'         => '', // These two are managed in a strange way by the filters
+        'filterall'             => 0, // setting page, so have to be initialised here.
+    );
     foreach($defaults as $key => $value) {
         set_config($key, $value);
     }
index 4050407a3f6c1f43c8f8b32fffd026013d3df536..0d83c786455b7b61e411b8e700fbb243ec79efbd 100644 (file)
@@ -1529,12 +1529,10 @@ function file_modify_html_header($text) {
         $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n";
     }
 
-    $filters = explode(",", $CFG->textfilters);
-    if (in_array('filter/mediaplugin', $filters)) {
+    $ufo = '';
+    if (filter_is_enabled('filter/mediaplugin')) {
         // this script is needed by most media filter plugins.
         $ufo = get_require_js_code(array($CFG->wwwroot . '/lib/ufo.js'));
-    } else {
-        $ufo = '';
     }
 
     preg_match('/\<head\>|\<HEAD\>/', $text, $matches);
index 129bfec20b4203c4dd5ac8b7b78dba5988bee9fe..45aad2fd5edcbe6afa21d6fd55935b06ad9f47c2 100644 (file)
@@ -32,10 +32,7 @@ class filter_manager {
     protected static $singletoninstance;
 
     protected function __construct() {
-        global $CFG;
-        if (!empty($CFG->filterall) && !empty($CFG->stringfilters)) {
-            $stringfilternames = explode(',', $CFG->stringfilters);
-        }
+        $stringfilternames = filter_get_string_filters();
     }
 
     /**
@@ -441,6 +438,69 @@ function filter_set_global_state($filter, $state, $sortorder = false) {
     }
 }
 
+/**
+ * @param string $filter The filter name, for example 'filter/tex' or 'mod/glossary'.
+ * @return boolean is this filter allowed to be used on this site. That is, the
+ *      admin has set the global 'active' setting to On, or Off, but available.
+ */
+function filter_is_enabled($filter) {
+    return array_key_exists($filter, filter_get_globally_enabled());
+}
+
+/**
+ * Return a list of all the filters that may be in use somewhere.
+ * @return array where the keys and values are both the filter name, like 'filter/tex'.
+ */
+function filter_get_globally_enabled() {
+    static $enabledfilters = null;
+    if (is_null($enabledfilters)) {
+        $filters = filter_get_global_states();
+        $enabledfilters = array();
+        foreach ($filters as $filter => $filerinfo) {
+            if ($filerinfo->active != TEXTFILTER_DISABLED) {
+                $enabledfilters[$filter] = $filter;
+            }
+        }
+    }
+    return $enabledfilters;
+}
+
+/**
+ * Return the names of the filters that should also be applied to strings
+ * (when they are enabled).
+ * @return array where the keys and values are both the filter name, like 'filter/tex'.
+ */
+function filter_get_string_filters() {
+    global $CFG;
+    $stringfilters = array();
+    if (!empty($CFG->filterall) && !empty($CFG->stringfilters)) {
+        $stringfilters = explode(',', $CFG->stringfilters);
+        $stringfilters = array_combine($stringfilters, $stringfilters);
+    }
+    return $stringfilters;
+}
+
+/**
+ * Sets whether a particular active filter should be applied to all strings by
+ * format_string, or just used by format_text.
+ * @param string $filter The filter name, for example 'filter/tex' or 'mod/glossary'.
+ * @param boolean $applytostrings if true, this filter will apply to format_string
+ *      and format_text, when it is enabled.
+ */
+function filter_set_applies_to_strings($filter, $applytostrings) {
+    $stringfilters = filter_get_string_filters();
+    $numstringfilters = count($stringfilters);
+    if ($applytostrings) {
+        $stringfilters[$filter] = $filter;
+    } else {
+        unset($stringfilters[$filter]);
+    }
+    if (count($stringfilters) != $numstringfilters) {
+        set_config('stringfilters', implode(',', $stringfilters));
+        set_config('filterall', !empty($stringfilters));
+    }
+}
+
 /**
  * Set the local activated state for a text filter.
  * @param string $filter The filter name, for example 'filter/tex' or 'mod/glossary'.
index e1ee4a9bc4640d9879e479097f033054c44a733e..e895a45c155a9070b7fbc60b3f36e9fa771c87f8 100644 (file)
@@ -44,6 +44,8 @@ class filter_active_global_test extends UnitTestCaseUsingDatabase {
     private $syscontextid;
 
     public function setUp() {
+        parent::setUp();
+
         // Make sure accesslib has cached a sensible system context object
         // before we switch to the test DB.
         $this->syscontextid = get_context_instance(CONTEXT_SYSTEM)->id;
@@ -239,6 +241,8 @@ class filter_active_global_test extends UnitTestCaseUsingDatabase {
  */
 class filter_active_local_test extends UnitTestCaseUsingDatabase {
     public function setUp() {
+        parent::setUp();
+
         // Create the table we need and switch to test DB.
         $this->create_test_table('filter_active', 'lib');
         $this->switch_to_test_db();
@@ -311,6 +315,8 @@ class filter_active_local_test extends UnitTestCaseUsingDatabase {
  */
 class filter_config_test extends UnitTestCaseUsingDatabase {
     public function setUp() {
+        parent::setUp();
+
         // Create the table we need and switch to test DB.
         $this->create_test_table('filter_config', 'lib');
         $this->switch_to_test_db();
@@ -363,6 +369,8 @@ class filter_get_active_in_context_test extends UnitTestCaseUsingDatabase {
     private $childcontext2;
 
     public function setUp() {
+        parent::setUp();
+
         // Make sure accesslib has cached a sensible system context object
         // before we switch to the test DB.
         $this->syscontext = get_context_instance(CONTEXT_SYSTEM);
@@ -493,17 +501,16 @@ class filter_get_active_in_context_test extends UnitTestCaseUsingDatabase {
 }
 
 class filter_delete_all_data_test extends UnitTestCaseUsingDatabase {
-    private $syscontext;
-    private $childcontext;
-    private $childcontext2;
-
     public function setUp() {
+        parent::setUp();
+
         // Create the table we need and switch to test DB.
         $this->create_test_tables(array('filter_active', 'filter_config', 'config', 'config_plugins'), 'lib');
         $this->switch_to_test_db();
     }
 
     public function test_filter_delete_all_data_filter() {
+        // Setup fixture.
         $syscontext = get_context_instance(CONTEXT_SYSTEM);
         filter_set_global_state('filter/name', TEXTFILTER_ON);
         filter_set_global_state('filter/other', TEXTFILTER_ON);
@@ -511,7 +518,9 @@ class filter_delete_all_data_test extends UnitTestCaseUsingDatabase {
         filter_set_local_config('filter/other', $syscontext->id, 'settingname', 'Other value');
         set_config('configname', 'A config value', 'filter_name');
         set_config('configname', 'Other config value', 'filter_other');
+        // Exercise SUT.
         filter_delete_all_data('filter/name');
+        // Validate.
         $this->assertEqual(1, $this->testdb->count_records('filter_active'));
         $this->assertTrue($this->testdb->record_exists('filter_active', array('filter' => 'filter/other')));
         $this->assertEqual(1, $this->testdb->count_records('filter_config'));
@@ -522,4 +531,65 @@ class filter_delete_all_data_test extends UnitTestCaseUsingDatabase {
         $this->assertFalse(get_config('filter_name'));
     }
 }
+
+class filter_filter_set_applies_to_strings extends UnitTestCaseUsingDatabase {
+    protected $origcfgstringfilters;
+    protected $origcfgfilterall;
+
+    public function setUp() {
+        global $CFG;
+        parent::setUp();
+
+        // Create the table we need and switch to test DB.
+        $this->create_test_table('config', 'lib');
+        $this->switch_to_test_db();
+
+        // Store original $CFG;
+        $this->origcfgstringfilters = $CFG->stringfilters;
+        $this->origcfgfilterall = $CFG->filterall;
+    }
+
+    public function tearDown() {
+        $CFG->stringfilters = $this->origcfgstringfilters;
+        $CFG->filterall = $this->origcfgfilterall;
+
+        parent::tearDown();
+    }
+
+    public function test_set() {
+        global $CFG;
+        // Setup fixture.
+        $CFG->filterall = 0;
+        $CFG->stringfilters = '';
+        // Exercise SUT.
+        filter_set_applies_to_strings('filter/name', true);
+        // Validate.
+        $this->assertEqual('filter/name', $CFG->stringfilters);
+        $this->assertTrue($CFG->filterall);
+    }
+
+    public function test_unset_to_empty() {
+        global $CFG;
+        // Setup fixture.
+        $CFG->filterall = 1;
+        $CFG->stringfilters = 'filter/name';
+        // Exercise SUT.
+        filter_set_applies_to_strings('filter/name', false);
+        // Validate.
+        $this->assertEqual('', $CFG->stringfilters);
+        $this->assertFalse($CFG->filterall);
+    }
+
+    public function test_unset_multi() {
+        global $CFG;
+        // Setup fixture.
+        $CFG->filterall = 1;
+        $CFG->stringfilters = 'filter/name,filter/other';
+        // Exercise SUT.
+        filter_set_applies_to_strings('filter/name', false);
+        // Validate.
+        $this->assertEqual('filter/other', $CFG->stringfilters);
+        $this->assertTrue($CFG->filterall);
+    }
+}
 ?>