From 604eb7be034eafe3d14153638864eae6b5f79b7a Mon Sep 17 00:00:00 2001 From: tjhunt Date: Mon, 13 Apr 2009 07:14:27 +0000 Subject: [PATCH] filters: MDL-7336 code to let people edit local filter config --- filter/local_settings_form.php | 91 ++++++++++++++++++++++++++++++++++ filter/manage.php | 41 ++++++++++++--- lang/en_utf8/error.php | 1 + lang/en_utf8/filters.php | 3 +- lib/filterlib.php | 17 +++++-- 5 files changed, 143 insertions(+), 10 deletions(-) create mode 100644 filter/local_settings_form.php diff --git a/filter/local_settings_form.php b/filter/local_settings_form.php new file mode 100644 index 0000000000..08a199b96c --- /dev/null +++ b/filter/local_settings_form.php @@ -0,0 +1,91 @@ +libdir . '/formslib.php'); + +abstract class filter_local_settings_form extends moodleform { + protected $filter; + protected $context; + + public function __construct($submiturl, $filter, $context) { + $this->filter = $filter; + $this->context = $context; + parent::moodleform($submiturl); + } + + /** + * Build the form definition. Rather than overriding this method, you + * should probably override definition_inner instead. + * + * This method adds the necessary hidden fields and submit buttons, + * and calls definition_inner to insert the custom controls in the appropriate place. + */ + public function definition() { + $mform =& $this->_form; + + $this->definition_inner($mform); + + $mform->addElement('hidden', 'contextid'); + $mform->setType('contextid', PARAM_INT); + $mform->setDefault('contextid', $this->context->id); + + $mform->addElement('hidden', 'filter'); + $mform->setType('filter', PARAM_ALPHAEXT); + $mform->setDefault('filter', $this->filter); + + $this->add_action_buttons(); + } + + /** + * Override this method to add your form controls. + * @param $mform the form we are building. $this->_form, but passed in for convinience. + */ + abstract protected function definition_inner($mform); + + /** + * Override this method to save the settings to the database. The default + * implementation will probalby be sufficient for most simple cases. + * @param object $data the form data that was submitted. + */ + public function save_changes($data) { + $data = (array) $data; + unset($data['filter']); + unset($data['contextid']); + foreach ($data as $name => $value) { + if ($value !== '') { + filter_set_local_config($this->filter, $this->context->id, $name, $value); + } else { + filter_unset_local_config($this->filter, $this->context->id, $name); + } + } + } +} +?> \ No newline at end of file diff --git a/filter/manage.php b/filter/manage.php index 05bd69b95f..6febdc5221 100644 --- a/filter/manage.php +++ b/filter/manage.php @@ -34,6 +34,7 @@ require_once(dirname(__FILE__) . '/../config.php'); require_once($CFG->libdir . '/adminlib.php'); $contextid = required_param('contextid',PARAM_INT); +$forfilter = optional_param('filter', '', PARAM_SAFEPATH); if (!$context = get_context_instance_by_id($contextid)) { print_error('wrongcontextid', 'error'); @@ -46,6 +47,7 @@ if (!in_array($context->contextlevel, array(CONTEXT_COURSECAT, CONTEXT_COURSE, C $isfrontpage = $context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID; $contextname = print_context_name($context); +$baseurl = $CFG->wwwroot . '/filter/manage.php?contextid=' . $context->id; if ($context->contextlevel == CONTEXT_COURSECAT) { $course = clone($SITE); @@ -70,8 +72,25 @@ if (!$isfrontpage && empty($availablefilters)) { print_error('nofiltersenabled', 'error'); } +// If we are handling local settings for a particular filter, start processing. +if ($forfilter) { + if (!filter_has_local_settings($forfilter)) { + print_error('filterdoesnothavelocalconfig', 'error', $forfilter); + } + require_once($CFG->dirroot . '/filter/local_settings_form.php'); + require_once($CFG->dirroot . '/' . $forfilter . '/filterlocalsettings.php'); + $formname = basename($forfilter) . '_filter_local_settings_form'; + $settingsform = new $formname($CFG->wwwroot . '/filter/manage.php', $forfilter, $context); + if ($settingsform->is_cancelled()) { + redirect($baseurl); + } else if ($data = $settingsform->get_data()) { + $settingsform->save_changes($data); + redirect($baseurl); + } +} + /// Process any form submission. -if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) { +if ($forfilter == '' && optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) { foreach ($availablefilters as $filter => $filterinfo) { $newstate = optional_param(str_replace('/', '_', $filter), false, PARAM_INT); if ($newstate !== false && $newstate != $filterinfo->localstate) { @@ -86,7 +105,14 @@ $assignableroles = get_assignable_roles($context, ROLENAME_BOTH); $overridableroles = get_overridable_roles($context, ROLENAME_BOTH); /// Work out an appropriate page title. -$title = get_string('filtersettingsfor', 'filters', $contextname); +if ($forfilter) { + $a = new stdClass; + $a->filter = filter_get_name($forfilter); + $a->context = $contextname; + $title = get_string('filtersettingsforin', 'filters', $a); +} else { + $title = get_string('filtersettingsin', 'filters', $contextname); +} $straction = get_string('filters', 'admin'); // Used by tabs.php /// Print the header and tabs @@ -103,6 +129,10 @@ print_heading_with_help($title, 'localfiltersettings'); if (empty($availablefilters)) { echo '

' . get_string('nofiltersenabled', 'filters') . "

\n"; +} else if ($forfilter) { + $current = filter_get_local_config($forfilter, $contextid); + $settingsform->set_data((object) $current); + $settingsform->display(); } else { $settingscol = false; foreach ($availablefilters as $filter => $notused) { @@ -122,7 +152,7 @@ if (empty($availablefilters)) { TEXTFILTER_ON => $stron, ); - echo '
'; + echo ''; echo "\n
\n"; echo ''; @@ -131,7 +161,7 @@ if (empty($availablefilters)) { $table->align = array('left', 'left'); if ($settingscol) { $table->head[] = $strsettings; - $table->align = 'left'; + $table->align[] = 'left'; } $table->width = ' '; $table->data = array(); @@ -156,8 +186,7 @@ if (empty($availablefilters)) { if ($settingscol) { $settings = ''; if ($filterinfo->hassettings) { - $settings = '' . $strsettings . ''; + $settings = '' . $strsettings . ''; } $row[] = $settings; } diff --git a/lang/en_utf8/error.php b/lang/en_utf8/error.php index 4edf487994..52f7d899f6 100644 --- a/lang/en_utf8/error.php +++ b/lang/en_utf8/error.php @@ -222,6 +222,7 @@ $string['fieldrequired'] = '\"$a\" is a required field'; $string['filenotfound'] = 'Sorry, the requested file could not be found'; $string['filenotreadable'] = 'File is not readable'; $string['filemismatch'] = 'Non-core file name mismatch. The file \"$a->current\" should be $a->file'; +$string['filterdoesnothavelocalconfig'] = 'The filter $a does not allow local configuration.'; $string['filternotinstalled'] = 'Filter $a is not currently installed'; $string['filternotactive'] = 'Filter $a is not currently active'; $string['forumblockingtoomanyposts'] = 'You have exceeded the posting threshold set for this forum'; diff --git a/lang/en_utf8/filters.php b/lang/en_utf8/filters.php index 7ba975133f..e7c91d955c 100644 --- a/lang/en_utf8/filters.php +++ b/lang/en_utf8/filters.php @@ -23,7 +23,8 @@ $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['filtersettingsfor'] = 'Filter settings for $a'; +$string['filtersettingsforin'] = 'Filter settings for $a->filter in $a->context'; +$string['filtersettingsin'] = 'Filter settings in $a'; $string['firstaccess'] = 'First access'; $string['globalrolelabel'] = '$a->label is $a->value'; $string['isactive'] = 'Active?'; diff --git a/lib/filterlib.php b/lib/filterlib.php index 7a7a1f9595..c9de7139b7 100644 --- a/lib/filterlib.php +++ b/lib/filterlib.php @@ -61,7 +61,7 @@ class filter_manager { protected static $singletoninstance; protected function __construct() { - $stringfilternames = filter_get_string_filters(); + $this->stringfilternames = filter_get_string_filters(); } /** @@ -602,6 +602,17 @@ function filter_set_local_config($filter, $contextid, $name, $value) { } } +/** + * Remove a particular local config variable for a filter in a context. + * @param string $filter The filter name, for example 'filter/tex' or 'mod/glossary'. + * @param integer $contextid The id of the context to get the local config for. + * @param string $name the setting name. + */ +function filter_unset_local_config($filter, $contextid, $name) { + global $DB; + $DB->delete_records('filter_config', array('filter' => $filter, 'contextid' => $contextid, 'name' => $name)); +} + /** * Get local config variables for a filter in a context. Normally (when your * filter is running) you don't need to call this, becuase the config is fetched @@ -764,8 +775,8 @@ function filter_has_global_settings($filter) { */ function filter_has_local_settings($filter) { global $CFG; - // TODO - return false; + $settingspath = $CFG->dirroot . '/' . $filter . '/filterlocalsettings.php'; + return is_readable($settingspath); } /** -- 2.39.5