]> git.mjollnir.org Git - moodle.git/commitdiff
filters: MDL-7336 implement get_active_filters
authortjhunt <tjhunt>
Mon, 13 Apr 2009 06:53:53 +0000 (06:53 +0000)
committertjhunt <tjhunt>
Mon, 13 Apr 2009 06:53:53 +0000 (06:53 +0000)
lib/filterlib.php
lib/simpletest/testfilterconfig.php

index 3e1fc915111428d454f6030d4eeec8e2551d3233..19aef380a342494409d9206fb38c2dfa1e1a6503 100644 (file)
@@ -328,6 +328,53 @@ function filter_get_local_config($filter, $contextid) {
     return $DB->get_records_menu('filter_config', array('filter' => $filter, 'contextid' => $contextid), '', 'name,value');
 }
 
+/**
+ * Get the list of active filters, in the order that they should be used
+ * for a particular context, along with any local configuration variables.
+ *
+ * @param object $context a context
+ *
+ * @return array an array where the keys are the filter names, for example
+ *      'filter/tex' or 'mod/glossary' and the values are any local
+ *      configuration for that filter, as an array of name => value pairs
+ *      from the filter_config table. In a lot of cases, this will be an
+ *      empty array. So, an example return value for this function might be
+ *      array('filter/tex' => array(), 'mod/glossary' => array('glossaryid', 123))
+ */
+function get_active_filters($context) {
+    global $DB;
+    $contextids = str_replace('/', ',', trim($context->path, '/'));
+
+    // The following SQL is tricky. It is explained on
+    // http://docs.moodle.org/en/Development:Filter_enable/disable_by_context
+    $rs = $DB->get_recordset_sql(
+        "SELECT active.filter, fc.name, fc.value
+         FROM (SELECT f.filter
+             FROM {filter_active} f
+             JOIN {context} ctx ON f.contextid = ctx.id
+             WHERE ctx.id IN ($contextids)
+             GROUP BY filter
+             HAVING MAX(f.active * ctx.depth) > -MIN(f.active * ctx.depth)
+             ORDER BY MAX(f.sortorder)) active
+         LEFT JOIN {filter_config} fc ON fc.filter = active.filter
+         WHERE fc.contextid = $context->id OR fc.contextid IS NULL");
+
+    // Masssage the data into the specified format to return.
+    $filters = array();
+    foreach ($rs as $row) {
+        if (!isset($filters[$row->filter])) {
+            $filters[$row->filter] = array();
+        }
+        if (!is_null($row->name)) {
+            $filters[$row->filter][$row->name] = $row->value;
+        }
+    }
+
+    $rs->close();
+
+    return $filters;
+}
+
 /**
  * Process phrases intelligently found within a HTML text (such as adding links)
  *
index 5603c317ef769fff18b27be6d6cf7a40f37a6324..1237fbb397f6a12ebdbb9038ed6815728238b4d9 100644 (file)
@@ -341,4 +341,140 @@ class filter_config_test extends UnitTestCaseUsingDatabase {
         $this->assertEqual(array('setting1' => 'An arbitrary value', 'setting2' => 'Another arbitrary value'), $config);
     }
 }
+
+class get_active_filters_test extends UnitTestCaseUsingDatabase {
+    private $syscontext;
+    private $childcontext;
+    private $childcontext2;
+
+    public function 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);
+
+        // Create the table we need and switch to test DB.
+        $this->create_test_tables(array('filter_active', 'filter_config', 'context'), 'lib');
+        $this->switch_to_test_db();
+
+        // Set up systcontext in the test database.
+        $this->testdb->insert_record('context', $this->syscontext);
+        $this->syscontext->id = 1;
+
+        // Set up a child context.
+        $this->childcontext = new stdClass;
+        $this->childcontext->contextlevel = CONTEXT_COURSECAT;
+        $this->childcontext->instanceid = 1;
+        $this->childcontext->depth = 2;
+        $this->childcontext->path = '/1/2';
+        $this->testdb->insert_record('context', $this->childcontext);
+        $this->childcontext->id = 2;
+
+        // Set up a grandchild context.
+        $this->childcontext2 = new stdClass;
+        $this->childcontext2->contextlevel = CONTEXT_COURSE;
+        $this->childcontext2->instanceid = 2;
+        $this->childcontext2->depth = 3;
+        $this->childcontext2->path = '/1/2/3';
+        $this->testdb->insert_record('context', $this->childcontext2);
+        $this->childcontext2->id = 3;
+    }
+
+    private function assert_filter_list($expectedfilters, $filters) {
+        $this->assert(new ArraysHaveSameValuesExpectation($expectedfilters), array_keys($filters));
+    }
+
+    public function test_globally_on_is_returned() {
+        // Setup fixture.
+        filter_set_global_state('filter/name', TEXTFILTER_ON);
+        // Exercise SUT.
+        $filters = get_active_filters($this->syscontext);
+        // Validate.
+        $this->assert_filter_list(array('filter/name'), $filters);
+        // Check no config returned correctly.
+        $this->assertEqual(array(), $filters['filter/name']);
+    }
+
+    public function test_globally_off_not_returned() {
+        // Setup fixture.
+        filter_set_global_state('filter/name', TEXTFILTER_OFF);
+        // Exercise SUT.
+        $filters = get_active_filters($this->childcontext2);
+        // Validate.
+        $this->assert_filter_list(array(), $filters);
+    }
+
+    public function test_globally_off_overridden() {
+        // Setup fixture.
+        filter_set_global_state('filter/name', TEXTFILTER_OFF);
+        filter_set_local_state('filter/name', $this->childcontext->id, TEXTFILTER_ON);
+        // Exercise SUT.
+        $filters = get_active_filters($this->childcontext2);
+        // Validate.
+        $this->assert_filter_list(array('filter/name'), $filters);
+    }
+
+    public function test_globally_on_overridden() {
+        // Setup fixture.
+        filter_set_global_state('filter/name', TEXTFILTER_ON);
+        filter_set_local_state('filter/name', $this->childcontext->id, TEXTFILTER_OFF);
+        // Exercise SUT.
+        $filters = get_active_filters($this->childcontext2);
+        // Validate.
+        $this->assert_filter_list(array(), $filters);
+    }
+
+    public function test_globally_disabled_not_overridden() {
+        // Setup fixture.
+        filter_set_global_state('filter/name', TEXTFILTER_DISABLED);
+        filter_set_local_state('filter/name', $this->childcontext->id, TEXTFILTER_ON);
+        // Exercise SUT.
+        $filters = get_active_filters($this->syscontext);
+        // Validate.
+        $this->assert_filter_list(array(), $filters);
+    }
+
+    public function test_single_config_returned() {
+        // Setup fixture.
+        filter_set_global_state('filter/name', TEXTFILTER_ON);
+        filter_set_local_config('filter/name', $this->childcontext->id, 'settingname', 'A value');
+        // Exercise SUT.
+        $filters = get_active_filters($this->childcontext);
+        // Validate.
+        $this->assertEqual(array('settingname' => 'A value'), $filters['filter/name']);
+    }
+
+    public function test_multi_config_returned() {
+        // Setup fixture.
+        filter_set_global_state('filter/name', TEXTFILTER_ON);
+        filter_set_local_config('filter/name', $this->childcontext->id, 'settingname', 'A value');
+        filter_set_local_config('filter/name', $this->childcontext->id, 'anothersettingname', 'Another value');
+        // Exercise SUT.
+        $filters = get_active_filters($this->childcontext);
+        // Validate.
+        $this->assertEqual(array('settingname' => 'A value', 'anothersettingname' => 'Another value'), $filters['filter/name']);
+    }
+
+    public function test_config_from_other_context_not_returned() {
+        // Setup fixture.
+        filter_set_global_state('filter/name', TEXTFILTER_ON);
+        filter_set_local_config('filter/name', $this->childcontext->id, 'settingname', 'A value');
+        filter_set_local_config('filter/name', $this->childcontext2->id, 'anothersettingname', 'Another value');
+        // Exercise SUT.
+        $filters = get_active_filters($this->childcontext2);
+        // Validate.
+        $this->assertEqual(array('anothersettingname' => 'Another value'), $filters['filter/name']);
+    }
+
+    public function test_config_from_other_filter_not_returned() {
+        // Setup fixture.
+        filter_set_global_state('filter/name', TEXTFILTER_ON);
+        filter_set_local_config('filter/name', $this->childcontext->id, 'settingname', 'A value');
+        filter_set_local_config('filter/other', $this->childcontext->id, 'anothersettingname', 'Another value');
+        // Exercise SUT.
+        $filters = get_active_filters($this->childcontext);
+        // Validate.
+        $this->assertEqual(array('settingname' => 'A value'), $filters['filter/name']);
+    }
+}
+
 ?>