Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
declare(strict_types=1);
18
 
19
namespace core_reportbuilder\local\filters;
20
 
21
use MoodleQuickForm;
22
use core_reportbuilder\local\helpers\database;
23
 
24
/**
25
 * Autocomplete report filter
26
 *
27
 * @package     core_reportbuilder
28
 * @copyright   2022 Nathan Nguyen <nathannguyen@catalyst-au.net>
29
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class autocomplete extends base {
32
 
33
    /**
34
     * Return the options for the filter as an array, to be used to populate the select input field
35
     *
36
     * @return array
37
     */
38
    protected function get_select_options(): array {
39
        return (array) $this->filter->get_options();
40
    }
41
 
42
    /**
43
     * Setup form
44
     *
45
     * @param MoodleQuickForm $mform
46
     */
47
    public function setup_form(MoodleQuickForm $mform): void {
48
        $operatorlabel = get_string('filterfieldvalue', 'core_reportbuilder', $this->get_header());
49
        $values = [0 => ''] + $this->get_select_options();
50
        $options = ['multiple' => true];
51
 
52
        $mform->addElement('autocomplete', $this->name . '_values', $operatorlabel, $values, $options)
53
            ->setHiddenLabel(true);
54
    }
55
 
56
    /**
57
     * Return filter SQL
58
     *
59
     * @param array $values
60
     * @return array
61
     */
62
    public function get_sql_filter(array $values): array {
63
        global $DB;
64
 
65
        $fieldsql = $this->filter->get_field_sql();
66
        $params = $this->filter->get_field_params();
67
 
68
        $invalues = $values["{$this->name}_values"] ?? [];
69
        if (empty($invalues)) {
70
            return ['', []];
71
        }
72
 
73
        [$insql, $inparams] = $DB->get_in_or_equal($invalues, SQL_PARAMS_NAMED, database::generate_param_name('_'));
74
 
75
        return ["{$fieldsql} $insql", array_merge($params, $inparams)];
76
    }
77
 
78
    /**
79
     * Return sample filter values
80
     *
81
     * @return array
82
     */
83
    public function get_sample_values(): array {
84
        return [
85
            "{$this->name}_values" => [1],
86
        ];
87
    }
88
}