Add contexts that contain user information for the specified user.

@param contextlist $contextlist The contextlist to add the contexts to.
@param int $userid The user to find the contexts for.
@return void
356542
public static function add_contexts_for_userid(contextlist $contextlist, $userid) {
        list($db, $table) = static::get_database_and_table();
        if (!$db || !$table) {
            return;
        }

        $sql = 'userid = :userid1 OR relateduserid = :userid2 OR realuserid = :userid3';
        $params = ['userid1' => $userid, 'userid2' => $userid, 'userid3' => $userid];
        $contextids = $db->get_fieldset_select($table, 'DISTINCT contextid', $sql, $params);
        if (empty($contextids)) {
            return;
        }

        $sql = implode(' UNION ', array_map(function($id) use ($db) {
            return 'SELECT ' . $id . $db->sql_null_from_clause();
        }, $contextids));
        $contextlist->add_from_sql($sql, []);
    }
86424
public static function get_contexts_for_userid(int $userid) : \core_privacy\local\request\contextlist {
        // We cannot associate files with a particular user as it would require to know all the details about
        // the source file, which only the component owning it knows about. And, the 'usermodified' attribute
        // of the conversion class is not an identifier of data ownership, merely that this user was logged in
        // when the conversion was requested. As such, as the owning component should declare the source file,
        // and we can't make sense of 'usermodified', we will not be reporting anything here. Also note that
        // the clean up task will ensure that whenever a source file is delete, its conversions also are.
        return new \core_privacy\local\request\contextlist();
    }
