/**
  * 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);
         }
     }
 }
예제 #2
0
 public function process_config($data)
 {
     $data = (object) $data;
     if (strpos($data->filter, 'filter/') === 0) {
         $data->filter = substr($data->filter, 7);
     } else {
         if (strpos($data->filter, '/') !== false) {
             // Unsupported old filter.
             return;
         }
     }
     if (!filter_is_enabled($data->filter)) {
         // Not installed or not enabled, nothing to do
         return;
     }
     filter_set_local_config($data->filter, $this->task->get_contextid(), $data->name, $data->value);
 }
예제 #3
0
    public function process_config($data) {

        $data = (object)$data;

        if (!filter_is_enabled($data->filter)) { // Not installed or not enabled, nothing to do
            return;
        }
        filter_set_local_config($data->filter, $this->task->get_contextid(), $data->name, $data->value);
    }
예제 #4
0
 public function test_filter_delete_all_for_context()
 {
     // Setup fixture.
     filter_set_global_state('filter/name', TEXTFILTER_ON);
     filter_set_local_state('filter/name', 123, TEXTFILTER_OFF);
     filter_set_local_config('filter/name', 123, 'settingname', 'A value');
     filter_set_local_config('filter/other', 123, 'settingname', 'Other value');
     filter_set_local_config('filter/other', 122, 'settingname', 'Other value');
     // Exercise SUT.
     filter_delete_all_for_context(123);
     // Validate.
     $this->assertEqual(1, $this->testdb->count_records('filter_active'));
     $this->assertTrue($this->testdb->record_exists('filter_active', array('contextid' => $this->syscontext->id)));
     $this->assertEqual(1, $this->testdb->count_records('filter_config'));
     $this->assertTrue($this->testdb->record_exists('filter_config', array('filter' => 'filter/other')));
 }
function populate_test_database($syscontext, $numcategories, $numcourses, $nummodules, $numoverrides, $numconfigs)
{
    global $DB, $OUTPUT;
    set_time_limit(600);
    $syscontext->id = $DB->insert_record('context', $syscontext);
    // Category contexts.
    $categoryparents = array($syscontext);
    $categories = array();
    for ($i = 0; $i < $numcategories; $i++) {
        $context = insert_context(CONTEXT_COURSECAT, $i, $categoryparents[array_rand($categoryparents)]);
        $categoryparents[] = $context;
        $categories[$context->id] = $context;
    }
    echo $OUTPUT->notification('Created ' . $numcategories . ' course category contexts.', 'notifysuccess');
    flush();
    // Course contexts.
    $courses = array();
    for ($i = 0; $i < $numcourses; $i++) {
        $context = insert_context(CONTEXT_COURSE, $i, $categories[array_rand($categories)]);
        $courses[$context->id] = $context;
    }
    echo $OUTPUT->notification('Created ' . $numcourses . ' course contexts.', 'notifysuccess');
    flush();
    // Activities contexts.
    $mods = array();
    $prog = new progress_bar('modbar', 500, true);
    $transaction = $DB->start_delegated_transaction();
    for ($i = 0; $i < $nummodules; $i++) {
        $context = insert_context(CONTEXT_MODULE, $i, $courses[array_rand($courses)]);
        $mods[$context->id] = $context;
        if ($i % 50) {
            $prog->update($i, $nummodules, '');
        }
    }
    $transaction->allow_commit();
    echo $OUTPUT->notification('Created ' . $nummodules . ' module contexts.', 'notifysuccess');
    flush();
    $contexts = $categories + $courses + $mods;
    // Global settings.
    $installedfilters = filter_get_all_installed();
    $counts = array(TEXTFILTER_DISABLED => 0, TEXTFILTER_OFF => 0, TEXTFILTER_ON => 0);
    foreach ($installedfilters as $filter => $notused) {
        $state = array_rand($counts);
        filter_set_global_state($filter, $state);
        $counts[$state]++;
    }
    echo $OUTPUT->notification('Set global setting: ' . $counts[TEXTFILTER_DISABLED] . ' disabled, ' . $counts[TEXTFILTER_OFF] . ' off and ' . $counts[TEXTFILTER_ON] . ' on.', 'notifysuccess');
    flush();
    // Local overrides.
    $localstates = array(TEXTFILTER_OFF => 0, TEXTFILTER_ON => 0);
    $prog = new progress_bar('locbar', 500, true);
    $transaction = $DB->start_delegated_transaction();
    for ($i = 0; $i < $numoverrides; $i++) {
        filter_set_local_state(array_rand($installedfilters), array_rand($contexts), array_rand($localstates));
        if ($i % 50) {
            $prog->update($i, $numoverrides, '');
        }
    }
    $transaction->allow_commit();
    echo $OUTPUT->notification('Set ' . $numoverrides . ' local overrides.', 'notifysuccess');
    flush();
    // Local config.
    $variablenames = array('frog' => 0, 'toad' => 0, 'elver' => 0, 'eft' => 0, 'tadpole' => 0);
    $prog = new progress_bar('confbar', 500, true);
    $transaction = $DB->start_delegated_transaction();
    for ($i = 0; $i < $numconfigs; $i++) {
        filter_set_local_config(array_rand($installedfilters), array_rand($contexts), array_rand($variablenames), random_string(rand(20, 40)));
        if ($i % 50) {
            $prog->update($i, $numconfigs, '');
        }
    }
    $transaction->allow_commit();
    echo $OUTPUT->notification('Set ' . $numconfigs . ' local configs.', 'notifysuccess');
    flush();
}
예제 #6
0
 public function test_filter_get_globally_enabled_filters_with_config()
 {
     $this->setup_available_in_context_tests();
     // Set few filters.
     filter_set_global_state('one', TEXTFILTER_ON);
     filter_set_global_state('three', TEXTFILTER_OFF, -1);
     filter_set_global_state('two', TEXTFILTER_DISABLED);
     // Set global config.
     filter_set_local_config('one', $this->syscontext->id, 'test1a', 'In root');
     filter_set_local_config('one', $this->syscontext->id, 'test1b', 'In root');
     filter_set_local_config('two', $this->syscontext->id, 'test2a', 'In root');
     filter_set_local_config('two', $this->syscontext->id, 'test2b', 'In root');
     // Set child config.
     filter_set_local_config('one', $this->childcontext->id, 'test1a', 'In child');
     filter_set_local_config('one', $this->childcontext->id, 'test1b', 'In child');
     filter_set_local_config('two', $this->childcontext->id, 'test2a', 'In child');
     filter_set_local_config('two', $this->childcontext->id, 'test2b', 'In child');
     filter_set_local_config('three', $this->childcontext->id, 'test3a', 'In child');
     filter_set_local_config('three', $this->childcontext->id, 'test3b', 'In child');
     // Check.
     $actual = filter_get_globally_enabled_filters_with_config();
     $this->assertCount(2, $actual);
     $this->assertEquals(['three', 'one'], array_keys($actual));
     // Checks sortorder.
     $this->assertArrayHasKey('one', $actual);
     $this->assertArrayNotHasKey('two', $actual);
     $this->assertArrayHasKey('three', $actual);
     $this->assertEquals(['test1a' => 'In root', 'test1b' => 'In root'], $actual['one']);
     $this->assertEquals([], $actual['three']);
 }
예제 #7
0
/**
 * Write any per-context filter settings from the backup XML to the DB.
 * @param object $restore the restore we are part of.
 * @param object $data sata loaded from the XML.
 * @param object $newmodcontext the restored context object.
 */
function restore_write_local_filter_settings($restore, $data, $newcontext)
{
    if (filter_context_may_have_filter_settings($newcontext)) {
        return;
    }
    $installedfilters = filter_get_all_installed();
    if (!isset($data->filteractives)) {
        $data->filteractives = array();
    }
    foreach ($data->filteractives as $filter => $state) {
        if (isset($installedfilters[$filter])) {
            filter_set_local_state($filter, $newcontext->id, $state);
        }
    }
    if (!isset($data->filterconfigs)) {
        $data->filterconfigs = array();
    }
    foreach ($data->filterconfigs as $fc) {
        if (isset($installedfilters[$fc->filter])) {
            filter_set_local_config($fc->filter, $newcontext->id, $fc->name, $fc->value);
        }
    }
}