public static function override_activity_completion_status($userid, $cmid, $newstate) {
        // Validate and normalize parameters.
        $params = self::validate_parameters(self::override_activity_completion_status_parameters(),
            array('userid' => $userid, 'cmid' => $cmid, 'newstate' => $newstate));
        $userid = $params['userid'];
        $cmid = $params['cmid'];
        $newstate = $params['newstate'];

        $context = context_module::instance($cmid);
        self::validate_context($context);

        list($course, $cm) = get_course_and_cm_from_cmid($cmid);

        // Set up completion object and check it is enabled.
        $completion = new completion_info($course);
        if (!$completion->is_enabled()) {
            throw new moodle_exception('completionnotenabled', 'completion');
        }

        // Update completion state and get the new state back.
        $completion->update_state($cm, $newstate, $userid, true);
        $completiondata = $completion->get_data($cm, false, $userid);

        // Return the current state of completion.
        return [
            'cmid' => $completiondata->coursemoduleid,
            'userid' => $completiondata->userid,
            'state' => $completiondata->completionstate,
            'timecompleted' => $completiondata->timemodified,
            'overrideby' => $completiondata->overrideby,
            'tracking' => $completion->is_enabled($cm)
        ];
    }