filters: MDL-7336 code to let people edit local filter config
authortjhunt <tjhunt>
Mon, 13 Apr 2009 07:14:27 +0000 (07:14 +0000)
committertjhunt <tjhunt>
Mon, 13 Apr 2009 07:14:27 +0000 (07:14 +0000)
filter/local_settings_form.php [new file with mode: 0644]
filter/manage.php
lang/en_utf8/error.php
lang/en_utf8/filters.php
lib/filterlib.php

diff --git a/filter/local_settings_form.php b/filter/local_settings_form.php
new file mode 100644 (file)
index 0000000..08a199b
--- /dev/null
@@ -0,0 +1,91 @@
+<?php // $Id$
+
+///////////////////////////////////////////////////////////////////////////
+//                                                                       //
+// NOTICE OF COPYRIGHT                                                   //
+//                                                                       //
+// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
+//          http://moodle.org                                            //
+//                                                                       //
+// Copyright (C) 1999 onwards Martin Dougiamas  http://dougiamas.com     //
+//                                                                       //
+// This program is free software; you can redistribute it and/or modify  //
+// it under the terms of the GNU General Public License as published by  //
+// the Free Software Foundation; either version 2 of the License, or     //
+// (at your option) any later version.                                   //
+//                                                                       //
+// This program is distributed in the hope that it will be useful,       //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of        //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
+// GNU General Public License for more details:                          //
+//                                                                       //
+//          http://www.gnu.org/copyleft/gpl.html                         //
+//                                                                       //
+///////////////////////////////////////////////////////////////////////////
+
+/**
+ * A Moodle form base class for editing local filter settings.
+ *
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ * @package moodlecore
+ *//** */
+require_once($CFG->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
index 05bd69b95fe176cd69f03b344c90599c828d0114..6febdc52210e53342a8b2a15012111e6bf7e7cca 100644 (file)
@@ -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 '<p class="centerpara">' . get_string('nofiltersenabled', 'filters') . "</p>\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 '<form action="' . $CFG->wwwroot . '/filter/manage.php?contextid=' . $context->id . '" method="post">';
+    echo '<form action="' . $baseurl . '" method="post">';
     echo "\n<div>\n";
     echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
 
@@ -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 = '<a href="' . $CFG->wwwroot . '/filter/settings.php?contextid=' .
-                        $context->id . '&amp;filter=' . $filter . '">' . $strsettings . '</a>';
+                $settings = '<a href="' . $baseurl . '&amp;filter=' . $filter . '">' . $strsettings . '</a>';
             }
             $row[] = $settings;
         }
index 4edf48799457eb5270d51e58a31279c7ad1dfc4b..52f7d899f67821ca76cf5554805e092330607738 100644 (file)
@@ -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';
index 7ba975133f7a49596341d204a81cd9382b211d26..e7c91d955cfee11ba305e2083733c382d7bdcdc7 100644 (file)
@@ -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?';
index 7a7a1f9595ab8c00bf3e2f067d08399cc70bc1d7..c9de7139b7e97e0ef95801ecdce3a63a0171c01c 100644 (file)
@@ -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);
 }
 
 /**