1229845
public static function get_contexts_for_userid(int $userid) : contextlist {
        global $DB;
        $contextlist = new \core_privacy\local\request\contextlist();

        // Find the contexts of the frameworks, and related data, modified by the user.
        $sql = "
             SELECT DISTINCT ctx.id
               FROM {context} ctx
               JOIN {" . competency_framework::TABLE . "} cf
                 ON cf.contextid = ctx.id
          LEFT JOIN {" . competency::TABLE . "} c
                 ON c.competencyframeworkid = cf.id
          LEFT JOIN {" . related_competency::TABLE . "} cr
                 ON cr.competencyid = c.id
              WHERE cf.usermodified = :userid1
                 OR c.usermodified = :userid2
                 OR cr.usermodified = :userid3";
        $params = ['userid1' => $userid, 'userid2' => $userid, 'userid3' => $userid];
        $contextlist->add_from_sql($sql, $params);

        // Find the contexts of the templates, and related data, modified by the user.
        $sql = "
             SELECT DISTINCT ctx.id
               FROM {context} ctx
               JOIN {" . template::TABLE . "} tpl
                 ON tpl.contextid = ctx.id
          LEFT JOIN {" . template_cohort::TABLE . "} tch
                 ON tch.templateid = tpl.id
                AND tch.usermodified = :userid2
          LEFT JOIN {" . template_competency::TABLE . "} tc
                 ON tc.templateid = tpl.id
                AND tc.usermodified = :userid3
              WHERE tpl.usermodified = :userid1
                 OR tch.id IS NOT NULL
                 OR tc.id IS NOT NULL";
        $params = ['userid1' => $userid, 'userid2' => $userid, 'userid3' => $userid];
        $contextlist->add_from_sql($sql, $params);

        // Find the possible course contexts.
        $sql = "
             SELECT DISTINCT ctx.id
               FROM {" . course_competency::TABLE . "} cc
               JOIN {context} ctx
                 ON ctx.instanceid = cc.courseid
                AND ctx.contextlevel = :courselevel
              WHERE cc.usermodified = :userid";
        $params = ['courselevel' => CONTEXT_COURSE, 'userid' => $userid];
        $contextlist->add_from_sql($sql, $params);

        $sql = "
             SELECT DISTINCT ctx.id
               FROM {" . course_competency_settings::TABLE . "} ccs
               JOIN {context} ctx
                 ON ctx.instanceid = ccs.courseid
                AND ctx.contextlevel = :courselevel
              WHERE ccs.usermodified = :userid";
        $params = ['courselevel' => CONTEXT_COURSE, 'userid' => $userid];
        $contextlist->add_from_sql($sql, $params);

        $sql = "
             SELECT DISTINCT ctx.id
               FROM {" . user_competency_course::TABLE . "} ucc
               JOIN {context} ctx
                 ON ctx.instanceid = ucc.courseid
                AND ctx.contextlevel = :courselevel
              WHERE ucc.usermodified = :userid";
        $params = ['courselevel' => CONTEXT_COURSE, 'userid' => $userid];
        $contextlist->add_from_sql($sql, $params);

        // Find the possible module contexts.
        $sql = "
             SELECT DISTINCT ctx.id
               FROM {" . course_module_competency::TABLE . "} cmc
               JOIN {context} ctx
                 ON ctx.instanceid = cmc.cmid
                AND ctx.contextlevel = :modulelevel
              WHERE cmc.usermodified = :userid";
        $params = ['modulelevel' => CONTEXT_MODULE, 'userid' => $userid];
        $contextlist->add_from_sql($sql, $params);

        // Add user contexts through usermodified/reviewing of plan related data.
        $sql = "
             SELECT DISTINCT ctx.id
               FROM {" . plan::TABLE . "} p
               JOIN {context} ctx
                 ON ctx.instanceid = p.userid
                AND ctx.contextlevel = :userlevel
          LEFT JOIN {" . plan_competency::TABLE . "} pc
                 ON pc.planid = p.id
                AND pc.usermodified = :userid3
          LEFT JOIN {" . user_competency_plan::TABLE . "} upc
                 ON upc.planid = p.id
                AND upc.usermodified = :userid4
              WHERE p.usermodified = :userid1
                 OR p.reviewerid = :userid2
                 OR pc.id IS NOT NULL
                 OR upc.id IS NOT NULL";
        $params = [
            'userlevel' => CONTEXT_USER,
            'userid1' => $userid,
            'userid2' => $userid,
            'userid3' => $userid,
            'userid4' => $userid,
        ];
        $contextlist->add_from_sql($sql, $params);

        // Add user contexts through usermodified/reviewing of competency data.
        $sql = "
             SELECT DISTINCT ctx.id
               FROM {context} ctx
          LEFT JOIN {" . user_competency::TABLE . "} uc
                 ON uc.userid = ctx.instanceid
                AND ctx.contextlevel = :userlevel1
          LEFT JOIN {" . evidence::TABLE . "} e
                 ON e.usercompetencyid = uc.id
                AND (e.usermodified = :userid3 OR e.actionuserid = :userid4)
          LEFT JOIN {" . user_evidence::TABLE . "} ue
                 ON ue.userid = ctx.instanceid
                AND ctx.contextlevel = :userlevel2
                AND ue.usermodified = :userid5
          LEFT JOIN {" . user_evidence_competency::TABLE . "} uec
                 ON uec.userevidenceid = ue.id
                AND uec.usermodified = :userid6
              WHERE uc.usermodified = :userid1
                 OR uc.reviewerid = :userid2
                 OR e.id IS NOT NULL
                 OR ue.id IS NOT NULL
                 OR uec.id IS NOT NULL";
        $params = [
            'userlevel1' => CONTEXT_USER,
            'userlevel2' => CONTEXT_USER,
            'userid1' => $userid,
            'userid2' => $userid,
            'userid3' => $userid,
            'userid4' => $userid,
            'userid5' => $userid,
            'userid6' => $userid,
        ];
        $contextlist->add_from_sql($sql, $params);

        // Now, the easy part, we fetch the user context for user plans and competencies.
        // We also fetch the course context for the state of competencies for the user in courses.
        $sql = "
             SELECT DISTINCT ctx.id
               FROM {context} ctx
          LEFT JOIN {" . plan::TABLE . "} p
                 ON p.userid = ctx.instanceid
                AND ctx.contextlevel = :userlevel1
          LEFT JOIN {" . user_competency::TABLE . "} uc
                 ON uc.userid = ctx.instanceid
                AND ctx.contextlevel = :userlevel2
                AND uc.userid = :userid2
          LEFT JOIN {" . user_evidence::TABLE . "} ue
                 ON ue.userid = ctx.instanceid
                AND ctx.contextlevel = :userlevel3
                AND ue.userid = :userid3
          LEFT JOIN {" . user_competency_course::TABLE . "} ucc
                 ON ucc.courseid = ctx.instanceid
                AND ctx.contextlevel = :courselevel
                AND ucc.userid = :userid4
              WHERE p.userid = :userid1
                 OR uc.id IS NOT NULL
                 OR ue.id IS NOT NULL
                 OR ucc.id IS NOT NULL";
        $params = [
            'userlevel1' => CONTEXT_USER,
            'userlevel2' => CONTEXT_USER,
            'userlevel3' => CONTEXT_USER,
            'courselevel' => CONTEXT_COURSE,
            'userid1' => $userid,
            'userid2' => $userid,
            'userid3' => $userid,
            'userid4' => $userid,
        ];
        $contextlist->add_from_sql($sql, $params);

        // Include the user contexts in which the user commented.
        $sql = "
            SELECT ctx.id
              FROM {context} ctx
              JOIN {comments} c
                ON c.contextid = ctx.id
             WHERE c.component = :component
               AND c.commentarea IN (:planarea, :usercomparea)
               AND c.userid = :userid";
        $params = [
            'component' => 'competency',    // Must not be core_competency.
            'planarea' => 'plan',
            'usercomparea' => 'user_competency',
            'userid' => $userid
        ];
        $contextlist->add_from_sql($sql, $params);

        return $contextlist;
    }
