public static function grade_competency_in_course($courseorid, $userid, $competencyid, $grade, $note = null) {
        global $USER, $DB;
        static::require_enabled();

        $course = $courseorid;
        if (!is_object($courseorid)) {
            $course = $DB->get_record('course', array('id' => $courseorid));
        }
        $context = context_course::instance($course->id);

        // Check that we can view the user competency details in the course.
        if (!user_competency::can_read_user_in_course($userid, $course->id)) {
            throw new required_capability_exception($context, 'moodle/competency:usercompetencyview', 'nopermissions', '');
        }

        // Validate the permission to grade.
        if (!user_competency::can_grade_user_in_course($userid, $course->id)) {
            throw new required_capability_exception($context, 'moodle/competency:competencygrade', 'nopermissions', '');
        }

        // Check that competency is in course and visible to the current user.
        $competency = course_competency::get_competency($course->id, $competencyid);
        $competencycontext = $competency->get_context();
        if (!has_any_capability(array('moodle/competency:competencyview', 'moodle/competency:competencymanage'),
                $competencycontext)) {
            throw new required_capability_exception($competencycontext, 'moodle/competency:competencyview', 'nopermissions', '');
        }

        // Check that the user is enrolled in the course, and is "gradable".
        if (!is_enrolled($context, $userid, 'moodle/competency:coursecompetencygradable')) {
            throw new coding_exception('The competency may not be rated at this time.');
        }

        $action = evidence::ACTION_OVERRIDE;
        $desckey = 'evidence_manualoverrideincourse';

        $result = self::add_evidence($userid,
                                  $competency,
                                  $context->id,
                                  $action,
                                  $desckey,
                                  'core_competency',
                                  $context->get_context_name(),
                                  false,
                                  null,
                                  $grade,
                                  $USER->id,
                                  $note);
        if ($result) {
            $all = user_competency_course::get_multiple($userid, $course->id, array($competency->get('id')));
            $uc = reset($all);
            $event = \core\event\competency_user_competency_rated_in_course::create_from_user_competency_course($uc);
            $event->trigger();
        }
        return $result;
    }