2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * List of users from the Privacy API Search functions.
20 * @package core_privacy
21 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_privacy\local\request;
27 defined('MOODLE_INTERNAL') || die();
30 * List of users from the Privacy API Search functions.
32 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class userlist extends userlist_base {
38 * Add a set of users from SQL.
40 * The SQL should only return a list of user IDs.
42 * @param string $fieldname The name of the field which holds the user id
43 * @param string $sql The SQL which will fetch the list of * user IDs
44 * @param array $params The set of SQL parameters
47 public function add_from_sql(string $fieldname, string $sql, array $params) : userlist {
50 // Able to guess a field name.
54 JOIN ({$sql}) target ON u.id = target.{$fieldname}";
56 $users = $DB->get_records_sql($wrapper, $params);
57 $this->add_userids(array_keys($users));
63 * Adds the user user for a given user.
68 public function add_user(int $userid) : userlist {
69 $this->add_users([$userid]);
75 * Adds the user users for given users.
77 * @param int[] $userids
80 public function add_users(array $userids) : userlist {
83 if (!empty($userids)) {
84 list($useridsql, $useridparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
85 $sql = "SELECT DISTINCT u.id
87 WHERE u.id {$useridsql}";
88 $this->add_from_sql('id', $sql, $useridparams);
94 * Sets the component for this userlist.
96 * @param string $component the frankenstyle component name.
99 public function set_component($component) : userlist_base {
100 parent::set_component($component);