Get the users that a user can make data request for.

E.g. User having a parent role and has the 'tool/dataprivacy:makedatarequestsforchildren' capability.
@param int $userid The user's ID.
@return array
181552
public static function get_children_of_user($userid) {
        global $DB;

        // Get users that the user has role assignments to.
        $allusernames = get_all_user_name_fields(true, 'u');
        $sql = "SELECT u.id, $allusernames
                  FROM {role_assignments} ra, {context} c, {user} u
                 WHERE ra.userid = :userid
                       AND ra.contextid = c.id
                       AND c.instanceid = u.id
                       AND c.contextlevel = :contextlevel";
        $params = [
            'userid' => $userid,
            'contextlevel' => CONTEXT_USER
        ];

        // The final list of users that we will return.
        $finalresults = [];

        // Our prospective list of users.
        if ($candidates = $DB->get_records_sql($sql, $params)) {
            foreach ($candidates as $key => $child) {
                $childcontext = \context_user::instance($child->id);
                if (has_capability('tool/dataprivacy:makedatarequestsforchildren', $childcontext, $userid)) {
                    $finalresults[$key] = $child;
                }
            }
        }
        return $finalresults;
    }
30734
protected function getUser4Id($userId): ?UserInterface
    {
        /** @var \PServerCore\Entity\Repository\User $userRepository */
        $userRepository = $this->entityManager->getRepository($this->entityOptions->getUser());

        return $userRepository->getUser4Id($userId);
    }
1256383
public function getWishList(int $userId)
    {
        $response = $this->handler->handle('GET', false, 'wishlist/' . $userId);

        return $this->make($response, true);
    }
834056
public function getUser($userId)
    {
        // Make sure users have been 'got' already.
        $this->getUsers();

        // In most cases by far, we'll request an ID, and we can return it here.
        if ($userId && array_key_exists($userId, $this->users)) {
            return $this->users[$userId];
        }

        // Fallback: See if we can get it by username or email address.
        try {
            if ($userEntity = $this->getRepository()->getUser($userId)) {
                return $userEntity->toArray();
            }
        } catch (TableNotFoundException $e) {
            return false;
        }

        return false;
    }
846359
public function getRolesByUser($userGuid)
    {
        if (!isset($userGuid) || $userGuid === '') {
            return [];
        }
        
        if ($userGuid instanceof User) {
            $userGuid = $userGuid->getGUID();
        }

        $query = (new Query)->select('b.*')
            ->from(['a' => $this->assignmentTable, 'b' => $this->itemTable])
            ->where('{{a}}.[[item_name]]={{b}}.[[name]]')
            ->andWhere(['a.user_guid' => (string) $userGuid])
            ->andWhere(['b.type' => Item::TYPE_ROLE]);

        $roles = [];
        foreach ($query->all($this->db) as $row) {
            $roles[$row['name']] = $this->populateItem($row);
        }
        return $roles;
    }
205073
protected function get_graders($userid) {
        // Potential graders should be active users only.
        $potentialgraders = get_enrolled_users($this->context, "mod/assign:grade", null, 'u.*', null, null, null, true);

        $graders = array();
        if (groups_get_activity_groupmode($this->get_course_module()) == SEPARATEGROUPS) {
            if ($groups = groups_get_all_groups($this->get_course()->id, $userid, $this->get_course_module()->groupingid)) {
                foreach ($groups as $group) {
                    foreach ($potentialgraders as $grader) {
                        if ($grader->id == $userid) {
                            // Do not send self.
                            continue;
                        }
                        if (groups_is_member($group->id, $grader->id)) {
                            $graders[$grader->id] = $grader;
                        }
                    }
                }
            } else {
                // User not in group, try to find graders without group.
                foreach ($potentialgraders as $grader) {
                    if ($grader->id == $userid) {
                        // Do not send self.
                        continue;
                    }
                    if (!groups_has_membership($this->get_course_module(), $grader->id)) {
                        $graders[$grader->id] = $grader;
                    }
                }
            }
        } else {
            foreach ($potentialgraders as $grader) {
                if ($grader->id == $userid) {
                    // Do not send self.
                    continue;
                }
                // Must be enrolled.
                if (is_enrolled($this->get_course_context(), $grader->id)) {
                    $graders[$grader->id] = $grader;
                }
            }
        }
        return $graders;
    }
646142
public function fetchUserForSession($userId) {
        $select = $this->select()
            ->from($this->getName(), Garp_Auth::getInstance()->getSessionColumns())
            ->where('id = ?', $userId);
        return $this->fetchRow($select);
    }
1348724
public function all($userId = null)
    {
        if (empty($this->items)) {
            return (is_null($userId)) ? parent::all() : $this->handler->retrieveAll($userId);
        }
        return $this->items;
    }
