MDL-63690 core_blog: Update providers for userlist deletion.
[moodle.git] / privacy / classes / local / request / userlist.php
CommitLineData
8ba35e31
AN
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/**
18 * List of users from the Privacy API Search functions.
19 *
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
23 */
24
25namespace core_privacy\local\request;
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * List of users from the Privacy API Search functions.
31 *
32 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class userlist extends userlist_base {
36
37 /**
38 * Add a set of users from SQL.
39 *
40 * The SQL should only return a list of user IDs.
41 *
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
45 * @return $this
46 */
47 public function add_from_sql(string $fieldname, string $sql, array $params) : userlist {
48 global $DB;
49
50 // Able to guess a field name.
51 $wrapper = "
52 SELECT DISTINCT u.id
53 FROM {user} u
54 JOIN ({$sql}) target ON u.id = target.{$fieldname}";
55
56 $users = $DB->get_records_sql($wrapper, $params);
57 $this->add_userids(array_keys($users));
58
59 return $this;
60 }
61
62 /**
63 * Adds the user user for a given user.
64 *
65 * @param int $userid
66 * @return $this
67 */
68 public function add_user(int $userid) : userlist {
69 $this->add_users([$userid]);
70
71 return $this;
72 }
73
74 /**
75 * Adds the user users for given users.
76 *
77 * @param int[] $userids
78 * @return $this
79 */
80 public function add_users(array $userids) : userlist {
81 global $DB;
82
83 list($useridsql, $useridparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
84 $sql = "SELECT DISTINCT u.id
85 FROM {user} u
86 WHERE u.id {$useridsql}";
87 $this->add_from_sql('id', $sql, $useridparams);
88
89 return $this;
90 }
91
92 /**
93 * Sets the component for this userlist.
94 *
95 * @param string $component the frankenstyle component name.
96 * @return $this
97 */
98 public function set_component($component) : userlist_base {
99 parent::set_component($component);
100
101 return $this;
102 }
103}