1285427
public static function get_users_in_context(\core_privacy\local\request\userlist $userlist) {
        $interface = \tool_log\local\privacy\logstore_userlist_provider::class;
        static::call_subplugins_method_with_args('add_userids_for_context', [$userlist], $interface);
    }
754777
public static function get_users_in_context(userlist $userlist) {
        $context = $userlist->get_context();

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

        // Find users with glossary entries.
        $sql = "SELECT ge.userid
                  FROM {context} c
                  JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = :contextlevel
                  JOIN {modules} m ON m.id = cm.module AND m.name = :modname
                  JOIN {glossary} g ON g.id = cm.instance
                  JOIN {glossary_entries} ge ON ge.glossaryid = g.id
                 WHERE c.id = :contextid";

        $params = [
            'contextid' => $context->id,
            'contextlevel' => CONTEXT_MODULE,
            'modname' => 'glossary',
        ];

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

        // Find users with glossary comments.
        \core_comment\privacy\provider::get_users_in_context_from_sql($userlist, 'com', 'mod_glossary', 'glossary_entry',
                $context->id);

        // Find users with glossary ratings.
        $sql = "SELECT ge.id
                  FROM {context} c
                  JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = :contextlevel
                  JOIN {modules} m ON m.id = cm.module AND m.name = :modname
                  JOIN {glossary} g ON g.id = cm.instance
                  JOIN {glossary_entries} ge ON ge.glossaryid = g.id
                 WHERE c.id = :contextid";

        $params = [
            'contextid' => $context->id,
            'contextlevel' => CONTEXT_MODULE,
            'modname' => 'glossary',
        ];

        \core_rating\privacy\provider::get_users_in_context_from_sql($userlist, 'rat', 'mod_glossary', 'entry', $sql, $params);
    }
