Create an approved userlist from a userlist.

@param   userlist        $userlist The source list
@return  approved_userlist   The newly created approved userlist.
975655
public static function create_from_userlist(userlist $userlist) : approved_userlist {
        $newlist = new static($userlist->get_context(), $userlist->get_component(), $userlist->get_userids());

        return $newlist;
    }
387467
public static function delete_data_for_users(approved_userlist $userlist) {

        $context = $userlist->get_context();

        if ($context instanceof \context_user) {
            \core_userkey\privacy\provider::delete_userkeys('rss', $context->instanceid);
        }
    }
1352657
public static function delete_data_for_users(approved_userlist $userlist) {
        $context = $userlist->get_context();

        if ($context instanceof \context_block && ($blockinstance = static::get_instance_from_context($context))) {
            blocks_delete_instance($blockinstance);
        }
    }
965992
public static function get_users_in_context(userlist $userlist) {

        if (empty($userlist)) {
            return;
        }

        $context = $userlist->get_context();

        // Include users who created or modified role capabilities.
        $sql = "SELECT modifierid as userid
                  FROM {role_capabilities}
                 WHERE contextid = :contextid";

        $params = [
            'contextid' => $context->id
        ];

        $userlist->add_from_sql('userid', $sql, $params);

        // Include users that have a role assigned to them.
        $sql = "SELECT userid
                  FROM {role_assignments}
                 WHERE contextid = :contextid";

        $userlist->add_from_sql('userid', $sql, $params);

        // Include users who created or modified the role assignment.
        // Differentiate and exclude special cases where tool_cohortroles adds records through the
        // "Assign user roles to cohort" feature into the role_assignments table.
        // These records should be separately processed in tool_cohortroles.
        $sql = "SELECT modifierid as userid
                  FROM {role_assignments}
                 WHERE contextid = :contextid
                       AND component != 'tool_cohortroles'";

        $userlist->add_from_sql('userid', $sql, $params);
    }
487503
public static function get_users_in_context(\core_privacy\local\request\userlist $userlist) {
        global $DB;

        $context = $userlist->get_context();
        if ($context->contextlevel != CONTEXT_BLOCK) {
            return;
        }

        $params = ['docked' => 'docked_block_instance_' . $context->instanceid,
                   'hidden' => 'block' . $context->instanceid . 'hidden'];

        $sql = "SELECT userid
                  FROM {user_preferences}
                 WHERE name = :hidden OR name = :docked";
        $userlist->add_from_sql('userid', $sql, $params);
    }
1236865
public static function get_users_in_context(userlist $userlist) {
        // This block doesn't know who information is stored against unless it
        // is at the user context.
        $context = $userlist->get_context();

        if (!$context instanceof \context_block) {
            return;
        }

        $sql = "SELECT bpc.instanceid AS userid
                  FROM {block_instances} bi
                  JOIN {context} bpc ON bpc.id = bi.parentcontextid
                 WHERE bi.blockname = 'html'
                   AND bpc.contextlevel = :contextuser
                   AND bi.id = :blockinstanceid";

        $params = [
            'contextuser' => CONTEXT_USER,
            'blockinstanceid' => $context->instanceid
        ];

        $userlist->add_from_sql('userid', $sql, $params);
    }
352151
public static function delete_data_for_users(approved_userlist $userlist) {
        global $DB;

        $context = $userlist->get_context();

        if ($context instanceof \context_user) {
            $DB->delete_records('repository_instances', ['userid' => $context->instanceid]);
        }
    }
574749
public static function delete_data_for_users(\core_privacy\local\request\approved_userlist $userlist) {
        $interface = \tool_log\local\privacy\logstore_userlist_provider::class;
        static::call_subplugins_method_with_args('delete_data_for_userlist', [$userlist], $interface);
    }
