MDL-67685 core_reportbuilder: Add autocomplete filter

This commit is contained in:
Nathan Nguyen
2022-08-02 18:21:37 +07:00
committed by Nathan Nguyen
parent 417d1b918b
commit f32913dc65
3 changed files with 175 additions and 0 deletions
@@ -0,0 +1,78 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_reportbuilder\local\filters;
use MoodleQuickForm;
use core_reportbuilder\local\helpers\database;
/**
* Autocomplete report filter
*
* @package core_reportbuilder
* @copyright 2022 Nathan Nguyen <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class autocomplete extends base {
/**
* Return the options for the filter as an array, to be used to populate the select input field
*
* @return array
*/
protected function get_select_options(): array {
return (array) $this->filter->get_options();
}
/**
* Setup form
*
* @param MoodleQuickForm $mform
*/
public function setup_form(MoodleQuickForm $mform): void {
$operatorlabel = get_string('filterfieldvalue', 'core_reportbuilder', $this->get_header());
$values = [0 => ''] + $this->get_select_options();
$options = ['multiple' => true];
$mform->addElement('autocomplete', $this->name . '_values', $operatorlabel, $values, $options)
->setHiddenLabel(true);
}
/**
* Return filter SQL
*
* @param array $values
* @return array
*/
public function get_sql_filter(array $values): array {
global $DB;
$fieldsql = $this->filter->get_field_sql();
$params = $this->filter->get_field_params();
$invalues = $values["{$this->name}_values"] ?? [];
if (empty($invalues)) {
return ['', []];
}
$paramprefix = database::generate_param_name() . '_';
[$insql, $inparams] = $DB->get_in_or_equal($invalues, SQL_PARAMS_NAMED, $paramprefix);
return ["{$fieldsql} $insql", array_merge($params, $inparams)];
}
}
@@ -0,0 +1,96 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_reportbuilder\local\filters;
use advanced_testcase;
use core_reportbuilder\local\report\filter;
/**
* Unit tests for course selector filter
*
* @package core_reportbuilder
* @covers \core_reportbuilder\local\filters\base
* @covers \core_reportbuilder\local\filters\autocomplete
* @copyright 2022 Nathan Nguyen <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class autocomplete_test extends advanced_testcase {
/**
* Data provider for {@see test_get_sql_filter}
*
* @return array
*/
public function get_sql_filter_provider(): array {
return [
[[], ["Course 1 full name", "Course 2 full name", "Course 3 full name", "PHPUnit test site"]],
[["course1", "course3"], ["Course 1 full name", "Course 3 full name"]],
[["course1"], ["Course 1 full name"]],
];
}
/**
* Test getting filter SQL
*
* @param array $shortnames list of course short name
* @param array $expected list of course full name
*
* @dataProvider get_sql_filter_provider
*/
public function test_get_sql_filter(array $shortnames, array $expected): void {
global $DB;
$this->resetAfterTest();
// Create courses as values for autocompletion.
$course1 = $this->getDataGenerator()->create_course([
'fullname' => "Course 1 full name",
'shortname' => 'course1',
]);
$course2 = $this->getDataGenerator()->create_course([
'fullname' => "Course 2 full name",
'shortname' => 'course2',
]);
$course3 = $this->getDataGenerator()->create_course([
'fullname' => "Course 3 full name",
'shortname' => 'course3',
]);
$filter = (new filter(
autocomplete::class,
'test',
new \lang_string('course'),
'testentity',
'shortname'
))->set_options([
$course1->shortname => $course1->fullname,
$course2->shortname => $course2->fullname,
$course3->shortname => $course3->fullname,
]);
[$select, $params] = text::create($filter)->get_sql_filter([
$filter->get_unique_identifier() . '_values' => $shortnames,
]);
$fullnames = $DB->get_fieldset_select('course', 'fullname', $select, $params);
$this->assertEqualsCanonicalizing($expected, $fullnames);
}
}
+1
View File
@@ -22,3 +22,4 @@ Information provided here is intended especially for developers.
* New report filter types:
- `category` for reports containing course categories
- `tags` for reports containing entities with support for core_tag API
- `autocomplete` for reports that contain pre-defined values for selection.