1171121
public static function get_contexts_for_userid(int $userid) : contextlist {
        $contextlist = new contextlist();

        // Calendar Events can exist at Site, Course Category, Course, Course Group, User, or Course Modules contexts.
        $params = [
            'sitecontext'        => CONTEXT_SYSTEM,
            'categorycontext'    => CONTEXT_COURSECAT,
            'coursecontext'      => CONTEXT_COURSE,
            'groupcontext'       => CONTEXT_COURSE,
            'usercontext'        => CONTEXT_USER,
            'cuserid'            => $userid,
            'modulecontext'      => CONTEXT_MODULE,
            'muserid'            => $userid
        ];

        // Get contexts of Calendar Events for the owner.
        $sql = "SELECT ctx.id
                  FROM {context} ctx
                  JOIN {event} e ON
                       (e.eventtype = 'site' AND ctx.contextlevel = :sitecontext) OR
                       (e.categoryid = ctx.instanceid AND e.eventtype = 'category' AND ctx.contextlevel = :categorycontext) OR
                       (e.courseid = ctx.instanceid AND e.eventtype = 'course' AND ctx.contextlevel = :coursecontext) OR
                       (e.courseid = ctx.instanceid AND e.eventtype = 'group' AND ctx.contextlevel = :groupcontext) OR
                       (e.userid = ctx.instanceid AND e.eventtype = 'user' AND ctx.contextlevel = :usercontext)
                 WHERE e.userid = :cuserid
                 UNION
                SELECT ctx.id
                  FROM {context} ctx
                  JOIN {course_modules} cm ON cm.id = ctx.instanceid AND ctx.contextlevel = :modulecontext
                  JOIN {modules} m ON m.id = cm.module
                  JOIN {event} e ON e.modulename = m.name AND e.courseid = cm.course AND e.instance = cm.instance
                 WHERE e.userid = :muserid";
        $contextlist->add_from_sql($sql, $params);

        // Calendar Subscriptions can exist at Site, Course Category, Course, Course Group, or User contexts.
        $params = [
            'sitecontext'       => CONTEXT_SYSTEM,
            'categorycontext'   => CONTEXT_COURSECAT,
            'coursecontext'     => CONTEXT_COURSE,
            'groupcontext'      => CONTEXT_COURSE,
            'usercontext'       => CONTEXT_USER,
            'userid'            => $userid
        ];

        // Get contexts for Calendar Subscriptions for the owner.
        $sql = "SELECT ctx.id
                  FROM {context} ctx
                  JOIN {event_subscriptions} s ON
                       (s.eventtype = 'site' AND ctx.contextlevel = :sitecontext) OR
                       (s.categoryid = ctx.instanceid AND s.eventtype = 'category' AND ctx.contextlevel = :categorycontext) OR
                       (s.courseid = ctx.instanceid AND s.eventtype = 'course' AND ctx.contextlevel = :coursecontext) OR
                       (s.courseid = ctx.instanceid AND s.eventtype = 'group' AND ctx.contextlevel = :groupcontext) OR
                       (s.userid = ctx.instanceid AND s.eventtype = 'user' AND ctx.contextlevel = :usercontext)
                 WHERE s.userid = :userid";
        $contextlist->add_from_sql($sql, $params);

        // Return combined contextlist for Calendar Events & Calendar Subscriptions.
        return $contextlist;
    }
579732
public static function get_users_in_context(userlist $userlist) {
        global $DB;

        $context = $userlist->get_context();

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

        // Add user if any messagelist data exists.
        if ($DB->record_exists('messageinbound_messagelist', ['userid' => $context->instanceid])) {
            // Only using user context, so instance ID will be the only user ID.
            $userlist->add_user($context->instanceid);
        }

        // Add users based on userkey (since we also delete those).
        \core_userkey\privacy\provider::get_user_contexts_with_script($userlist, $context, 'messageinbound_handler');
    }
425361
public static function delete_data_for_user(approved_contextlist $contextlist) {
        global $DB;

        if (empty($contextlist->count())) {
            return;
        }

        $userid = $contextlist->get_user()->id;

        foreach ($contextlist->get_contexts() as $context) {
            $conditions = [
                'module'        => 'notes',
                'courseid'      => $context->instanceid,
                'usermodified'  => $userid
            ];

            $DB->delete_records('post', $conditions);
        }
    }
224958
public static function get_contexts_for_userid(int $userid) : contextlist {
        $params = ['userid' => $userid, 'contextcourse' => CONTEXT_COURSE];
        $sql = "SELECT ctx.id
                FROM {context} ctx
                JOIN {stats_user_daily} sud ON sud.courseid = ctx.instanceid AND sud.userid = :userid
                WHERE ctx.contextlevel = :contextcourse";

        $contextlist = new contextlist();
        $contextlist->add_from_sql($sql, $params);

        $sql = "SELECT ctx.id
                FROM {context} ctx
                JOIN {stats_user_weekly} suw ON suw.courseid = ctx.instanceid AND suw.userid = :userid
                WHERE ctx.contextlevel = :contextcourse";
        $contextlist->add_from_sql($sql, $params);

        $sql = "SELECT ctx.id
                FROM {context} ctx
                JOIN {stats_user_monthly} sum ON sum.courseid = ctx.instanceid AND sum.userid = :userid
                WHERE ctx.contextlevel = :contextcourse";
        $contextlist->add_from_sql($sql, $params);

        return $contextlist;
    }