783050
public static function delete_data_for_users(approved_userlist $userlist) {
        global $DB;

        $context = $userlist->get_context();

        if ($context instanceof \context_course) {
            list($usersql, $userparams) = $DB->get_in_or_equal($userlist->get_userids(), SQL_PARAMS_NAMED);

            $sql = "SELECT ue.id
                      FROM {user_enrolments} ue
                      JOIN {enrol} e ON e.id = ue.enrolid
                     WHERE e.courseid = :courseid
                           AND ue.userid {$usersql}";

            $params = ['courseid' => $context->instanceid] + $userparams;
            $enrolsids = $DB->get_fieldset_sql($sql, $params);

            if (!empty($enrolsids)) {
                list($insql, $inparams) = $DB->get_in_or_equal($enrolsids, SQL_PARAMS_NAMED);
                static::delete_user_data($insql, $inparams);
            }
        }
    }
338847
public static function get_users_in_context(userlist $userlist) {
        $context = $userlist->get_context();

        if (!is_a($context, \context_module::class)) {
            return;
        }

        $params = [
            'lesson' => 'lesson',
            'modulelevel' => CONTEXT_MODULE,
            'contextid' => $context->id,
        ];

        // Mapping of lesson tables which may contain user data.
        $joins = [
            'lesson_attempts',
            'lesson_branch',
            'lesson_grades',
            'lesson_overrides',
            'lesson_timer',
        ];

        foreach ($joins as $join) {
            $sql = "
                SELECT lx.userid
                  FROM {lesson} l
                  JOIN {modules} m
                    ON m.name = :lesson
                  JOIN {course_modules} cm
                    ON cm.instance = l.id
                   AND cm.module = m.id
                  JOIN {context} ctx
                    ON ctx.instanceid = cm.id
                   AND ctx.contextlevel = :modulelevel
                  JOIN {{$join}} lx
                    ON lx.lessonid = l.id
                 WHERE ctx.id = :contextid";

            $userlist->add_from_sql('userid', $sql, $params);
        }
    }
335832
public static function delete_data_for_users(approved_userlist $userlist) {
        global $DB;
        $context = $userlist->get_context();

        if (!is_a($context, \context_module::class)) {
            return;
        }

        // Prepare SQL to gather all completed IDs.
        $userids = $userlist->get_userids();
        list($insql, $inparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);

        $sql = "SELECT ss.id
                  FROM {%s} ss
                  JOIN {modules} m
                    ON m.name = 'scorm'
                  JOIN {course_modules} cm
                    ON cm.instance = ss.scormid
                   AND cm.module = m.id
                  JOIN {context} ctx
                    ON ctx.instanceid = cm.id
                 WHERE ctx.id = :contextid
                   AND ss.userid $insql";
        $params = array_merge($inparams, ['contextid' => $context->id]);

        static::delete_data('scorm_scoes_track', $sql, $params);
        static::delete_data('scorm_aicc_session', $sql, $params);
    }
