1<?php
2// This file is part of Moodle - https://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/**
18 * Provides {@link testable_user_selector} class.
19 *
20 * @package     core_user
21 * @subpackage  fixtures
22 * @category    test
23 * @copyright   2018 David Mudrák <david@moodle.com>
24 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Testable subclass of the user selector base class.
31 *
32 * @copyright 2018 David Mudrák <david@moodle.com>
33 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class testable_user_selector extends user_selector_base {
36
37    /**
38     * Basic implementation of the users finder.
39     *
40     * @param string $search
41     * @return array of (string)optgroupname => array of users
42     */
43    public function find_users($search) {
44        global $DB;
45
46        list($wherecondition, $whereparams) = $this->search_sql($search, 'u');
47        list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
48        $params = array_merge($whereparams, $sortparams);
49        $fields = $this->required_fields_sql('u');
50
51        $sql = "SELECT $fields
52                  FROM {user} u
53                 WHERE $wherecondition
54              ORDER BY $sort";
55
56        $found = $DB->get_records_sql($sql, $params);
57
58        if (empty($found)) {
59            return [];
60        }
61
62        return [get_string('potusers', 'core_role') => $found];
63    }
64
65}
66