321847
public static function get_users_in_context(\core_privacy\local\request\userlist $userlist) {
        $context = $userlist->get_context();

        if ($context->contextlevel == CONTEXT_COURSE) {
            $params = ['contextinstanceid' => $context->instanceid];

            $sql = "SELECT usermodified
                      FROM {grade_outcomes}
                     WHERE courseid = :contextinstanceid";
            $userlist->add_from_sql('usermodified', $sql, $params);

            $sql = "SELECT loggeduser
                      FROM {grade_outcomes_history}
                     WHERE courseid = :contextinstanceid";
            $userlist->add_from_sql('loggeduser', $sql, $params);

            $sql = "SELECT userid
                      FROM {scale}
                     WHERE courseid = :contextinstanceid";
            $userlist->add_from_sql('userid', $sql, $params);

            $sql = "SELECT loggeduser, userid
                      FROM {scale_history}
                     WHERE courseid = :contextinstanceid";
            $userlist->add_from_sql('loggeduser', $sql, $params);
            $userlist->add_from_sql('userid', $sql, $params);

            $sql = "SELECT loggeduser
                      FROM {grade_items_history}
                     WHERE courseid = :contextinstanceid";
            $userlist->add_from_sql('loggeduser', $sql, $params);

            $sql = "SELECT ggh.userid
                      FROM {grade_grades_history} ggh
                      JOIN {grade_items} gi ON ggh.itemid = gi.id
                     WHERE gi.courseid = :contextinstanceid";
            $userlist->add_from_sql('userid', $sql, $params);

            $sql = "SELECT gg.userid, gg.usermodified
                      FROM {grade_grades} gg
                      JOIN {grade_items} gi ON gg.itemid = gi.id
                     WHERE gi.courseid = :contextinstanceid";
            $userlist->add_from_sql('userid', $sql, $params);
            $userlist->add_from_sql('usermodified', $sql, $params);

            $sql = "SELECT loggeduser
                      FROM {grade_categories_history}
                     WHERE courseid = :contextinstanceid";
            $userlist->add_from_sql('loggeduser', $sql, $params);
        }

        // None of these are currently used (user deletion).
        if ($context->contextlevel == CONTEXT_SYSTEM) {
            $params = ['contextinstanceid' => 0];

            $sql = "SELECT usermodified
                      FROM {grade_outcomes}
                     WHERE (courseid IS NULL OR courseid < 1)";
            $userlist->add_from_sql('usermodified', $sql, []);

            $sql = "SELECT loggeduser
                      FROM {grade_outcomes_history}
                     WHERE (courseid IS NULL OR courseid < 1)";
            $userlist->add_from_sql('loggeduser', $sql, []);

            $sql = "SELECT userid
                      FROM {scale}
                     WHERE courseid = :contextinstanceid";
            $userlist->add_from_sql('userid', $sql, $params);

            $sql = "SELECT loggeduser, userid
                      FROM {scale_history}
                     WHERE courseid = :contextinstanceid";
            $userlist->add_from_sql('loggeduser', $sql, $params);
            $userlist->add_from_sql('userid', $sql, $params);
        }

        if ($context->contextlevel == CONTEXT_USER) {
            // If the grade item has been removed and we have an orphan entry then we link to the
            // user context.
            $sql = "SELECT ggh.userid
                      FROM {grade_grades_history} ggh
                 LEFT JOIN {grade_items} gi ON ggh.itemid = gi.id
                     WHERE gi.id IS NULL
                       AND ggh.userid = :contextinstanceid";
            $userlist->add_from_sql('userid', $sql, ['contextinstanceid' => $context->instanceid]);
        }
    }
