]> git.mjollnir.org Git - moodle.git/commitdiff
filters: MDL-7336 function to get the information the local settings page will need.
authortjhunt <tjhunt>
Mon, 13 Apr 2009 07:07:03 +0000 (07:07 +0000)
committertjhunt <tjhunt>
Mon, 13 Apr 2009 07:07:03 +0000 (07:07 +0000)
lib/filterlib.php
lib/simpletest/testfilterconfig.php

index a0d5e2fb1bd14fe3d87ef693978119967c981a63..9844b37df2a3a8617afebdb02a7cfa06be7004bf 100644 (file)
@@ -649,6 +649,46 @@ function filter_get_active_in_context($context) {
     return $filters;
 }
 
+/**
+ * List all of the filters that are available in this context, and what the
+ * local and interited states of that filter are.
+ * @param object $context a context that is not the system context.
+ * @return array an array with filter names, for example 'filter/tex' or
+ *      'mod/glossary' as keys. and and the values are objects with fields:
+ *      ->filter filter name, same as the key.
+ *      ->localstate TEXTFILTER_ON/OFF/INHERIT
+ *      ->inheritedstate TEXTFILTER_ON/OFF - the state that will be used if localstate is set to TEXTFILTER_INHERIT.
+ */
+function filter_get_available_in_context($context) {
+    global $DB;
+
+    // The complex logic is working out the active state in the parent context,
+    // so strip the current context from the list.
+    $contextids = explode('/', trim($context->path, '/'));
+    array_pop($contextids);
+    $contextids = implode(',', $contextids);
+    if (empty($contextids)) {
+        throw new coding_exception('filter_get_available_in_context cannot be called with the system context.');
+    }
+
+    // The following SQL is tricky, in the same way at the SQL in filter_get_active_in_context.
+    return $DB->get_records_sql(
+        "SELECT parent_states.filter,
+                CASE WHEN fa.active IS NULL THEN " . TEXTFILTER_INHERIT . "
+                ELSE fa.active END AS localstate,
+             parent_states.inheritedstate
+         FROM (SELECT f.filter,
+                    CASE WHEN MAX(f.active * ctx.depth) > -MIN(f.active * ctx.depth) THEN " . TEXTFILTER_ON . "
+                    ELSE " . TEXTFILTER_OFF . " END AS inheritedstate
+             FROM {filter_active} f
+             JOIN {context} ctx ON f.contextid = ctx.id
+             WHERE ctx.id IN ($contextids)
+             GROUP BY filter
+             HAVING MIN(f.active) > " . TEXTFILTER_DISABLED . "
+             ORDER BY MAX(f.sortorder)) parent_states
+         LEFT JOIN {filter_active} fa ON fa.filter = parent_states.filter AND fa.contextid = $context->id");
+}
+
 /**
  * This function is for use by the filter administration page.
  * @return array 'filtername' => object with fields 'filter' (=filtername), 'active' and 'sortorder'
index e895a45c155a9070b7fbc60b3f36e9fa771c87f8..7f14440bd6f11092d02acc94799d8834772f1544 100644 (file)
@@ -363,7 +363,7 @@ class filter_config_test extends UnitTestCaseUsingDatabase {
     }
 }
 
-class filter_get_active_in_context_test extends UnitTestCaseUsingDatabase {
+class filter_get_active_available_in_context_test extends UnitTestCaseUsingDatabase {
     private $syscontext;
     private $childcontext;
     private $childcontext2;
@@ -498,6 +498,53 @@ class filter_get_active_in_context_test extends UnitTestCaseUsingDatabase {
         // Validate.
         $this->assertEqual(array('settingname' => 'A value'), $filters['filter/name']);
     }
+
+    protected function assert_one_available_filter($filter, $localstate, $inheritedstate, $filters) {
+        $this->assertEqual(1, count($filters), 'More than one record returned %s.');
+        $rec = $filters[$filter];
+        $expectedrec = new stdClass;
+        $expectedrec->filter = $filter;
+        $expectedrec->localstate = $localstate;
+        $expectedrec->inheritedstate = $inheritedstate;
+        $this->assert(new CheckSpecifiedFieldsExpectation($expectedrec), $rec);
+    }
+
+    public function test_available_in_context_localoverride() {
+        // Setup fixture.
+        filter_set_global_state('filter/name', TEXTFILTER_ON);
+        filter_set_local_state('filter/name', $this->childcontext->id, TEXTFILTER_OFF);
+        // Exercise SUT.
+        $filters = filter_get_available_in_context($this->childcontext);
+        // Validate.
+        $this->assert_one_available_filter('filter/name', TEXTFILTER_OFF, TEXTFILTER_ON, $filters);
+    }
+
+    public function test_available_in_context_nolocaloverride() {
+        // Setup fixture.
+        filter_set_global_state('filter/name', TEXTFILTER_ON);
+        filter_set_local_state('filter/name', $this->childcontext->id, TEXTFILTER_OFF);
+        // Exercise SUT.
+        $filters = filter_get_available_in_context($this->childcontext2);
+        // Validate.
+        $this->assert_one_available_filter('filter/name', TEXTFILTER_INHERIT, TEXTFILTER_OFF, $filters);
+    }
+
+    public function test_available_in_context_disabled_not_returned() {
+        // Setup fixture.
+        filter_set_global_state('filter/name', TEXTFILTER_DISABLED);
+        filter_set_local_state('filter/name', $this->childcontext->id, TEXTFILTER_ON);
+        // Exercise SUT.
+        $filters = filter_get_available_in_context($this->childcontext);
+        // Validate.
+        $this->assertEqual(array(), $filters);
+    }
+
+    public function test_available_in_context_exception_with_syscontext() {
+        // Set expectation.
+        $this->expectException();
+        // Exercise SUT.
+        filter_get_available_in_context($this->syscontext);
+    }
 }
 
 class filter_delete_all_data_test extends UnitTestCaseUsingDatabase {