Skip to content

Commit

Permalink
MDLSITE-6660 Improve the substring filter settings
Browse files Browse the repository at this point in the history
Checkboxes swapped to have the same order as the columns with texts.
Labels wording improved (credit goes to Helen Foster). The behaviour
made more intuitive so that when both are ticked, the substring is
searched for in either source.
  • Loading branch information
mudrd8mz committed Aug 6, 2024
1 parent 51329d2 commit 5f93926
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 41 deletions.
86 changes: 63 additions & 23 deletions classes/output/translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,41 +346,81 @@ public function __construct(\local_amos\output\filter $filter, object $user) {
continue;
}

if ($substring !== '' && ($substringtra || $substringeng)) {
$searcheng = ($substringeng && $string->original !== null);
$searchtra = ($substringtra && $string->translation !== null);

if ($substring !== '' && ($searcheng || $searchtra)) {
if (empty($substringregex)) {
if (empty($substringcs)) {
if ($substringtra && $string->translation !== null
&& !stristr($string->translation, $substring)) {
continue;
}
if ($substringeng && $string->original !== null && !stristr($string->original, $substring)) {
continue;
if ($searcheng && $searchtra) {
if (!stristr($string->original, $substring) && !stristr($string->translation, $substring)) {
continue;
}

} else if ($searcheng) {
if (!stristr($string->original, $substring)) {
continue;
}

} else if ($searchtra) {
if (!stristr($string->translation, $substring)) {
continue;
}
}

} else {
if ($substringtra && $string->translation !== null
&& !strstr($string->translation, $substring)) {
continue;
}
if ($substringeng && $string->original !== null && !strstr($string->original, $substring)) {
continue;
if ($searcheng && $searchtra) {
if (!strstr($string->original, $substring) && !stristr($string->translation, $substring)) {
continue;
}

} else if ($searcheng) {
if (!strstr($string->original, $substring)) {
continue;
}

} else if ($searchtra) {
if (!strstr($string->translation, $substring)) {
continue;
}
}
}

} else {
if (empty($substringcs)) {
if ($substringtra && !preg_match("/$substring/i", $string->translation)) {
continue;
}
if ($substringeng && !preg_match("/$substring/i", $string->original)) {
continue;
if ($searcheng && $searchtra) {
if (!preg_match("/$substring/i", $string->translation)
&& !preg_match("/$substring/i", $string->original)) {
continue;
}

} else if ($searcheng) {
if (!preg_match("/$substring/i", $string->original)) {
continue;
}

} else if ($searchtra) {
if (!preg_match("/$substring/i", $string->translation)) {
continue;
}
}

} else {
if ($substringtra && !preg_match("/$substring/", $string->translation)) {
continue;
}
if ($substringeng && !preg_match("/$substring/", $string->original)) {
continue;
if ($searcheng && $searchtra) {
if (!preg_match("/$substring/", $string->translation)
&& !preg_match("/$substring/", $string->original)) {
continue;
}

} else if ($searcheng) {
if (!preg_match("/$substring/", $string->original)) {
continue;
}

} else if ($searchtra) {
if (!preg_match("/$substring/", $string->translation)) {
continue;
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions lang/en/local_amos.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@
$string['filtertxt'] = 'Substring';
$string['filtertxt_desc'] = 'String must contain given text';
$string['filtertxtcasesensitive'] = 'case-sensitive';
$string['filtertxteng'] = 'apply to English originals';
$string['filtertxteng'] = 'in English strings';
$string['filtertxtregex'] = 'regex';
$string['filtertxttra'] = 'apply to translations';
$string['filtertxttra'] = 'in translated strings';
$string['filterver'] = 'Version';
$string['filtervernothingselected'] = 'Please select some version';
$string['filterver_desc'] = 'Show strings for this Moodle version';
Expand Down
12 changes: 6 additions & 6 deletions templates/filter.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,17 @@
<label class="form-check-label" for="amosfilter_ftxs">{{#str}} filtertxtcasesensitive, local_amos {{/str}}</label>
</div>

<div class="form-check form-check-inline">
<input type="hidden" name="ftxn" value="0">
<input class="form-check-input" {{#filterdata.substringtra}} checked {{/filterdata.substringtra}} type="checkbox" name="ftxn" id="amosfilter_ftxn" value="1">
<label class="form-check-label" for="amosfilter_ftxn">{{#str}} filtertxttra, local_amos {{/str}}</label>
</div>

<div class="form-check form-check-inline">
<input type="hidden" name="ftxe" value="0">
<input class="form-check-input" {{#filterdata.substringeng}} checked {{/filterdata.substringeng}} type="checkbox" name="ftxe" id="amosfilter_ftxe" value="1">
<label class="form-check-label" for="amosfilter_ftxe">{{#str}} filtertxteng, local_amos {{/str}}</label>
</div>

<div class="form-check form-check-inline">
<input type="hidden" name="ftxn" value="0">
<input class="form-check-input" {{#filterdata.substringtra}} checked {{/filterdata.substringtra}} type="checkbox" name="ftxn" id="amosfilter_ftxn" value="1">
<label class="form-check-label" for="amosfilter_ftxn">{{#str}} filtertxttra, local_amos {{/str}}</label>
</div>
</div>
</div>

Expand Down
48 changes: 38 additions & 10 deletions tests/output/translator_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ public function test_filter_by_substring() {
$stage = new \mlang_stage();

$component = new \mlang_component('foo_bar', 'en', \mlang_version::by_code(39));
$component->add_string(new \mlang_string('foobar', 'Foo bar'));
$component->add_string(new \mlang_string('foobaz', 'Foo baz'));
$component->add_string(new \mlang_string('s1', 'AAA'));
$component->add_string(new \mlang_string('s2', 'BBB'));
$component->add_string(new \mlang_string('s3', 'CCC'));
$stage->add($component);
$component->clear();

$component = new \mlang_component('foo_bar', 'cs', \mlang_version::by_code(39));
$component->add_string(new \mlang_string('foobar', 'Fů bar'));
$component->add_string(new \mlang_string('foobaz', 'Foo baz'));
$component->add_string(new \mlang_string('s1', 'Ddd'));
$component->add_string(new \mlang_string('s2', 'Aaa'));
$component->add_string(new \mlang_string('s3', 'aaa'));
$stage->add($component);
$component->clear();

Expand All @@ -59,24 +61,50 @@ public function test_filter_by_substring() {
$_POST['flng'] = ['cs'];
$_POST['fcmp'] = ['foo_bar'];
$_POST['sesskey'] = sesskey();
$_POST['ftxt'] = 'Foo';
$_POST['ftxt'] = 'aaa';

$filter = new \local_amos\output\filter(new \moodle_url('/'));
$translator = new \local_amos\output\translator($filter, $USER);
$data = $translator->export_for_template($PAGE->get_renderer('core'));

// By default, search in both English and Czech.
$this->assertEquals(3, count($data['strings']));
$this->assertEquals('s1', $data['strings'][0]->stringid);
$this->assertEquals('s2', $data['strings'][1]->stringid);
$this->assertEquals('s3', $data['strings'][2]->stringid);

// Do not apply to translations, search in English only.
$_POST['ftxe'] = 1;
$_POST['ftxn'] = 0;

$filter = new \local_amos\output\filter(new \moodle_url('/'));
$translator = new \local_amos\output\translator($filter, $USER);
$data = $translator->export_for_template($PAGE->get_renderer('core'));

$this->assertEquals(1, count($data['strings']));
$this->assertEquals('s1', $data['strings'][0]->stringid);

// Search in Czech only.
$_POST['ftxe'] = 0;
$_POST['ftxn'] = 1;
$_POST['ftxt'] = 'ddd';

$filter = new \local_amos\output\filter(new \moodle_url('/'));
$translator = new \local_amos\output\translator($filter, $USER);
$data = $translator->export_for_template($PAGE->get_renderer('core'));

// By default, both English and Czech are to be filtered so only 'foobaz' string is returned.
$this->assertEquals(1, count($data['strings']));
$this->assertEquals('foobaz', $data['strings'][0]->stringid);
$this->assertEquals('s1', $data['strings'][0]->stringid);

// Do not apply to translations.
// Same string but in English only.
$_POST['ftxe'] = 1;
$_POST['ftxn'] = 0;
$_POST['ftxt'] = 'ddd';

$filter = new \local_amos\output\filter(new \moodle_url('/'));
$translator = new \local_amos\output\translator($filter, $USER);
$data = $translator->export_for_template($PAGE->get_renderer('core'));

// Now the filter applies to the English only so both strings should be returned.
$this->assertEquals(2, count($data['strings']));
$this->assertEquals(0, count($data['strings']));
}
}

0 comments on commit 5f93926

Please sign in to comment.