protected static function export_policy_agreements_for_context(\context_user $context) {
        global $DB;

        $sysctx = \context_system::instance();
        $fs = get_file_storage();
        $agreementsql = "
            SELECT
                a.id AS agreementid, a.userid, a.timemodified, a.note, a.status,
                a.policyversionid AS versionid, a.usermodified, a.timecreated,
                v.id, v.archived, v.name, v.revision,
                v.summary, v.summaryformat,
                v.content, v.contentformat,
                p.currentversionid
             FROM {tool_policy_acceptances} a
             JOIN {tool_policy_versions} v ON v.id = a.policyversionid
             JOIN {tool_policy} p ON v.policyid = p.id
            WHERE a.userid = :userid OR a.usermodified = :usermodified";

        // Fetch all agreements related to this user.
        $agreements = $DB->get_recordset_sql($agreementsql, [
            'userid' => $context->instanceid,
            'usermodified' => $context->instanceid,
        ]);

        $basecontext = [
            get_string('privacyandpolicies', 'admin'),
            get_string('useracceptances', 'tool_policy'),
        ];

        foreach ($agreements as $agreement) {
            $subcontext = array_merge($basecontext, [get_string('policynamedversion', 'tool_policy', $agreement)]);

            $summary = writer::with_context($context)->rewrite_pluginfile_urls(
                $subcontext,
                'tool_policy',
                'policydocumentsummary',
                $agreement->versionid,
                $agreement->summary
            );
            $content = writer::with_context($context)->rewrite_pluginfile_urls(
                $subcontext,
                'tool_policy',
                'policydocumentcontent',
                $agreement->versionid,
                $agreement->content
            );
            $agreementcontent = (object) [
                'name' => $agreement->name,
                'revision' => $agreement->revision,
                'isactive' => transform::yesno($agreement->versionid == $agreement->currentversionid),
                'isagreed' => transform::yesno($agreement->status),
                'agreedby' => transform::user($agreement->usermodified),
                'timecreated' => transform::datetime($agreement->timecreated),
                'timemodified' => transform::datetime($agreement->timemodified),
                'note' => $agreement->note,
                'summary' => format_text($summary, $agreement->summaryformat),
                'content' => format_text($content, $agreement->contentformat),
            ];

            writer::with_context($context)->export_data($subcontext, $agreementcontent);
            // Manually export the files as they reside in the system context so we can't use
            // the write's helper methods.
            foreach ($fs->get_area_files($sysctx->id, 'tool_policy', 'policydocumentsummary', $agreement->versionid) as $file) {
                writer::with_context($context)->export_file($subcontext, $file);
            }
            foreach ($fs->get_area_files($sysctx->id, 'tool_policy', 'policydocumentcontent', $agreement->versionid) as $file) {
                writer::with_context($context)->export_file($subcontext, $file);
            }
        }
        $agreements->close();
    }