416853
public static function get_contexts_for_userid(int $userid) : contextlist {
        $sql = "SELECT id
                  FROM {context}
                 WHERE instanceid = :userid
                       AND contextlevel = :contextlevel";

        $contextlist = new contextlist();
        $contextlist->set_component('tool_dataprivacy');
        $contextlist->add_from_sql($sql, ['userid' => $userid, 'contextlevel' => CONTEXT_USER]);
        return $contextlist;
    }
124984
public function getUserQuery($userId)
    {
        $qb = $this->createQueryBuilder();
        $qb->select('*');

        if (is_numeric($userId)) {
            $qb->where('id = :userId');
        } else {
            $qb
                ->where($qb->expr()->like('username', ':userId'))
                ->orWhere('email = :userId')
            ;
        }
        $qb->setParameter('userId', $userId);

        return $qb;
    }
151881
public function getPermissionByUserId($userId)
    {
        $prefix = $this->getConfig('prefix');
        $permission = Db::name('permission')->setConnection($this->getConnection())->alias('p')
            ->join(["{$prefix}role_permission" => 'rp'], 'p.id = rp.permission_id')
            ->join(["{$prefix}user_role" => 'ur'], 'rp.role_id = ur.role_id')
            ->where('ur.user_id', $userId)->select();
        return $permission;
    }
1060388
public static function get_contexts_for_userid(int $userid) : \core_privacy\local\request\contextlist {
        $contextlist = new \core_privacy\local\request\contextlist();

        // Always add the user context so we're sure we're not dodging user keys, besides it's not costly to do so.
        $contextlist->add_user_context($userid);

        return $contextlist;
    }
338651
public function getRoles($user_id) {
        $results = $this->find('all', array(
            'conditions' => array(
                'RequestObject.foreign_key' => $user_id,
                'RequestObject.model' => USER_MODEL
            )
        ));

        if ($results) {
            // Grab the direct parents (or roles)
            $results = $this->find('all', array(
                'conditions' => array('RequestObject.id' => Hash::extract($results, '{n}.RequestObject.parent_id'))
            ));

            // Also grab the children of the parents since the user has access to them as well
            $results = array_merge($results, $this->find('all', array(
                'conditions' => array(
                    'RequestObject.parent_id' => Hash::extract($results, '{n}.RequestObject.id'),
                    'RequestObject.model' => null,
                    'RequestObject.foreign_key' => null
                )
            )));
        }

        return $results;
    }
999560
protected static function get_records($userid) {
        global $DB;

        $sql = "SELECT *
                  FROM {user_info_data} uda
                  JOIN {user_info_field} uif ON uda.fieldid = uif.id
                 WHERE uda.userid = :userid
                       AND uif.datatype = :datatype";
        $params = [
            'userid' => $userid,
            'datatype' => 'checkbox'
        ];

        return $DB->get_records_sql($sql, $params);
    }
80142
public function loadPoliciesByUserId($userId)
    {
        $data = $this->roleGateway->loadPoliciesByUserId($userId);

        $policies = $this->mapper->mapPolicies($data);

        foreach ($policies as $policy) {
            $this->limitationConverter->toSPI($policy);
        }

        return $policies;
    }
1262146
public function findAllByUser( $userId )
    {
        $select = $this->select()
                       ->join( 'user_right_x_user',
                               new Sql\Expression(
                                    '?.? = ?.? AND ?.? = ?',
                                    array(
                                        self::$tableName,
                                        'id',
                                        'user_right_x_user',
                                        'rightId',
                                        'user_right_x_user',
                                        'userId',
                                        $userId,
                                    ),
                                    array(
                                        Sql\Expression::TYPE_IDENTIFIER,
                                        Sql\Expression::TYPE_IDENTIFIER,
                                        Sql\Expression::TYPE_IDENTIFIER,
                                        Sql\Expression::TYPE_IDENTIFIER,
                                        Sql\Expression::TYPE_IDENTIFIER,
                                        Sql\Expression::TYPE_IDENTIFIER,
                                        Sql\Expression::TYPE_VALUE,
                                    )
                               ),
                               array(
                                   'granted' => 'userId'
                               ),
                               Sql\Select::JOIN_LEFT )
                        ->order( array(
                            'group'     => 'ASC',
                            'resource'  => 'ASC',
                            'privilege' => 'ASC',
                        ) );

        /* @var $result \Zend\Db\Adapter\Driver\ResultInterface */

        $return = array();
        $result = $this->sql()
                       ->prepareStatementForSqlObject( $select )
                       ->execute();

        foreach ( $result as $row )
        {
            $return[] = $this->selected( $row );
        }

        return $return;
    }
1104263
public function listMonetaryAccounts($userId)
    {
        $monetaryAccounts = $this->client->get($this->getResourceEndpoint($userId));

        return $monetaryAccounts;
    }