938182
public static function get_users_in_context(userlist $userlist) {
        $context = $userlist->get_context();
        $params = ['contextid' => $context->id];

        // Add users who have modified the frameworks and related data in the context.
        $sql = "
             SELECT cf.usermodified
               FROM {" . competency_framework::TABLE . "} cf
              WHERE cf.contextid = :contextid";
        $userlist->add_from_sql('usermodified', $sql, $params);

        $sql = "
             SELECT c.usermodified
               FROM {" . competency_framework::TABLE . "} cf
               JOIN {" . competency::TABLE . "} c
                 ON c.competencyframeworkid = cf.id
              WHERE cf.contextid = :contextid";
        $userlist->add_from_sql('usermodified', $sql, $params);

        $sql = "
             SELECT cr.usermodified
               FROM {" . competency_framework::TABLE . "} cf
               JOIN {" . competency::TABLE . "} c
                 ON c.competencyframeworkid = cf.id
               JOIN {" . related_competency::TABLE . "} cr
                 ON cr.competencyid = c.id
              WHERE cf.contextid = :contextid";
        $userlist->add_from_sql('usermodified', $sql, $params);

        // Add users who have modified the templates and related data in the context.
        $sql = "
             SELECT tpl.usermodified
               FROM {" . template::TABLE . "} tpl
              WHERE tpl.contextid = :contextid";
        $userlist->add_from_sql('usermodified', $sql, $params);

        $sql = "
             SELECT tch.usermodified
               FROM {" . template::TABLE . "} tpl
               JOIN {" . template_cohort::TABLE . "} tch
                 ON tch.templateid = tpl.id
              WHERE tpl.contextid = :contextid";
        $userlist->add_from_sql('usermodified', $sql, $params);

        $sql = "
             SELECT tc.usermodified
               FROM {" . template::TABLE . "} tpl
               JOIN {" . template_competency::TABLE . "} tc
                 ON tc.templateid = tpl.id
              WHERE tpl.contextid = :contextid";
        $userlist->add_from_sql('usermodified', $sql, $params);

        // Add users if userlist is in course context.
        if (is_a($context, \context_course::class)) {
            $params = ['courseid' => $context->instanceid];

            $sql = "
                 SELECT cc.usermodified
                   FROM {" . course_competency::TABLE . "} cc
                  WHERE cc.courseid = :courseid";
            $userlist->add_from_sql('usermodified', $sql, $params);

            $sql = "
                 SELECT ccs.usermodified
                   FROM {" . course_competency_settings::TABLE . "} ccs
                  WHERE ccs.courseid = :courseid";
            $userlist->add_from_sql('usermodified', $sql, $params);

            $sql = "
                 SELECT ucc.userid, ucc.usermodified
                   FROM {" . user_competency_course::TABLE . "} ucc
                  WHERE ucc.courseid = :courseid";
            $userlist->add_from_sql('userid', $sql, $params);
            $userlist->add_from_sql('usermodified', $sql, $params);

        } else if (is_a($context, \context_module::class)) {
            // Add users if userlist is in module context.
            $params = ['moduleid' => $context->instanceid];

            $sql = "
                 SELECT cmc.usermodified
                   FROM {" . course_module_competency::TABLE . "} cmc
                  WHERE cmc.cmid = :moduleid";
            $userlist->add_from_sql('usermodified', $sql, $params);

        } else if (is_a($context, \context_user::class)) {
            $params = ['userid' => $context->instanceid];

            // Add users through plan related data.
            $sql = "
                 SELECT p.userid, p.usermodified, p.reviewerid
                   FROM {" . plan::TABLE . "} p
                  WHERE p.userid = :userid";
            $userlist->add_from_sql('userid', $sql, $params);
            $userlist->add_from_sql('usermodified', $sql, $params);
            $userlist->add_from_sql('reviewerid', $sql, $params);

            $sql = "
                 SELECT pc.usermodified
                   FROM {" . plan::TABLE . "} p
                   JOIN {" . plan_competency::TABLE . "} pc
                     ON pc.planid = p.id
                  WHERE p.userid = :userid";
            $userlist->add_from_sql('usermodified', $sql, $params);

            $sql = "
                 SELECT upc.usermodified
                   FROM {" . user_competency_plan::TABLE . "} upc
                  WHERE upc.userid = :userid";
            $userlist->add_from_sql('usermodified', $sql, $params);

            // Add users through competency data.
            $sql = "
                 SELECT uc.userid, uc.usermodified, uc.reviewerid
                   FROM {" . user_competency::TABLE . "} uc
                  WHERE uc.userid = :userid";
            $userlist->add_from_sql('userid', $sql, $params);
            $userlist->add_from_sql('usermodified', $sql, $params);
            $userlist->add_from_sql('reviewerid', $sql, $params);

            $sql = "
                 SELECT e.usermodified, e.actionuserid
                   FROM {" . user_competency::TABLE . "} uc
                   JOIN {" . evidence::TABLE . "} e
                     ON e.usercompetencyid = uc.id
                  WHERE uc.userid = :userid";
            $userlist->add_from_sql('usermodified', $sql, $params);
            $userlist->add_from_sql('actionuserid', $sql, $params);

            // Add users through evidence data.
            $sql = "
                 SELECT ue.userid, ue.usermodified
                   FROM {" . user_evidence::TABLE . "} ue
                  WHERE ue.userid = :userid";
            $userlist->add_from_sql('userid', $sql, $params);
            $userlist->add_from_sql('usermodified', $sql, $params);

            $sql = "
                 SELECT ue.usermodified
                   FROM {" . user_evidence::TABLE . "} ue
                   JOIN {" . user_evidence_competency::TABLE . "} uec
                     ON uec.userevidenceid = ue.id
                  WHERE ue.userid = :userid";
            $userlist->add_from_sql('usermodified', $sql, $params);
        }

        // Add users who commented in the context.
        // Note: Comment component must be competency and not core_competency.
        \core_comment\privacy\provider::get_users_in_context_from_sql(
                $userlist, 'com', 'competency', 'plan', $context->id);

        \core_comment\privacy\provider::get_users_in_context_from_sql(
                $userlist, 'com', 'competency', 'user_competency', $context->id);
    }