1135400
protected static function delete_data(\context $context, int $userid = null) {
        global $DB;

        $cohortids = $DB->get_fieldset_select('cohort', 'id', 'contextid = :contextid', ['contextid' => $context->id]);
        foreach ($cohortids as $cohortid) {
            $params = ['cohortid' => $cohortid];
            if (!empty($userid)) {
                $params['userid'] = $userid;
            }
            $DB->delete_records('cohort_members', $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;
    }
121396
public static function add_userids_for_context(\core_privacy\local\request\userlist $userlist,
                                                   string $itemtype = null) {
        if (empty($userlist)) {
            return;
        }

        $params = [
            'contextid' => $userlist->get_context()->id,
            'component' => $userlist->get_component()
        ];

        $sql = "SELECT userid
                  FROM {favourite}
                 WHERE contextid = :contextid
                       AND component = :component";

        if (!is_null($itemtype)) {
            $sql .= " AND itemtype = :itemtype";
            $params['itemtype'] = $itemtype;
        }

        $userlist->add_from_sql('userid', $sql, $params);
    }
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);
    }
1086736
public static function get_users_in_context(userlist $userlist) {
        $context = $userlist->get_context();

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

        // The data is associated at the user context level, so retrieve the user's context id.
        $sql = "SELECT usermodified AS userid
                  FROM {repository_onedrive_access}
                 WHERE usermodified = ?";
        $params = [$context->instanceid];
        $userlist->add_from_sql('userid', $sql, $params);
    }
625917
public static function get_users_in_context(userlist $userlist) {
        global $DB;

        $context = $userlist->get_context();

        // Calendar Events can exist at Site (CONTEXT_SYSTEM), Course Category (CONTEXT_COURSECAT),
        // Course and Course Group (CONTEXT_COURSE), User (CONTEXT_USER), or Course Modules (CONTEXT_MODULE) contexts.
        if ($context->contextlevel == CONTEXT_MODULE) {
            $params = ['cmid' => $context->instanceid];

            $sql = "SELECT e.userid
                      FROM {course_modules} cm
                      JOIN {modules} m ON m.id = cm.module
                      JOIN {event} e ON e.modulename = m.name AND e.courseid = cm.course AND e.instance = cm.instance
                     WHERE cm.id = :cmid";

            $userlist->add_from_sql('userid', $sql, $params);
        } else if ($context->contextlevel == CONTEXT_SYSTEM) {
            // Get contexts of Calendar Events for the owner.
            $sql = "SELECT userid FROM {event} WHERE eventtype = 'site'";
            $userlist->add_from_sql('userid', $sql, []);

            // Get contexts for Calendar Subscriptions for the owner.
            $sql = "SELECT userid FROM {event_subscriptions} WHERE eventtype = 'site'";
            $userlist->add_from_sql('userid', $sql, []);
        } else if (in_array($context->contextlevel, [CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_USER])) {
            $eventfields = [
                CONTEXT_COURSECAT   => 'categoryid',
                CONTEXT_COURSE      => 'courseid',
                CONTEXT_USER        => 'userid'
            ];
            $eventfield = $eventfields[$context->contextlevel];

            $eventtypes = [
                CONTEXT_COURSECAT   => 'category',
                CONTEXT_COURSE      => ['course' , 'group'],
                CONTEXT_USER        => 'user'
            ];
            list($eventtypesql, $eventtypeparams) = $DB->get_in_or_equal($eventtypes[$context->contextlevel], SQL_PARAMS_NAMED);

            $params = $eventtypeparams + ['instanceid' => $context->instanceid];

            // Get contexts of Calendar Events for the owner.
            $sql = "SELECT userid
                      FROM {event}
                     WHERE eventtype $eventtypesql
                           AND $eventfield = :instanceid";
            $userlist->add_from_sql('userid', $sql, $params);

            // Get contexts for Calendar Subscriptions for the owner.
            $sql = "SELECT userid
                      FROM {event_subscriptions}
                     WHERE eventtype $eventtypesql
                           AND $eventfield = :instanceid";
            $userlist->add_from_sql('userid', $sql, $params);
        }
    }
892643
public static function delete_data_for_user(approved_contextlist $contextlist) {
        global $DB;
        if (!static::approved_contextlist_contains_my_context($contextlist)) {
            // We only care about the user's user context.
            return;
        }

        static::delete_user_data($contextlist->get_user()->id);
    }