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
 * Select report filter
26
 *
27
 * The options for the select are defined when creating the filter by calling {@see set_options} or {@see set_options_callback}
28
 *
29
 * To extend this class in your own filter (e.g. to pre-populate available options), you should override the {@see get_operators}
30
 * and/or {@see get_select_options} methods
31
 *
32
 * @package     core_reportbuilder
33
 * @copyright   2021 David Matamoros <davidmc@moodle.com>
34
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class select extends base {
37
 
38
    /** @var int Any value */
39
    public const ANY_VALUE = 0;
40
 
41
    /** @var int Equal to */
42
    public const EQUAL_TO = 1;
43
 
44
    /** @var int Not equal to */
45
    public const NOT_EQUAL_TO = 2;
46
 
47
    /**
48
     * Returns an array of comparison operators
49
     *
50
     * @return array
51
     */
52
    protected function get_operators(): array {
53
        $operators = [
54
            self::ANY_VALUE => get_string('filterisanyvalue', 'core_reportbuilder'),
55
            self::EQUAL_TO => get_string('filterisequalto', 'core_reportbuilder'),
56
            self::NOT_EQUAL_TO => get_string('filterisnotequalto', 'core_reportbuilder')
57
        ];
58
 
59
        return $this->filter->restrict_limited_operators($operators);
60
    }
61
 
62
    /**
63
     * Return the options for the filter as an array, to be used to populate the select input field
64
     *
65
     * @return array
66
     */
67
    protected function get_select_options(): array {
68
        return (array) $this->filter->get_options();
69
    }
70
 
71
    /**
72
     * Adds controls specific to this filter in the form.
73
     *
74
     * @param MoodleQuickForm $mform
75
     */
76
    public function setup_form(MoodleQuickForm $mform): void {
77
        $elements = [];
78
        $elements['operator'] = $mform->createElement('select', $this->name . '_operator',
79
            get_string('filterfieldoperator', 'core_reportbuilder', $this->get_header()), $this->get_operators());
80
 
81
        // If a multi-dimensional array is passed, we need to use a different element type.
82
        $options = $this->get_select_options();
83
        $element = (count($options) == count($options, COUNT_RECURSIVE) ? 'select' : 'selectgroups');
84
        $elements['value'] = $mform->createElement($element, $this->name . '_value',
85
            get_string('filterfieldvalue', 'core_reportbuilder', $this->get_header()), $options);
86
 
87
        $mform->addGroup($elements, $this->name . '_group', $this->get_header(), '', false)
88
            ->setHiddenLabel(true);
89
 
90
        $mform->hideIf($this->name . '_value', $this->name . '_operator', 'eq', self::ANY_VALUE);
91
    }
92
 
93
    /**
94
     * Return filter SQL
95
     *
96
     * Note that operators must be of type integer, while values can be integer or string.
97
     *
98
     * @param array $values
99
     * @return array array of two elements - SQL query and named parameters
100
     */
101
    public function get_sql_filter(array $values): array {
102
        $name = database::generate_param_name();
103
 
104
        $operator = $values["{$this->name}_operator"] ?? self::ANY_VALUE;
105
        $value = $values["{$this->name}_value"] ?? 0;
106
 
107
        $fieldsql = $this->filter->get_field_sql();
108
        $params = $this->filter->get_field_params();
109
 
110
        // Validate filter form values.
111
        if (!$this->validate_filter_values((int) $operator, $value)) {
112
            // Filter configuration is invalid. Ignore the filter.
113
            return ['', []];
114
        }
115
 
116
        switch ($operator) {
117
            case self::EQUAL_TO:
118
                $fieldsql .= "=:$name";
119
                $params[$name] = $value;
120
                break;
121
            case self::NOT_EQUAL_TO:
122
                $fieldsql .= "<>:$name";
123
                $params[$name] = $value;
124
                break;
125
            default:
126
                return ['', []];
127
        }
128
        return [$fieldsql, $params];
129
    }
130
 
131
    /**
132
     * Validate filter form values
133
     *
134
     * @param int|null $operator
135
     * @param mixed|null $value
136
     * @return bool
137
     */
138
    private function validate_filter_values(?int $operator, $value): bool {
139
        return !($operator === null || $value === '');
140
    }
141
 
142
    /**
143
     * Return sample filter values
144
     *
145
     * @return array
146
     */
147
    public function get_sample_values(): array {
148
        return [
149
            "{$this->name}_operator" => self::EQUAL_TO,
150
            "{$this->name}_value" => 1,
151
        ];
152
    }
153
}