diff --git a/filter/local_settings_form.php b/filter/local_settings_form.php new file mode 100644 index 00000000000..08a199b96cb --- /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 05bd69b95fe..6febdc52210 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 '