protected static function set_acceptances_status($policyversionid, $userid = null, $note = null, $lang = null, $status = 1) {
        global $DB, $USER;

        // Validate arguments and capabilities.
        if (empty($policyversionid)) {
            return;
        } else if (!is_array($policyversionid)) {
            $policyversionid = [$policyversionid];
        }
        if (!$userid) {
            $userid = $USER->id;
        }
        self::can_accept_policies([$policyversionid], $userid, true);

        // Retrieve the list of policy versions that need agreement (do not update existing agreements).
        list($sql, $params) = $DB->get_in_or_equal($policyversionid, SQL_PARAMS_NAMED);
        $sql = "SELECT v.id AS versionid, a.*
                  FROM {tool_policy_versions} v
             LEFT JOIN {tool_policy_acceptances} a ON a.userid = :userid AND a.policyversionid = v.id
                 WHERE v.id $sql AND (a.id IS NULL OR a.status <> :status)";

        $needacceptance = $DB->get_records_sql($sql, $params + [
            'userid' => $userid,
            'status' => $status,
        ]);

        $realuser = manager::get_realuser();
        $updatedata = ['status' => $status, 'lang' => $lang ?: current_language(),
            'timemodified' => time(), 'usermodified' => $realuser->id, 'note' => $note];
        foreach ($needacceptance as $versionid => $currentacceptance) {
            unset($currentacceptance->versionid);
            if ($currentacceptance->id) {
                $updatedata['id'] = $currentacceptance->id;
                $DB->update_record('tool_policy_acceptances', $updatedata);
                acceptance_updated::create_from_record((object)($updatedata + (array)$currentacceptance))->trigger();
            } else {
                $updatedata['timecreated'] = $updatedata['timemodified'];
                $updatedata['policyversionid'] = $versionid;
                $updatedata['userid'] = $userid;
                $updatedata['id'] = $DB->insert_record('tool_policy_acceptances', $updatedata);
                acceptance_created::create_from_record((object)($updatedata + (array)$currentacceptance))->trigger();
            }
        }

        static::update_policyagreed($userid);
    }