Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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  
  25  namespace core_privacy\local\request;
  26  
  27  defined('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   */
  35  class 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          if (!empty($userids)) {
  84              list($useridsql, $useridparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
  85              $sql = "SELECT DISTINCT u.id
  86                        FROM {user} u
  87                       WHERE u.id {$useridsql}";
  88              $this->add_from_sql('id', $sql, $useridparams);
  89          }
  90          return $this;
  91      }
  92  
  93      /**
  94       * Sets the component for this userlist.
  95       *
  96       * @param   string  $component the frankenstyle component name.
  97       * @return  $this
  98       */
  99      public function set_component($component) : userlist_base {
 100          parent::set_component($component);
 101  
 102          return $this;
 103      }
 104  }