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_viewquestionname;
18
 
19
use core\output\datafilter;
20
 
21
/**
22
 * Filter condition for filtering on the question name
23
 *
24
 * @package   qbank_viewquestionname
25
 * @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
26
 * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
27
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class question_name_condition extends \core_question\local\bank\condition {
30
    #[\Override]
31
    public function get_title() {
32
        return get_string('questionnamecondition', 'qbank_viewquestionname');
33
    }
34
 
35
    #[\Override]
36
    public static function get_condition_key() {
37
        return 'questionname';
38
    }
39
 
40
    #[\Override]
41
    public function get_filter_class() {
42
        return 'core/datafilter/filtertypes/keyword';
43
    }
44
 
45
    /**
46
     * Return an SQL condition and parameters for filtering on q.name.
47
     *
48
     * This will search for the terms provided anywhere in the name.
49
     *
50
     * @param array $filter
51
     * @return array
52
     */
53
    public static function build_query_from_filter(array $filter): array {
54
        global $DB;
55
 
56
        $conditions = [];
57
        $params = [];
58
        $notlike = $filter['jointype'] === datafilter::JOINTYPE_NONE;
59
        foreach ($filter['values'] as $key => $value) {
60
            $params["questionname{$key}"] = "%{$value}%";
61
            $conditions[] = $DB->sql_like('q.name', ":questionname{$key}", casesensitive: false, notlike: $notlike);
62
        }
63
        $delimiter = $filter['jointype'] === datafilter::JOINTYPE_ANY ? ' OR ' : ' AND ';
64
        return [
65
            implode($delimiter, $conditions),
66
            $params,
67
        ];
68
    }
69
}