1027953
public static function add_contexts_for_userid(contextlist $contextlist, $userid) {
        $sql = "
            SELECT ctx.id
              FROM {context} ctx
              JOIN {log} l
                ON (l.cmid = 0 AND l.course = ctx.instanceid AND ctx.contextlevel = :courselevel)
                OR (l.cmid > 0 AND l.cmid = ctx.instanceid AND ctx.contextlevel = :modulelevel)
                OR (l.course <= 0 AND ctx.id = :syscontextid)
             WHERE l.userid = :userid";
        $params = [
            'courselevel' => CONTEXT_COURSE,
            'modulelevel' => CONTEXT_MODULE,
            'syscontextid' => SYSCONTEXTID,
            'userid' => $userid,
        ];
        $contextlist->add_from_sql($sql, $params);
    }
1092271
protected static function approved_contextlist_contains_my_context(approved_contextlist $contextlist) {
        $userid = $contextlist->get_user()->id;
        foreach ($contextlist->get_contexts() as $context) {
            if ($context->contextlevel == CONTEXT_USER && $context->instanceid == $userid) {
                return true;
            }
        }
        return false;
    }
535725
public static function get_users_in_context(userlist $userlist) {
        $context = $userlist->get_context();

        if (!$context instanceof \context_course) {
            return;
        }

        static::get_group_members_in_context($userlist, '');
    }
387551
public static function delete_data_for_users(approved_userlist $userlist) {
        global $DB;

        // Don't remove data from role_capabilities.
        // Because this data affects the whole Moodle, there are override capabilities.
        // Don't belong to the modifier user.
        $context = $userlist->get_context();
        $userids = $userlist->get_userids();

        if (empty($userids)) {
            return;
        }
        list($usersql, $userparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
        $params = ['contextid' => $context->id] + $userparams;

        // Remove data from role_assignments.
        $DB->delete_records_select('role_assignments',
            "contextid = :contextid AND userid {$usersql}", $params);
    }
795850
public static function get_users_in_context(userlist $userlist) {
        $context = $userlist->get_context();

        if (!is_a($context, \context_module::class)) {
            return;
        }

        $sql = "SELECT ss.userid
                  FROM {%s} ss
                  JOIN {modules} m
                    ON m.name = 'scorm'
                  JOIN {course_modules} cm
                    ON cm.instance = ss.scormid
                   AND cm.module = m.id
                  JOIN {context} ctx
                    ON ctx.instanceid = cm.id
                   AND ctx.contextlevel = :modlevel
                 WHERE ctx.id = :contextid";

        $params = ['modlevel' => CONTEXT_MODULE, 'contextid' => $context->id];

        $userlist->add_from_sql('userid', sprintf($sql, 'scorm_scoes_track'), $params);
        $userlist->add_from_sql('userid', sprintf($sql, 'scorm_aicc_session'), $params);
    }