public static function delete_completion(\stdClass $user = null, int $courseid = null, int $cmid = null) {
        global $DB;

        if (isset($cmid)) {
            $params = (isset($user)) ? ['userid' => $user->id, 'coursemoduleid' => $cmid] : ['coursemoduleid' => $cmid];
            // Only delete the record for course modules completion.
            $DB->delete_records('course_modules_completion', $params);
            return;
        }

        if (isset($courseid)) {

            $usersql = isset($user) ? 'AND cmc.userid = :userid' : '';
            $params = isset($user) ? ['course' => $courseid, 'userid' => $user->id] : ['course' => $courseid];

            // Find records relating to course modules.
            $sql = "SELECT cmc.id
                      FROM {course_completion_criteria} ccc
                      JOIN {course_modules_completion} cmc ON ccc.moduleinstance = cmc.coursemoduleid
                     WHERE ccc.course = :course $usersql";
            $recordids = $DB->get_records_sql($sql, $params);
            $ids = array_keys($recordids);
            if (!empty($ids)) {
                list($deletesql, $deleteparams) = $DB->get_in_or_equal($ids);
                $deletesql = 'id ' . $deletesql;
                $DB->delete_records_select('course_modules_completion', $deletesql, $deleteparams);
            }
            $DB->delete_records('course_completion_crit_compl', $params);
            $DB->delete_records('course_completions', $params);
        }
    }