From: tjhunt Date: Mon, 13 Apr 2009 07:04:07 +0000 (+0000) Subject: filters: MDL-7336 Finish admin settings page. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=5b8fa09b00bb01898ed2dc4cdbc1c1c80a7b03b1;p=moodle.git filters: MDL-7336 Finish admin settings page. --- diff --git a/admin/filters.php b/admin/filters.php index 71e324e11c..4d5c8073ea 100644 --- a/admin/filters.php +++ b/admin/filters.php @@ -64,10 +64,18 @@ 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; diff --git a/admin/settings/plugins.php b/admin/settings/plugins.php index ed32575d14..9a82a275c0 100644 --- a/admin/settings/plugins.php +++ b/admin/settings/plugins.php @@ -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"); } diff --git a/lang/en_utf8/filters.php b/lang/en_utf8/filters.php index 7758886f2b..06fd8ceab8 100644 --- a/lang/en_utf8/filters.php +++ b/lang/en_utf8/filters.php @@ -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?'; diff --git a/lib/adminlib.php b/lib/adminlib.php index 524c727bf8..d3d90e891b 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -3747,16 +3747,19 @@ class admin_setting_managefilters extends admin_setting { '' . $straction . ' '; } - 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') . '&newstate=', $this->activechoices, 'active' . basename($filter), $filterinfo->active, '', '', '', true, 'self', '', NULL, get_string('save')); + // Re-order $updown = ''; $spacer = ' '; 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') . '&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 = '' . get_string('tablenosave', 'filters') . '

'; + $return .= '

' . get_string('filterallwarning', 'filters') . '

'; $return .= print_box_end(true); return highlight($query, $return); } diff --git a/lib/db/install.php b/lib/db/install.php index d703cadafa..691bf0f9a7 100644 --- a/lib/db/install.php +++ b/lib/db/install.php @@ -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); } diff --git a/lib/filelib.php b/lib/filelib.php index 4050407a3f..0d83c78645 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -1529,12 +1529,10 @@ function file_modify_html_header($text) { $stylesheetshtml .= ''."\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('/\|\/', $text, $matches); diff --git a/lib/filterlib.php b/lib/filterlib.php index 129bfec20b..45aad2fd5e 100644 --- a/lib/filterlib.php +++ b/lib/filterlib.php @@ -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'. diff --git a/lib/simpletest/testfilterconfig.php b/lib/simpletest/testfilterconfig.php index e1ee4a9bc4..e895a45c15 100644 --- a/lib/simpletest/testfilterconfig.php +++ b/lib/simpletest/testfilterconfig.php @@ -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); + } +} ?>