Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 42... Línea 42...
42
    public const EQUAL_TO = 1;
42
    public const EQUAL_TO = 1;
Línea 43... Línea 43...
43
 
43
 
44
    /** @var int Not equal to */
44
    /** @var int Not equal to */
Línea -... Línea 45...
-
 
45
    public const NOT_EQUAL_TO = 2;
-
 
46
 
-
 
47
    /** @var int Value to indicate "Any value" for the simplified filter options  */
45
    public const NOT_EQUAL_TO = 2;
48
    private const OPTION_ANY_VALUE = -124567;
46
 
49
 
47
    /**
50
    /**
48
     * Returns an array of comparison operators
51
     * Returns an array of comparison operators
49
     *
52
     *
Línea 63... Línea 66...
63
     * Return the options for the filter as an array, to be used to populate the select input field
66
     * Return the options for the filter as an array, to be used to populate the select input field
64
     *
67
     *
65
     * @return array
68
     * @return array
66
     */
69
     */
67
    protected function get_select_options(): array {
70
    protected function get_select_options(): array {
-
 
71
        static $options = [];
-
 
72
 
-
 
73
        if (!array_key_exists($this->name, $options)) {
68
        return (array) $this->filter->get_options();
74
            $options[$this->name] = (array) $this->filter->get_options();
-
 
75
        }
-
 
76
 
-
 
77
        return $options[$this->name];
69
    }
78
    }
Línea 70... Línea 79...
70
 
79
 
71
    /**
80
    /**
72
     * Adds controls specific to this filter in the form.
81
     * Adds controls specific to this filter in the form.
73
     *
82
     *
74
     * @param MoodleQuickForm $mform
83
     * @param MoodleQuickForm $mform
75
     */
84
     */
76
    public function setup_form(MoodleQuickForm $mform): void {
-
 
77
        $elements = [];
85
    public function setup_form(MoodleQuickForm $mform): void {
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.
86
        $operators = $this->get_operators();
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',
-
 
Línea -... Línea 87...
-
 
87
        $options = $this->get_select_options();
-
 
88
 
-
 
89
        // If a multidimensional array is passed, we need to use a different element type.
-
 
90
        $optioncountrecursive = count($options, COUNT_RECURSIVE);
-
 
91
        $element = (count($options) === $optioncountrecursive ? 'select' : 'selectgroups');
-
 
92
 
-
 
93
        // If operators are unrestricted, and we have upto two options, then simplify the filter to list only those.
-
 
94
        if (count($operators) === 3 && $optioncountrecursive <= 2) {
-
 
95
            $mform->addElement('hidden', "{$this->name}_operator");
-
 
96
            $mform->setType("{$this->name}_operator", PARAM_INT);
-
 
97
            $mform->setConstant("{$this->name}_operator", self::EQUAL_TO);
-
 
98
 
-
 
99
            $mform->addElement(
-
 
100
                $element,
-
 
101
                "{$this->name}_value",
-
 
102
                get_string('filterfieldvalue', 'core_reportbuilder', $this->get_header()),
-
 
103
                [self::OPTION_ANY_VALUE => $operators[self::ANY_VALUE]] + $options,
-
 
104
            )->setHiddenLabel(true);
-
 
105
        } else {
-
 
106
            $elements = [];
-
 
107
 
-
 
108
            $elements[] = $mform->createElement(
-
 
109
                'select',
-
 
110
                "{$this->name}_operator",
-
 
111
                get_string('filterfieldoperator', 'core_reportbuilder', $this->get_header()),
-
 
112
                $operators,
-
 
113
            );
-
 
114
 
-
 
115
            $elements[] = $mform->createElement(
-
 
116
                $element,
-
 
117
                "{$this->name}_value",
-
 
118
                get_string('filterfieldvalue', 'core_reportbuilder', $this->get_header()),
-
 
119
                $options,
85
            get_string('filterfieldvalue', 'core_reportbuilder', $this->get_header()), $options);
120
            );
86
 
121
 
Línea 87... Línea 122...
87
        $mform->addGroup($elements, $this->name . '_group', $this->get_header(), '', false)
122
            $mform->addGroup($elements, "{$this->name}_group", $this->get_header(), '', false)
-
 
123
                ->setHiddenLabel(true);
88
            ->setHiddenLabel(true);
124
 
Línea 89... Línea 125...
89
 
125
            $mform->hideIf("{$this->name}_value", "{$this->name}_operator", 'eq', self::ANY_VALUE);
90
        $mform->hideIf($this->name . '_value', $this->name . '_operator', 'eq', self::ANY_VALUE);
126
        }
91
    }
127
    }
Línea 99... Línea 135...
99
     * @return array array of two elements - SQL query and named parameters
135
     * @return array array of two elements - SQL query and named parameters
100
     */
136
     */
101
    public function get_sql_filter(array $values): array {
137
    public function get_sql_filter(array $values): array {
102
        $name = database::generate_param_name();
138
        $name = database::generate_param_name();
Línea 103... Línea 139...
103
 
139
 
104
        $operator = $values["{$this->name}_operator"] ?? self::ANY_VALUE;
140
        $operator = (int) ($values["{$this->name}_operator"] ?? self::ANY_VALUE);
Línea 105... Línea 141...
105
        $value = $values["{$this->name}_value"] ?? 0;
141
        $value = (string) ($values["{$this->name}_value"] ?? self::OPTION_ANY_VALUE);
106
 
142
 
Línea -... Línea 143...
-
 
143
        $fieldsql = $this->filter->get_field_sql();
-
 
144
        $params = $this->filter->get_field_params();
-
 
145
 
-
 
146
        // Get available options, if multidimensional then flatten the array.
-
 
147
        $options = $this->get_select_options();
-
 
148
        if (count($options) !== count($options, COUNT_RECURSIVE)) {
107
        $fieldsql = $this->filter->get_field_sql();
149
            $options = array_merge(...array_values($options));
108
        $params = $this->filter->get_field_params();
150
        }
109
 
-
 
110
        // Validate filter form values.
151
 
111
        if (!$this->validate_filter_values((int) $operator, $value)) {
152
        // Validate filter form values.
Línea 112... Línea 153...
112
            // Filter configuration is invalid. Ignore the filter.
153
        if ($operator === self::ANY_VALUE || !array_key_exists($value, $options)) {
113
            return ['', []];
154
            return ['', []];
Línea 127... Línea 168...
127
        }
168
        }
128
        return [$fieldsql, $params];
169
        return [$fieldsql, $params];
129
    }
170
    }
Línea 130... Línea 171...
130
 
171
 
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
    /**
172
    /**
143
     * Return sample filter values
173
     * Return sample filter values
144
     *
174
     *
145
     * @return array
175
     * @return array
146
     */
176
     */