Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
namespace qbank_viewcreator;
18
 
19
use core\output\datafilter;
20
use core_question\local\bank\condition;
21
 
22
/**
23
 * Abstract class for conditions filtering by user.
24
 *
25
 * @package   qbank_viewcreator
26
 * @copyright 2025 onwards Catalyst IT EU {@link https://catalyst-eu.net}
27
 * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
28
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
abstract class user_condition extends condition {
31
 
32
    /**
33
     * Return the alias for the instance of the user table to filter on.
34
     *
35
     * @return string
36
     */
37
    abstract protected static function get_table_alias(): string;
38
 
39
    #[\Override]
40
    public function get_filter_class() {
41
        return 'core/datafilter/filtertypes/keyword';
42
    }
43
 
44
    #[\Override]
45
    public static function build_query_from_filter(array $filter): array {
46
        global $DB;
47
 
48
        $conditions = [];
49
        $params = [];
50
        $notlike = $filter['jointype'] === datafilter::JOINTYPE_NONE;
51
        $tablealias = static::get_table_alias();
52
        $allnames = array_map(fn($field) => "{$tablealias}.{$field}", \core_user\fields::get_name_fields());
53
        $allnames = $DB->sql_concat(...$allnames);
54
        $conditionkey = static::get_condition_key();
55
        foreach ($filter['values'] as $key => $value) {
56
            $params["{$conditionkey}{$key}"] = "%$value%";
57
            $conditions[] = $DB->sql_like($allnames, ":{$conditionkey}{$key}", casesensitive: false, notlike: $notlike);
58
        }
59
        $delimiter = $filter['jointype'] === datafilter::JOINTYPE_ANY ? ' OR ' : ' AND ';
60
        return [
61
            implode($delimiter, $conditions),
62
            $params,
63
        ];
64
    }
65
}